I've got a simple menu like this:
<ul class="menu">
<li>
Home
</li>
<li>
Products
</li>
<li>
Contact
</li>
</ul>
The list elements of the unordered list are floated to the left and have a fixed height:
ul.menu li {
float: left;
height: 50px;
list-style: none;
padding: 0 10px;
background-color: yellow;
}
jsFiddle
Now I would like to vertically center the anchors inside the list elements.
What is the best approach for that?
To vertically center text, set a line-height to the same value as the height of the element. Seeing as you have a set height, this will work with no problems:
.menu li a {
line-height: 50px;
}
http://jsfiddle.net/QNMy7/3/
add line-height equal the height:
.menu li {
float: left;
height: 50px;
**line-height: 50px;**
padding: 0 10px;
list-style: none;
background-color: yellow;
}
.menu li {
float: left;
height: 50px;
padding: 0 10px;
list-style: none;
background-color: yellow;
text-align: center;
min-width: 100px;
}
.menu li a
{
line-height: 50px;
}
The issue is with .menu li class. You have declared a float for it to move to the left, but to make it vertically-aligned, you have to remove the float and apply a display:table-cell; to get the vertical-alignment working.
For Instance,
.menu li {
/*float: left;*/
display:table-cell;
vertical-align:middle;
height: 50px;
padding: 0 10px;
list-style: none;
background-color: yellow;
}
Here is the WORKING SOLUTION
Hope this helps.
You can use line-height to control vertical alignment, but this only works reliably across each list item if they're all restricted to one line.
The most reliable way to achieve this is by removing the float & displaying the list item as a CSS table-cell:
.menu {
display: table;
}
.menu li {
height: 50px;
padding: 0 10px;
list-style: none;
background-color: yellow;
display: table-cell;
vertical-align: middle;
}
.menu li a {
display: block;
}
This way, if one of the links drops onto two lines, it's still vertically centered, as shown here:
http://jsfiddle.net/QNMy7/6/
In addition to float and table-cell, you can also use inline-block:
.menu li {
display: inline-block;
line-height: 50px;
padding: 0 10px;
list-style: none;
background-color: yellow;
}
.menu li a {
vertical-align: middle;
}
Similar to the other posts, I set line-height: 50px on .menu li and vertical-align: middle on the a elements. You can omit the height in this example.
See demo: http://jsfiddle.net/audetwebdesign/QNMy7/7/
Related
I am styling a ul so that I can make a menu that sits on the left side. They will basically be square boxes with icons (using ionicons). The only problem I run into is, I can't get a perfect square and cannot center the icons. Here is what I have now.
.menu_simple ul {
margin: 0;
padding: 100px 0 0 0;
width:185px;
list-style-type: none;
position: fixed;
font-size: 60px;
}
.menu_simple ul li a {
text-decoration: none;
color: white;
padding: 10.5px 60px;
background-color: #F0541E;
display:block; width: 120px; height: 120px;
vertical-align: middle;
}
.menu_simple ul li a:visited {
color: white;
}
.menu_simple ul li a:hover, .menu_simple ul li .current {
color: white;
background-color: #d84b1b
;
}
Also, the html I am currently using looks is this:
<div class="menu_simple">
<ul>
<li><i class="ion-ios-person-outline" style="color:#FFFFFF;" href="#"></i></li>
<li><a class="ion-ios-baseball-outline" style="color:#FFFFFF;" href="#"></a></li>
<li><a class="ion-ios-calendar-outline" style="color:#FFFFFF;" href="#"></a></li>
<li><a class="ion-ios-plus" style="color:#FFFFFF;" href="#"></a></li>
<li><a class="ion-log-out" style="color:#FFFFFF;" href="#">a</a></li>
</ul>
</div>
It currently looks like this:
Thanks everyone. After a little research, this ended up working for me:
.menu_simple ul {
margin: 0;
padding: 100px 0 0 0;
width:185px;
list-style-type: none;
position: fixed;
font-size: 60px;
}
.menu_simple ul li a {
color: #FFF;
padding: 0px;
margin: 0px;
background-color: #F0541E;
display: block;
width: 120px;
height: 120px;
vertical-align: middle;
display: table-cell;
text-align: center;
}
Unless you have removed it with a normalize.css reset or something similar you will need to set padding and margin to zero for your li's in your css. By default li elements in an unordered or ordered list get some left margin to set them apart from their parent ul.
i am trying to make a responsive navigation bar with these 4 elements. however, if i drag the browserwindow to a certrain point it starts placing one of the 4 below the rest. but i dont know what i'm doing wrong.
The HTML:
<div id="nav">
<ul>
<li class="blue">Home</li>
<li class="blue">Trailer</li>
<li class="red">Gallery</li>
<li clas="red">Contact Us</li>
</ul>
</div>
The CSS:
#nav {
width: 100%;
background-color:transparant;
}
#nav ul {
width: 85%;
max-width: 1200px;
padding: 0;
margin: 0 auto;
list-style: none;
text-align: center;
}
#nav ul li {
display: inline-table;
width: 24%;
padding: 4px;
background-color:#242424;
border-radius: 5px;
}
#nav ul li a {
color:white;
text-decoration: none;
width: 100%;
padding-top: 10px;
padding-bottom: 10px;
display: inline-block;
border-radius: 5px;
}
You are maybe having problems with your padding.
For that, add the box-sizing property in your lis and the padding will be included in the width of the element, like:
#nav ul li {
display: inline-table;
width: 24%;
padding: 4px;
background-color:#242424;
border-radius: 5px;
box-sizing: border-box;
}
Fiddle: http://jsfiddle.net/begyu5v3/
Info: http://www.w3schools.com/cssref/css3_pr_box-sizing.asp
And there's those undesirable white spaces between your lis. In this link, there are some ways to avoid this problem, like font-size:
#nav ul {
font-size: 0;
}
#nav ul li {
font-size: 16px;
}
Give it a try and let me know if it helps!
I have a simple menu (centered with margin:0 auto) with some list items.
Now, I'm trying to keep the menu on the same centered position when I add an additional list items.
Here is the fiddle play with it
ul{
list-style-type: none;
background: red;
margin:0 auto;
width: 56%;
max-width:600px
}
ul li{
display: inline-block;
width: 100px;
background-color: #000;
color: #fff;
}
I want to an additional li's to the ul to wrap it and still be centered.
I don't want to use flexbox because IE doesn't support it :D
The problem is solved. Giving the ul {display:table} Thank you all,especially Coop !
Not sure if this is exactly what you're after but I've often had issues centering nav menus and came up with this solution:
ul{
display: table;
margin: 0 auto;
list-style-type: none;
background: red; }
ul li {
float: left;
color: #fff;
background-color: #000; }
Note the li's are floated so you also need to clear them on the ul. I'd suggest a clearfix solution to do that. For example the full code could be:
ul {
display: table;
margin: 0 auto;
list-style-type: none;
background: red; }
ul li {
float: left;
color: #fff;
background-color: #000; }
.clear:before, .clear:after { content: " "; display: table; line-height: 0; }
.clear:after { clear: both; }
.clear { *zoom: 1; }
...
<ul class="clear">
<li>First item here</li>
<li>Second item here</li>
<li>Third item here</li>
</ul>
Take a look at this website I am working on: new.AudioManiacProductions.com
A screenshot from Dreamweaver showing the divs can be found here: new.AudioManiacProductions.com/images/stack.png
Notice how to navigation bar is not centered and the "home" has an intent on the left side.
I want the nav bar to stretch the whole span of it's parent container and be centered both vertically and horizontally.
Here's my CSS for the navigation section of the page, the nav list item, and the links.
#nav {
width: 100%;
height: 40px;
margin: 0 auto;
padding: 0px;
text-align: center;
color: #FFF;
list-style: none;
line-height: auto;
verticle-align: middle;
}
#nav li {
padding: 0px;
display: block;
width: 20%;
list-style-type: none;
float: left;
text-align: center;
overflow: hidden;
line-height: auto;
verticle-align: middle;
}
#nav li a {
font-size: 18px;
font-weight: 600px;
text-decoration: none;
font-weight:800;
color:#FFF;
}
#nav li a:hover, a:visited {
color: #00F;
}
And here is my HTML:
<div id="nav">
<ul>
<li>Home</li>
<li>About Us</li>
<li>Packages</li>
<li>Quote</li>
<li>Contact Us</li>
</ul>
</div>
Any help is greatly appreciated!
Browsers set padding-left by default on UL tags. Remove the padding and that should help out.
#nav ul {
padding-left: 0;
}
or just clear all padding:
#nav ul {
padding: 0;
}
I've got a list here for my page's footer, that I want displayed horizontally.
But because I've turned it into an inline list to go horizontally, the background images get cut off vertically. The biggest one is 27px high.
So I'm stuck.. I know why the following is doing what it's doing. But how do I get around it?
Here's the html:
<div id="footer">
<ul>
<li id="footer-tmdb">Film data courtesy of TMDB</li>
<li id="footer-email">Contact Us</li>
<li id="footer-twitter">Follow Us</li>
</ul>
</div>
and the CSS:
#footer ul {
height: 27px;
}
#footer ul li {
display: inline;
list-style: none;
margin-right: 20px;
}
#footer-tmdb {
background: url('../images/logo-tmdb.png') no-repeat 0 0;
padding-left: 140px;
}
#footer-email {
background: url('../images/icon-email.png') no-repeat 0 3px;
padding-left: 40px;
}
#footer-twitter {
background: url('../images/icon-twitter.png') no-repeat 0 0;
padding-left: 49px;
}
Here's what it looks like:
As you can see, half of the images are cut off.
The simpler the solution, the better, please.
#footer ul li {
display: block;
float: left;
height: 27px;
list-style: none;
margin-right: 20px;
}
Use inline-block
#footer li {
height: 27px;
display: inline-block;
}
Try this:
#footer ul {
overflow: auto
}
#footer ul li {
display: block;
list-style: none;
margin-right: 20px;
float: left;
}
Try this:
#footer li,
#footer ul {
height: 27px;
}