【发布时间】:2021-10-31 05:20:46
【问题描述】:
const spans = document.querySelectorAll("span");
const animation = function () {
for (let span of spans) span.classList.toggle("fade");
};
//setInterval(animation, 2400);
.animated {
font-size: 40px;
color: #d1d8e0;
margin-top: 20px;
font-weight: 900;
height: 60px;
line-height: 50px;
}
.animated span {
opacity: 1;
transition: all 0.5s;
}
.animated>.fade {
opacity: 0;
}
@media (max-width: 800px) {
.animated {
height: 70px;
font-size: 25px;
}
}
<div class="animated">
Hey, I'm<br>
<span class="color-primary fade" id="animated-name">Name Name</span>
<span class="color-primary" id="animated-text">Description about something <br>Text & Text</span>
</div>
我正在尝试将文本与页面中心对齐并响应所有设备。
我尝试了几个选项,但都没有成功:
审判一号:
在 span 中添加了style="margin:auto;
审判二:
在 span 中添加了style="margin-left:-188;
审判3:
在 span 之前的 div 中添加了style="text-align: center;"
试炼4号
在动画跨度中添加了position: absolute;
在home.html中
<div class="animated">
Hey, I'm<br>
<span class="color-primary fade" id="animated-name">Name Name</span>
<span class="color-primary" id="animated-text">Description about something</br>Text &
Text</span>
</div>
在css中
.animated {
font-size: 40px;
color: #d1d8e0;
margin-top: 20px;
font-weight: 900;
height: 60px;
line-height: 50px;
}
.animated span {
opacity: 1;
transition: all 0.5s;
}
.animated>.fade {
opacity: 0;
}
@media (max-width: 800px) {
.animated {
height: 70px;
font-size: 25px;
}
}
在base.html中
<script>
const spans = document.querySelectorAll("span");
const animation = function () {
for (let span of spans) span.classList.toggle("fade");
};
setInterval(animation, 2400);
</script>
我的问题是如何让span中的文本居中,以及当有类似于第二个span的长句也要居中
【问题讨论】:
-
用 div 替换 span。 Span 是一个内联元素。它没有宽度或高度。
标签: javascript html css