How can I center <ul> <li> into a div? - css

How can I center an unordered list of <li> into a fixed-width div?
<table width="100%">
<tbody>
<tr>
<td width="41%"><img src="/web/20100104192317im_/http://www.studioteknik.com/html2/html/images/hors-service.jpg" width="400" height="424"></td>
<td width="59%"><p align="left"> </p>
<h1 align="left">StudioTeknik.com</h1>
<p><br align="left">
<strong>Marc-André Ménard</strong></p>
<ul>
<li>Photographie digitale</li>
<li>Infographie </li>
<li>Débug et IT (MAC et PC)</li>
<li> Retouche </li>
<li>Site internet</li>
<li>Graphisme</li>
</ul>
<p align="left"><span class="style1"><strong>Cellulaire en suisse : </strong></span>+41 079 573 48 99</p>
<p align="left"><strong class="style1">Skype : </strong> menardmam</p>
<p align="left"><strong class="style1">Courriel :</strong> info#studioteknik.com</p></td>
</tr>
</tbody>
</table>

To center the ul and also have the li elements centered in it as well, and make the width of the ul change dynamically, use display: inline-block; and wrap it in a centered div.
<style type="text/css">
.wrapper {
text-align: center;
}
.wrapper ul {
display: inline-block;
margin: 0;
padding: 0;
/* For IE, the outcast */
zoom:1;
*display: inline;
}
.wrapper li {
float: left;
padding: 2px 5px;
border: 1px solid black;
}
</style>
<div class="wrapper">
<ul>
<li>Three</li>
<li>Blind</li>
<li>Mice</li>
</ul>
</div>
Update
Here is a jsFiddle link to the code above.

Since ul and li elements are display: block by default — give them auto margins and a width that is smaller than their container.
ul {
width: 70%;
margin: auto;
}
If you've changed their display property, or done something that overrides normal alignment rules (such as floating them) then this won't work.

I love flexbox:
ul {
justify-content: center;
display: flex;
}

Steps :
Write style="text-align:center;" to parent div of ul
Write style="display:inline-table;" to ul
Write style="display:inline;" to li
or use
<div class="menu">
<ul>
<li>item 1 </li>
<li>item 2 </li>
<li>item 3 </li>
</ul>
</div>
<style>
.menu { text-align: center; }
.menu ul { display:inline-table; }
.menu li { display:inline; }
</style>

This is a better way to center UL's inside of any DIV container.
This CSS solution does not use Width and Float properties. Float:Left and Width: 70%, will cause you headaches when you need to duplicate your menu on different pages with different menu items.
Instead of using width, we use padding and margin to determine the space around the text/menu item. Also, instead of using Float:Left in the LI element, use display:inline-block.
By floating your LI left, you literally float your content to the left and then you must use one of the Hacks mentioned above to center your UL. Display:inline-block creates your Float property for you (sort of). It takes your LI element and turns it into a block element that lays side by side each other (not floating).
With Responsive design and using frameworks like Bootstrap or Foundation, there will be issues when trying to float and center content. They have some built-in classes, but it's always better to do it from scratch. This solution is much better for dynamic menus (Such as Adobe Business Catalyst menu system).
Reference for this tutorial can be found at: http://html-tuts.com/center-div-image-table-ul-inside-div/
HTML
<div class="container">
<ul>
<li>Button</li>
<li>Button</li>
<li>Button</li>
<li>Button</li>
<li>Button</li>
</ul>
</div>
CSS
.container {
text-align: center;
border: 1px solid green;
}
.container ul {
border: 2px solid red;
display: inline-block;
margin: 10px 0;
padding: 2px;
}
.container li {
display: inline-block;
}
.container li a {
display: inline-block;
background: #444;
color: #FFF;
padding: 5px;
text-decoration: none;
}

Could either be
div ul
{
width: [INSERT FIXED WIDTH]
margin: 0 auto;
}
or
div li
{
text-align: center;
}
depends on how it should look like (or combining those)

To center a block object (e.g. the ul) you need to set a width on it and then you can set that objects left and right margins to auto.
To center the inline content of block object (e.g. the inline content of li) you can set the css property text-align: center;.

Try
div#divID ul {margin:0 auto;}

Just add text-align: center; to your <ul>. Problem solved.

Interesting but try this with floated li elements inside the ul:
Example here
The problem now: the ul needs a fixed width to actually sit in the center. However we want to be it relative to the container width (or dynamic), margin: 0 auto on the ul does not work.
A better way is to let go of UL/Li list and use a different approach example here

If you know the width of the ul then you can simply set the margin of the ul to 0 auto;
This will align the ul in the middle of the containing div
Example:
HTML:
<div id="container">
<ul>
<li>Item1</li>
<li>Item2</li>
</ul>
<div>
CSS:
#container ul{
width:300px;
margin:0 auto;
}

Here is the solution I could find:
#wrapper {
float:right;
position:relative;
left:-50%;
text-align:left;
}
#wrapper ul {
list-style:none;
position:relative;
left:50%;
}
#wrapper li{
float:left;
position:relative;
}

Another option is:
HTML
<nav>
<ul class = "main-nav">
<li> Productos </li>
<li> Catalogo </li>
<li> Contact </li>
<li> Us </li>
</ul>
</nav>
CSS:
nav {
text-align: center;
}
nav .main-nav li {
float: left;
width: 20%;
margin-right: 5%;
font-size: 36px;
text-align: center;
}

I have been looking for the same case and tried all answers by change the width of <li>.
Unfortunately all were failed to get the same distance on left and right of the <ul> box.
The closest match is this answer but it needs to adjust the change of width with padding
.container ul {
...
padding: 10px 25px;
}
.container li {
...
width: 100px;
}
See the result below, all distance between <li> also to the <ul> box are the same.
You may check it on this jsFiddle:
http://jsfiddle.net/qwbexxog/14/

<div id="container">
<table width="100%" height="100%">
<tr>
<td align="center" valign="middle">
<ul>
<li>item 1</li>
<li>item 2</li>
<li>item 3</li>
</ul>
</td>
</tr>
</table>
</div>

use oldschool center-tags
<div> <center> <ul> <li>...</li> </ul></center> </div>
:-)

Related

Centering Navigation around a centre logo image

I have a navigation bar as shown here: http://i.imgur.com/4rxkS2K.jpg
I am using foundation to build a website, the way I have built the nav bar is as follows:
HTML:
<nav class="top-bar">
<ul>
<li>About</li>
<li id="menu-divider">|</li>
<li>Testimonials</li>
<li><img src="images/logo.png" alt=""></li>
<li>Services</li>
<li id="menu-divider">|</li>
<li>Contact</li>
</ul>
</nav>
CSS:
.top-bar { font-family: 'bebas_neueregular';
height: 150px;
line-height: 100px;
padding: 18px;
width: 100%;
position: relative;
text-align:center;
margin-bottom:10px; }
.top-bar ul { display:inline-block;
margin-left: auto ;
margin-right: auto ;}
.top-bar ul > li { display:inline-block;
margin-left: auto ;
margin-right: auto ;}
#menu-divider { color:#ffffff;
font-size: 24px;}
As you can see in the picture, the way I have built it means that my center li element (my logo picture) is not in exact center as the other li elements are of different widths meaning they are all centered collectively. What I'm after is the logo in the dead center then the other li elements as they are centered around the logo.
Thanks in advance for any help!
You can play around but I'm pretty sure this does the trick:
http://codepen.io/anon/pen/dYXQpz
Use 3 containers (that means you lose your nav as a ul). Flex them and inside of the left and right one, flex the elements (end for the first, start for the other)
<div class="nav-bar">
<div class="sideNav leftNav">
<div class="menu">
MENU 1
</div>
<div class="split"></div>
<div class="menu">
MENU 2
</div>
</div>
<div class="logo">
<img src="https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcSN9qhGx6NftAepiMOjdGXkcW-UxkO9dtQ4VGRlepyzNC2S8xQCcA" />
</div>
<div class="sideNav rightNav">
<div class="menu">
MENU 3
</div>
<div class="split"></div>
<div class="menu">
MENU 4
</div>
</div>
</div>
Then apply the css. It can be improved but it can help you get started.
.nav-bar {
background: pink;
display: flex;
}
.sideNav {
flex: 1 0 auto;
background: red;
display: flex;
}
.leftNav {
justify-content: flex-end;
}
.rightNav {
justify-content: flex-start;
}
.sideNav > div {
margin: 100px 20px 0 20px;
}
.split{width: 2px;background: white;height: 16px}
Hope that helps. I loves flexbox.

CSS: can't center a UL

On this page, I have a footer menu #menu-footer-menu; "About - Select Your City - How Walks Work..." generated by Wordpress.
<div class="menu-footer-menu-container">
<ul id="menu-footer-menu" class="menu">
<li id="menu-item-143">About</li>
<li id="menu-item-151">Select Your City</li>
<li id="menu-item-162">How Walks Work</li>
<li id="menu-item-160">FAQs</li>
<li id="menu-item-166">Blog</li>
<li id="menu-item-153">Partners</li>
<li id="menu-item-154">Press</li>
<li id="menu-item-144">Privacy Policy</li>
<li id="menu-item-145">Site Map</li>
<li id="menu-item-146">Terms & Conditions</li>
</ul>
</div>
I want to center this menu.
I tried the following CSS but it didn't work:
div.menu-footer-menu-container {text-align:center;}
ul#menu-footer-menu.menu {
list-style-type: none;
width: 760px !important;
margin: 0 auto !important;
display: inline-block !important;
}
ul#menu-footer-menu li {
display: inline-block !important;
margin-right: 14px;
margin-right: 1rem;
}
Use following css instead of what you have.
I just changed display for ul#menu-footer-menu.menu to block nothing else.
div.menu-footer-menu-container {text-align:center;}
ul#menu-footer-menu.menu {
list-style-type: none;
width: 760px !important;
margin: 0 auto !important;
display: block !important;
}
ul#menu-footer-menu li {
display: inline-block !important;
margin-right: 14px;
margin-right: 1rem;
}
You need to add one wrapper element with a width of 100% to center your div in.
Like:
<div class="wrapper">
<div class="menu-footer-menu-container">
...
</div>
.wrapper{ width:100% }
Use display:block instead of inline-block
ul#menu-footer-menu.menu {
list-style-type: none;
width: 760px !important;
margin: 0 auto !important;
display: block !important;
}
Try to add CSS Style :
.menu-footer-menu-container
{
text-align: center;
}
You have two ways to do it
width degradation center tag
<center>
<ul>
...
</ul>
</center>
Create another div to force the center
.center { margin: auto; text-align: center; }
<div class="center">
<ul>
...
</ul>
</div>
Note: The tag must be inline-block.
width text-align:center;
ul{ text-align: center; }
<center>
<ul>
...
</ul>
</center>
Add one more style text-align:center to #footer-menu in css.

HTML 5 Doctype creates extra space for justified horizontal unorderd list menu

Okay I have been attempting to find the best way to evenly space <li> menu items horizontally for a variable amount of menu items.
I have found that applying text-align:justify to the containing div, display:inline to the ul, display:inline-block to the <li>, and adding a span after the ul with:
width:100%;
display:inline-block;
height:0
will generate the desired effect.
However, there is a lot of inexplicable extra space under the ul. After many hours of headaches I figured out that it appears only in an html file with an HTML 5 Doctype. If it has the old XHTML Transitional Doctype it displays exactly as expected. My question is: why is this extra space there and how do I get rid of it?
Here's the markup I've been toying with:
body {
margin: 0;
padding: 0;
}
.nav {
width: 900px;
margin: 0 auto;
position: relative;
text-align: justify;
background-color: #666;
font-family: Arial, Helvetica, sans-serif;
}
.nav ul {
display: inline;
width: 100%;
padding: 0;
margin: 0;
list-style: none;
}
.nav li {
display: inline-block;
font-size: 1.2em;
}
.nav li a {
display: inline-block;
color: #FFF;
text-decoration: none;
padding: 10px;
}
span {
display: inline-block;
width: 100%;
height: 0;
color: #F00;
}
<div class="nav">
<ul>
<li> texttexttext</li>
<li> tex </li>
<li> texttext </li>
<li> text </li>
<li> text </li>
<li> texttexttext </li>
<li> texttexttext </li>
<li> texttext </li>
</ul>
<span></span>
</div>
jsfiddle
Add line-height:0 to the div with class nav, line-height:1.2 to the ul and vertical-align:top to the span.
Also consider replacing the span with a .nav:after pseudo-element.
Like this: http://jsfiddle.net/frhxS/13/
The principle behind the changes is to remove the effect of the strut on the two line boxes. The strut has no effect on the height of the line boxes when using XHTML 1.0 Transitional doctype or the HTML 4.01 Transitional doctype but does set a minimum height for the line boxes when used with either the HTML5 doctype or the XHTML 1.0 Strict or HTML 4.01 Strict doctypes.
did you try:
.nav ul{
display:inline;
width:100%;
padding:0;
margin:0;
list-style:none;
}
instead of:
.nav>ul{
display:inline;
width:100%;
padding:0;
margin:0;
list-style:none;
}
Try setting the width property of the li to 12% and getting rid of the span element.
body{
margin:0;
padding:0;
}
.nav{
width:900px;
margin:0 auto;
position:relative;
text-align:justify;
background-color:#666;
white-space:no-wrap;
font-family:Arial, Helvetica, sans-serif;
}
.nav>ul{
display:inline;
width:100%;
padding:0;
margin:0;
list-style:none;
}
.nav li{
display:inline-block;
font-size:1.2em;
width: 12%;
text-align:center;
}
.nav li a{
display:inline-block;
color:#FFF;
text-decoration:none;
padding:10px;
}
and
<div class="nav">
<ul>
<li> texttexttext</li>
<li> tex </li>
<li> texttext </li>
<li> text </li>
<li> text </li>
<li> texttexttext </li>
<li> texttexttext </li>
<li> texttext </li>
</ul>
</div>
.nav{ white-space:nowrap; } not white-space:no-wrap. This will fix your problem

Centering floating elements

Currently I have the following code...
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<meta charset="UTF-8" />
<link href="verna.css" type="text/css" rel="stylesheet" />
</head>
<body>
<section id="devices">
<h1>Gruppen</h1>
<div class="group">
<h2>Gruppe 1</h2>
<ul>
<li class="device">Schalter 1</li>
<li class="device">Schalter 2</li>
<li class="device">Schalter 3</li>
</ul>
</div>
<div class="group">
<h2>Gruppe 2</h2>
<ul>
<li class="device">Schalter 1</li>
<li class="device">Schalter 2</li>
<li class="device">Schalter 3</li>
</ul>
</div>
</section>
</body>
</html>
* {
margin: 0;
padding: 0;
}
ul {
list-style-type: none;
}
#devices {
background-color: yellow;
}
#devices .group {
background-color: gray;
}
#devices .group ul {
text-align: center;
}
#devices .group .device {
background-color: green;
display: inline-block;
margin: 5px;
max-width: 200px;
width: 100%;
}
... which looks like this:
But I want that the green <li>-elements floats in columns. It should look like this:
Please note: The number of the green <li>-elements is variable, there can be more or less than three elements and there can be more than two columns! I want to order the <li>-elements "column-wise" and center them...
Thanks for help :-)
The thing that is centering "Schalter 3" is text-align: center;. We can fix this by removing
#devices .group ul {
text-align: center;
}
and adding:
#devices .group ul {
margin-left: 50px;
}
#devices .group li {
text-align: center;
}
in its place. This centers the text in the li element, instead of centering the li element inside the ul. And it adds the margin to the left to get the indent you wanted.
Working Fiddle.
Restructure your html so that each "column" is it's own container (div) which has an appropriate width. Alternatively, if you hate yourself, use a table.
Check out this fiddle.
The trick was to turn the lis back into block-level elements so that they could have width. Then set width: 40%; (40% leaves a little room for the 5px margin) and float: left;. Also adding overflow: auto; to the ul so that it would contain its floated lis.

CSS Positioning - Unordered List

I want to position an unordered list of items on the left-hand side of a menu header (horizontally displayed, not vertically). For example where you see Home, HTML, etc:
How do I accomplish this effect with CSS?
Floats
ul
{
margin: 0;
list-style-type: none;
}
ul li
{
margin: 0;
list-style-type: none;
float: left;
}
ul li a
{
display: block;
}
<ul>
<li>Home</li>
<li>HTML</li>
<li>...</li>
</ul>
To get the lists like you have in your image, you will need to have two sets of UL and then apply a float: left; to the left one and a float: right; to the right.
With floats you have to clear them to avoid "breaking" your design later. You can do this by adding a <br style="clear: both;" /> below the lists. You can also add that style to the next div.
.menu{
text-align:left;
}
.menu ul{
margin:0px;
padding:0px;
}
.menu ul li{
display:inline;
margin:0px;
padding:0px 10px 0px 10px;
text-align:center;
}
<div class="menu">
<ul>
<li>Menu1</li>
<li>Menu2</li>
<li>Menu3</li>
</ul>
</div>

Resources