首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CSS -文本始终居中的打字机效果

问CSS -文本始终居中的打字机效果
EN

Stack Overflow用户
提问于 2020-05-05 21:22:32
回答 1查看 772关注 0票数 2

所以,我在网上找到了下面的例子,它工作得很好,除了我想补充的一个事实。随着打字机效果的应用,左边的字母总是移动和居中的,但我希望我的文本始终居中,并使其字母从左到右显示。我怎么才能添加这个?

​

代码语言:javascript
复制
/* GLOBAL STYLES */

body {
  background: #333;
  padding-top: 5em;
  display: flex;
  justify-content: center;
}


/* DEMO-SPECIFIC STYLES */

.typewriter h1 {
  color: #fff;
  font-family: monospace;
  overflow: hidden;
  /* Ensures the content is not revealed until the animation */
  border-right: .15em solid orange;
  /* The typwriter cursor */
  white-space: nowrap;
  /* Keeps the content on a single line */
  margin: 0 auto;
  /* Gives that scrolling effect as the typing happens */
  letter-spacing: .15em;
  /* Adjust as needed */
  animation: typing 3.5s steps(30, end), blink-caret .5s step-end infinite;
}


/* The typing effect */

@keyframes typing {
  from {
    width: 0
  }
  to {
    width: 100%
  }
}
代码语言:javascript
复制
<div class="typewriter">
  <h1>The cat and the hat.</h1>
</div>

​

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-05 21:26:46

将margin-left设置为0,而不是auto。

​

代码语言:javascript
复制
/* GLOBAL STYLES */

body {
  background: #333;
  padding-top: 5em;
  display: flex;
  justify-content: center;
}


/* DEMO-SPECIFIC STYLES */

.typewriter h1 {
  color: #fff;
  font-family: monospace;
  overflow: hidden;
  /* Ensures the content is not revealed until the animation */
  border-right: .15em solid orange;
  /* The typwriter cursor */
  white-space: nowrap;
  /* Keeps the content on a single line */
  margin: 0;
  margin-right: auto;
  /* Gives that scrolling effect as the typing happens */
  letter-spacing: .15em;
  /* Adjust as needed */
  animation: typing 3.5s steps(30, end), blink-caret .5s step-end infinite;
}


/* The typing effect */

@keyframes typing {
  from {
    width: 0
  }
  to {
    width: 100%
  }
}
代码语言:javascript
复制
<div class="typewriter">
  <h1>The cat and the hat.</h1>
</div>

​

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61614210

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档