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 the above code I have created a <header>
containing of an <image>
and a <navigation>
. All this works fine so far.
Now I want that the text inside the <li>
element appears in the center. Therefore, I tried to go with the property text-align: center;
in the li CSS
but it did not work.
What do I have to change in my code to get the text within the <li>
elements centered?
You can also find my code here: https://jsfiddle.net/5jv8m5xf/39/
All that needs to be done is to set your <ul> or <ol> as position: relative; so it holds down the fort, display: block; so it takes up the space, and then give it a text-align: center; Next, tell your <li> to have a width: auto; and to, and this is the magic, display: inline; Works every time, palm to forehead.
Just give the list centered text (e.g. ul. nav { text-align: center; } ) and the list items inline-block (e.g. ul. nav li { display: inline-block; } ). If you want to do it with margin for whatever reason, look into width: fit-content; .
To just center the text inside an element, use text-align: center; This text is centered.
text-align:center
does center the text -- but you have to set a specific width for the li
elements; otherwise each of them just collapses to the width of the text itself, so the centering isn't visible.
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>
If you want vertical centering as well, there are a bunch of techniques -- the quickest and dirtiest would be to either add some padding-top
to the li
elements, or set a line-height
that matches the height of the element as a whole.
But a cleaner solution for this particular design would probably be to switch over to flexbox; the code is a bit simpler and it solves the layout problems that occur when the text within a li
wraps over multiple lines:
.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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With