[英]Maintain text-align: left and center
I have built the following page header which is vertically aligned to the center. 我建立了以下页面标题,该页面标题与中心垂直对齐。
h1 span { display: block; } .parent { width: 300px; height: 400px; display: table; background: #ccc; } .center { display: table-cell; text-align: left; vertical-align: middle; }
<div class="parent"> <div class="center"> <header> <h1> <span>First line</span> <span>Second line</span> </h1> </header> </div> </div>
I would like to align the <h1>
text to the center, but maintain text-align: left
, like so: 我想将
<h1>
文本对齐到中心,但要保持text-align: left
,如下所示:
I know this is possible by adding: 我知道这可以通过添加:
h1 {
margin: 0 auto;
width: 156px;
}
However, the text is different on each page, so the width
would need to be automatically detected. 但是,每页上的文本都不相同,因此需要自动检测
width
。 Is there a way to do this, preferably without Javascript? 有没有办法做到这一点,最好不要使用Javascript?
EDIT : I'm looking for a solution that works in IE9+. 编辑 :我正在寻找在IE9 +中工作的解决方案。
you can use flexbox
for this, align-items
to center vertically (the cross-axis) and justify-content
to center along the line 您可以为此使用
flexbox
, align-items
以垂直居中(横轴), justify-content
以沿线居中
.parent { width: 300px; height: 400px; background: #ccc; display: flex; align-items: center; justify-content: center; } span { display: block }
<div class="parent"> <header> <h1> <span>First line</span> <span>Second line</span> </h1> </header> </div>
UPDATE: For IE9+: 更新:对于IE9 +:
.parent { width: 300px; height: 400px; background: #ccc; display: table; text-align: center } h1 { display: inline-block; } span { display: block; text-align:left } header { vertical-align: middle; display: table-cell }
<div class="parent"> <header> <h1> <span>First line</span> <span>Second line</span> </h1> </header> </div>
Set the wrapping h1
to display:inline-block
and center that with text-align:center
on the parent div. 将换行
h1
设置为在父div上display:inline-block
并使用text-align:center
。
Then text-align:left
on the inner span
aligns the text as required. 然后,
text-align:left
内部span
上的text-align:left
将根据需要对齐文本。
h1 span { display: block; text-align: left; } h1 { display: inline-block; } .parent { width: 300px; height: 400px; display: table; background: #ccc; text-align: center; } .center { display: table-cell; vertical-align: middle; }
<div class="parent"> <div class="center"> <header> <h1> <span>First line</span> <span>Second line</span> </h1> </header> </div> </div>
HTML HTML
<div class="parent">
<div class="center">
<header>
<h1>
<span>First line</span>
<span>Second line</span>
</h1>
</header>
</div>
</div>
CSS CSS
h1 span {
display: block;
}
.parent {
width: 300px;
height: 400px;
display: table;
background: #ccc;
}
.center {
display: table-cell;
text-align: center;
vertical-align: middle;
}
h1{
display: inline-block;
text-align: left;
}
You can do this more succinctly: 您可以更简洁地执行此操作:
header { width: 300px; height: 400px; background: #ccc; text-align: center; display: table-cell; vertical-align: middle; } header h1 { display: inline-block; text-align: left; }
<header> <h1> First line<br/> Second line </h1> </header>
使用跟随CSS:
h1{position:relative; left:50%; transform:translateX(-50%) translateY(0); -o-transform:translateX(-50%) translateY(0); -webkit-transform:translateX(-50%) translateY(0); -o-transform:translateX(-50%) translateY(0); -ms-transform:translateX(-50%) translateY(0); -moz-transform:translateX(-50%) translateY(0); text-align:left}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.