我有一个class名称为"headertekst"的
标题文本。我想将其居中。我尝试使用margin-left: auto
,margin-right: auto
和width: 100%
,但没有效果。
我可以使用margin-left: 20%
将其居中,但这只是在我的笔记本电脑上。在其他屏幕大小和移动设备上,它将无法居中。
如何以正确的方式将其居中?
You can just add style in h2 as:
<h2 style="text-align: center;">Text</h2>
h2.headertekst {
text-align: center;
}
<h2 class="headertekst">Test 1</h2>
h2 {
width: 100%;
text-align:center;
}
或者
header {
text-align: center;
}
h2 {
display: inline-block;
}
.headertekst {
text-align: center;
}
如果您需要使 h2
居中,只需将 text-align: center
规则添加到 h2
标签的父元素即可。
<h2 class="headertekst"> centered text </h2>
.headertekst {
{
text-align: center;
width:100%
}
width:100%
属性,因为如果没有宽度,text-align:center
属性将无法生效。或者将 text-align:center
属性设置到父级 div 上。 - Jishnu V Swidth:100%
。 - Andy Tschierschbody {
background: #363636;
}
.headertekst {
color: white;
font-size: 20px;
font-family: "Raleway", Helvetica, Arial, sans-serif;
font-weight: 600;
text-transform: uppercase;
text-align: center;
}
.headertekstrotate {
position: relative;
display: inline-block;
padding: 0 0 0 8px;
width: 150px;
}
.headertekstrotate span {
animation: clock 12s linear infinite 0s;
-ms-animation: clock 12s linear infinite 0s;
-webkit-animation: clock 12s linear infinite 0s;
position: absolute;
left: 0;
top: 0;
opacity: 0;
}
.headertekstrotate span:nth-child(2) {
animation-delay: 4s;
-ms-animation-delay: 4s;
-webkit-animation-delay: 4s;
}
.headertekstrotate span:nth-child(3) {
animation-delay: 8s;
-ms-animation-delay: 8s;
-webkit-animation-delay: 8s;
}
@keyframes clock {
0% {
opacity: 0;
}
10% {
opacity: 1;
}
25% {
opacity: 1;
}
30% {
opacity: 0;
}
80% {
opacity: 0;
}
100% {
opacity: 0;
}
}
<h2 class="headertekst">Interlaser is
<div class="headertekstrotate">
<span>professioneel.</span>
<span>voordelig.</span>
<span>betrouwbaar.</span>
</div>
</h2>
body {
background: #363636;
}
.headertekst {
color: white;
font-size: 20px;
font-family: "Raleway", Helvetica, Arial, sans-serif;
font-weight: 600;
text-transform: uppercase;
text-align: center;
}
.headertekstrotate {
position: relative;
}
.headertekstrotate span {
animation: clock 12s linear infinite 0s;
-ms-animation: clock 12s linear infinite 0s;
-webkit-animation: clock 12s linear infinite 0s;
position: absolute;
left: 0;
top: 0;
width: 100%;
opacity: 0;
}
.headertekstrotate span:nth-child(2) {
animation-delay: 4s;
-ms-animation-delay: 4s;
-webkit-animation-delay: 4s;
}
.headertekstrotate span:nth-child(3) {
animation-delay: 8s;
-ms-animation-delay: 8s;
-webkit-animation-delay: 8s;
}
@keyframes clock {
0% {
opacity: 0;
}
10% {
opacity: 1;
}
25% {
opacity: 1;
}
30% {
opacity: 0;
}
80% {
opacity: 0;
}
100% {
opacity: 0;
}
}
<h2 class="headertekst">
<div class="headertekstrotate">
<span>Interlaser is short.</span>
<span>Interlaser is very very long.</span>
<span>Interlaser is professioneel.</span>
</div>
</h2>
.headertekst{
text-align: center;
}
.headertekst{
text-align: center;
}
<h2 class="headertekst">Align this to center</h2>
尝试像这样做些什么。
.headertekst{
left: 0;
right: 0;
width: 100%;
text-align: center;
}
<h2 class="headertekst">This is Heading</h2>
center
标签,我宁愿说使用center,虽然这是一种旧方法,但它很好。如果你想的话,可以使用CSS。
<center><h2 class="headertekst">这是标题</h2></center>
试一下这个
.headertekst
{
text-align:center;
width:100%;
}
.headertekst
{
text-align:center;
width:100%;
}
<h2 class="headertekst">This is center text</h2>