CSS text-align delay with width animation

China☆狼群 提交于 2019-12-05 14:01:39

You're animating the width of the div. So the content will be revealed as the width increases. Alternatively, you could animate pseudo-selectors and reveal the text.

Hope this is the result you're expecting.

JS Fiddle

You're animating the width of the element. Center alignment of text is only apparent when there is extra space. A very simple fix however is to center the div itself with margin: 0 auto;.

@keyframes leftright {
  0% {
    width: 0%;
  }
  100% {
    width: 100%;
  }
}

.test_1 {
  overflow: hidden;
  white-space: nowrap;
  margin: 0 auto;
  width: 80vw;
  border: 1px solid red;
  font: bold 15vmin 'Playfair Display', serif;
  text-align: center;
  animation: leftright 1s infinite;
}
<div class="test_1">Why hello there</div>

You may consider a nested element where you apply the same width and then rely on overflow to hide it and keep the text unchanged:

@keyframes leftright {
  0% {
    max-width: 0%;
  }
  100% {
    max-width: 100%;
  }
}

.test_1 {
  overflow: hidden;
  width: 80vw;
  border: 1px solid red;
  font: bold 15vmin 'Playfair Display', serif;
  text-align: center;
  animation: leftright 1s;
}

.test_1>span {
  display: inline-block;
  white-space: nowrap;
  width: inherit;
}
<div class="test_1"><span>Why hello there</span></div>

Welcome @user10294993! You can do it adding two keyframes: one for de box and othe for the text. Here is an example:

.test_1 {
	overflow:hidden;
	white-space: nowrap;
	width: 80vw;
	border: 1px solid red;
	font: bold 15vmin 'Playfair Display', serif ;
	text-align: center;
	animation: leftright 1s infinite;
}

.test_1 p {
  margin: 0;
  animation: display 1s infinite;
} 

@keyframes leftright{
  from {max-width: 0%}
  to {max-width: 100%;}
}

@keyframes display{
  0% {opacity: 0}
  50% {opacity: 0}
  100% {opacity: 1}
}
<div class="test_1"><p>Why hello there</p></div>

Hope it helps you.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!