<li> 元素中的文本居中对齐
text-align center in <li> element
body {
margin: 0;
}
.header {
width: 80%;
height: 20%;
margin-left: 10%;
position: fixed;
top: 0;
box-sizing: border-box;
border-style: solid;
border-width: 1px;
background-color: green;
}
.image {
width: 20%;
height: 100%;
float: left;
box-sizing: border-box;
border-style: solid;
border-width: 1px;
}
.navigation {
width: 79%;
height: 100%;
float: right;
text-align: right;
box-sizing: border-box;
border-style: solid;
border-width: 1px;
}
ul {
height: 100%;
font-size: 0;
box-sizing: border-box;
border-style: solid;
border-width: 1px;
background-color: yellow;
}
li {
height: 100%;
font-size: initial;
display: inline-block;
box-sizing: border-box;
border-style: solid;
border-width: 1px;
background-color: blue;
}
<div class="header">
<div class="image">
Image
</div>
<nav class="navigation">
<ul>
<li> 1.0 Main Menu </li>
<li> 2.0 Main Menu </li>
<li> 3.0 Main Menu </li>
</ul>
</nav>
</div>
使用上面的代码,我创建了一个 <header>
,其中包含一个 <image>
和一个 <navigation>
。到目前为止,这一切都很好。
现在我希望 <li>
元素中的 text 出现在 center 中。因此,我尝试在 li CSS
中使用 属性 text-align: center;
但它没有用。
我必须在我的代码中更改什么才能在 <li>
元素 centered 中获得 text?
你也可以在这里找到我的代码:https://jsfiddle.net/5jv8m5xf/39/
text-align:center
确实使文本居中——但是您必须为 li
元素设置一个特定的宽度;否则它们中的每一个都只是折叠到文本本身的宽度,所以居中是不可见的。
li {
width: 100px;
text-align:center;
}
/* Everything below is the same as your original code */
body {
margin: 0;
}
.header {
width: 80%;
height: 20%;
margin-left: 10%;
position: fixed;
top: 0;
box-sizing: border-box;
border-style: solid;
border-width: 1px;
background-color: green;
}
.image {
width: 20%;
height: 100%;
float: left;
box-sizing: border-box;
border-style: solid;
border-width: 1px;
}
.navigation {
width: 79%;
height: 100%;
float: right;
text-align: right;
box-sizing: border-box;
border-style: solid;
border-width: 1px;
}
ul {
height: 100%;
font-size: 0;
box-sizing: border-box;
border-style: solid;
border-width: 1px;
background-color: yellow;
}
li {
height: 100%;
font-size: initial;
display: inline-block;
box-sizing: border-box;
border-style: solid;
border-width: 1px;
background-color: blue;
}
<div class="header">
<div class="image">
Image
</div>
<nav class="navigation">
<ul>
<li> Longer text</li>
<li> Short </li>
<li> X </li>
</ul>
</nav>
</div>
如果你也想要垂直居中,有很多技巧——最快和最脏的方法是向 li
元素添加一些 padding-top
,或者设置一个 line-height
匹配整个元素的高度。
但是针对这个特定设计的更简洁的解决方案可能是切换到 flexbox;代码有点简单,它解决了 li
中的文本换行时出现的布局问题:
.header {
background-color: yellow;
display: flex;
justify-content: space-between; /* Puts the image at far left, nav at far right */
}
.image {
width: 100px;
background-color: green
}
ul {
display: flex; /* Puts the `li` in a row */
list-style: none; margin: 0; padding: 0;
background-color: blue;
height: 100px;
}
li {
border: 1px solid;
width: 100px; /* Alternatively, you could set a specific width on the ul, and use flex-grow:1 here to make all the li elements the same width */
display: flex; /* allows align-items to work below */
justify-content: center; /* Horizontally centers single-line elements */
text-align:center; /* Horizontally centers text within line-wrapped elements */
align-items: center; /* vertical */
}
<div class="header">
<div class="image">
Image
</div>
<nav class="navigation">
<ul>
<li>Very long text with line wrapping</li>
<li>Short</li>
<li>X</li>
</ul>
</nav>
</div>
将 宽度 设置为 li
以使 text-align: center
相关。
使用 psuedo 元素 垂直 对齐元素的一种方法 - 添加 vertical-align: middle
到您的 li
和这个 psuedo after
元素到你的 css:
li:after {
content: '';
height: 100%;
display: inline-block;
vertical-align: middle;
}
参见下面的演示:
body {
margin: 0;
}
.header {
width: 80%;
height: 20%;
margin-left: 10%;
position: fixed;
top: 0;
box-sizing: border-box;
border-style: solid;
border-width: 1px;
background-color: green;
}
.image {
width: 20%;
height: 100%;
float: left;
box-sizing: border-box;
border-style: solid;
border-width: 1px;
}
.navigation {
width: 79%;
height: 100%;
float: right;
text-align: right;
box-sizing: border-box;
border-style: solid;
border-width: 1px;
}
ul {
height: 100%;
font-size: 0;
box-sizing: border-box;
border-style: solid;
border-width: 1px;
background-color: yellow;
}
li {
height: 100%;
font-size: initial;
display: inline-block;
box-sizing: border-box;
border-style: solid;
border-width: 1px;
background-color: blue;
text-align: center;
vertical-align: middle;
}
li:after {
content: '';
height: 100%;
display: inline-block;
vertical-align: middle;
}
<div class="header">
<div class="image">
Image
</div>
<nav class="navigation">
<ul>
<li> 1.0 Main Menu </li>
<li> 2.0 Main Menu </li>
<li> 3.0 Main Menu </li>
</ul>
</nav>
</div>
如果 flexbox
是您可以使用 display: inline-flex
的选项,事情可以像为 [=46= 添加 justify-content: center
一样简单]水平和align-items: center
用于垂直对齐。
参见下面的演示:
body {
margin: 0;
}
.header {
width: 80%;
height: 20%;
margin-left: 10%;
position: fixed;
top: 0;
box-sizing: border-box;
border-style: solid;
border-width: 1px;
background-color: green;
}
.image {
width: 20%;
height: 100%;
float: left;
box-sizing: border-box;
border-style: solid;
border-width: 1px;
}
.navigation {
width: 79%;
height: 100%;
float: right;
text-align: right;
box-sizing: border-box;
border-style: solid;
border-width: 1px;
}
ul {
height: 100%;
font-size: 0;
box-sizing: border-box;
border-style: solid;
border-width: 1px;
background-color: yellow;
}
li {
height: 100%;
font-size: initial;
display: inline-block;
box-sizing: border-box;
border-style: solid;
border-width: 1px;
background-color: blue;
/*ADDED THESE*/
display: inline-flex;
justify-content: center;
align-items: center;
}
<div class="header">
<div class="image">
Image
</div>
<nav class="navigation">
<ul>
<li> 1.0 Main Menu </li>
<li> 2.0 Main Menu </li>
<li> 3.0 Main Menu </li>
</ul>
</nav>
</div>
body {
margin: 0;
}
.header {
width: 80%;
height: 20%;
margin-left: 10%;
position: fixed;
top: 0;
box-sizing: border-box;
border-style: solid;
border-width: 1px;
background-color: green;
}
.image {
width: 20%;
height: 100%;
float: left;
box-sizing: border-box;
border-style: solid;
border-width: 1px;
}
.navigation {
width: 79%;
height: 100%;
float: right;
text-align: right;
box-sizing: border-box;
border-style: solid;
border-width: 1px;
}
ul {
height: 100%;
font-size: 0;
box-sizing: border-box;
border-style: solid;
border-width: 1px;
background-color: yellow;
}
li {
height: 100%;
font-size: initial;
display: inline-block;
box-sizing: border-box;
border-style: solid;
border-width: 1px;
background-color: blue;
}
<div class="header">
<div class="image">
Image
</div>
<nav class="navigation">
<ul>
<li> 1.0 Main Menu </li>
<li> 2.0 Main Menu </li>
<li> 3.0 Main Menu </li>
</ul>
</nav>
</div>
使用上面的代码,我创建了一个 <header>
,其中包含一个 <image>
和一个 <navigation>
。到目前为止,这一切都很好。
现在我希望 <li>
元素中的 text 出现在 center 中。因此,我尝试在 li CSS
中使用 属性 text-align: center;
但它没有用。
我必须在我的代码中更改什么才能在 <li>
元素 centered 中获得 text?
你也可以在这里找到我的代码:https://jsfiddle.net/5jv8m5xf/39/
text-align:center
确实使文本居中——但是您必须为 li
元素设置一个特定的宽度;否则它们中的每一个都只是折叠到文本本身的宽度,所以居中是不可见的。
li {
width: 100px;
text-align:center;
}
/* Everything below is the same as your original code */
body {
margin: 0;
}
.header {
width: 80%;
height: 20%;
margin-left: 10%;
position: fixed;
top: 0;
box-sizing: border-box;
border-style: solid;
border-width: 1px;
background-color: green;
}
.image {
width: 20%;
height: 100%;
float: left;
box-sizing: border-box;
border-style: solid;
border-width: 1px;
}
.navigation {
width: 79%;
height: 100%;
float: right;
text-align: right;
box-sizing: border-box;
border-style: solid;
border-width: 1px;
}
ul {
height: 100%;
font-size: 0;
box-sizing: border-box;
border-style: solid;
border-width: 1px;
background-color: yellow;
}
li {
height: 100%;
font-size: initial;
display: inline-block;
box-sizing: border-box;
border-style: solid;
border-width: 1px;
background-color: blue;
}
<div class="header">
<div class="image">
Image
</div>
<nav class="navigation">
<ul>
<li> Longer text</li>
<li> Short </li>
<li> X </li>
</ul>
</nav>
</div>
如果你也想要垂直居中,有很多技巧——最快和最脏的方法是向 li
元素添加一些 padding-top
,或者设置一个 line-height
匹配整个元素的高度。
但是针对这个特定设计的更简洁的解决方案可能是切换到 flexbox;代码有点简单,它解决了 li
中的文本换行时出现的布局问题:
.header {
background-color: yellow;
display: flex;
justify-content: space-between; /* Puts the image at far left, nav at far right */
}
.image {
width: 100px;
background-color: green
}
ul {
display: flex; /* Puts the `li` in a row */
list-style: none; margin: 0; padding: 0;
background-color: blue;
height: 100px;
}
li {
border: 1px solid;
width: 100px; /* Alternatively, you could set a specific width on the ul, and use flex-grow:1 here to make all the li elements the same width */
display: flex; /* allows align-items to work below */
justify-content: center; /* Horizontally centers single-line elements */
text-align:center; /* Horizontally centers text within line-wrapped elements */
align-items: center; /* vertical */
}
<div class="header">
<div class="image">
Image
</div>
<nav class="navigation">
<ul>
<li>Very long text with line wrapping</li>
<li>Short</li>
<li>X</li>
</ul>
</nav>
</div>
将 宽度 设置为 li
以使 text-align: center
相关。
使用 psuedo 元素 垂直 对齐元素的一种方法 - 添加 vertical-align: middle
到您的 li
和这个 psuedo after
元素到你的 css:
li:after {
content: '';
height: 100%;
display: inline-block;
vertical-align: middle;
}
参见下面的演示:
body {
margin: 0;
}
.header {
width: 80%;
height: 20%;
margin-left: 10%;
position: fixed;
top: 0;
box-sizing: border-box;
border-style: solid;
border-width: 1px;
background-color: green;
}
.image {
width: 20%;
height: 100%;
float: left;
box-sizing: border-box;
border-style: solid;
border-width: 1px;
}
.navigation {
width: 79%;
height: 100%;
float: right;
text-align: right;
box-sizing: border-box;
border-style: solid;
border-width: 1px;
}
ul {
height: 100%;
font-size: 0;
box-sizing: border-box;
border-style: solid;
border-width: 1px;
background-color: yellow;
}
li {
height: 100%;
font-size: initial;
display: inline-block;
box-sizing: border-box;
border-style: solid;
border-width: 1px;
background-color: blue;
text-align: center;
vertical-align: middle;
}
li:after {
content: '';
height: 100%;
display: inline-block;
vertical-align: middle;
}
<div class="header">
<div class="image">
Image
</div>
<nav class="navigation">
<ul>
<li> 1.0 Main Menu </li>
<li> 2.0 Main Menu </li>
<li> 3.0 Main Menu </li>
</ul>
</nav>
</div>
如果 flexbox
是您可以使用 display: inline-flex
的选项,事情可以像为 [=46= 添加 justify-content: center
一样简单]水平和align-items: center
用于垂直对齐。
参见下面的演示:
body {
margin: 0;
}
.header {
width: 80%;
height: 20%;
margin-left: 10%;
position: fixed;
top: 0;
box-sizing: border-box;
border-style: solid;
border-width: 1px;
background-color: green;
}
.image {
width: 20%;
height: 100%;
float: left;
box-sizing: border-box;
border-style: solid;
border-width: 1px;
}
.navigation {
width: 79%;
height: 100%;
float: right;
text-align: right;
box-sizing: border-box;
border-style: solid;
border-width: 1px;
}
ul {
height: 100%;
font-size: 0;
box-sizing: border-box;
border-style: solid;
border-width: 1px;
background-color: yellow;
}
li {
height: 100%;
font-size: initial;
display: inline-block;
box-sizing: border-box;
border-style: solid;
border-width: 1px;
background-color: blue;
/*ADDED THESE*/
display: inline-flex;
justify-content: center;
align-items: center;
}
<div class="header">
<div class="image">
Image
</div>
<nav class="navigation">
<ul>
<li> 1.0 Main Menu </li>
<li> 2.0 Main Menu </li>
<li> 3.0 Main Menu </li>
</ul>
</nav>
</div>