html - 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>
with above code have created <header>
containing of <image>
, <navigation>
. works fine far.
now want text inside <li>
element appears in center. therefore, tried go property text-align: center;
in li css
did not work.
what have change in code text within <li>
elements centered?
you can find code here: https://jsfiddle.net/5jv8m5xf/39/
text-align:center
center text -- have set specific width li
elements; otherwise each of them collapses width of text itself, centering isn't visible.
li { width: 100px; text-align:center; } /* below same 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>
if want vertical centering well, there bunch of techniques -- quickest , dirtiest either add padding-top
li
elements, or set line-height
matches height of element whole.
but cleaner solution particular design switch on flexbox; code bit simpler , solves layout problems occur when text within li
wraps on multiple lines:
.header { background-color: yellow; display: flex; justify-content: space-between; /* puts image @ far left, nav @ far right */ } .image { width: 100px; background-color: green } ul { display: flex; /* puts `li` in row */ list-style: none; margin: 0; padding: 0; background-color: blue; height: 100px; } li { border: 1px solid; width: 100px; /* alternatively, set specific width on ul, , use flex-grow:1 here make li elements same width */ display: flex; /* allows align-items 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 line wrapping</li> <li>short</li> <li>x</li> </ul> </nav> </div>
Comments
Post a Comment