Horizontal unordered list - css

I struggle with unordered list. Is there a way to make them horizontal? Please see the jsFiddle. The red boxes are suppose to be in front of each text and spaced accordingly. What am I missing, which I am sure a lot.
http://jsfiddle.net/5Lmymdwh/
#alertLegend li {
display: inline;
font-size:10px;
height:15px;
left:10px;
list-style-type:none;
margin:.5em .5em 0em -3.4em;
position:relative;
text-align:left;
}
.legendDiv {
background-color:#8B0000;
border:solid 1px #333;
float:left;
margin-right:2px;
width:15px;
}
.legendTxt {
font-size:12px;
color:#555;
padding-left:25px;
}
<ul id='alertLegend'>
<li><div class='legendDiv'> </div><span class='legendTxt'> Demo 1</span></li>
<li><div class='legendDiv'> </div><span class='legendTxt'> Demo 2</span></li>
<li><div class='legendDiv'> </div><span class='legendTxt'> Demo 3</span></li>
</ul>
-Thanks

I updated your JSFiddler HERE
You basically had a combination of mistakes. The code is simpler that you started with. The biggest problem you had was to give your red box a float:left This automaticaly pushes the element to the absolute left. The other problem you had was to create the divs inside. Your list item doesn't need a span tag. It is already wrapped by the <li> tags. The square however does need span tags to align it vertically and style it differently without affecting the rest of the <li> content.

Instead of using extra div for that custom bullet you could use :before : pseudo-element.
#alertLegend li {
display: inline-block;
font-size: 10px;
height: 15px;
font-size: 12px;
color: #555;
list-style-type: none;
}
#alertLegend li:not(:last-child) {
margin-right: 10px;
}
#alertLegend li:before {
content: '';
border: solid 1px #333;
background-color: #8B0000;
float: left;
height: 12px;
width: 15px;
}
<ul id='alertLegend'>
<li>Demo 1</li>
<li>Demo 2</li>
<li>Demo 3</li>
</ul>

Here is working css:
#alertLegend li {
display: inline-block;
list-style-type:none;
height:15px;
}
.legendDiv {
background-color:#8B0000;
border:solid 1px #333;
float:left;
margin-right:2px;
width:15px;
height: 15px;
}
.legendTxt {
font-size:12px;
color:#555;
}

Not too sure what you're exactly trying to achieve but you can simplify your HTML/CSS a lot further by using border-left or background.
See here: http://jsfiddle.net/pavkr/yrxbLvkf/2/

When I looked at the JS fiddle, I noticed that the text wasn't in the div's at all. I have since moved them, and altered the text color to be more legible inside the div's. Also if you want to format the text inside a div, you can make the edits there instead of a span class like you had (has been removed, albeit altered but to show simplicity).
#alertLegend li {
display: inline;
font-size:10px;
height:15px;
left:10px;
list-style-type:none;
margin:.5em .5em 0em -3.4em;
position:relative;
text-align:left;
}
.legendDiv {
background-color:#8B0000;
border:solid 1px #333;
float:left;
margin-right:2px;
width:30px;
color: white;
}
<ul id='alertLegend'>
<li>
<div class='legendDiv'>Demo1</div>
</li>
<li>
<div class='legendDiv'>Demo2</div>
</li>
<li>
<div class='legendDiv'>Demo3</div>
</li>
</ul>

Related

Wordpress Avada Theme Secondary Menu Align Center [duplicate]

I need to center align a horizontal menu.
I've tried various solutions, including the mix of inline-block / block / center-align etc., but haven't succeeded.
Here is my code:
<div class="topmenu-design">
<!-- Top menu content: START -->
<ul id="topmenu firstlevel">
<li class="firstli" id="node_id_64"><div><span>Om kampanjen</span></div></li>
<li id="node_id_65"><div><span>Fakta om inneklima</span></div></li>
<li class="lastli" id="node_id_66"><div><span>Statistikk</span></div></li>
</ul>
<!-- Top menu content: END -->
</div>
UPDATE
I know how to center align the ul within the div. That can be accomplished using Sarfraz's suggestion.
But the list items are still floated left within the ul.
Do I need Javascript to accomplish this?
From http://pmob.co.uk/pob/centred-float.htm:
The premise is simple and basically just involves a widthless float wrapper that is floated to the left and then shifted off screen to the left width position:relative; left:-50%. Next the nested inner element is reversed and a relative position of +50% is applied. This has the effect of placing the element dead in the center. Relative positioning maintains the flow and allows other content to flow underneath.
Code
#buttons{
float:right;
position:relative;
left:-50%;
text-align:left;
}
#buttons ul{
list-style:none;
position:relative;
left:50%;
}
#buttons li{float:left;position:relative;}/* ie needs position:relative here*/
#buttons a{
text-decoration:none;
margin:10px;
background:red;
float:left;
border:2px outset blue;
color:#fff;
padding:2px 5px;
text-align:center;
white-space:nowrap;
}
#buttons a:hover{ border:2px inset blue;color:red;background:#f2f2f2;}
#content{overflow:hidden}/* hide horizontal scrollbar*/
<div id="buttons">
<ul>
<li>Button 1</li>
<li>Button 2's a bit longer</li>
<li>Butt 3</li>
<li>Button 4</li>
</ul>
</div>
This works for me. If I haven't misconstrued your question, you might give it a try.
div#centerDiv {
width: 100%;
text-align: center;
border: 1px solid red;
}
ul.centerUL {
margin: 2px auto;
line-height: 1.4;
padding-left: 0;
}
.centerUL li {
display: inline;
text-align: center;
}
<div id="centerDiv">
<ul class="centerUL">
<li>Amazon 1 </li>
<li>Amazon 2 </li>
<li>Amazon 3</li>
</ul>
</div>
With CSS3 flexbox. Simple.
ul {
display: flex;
justify-content: center;
}
ul li {
padding: 0 8px;
}
This is the simplest way I found. I used your html. The padding is just to reset browser defaults.
ul {
text-align: center;
padding: 0;
}
li {
display: inline-block;
}
<div class="topmenu-design">
<!-- Top menu content: START -->
<ul id="topmenu firstlevel">
<li class="firstli" id="node_id_64">
<div><span>Om kampanjen</span>
</div>
</li>
<li id="node_id_65">
<div><span>Fakta om inneklima</span>
</div>
</li>
<li class="lastli" id="node_id_66">
<div><span>Statistikk</span>
</div>
</li>
</ul>
<!-- Top menu content: END -->
</div>
Here's a good article on how to do it in a pretty rock-solid way, without any hacks and full cross-browser support. Works for me:
--> http://matthewjamestaylor.com/blog/beautiful-css-centered-menus-no-hacks-full-cross-browser-support
Try this:
div.topmenu-design ul
{
display:block;
width:600px; /* or whatever width value */
margin:0px auto;
}
Do it like this :
<div id="footer">
<ul>
<li>Link 1</li>
<li>Link 2</li>
<li>Link 3</li>
<li>Link 4</li>
<li>Link 5</li>
</ul>
</div>
And the CSS:
#footer {
background-color:#ccc;
height:39px;
line-height:36px;
margin:0 auto;
text-align:center;
width:950px;
}
#footer ul li {
display:inline;
font-family:Arial,sans-serif;
font-size:1em;
padding:0 2px;
text-decoration:none;
}
Like so many of you, I've been struggling with this for a while. The solution ultimately had to do with the div containing the UL. All suggestions on altering padding, width, etc. of the UL had no effect, but the following did.
It's all about the margin:0 auto; on the containing div. I hope this helps some people, and thanks to everyone else who already suggested this in combination with other things.
.divNav
{
width: 99%;
text-align:center;
margin:0 auto;
}
.divNav ul
{
display:inline-block;
list-style:none;
zoom: 1;
}
.divNav ul li
{
float:left;
margin-right: .8em;
padding: 0;
}
.divNav a, #divNav a:visited
{
width: 7.5em;
display: block;
border: 1px solid #000;
border-bottom:none;
padding: 5px;
background-color:#F90;
text-decoration: none;
color:#FFF;
text-align: center;
font-family:Verdana, Geneva, sans-serif;
font-size:1em;
}
Demo - http://codepen.io/grantex/pen/InLmJ
<div class="navigation">
<ul>
<li>Home</li>
<li>About</li>
<li>Contact</li>
<li>Menu</li>
<li>Others</li>
</ul>
</div>
.navigation {
max-width: 700px;
margin: 0 auto;
}
.navigation ul {
list-style: none;
display: table;
width: 100%;
}
.navigation ul li {
display: table-cell;
text-align: center;
}
.navigation ul li a {
padding: 5px 10px;
width: 100%;
}
Omg so much cleaner.
Generally speaking the way to center a black level element (like a <ul>) is using the margin:auto; property.
To align text and inline level elements within a block level element use text-align:center;. So all together something like...
ul {
margin:auto;
}
ul li {
text-align:center;
list-style-position:inside; /* so that the bullet points are also centered */
}
ul li div {
display:inline; /* so that the bullet points aren't above the content */
}
... should work.
The fringe case is Internet Explorer6... or even other IEs when not using a <!DOCTYPE>. IE6 incorrectly aligns block level elemnts using text-align. So if you're looking to support IE6 (or not using a <!DOCTYPE>) your full solution is...
div.topmenu-design {
text-align:center;
}
div.topmenu-design ul {
margin:auto;
}
div.topmenu-design ul li {
text-align:center;
list-style-position:inside; /* so that the bullet points are also centered */
}
div.topmenu-design ul li div {
display:inline; /* so that the bullet points aren't above the content */
}
As a footnote, I think id="topmenu firstlevel" is invalid as an id attribute can't contain spaces... ? Indeed the w3c recommendation defines the id attribute as a 'name' type...
ID and NAME tokens must begin with a
letter ([A-Za-z]) and may be followed
by any number of letters, digits
([0-9]), hyphens ("-"), underscores
("_"), colons (":"), and periods
(".").
I used the display:inline-block property: the solution consist in use a wrapper with fixed width. Inside, the ul block with the inline-block for display. Using this, the ul just take the width for the real content! and finally margin: 0 auto, to center this inline-block =)
/*ul wrapper*/
.gallery_wrapper{
width: 958px;
margin: 0 auto;
}
/*ul list*/
ul.gallery_carrousel{
display: inline-block;
margin: 0 auto;
}
.contenido_secundario li{
float: left;
}
i use jquery code for this. (Alternative solution)
$(document).ready(function() {
var margin = $(".topmenu-design").width()-$("#topmenu").width();
$("#topmenu").css('margin-left',margin/2);
});
div {
text-align: center;
}
div ul {
display: inline-table;
}
ul as inline-table fixes the with issue. I used the parent div to align the text to center.
this way it looks good even in other languages (translation, different width)
#Robusto's solution was the simplest for what I was trying to do, I suggest you use it. I was trying to do the same thing for images in an unordered list to make a gallery... I made a js fiddle to fool around with it. Feel free to try it here.
[it was set up using robusto's sample code]
HTML:
<div id="centerDiv">
<ul class="centerUL">
<li><img src="http://placehold.it/200x150> </li>
<li><img src="http://placehold.it/200x150"> </li>
<li><img src="http://placehold.it/200x150"></li>
</ul>
</div>
CSS:
div#centerDiv {
width: 700px;
text-align: center;
border: 1px solid red;
}
ul.centerUL {
margin: 2px auto;
line-height: 1.4;
}
.centerUL li {
display: inline;
text-align: center;
}
ul{margin-left:33%}
Is a decent approximation on big screens. Its not good, but a good dirty fix.
What worked for me was just setting the li item's display property to inline-flex:
li {
display: inline-flex;
}
<ul>
<li>Item 1</li>
<li>Item 1</li>
</ul>
You may choose to add justify-content: center to the lis, and padding: 0 to the ul to straighten things out.
.topmenu-design
{
display: inline-table;
}
That all!

horizontally align breadcrumbs - CSS

I am having a hard time aligning the breadcrumbs horizontally.
There is an existing style sheet for the container divs and something in it is preventing the output.
The ul li appear one below the other.
http://jsfiddle.net/y9tyc2cu/1/
HTML:
<div class="chatWrapper">
<div class="chatContainer">
<div class="chatMsgWrapper">
<ul id="crumbs">
<li>Home
</li>
<li>Main section
</li>
<li>Sub section
</li>
<li>Sub sub section
</li>
</ul>
</div>
</div>
</div>
CSS:
ul#crumbs, ul#crumbs li {
list-style-type:none;
padding:0;
margin:0;
}
#crumbs {
height:2.3em;
border:1px solid #dedede;
}
#crumbs li {
float:left;
line-height:2.3em;
color:#777;
padding-left:.75em;
}
#crumbs li a {
/*background:url(/Assets/Images/crumbs.gif) no-repeat right center;*/
background:gray;
padding:5px 15px 5px 0;
}
Here is the solution. float: none; for each li and display: inline; for ul.
Check here!
if you are a bootstrap user you need
you should have bootstrap.min.js
http://getbootstrap.com/2.3.2/components.html#breadcrumbs
<ul class="breadcrumb">
<li>Home <span class="divider">/</span></li>
<li>Library <span class="divider">/</span></li>
<li class="active">Data</li>
</ul>
just add display:inline and remove float: left from li
example
http://jsfiddle.net/y9tyc2cu/3/
You have two redundant styles for the li.
You may remove this style:
.chatContainer ul li{
float: left; clear: both; margin: 10px 0;
width: 100%; padding: 10px;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
Also, make the li as display: inline-block or clear the floats properly:
#crumbs li {
display: inline-block;
line-height: 2.3em;
color: #777;
padding-left: .75em;
}
Your updated fiddle: http://jsfiddle.net/abhitalks/y9tyc2cu/2/
Update:
As per your comment, you can't remove or change an existing style. In that case, you need to override the styles which are set in the earlier defined style. Just add these two overrides in #crumbs li style, without changing or removing anything elsewhere:
width: auto; float: none;
So, your complete style now looks like this:L
#crumbs li {
display: inline-block;
line-height: 2.3em;
color: #777;
padding-left: .75em;
width: auto;
float: none;
}
Updated fiddle: http://jsfiddle.net/abhitalks/y9tyc2cu/6/
.

Firefox weird behaviour right-align span inside li tag

Firefox messes up the span tag inside li.
Chrome, Opera and IE do a good job of displaying the formatting correctly.
How can i fix it.
Here is my Code :
<div class="navcontainer">
<div class="nav">
<ul>
<li>Dropdown
<ul>
<li>One<span class="bubble-info">32</span>
</li>
<li>Two<span class="bubble-warning">32</span>
</li>
<li>Three<span class="bubble-danger">32</span>
</li>
<li>Four<span class="bubble-success">32</span>
</li>
<li>Five<span class="bubble-default">32</span>
</li>
</ul>
</li>
</ul>
</div>
Here is my fiddle http://jsfiddle.net/TgD65/1/
It's actually pretty simple. When you set an element to be floating, you need to put it BEFORE the other element.
The browser first places the floating elements, then the rest.
So in your li if you have:
<span class="bubble-info">32</span>One
it will work in all browsers.
http://jsfiddle.net/TgD65/5/
Try using white-space:normal for .nav ul ul a
Demo Fiddle
Change the CSS of
.bubble-info, .bubble-danger, .bubble-success, .bubble-default, .bubble-warning {
To:
.bubble-info, .bubble-danger, .bubble-success, .bubble-default, .bubble-warning {
width: 20px;
height: 20px;
display:inline-block;
position:absolute;
right:0;
text-align: center;
color: #000000;
font-weight: bold;
-moz-border-radius: 4px;
-webkit-border-radius: 4px;
border-radius: 4px;
border: 1px solid #000;
}
And add position:relative; to .nav ul ul li li {
What this does is replace the relative positioning of the bubbles with absolute positioning and give them block level characteristics so they can be given right:0; to be right aligned. It also removed the floating so you escape any related issues with not clearing it between items.
#Madi,
you have to define position:absolute; in Bubbles classes Here is the Demo.
.bubble-info, .bubble-danger, .bubble-success, .bubble-default, .bubble-warning {
width: 20px;
height: 20px;
float: right;
text-align: center;
color: #000000;
font-weight: bold;
position:absolute; right:0; top:0; /*Added this line*/
-moz-border-radius: 4px;
-webkit-border-radius: 4px;
border-radius: 4px;
border: 1px solid #000;
}
.nav ul a {
display:inline-block;
position:relative; /*Added this line*/
padding:10px;
}

ul does not display bullets

One of my UL is not displaying correctly. It is both displaying things inline and also the bullets do not show.
I know that this issue is isolated strictly to my divs that are used with my tabbed interface. I'm also assuming this is due to the hierarchy above. (The .tabs li says to display things 'inline'.) If I delete that, obviously the tab interface is screwed. How can I keep my divs displaying horizontally, and have the lists inside of the div box display in a block?
Thanks!
CSS
.basics ul {font-family:terminal;
font-size:9px;
text-transform:uppercase;
color:gray;
padding:0px;
display:block;
}
li.basics { list-style-image: url('/bullet.png');
}
ul b {font-family: terminal;
font-size: 8px;
text-transform:uppercase;
color:#fff3a2;
font-weight:lighter;
}
div.tabs {
padding:15px;
width:760px;
background:#000;
color:#fff;
border-radius: 0em 4em 1em;
-moz-border-radius: 10px;
margin-left:3px;
height:200px;
overflow: auto;
}
ul.tabs {
padding:0;
margin:0;
margin-top:10px;
margin-left:3px;
}
.tabs li {
list-style:none;
display:inline;
}
and the html code:
<ul class='tabs'>
<li><a href='#tab1'>BASICS</a></li>
<li><a href='#tab2'>BIOGRAPHY</a></li>
<li><a href='#tab3'>RANDOM</a></li>
<li><a href='#tab4'>NPCs</a></li>
</ul>
<div id="tab1" class="tabs">
<ul class="basics">
<li><b>Full Name:</b>
<li><b>DOB:</b> 27 May 1985; 28 YRS </li>
<li><b>Birthplace:</b>Olinda, Brasil</li>
<li><b>Nationality:</b>Brazilian, French, & American</li>
</ul>
If I'm understanding your question correctly, you want the top to be horizontal, and the lower one to be vertical. I just changed your rule from .tabs li to ul.tabs li
http://jsfiddle.net/itsmikem/3RwMn/
Let me know if that's your issue.

How do I center align horizontal <UL> menu?

I need to center align a horizontal menu.
I've tried various solutions, including the mix of inline-block / block / center-align etc., but haven't succeeded.
Here is my code:
<div class="topmenu-design">
<!-- Top menu content: START -->
<ul id="topmenu firstlevel">
<li class="firstli" id="node_id_64"><div><span>Om kampanjen</span></div></li>
<li id="node_id_65"><div><span>Fakta om inneklima</span></div></li>
<li class="lastli" id="node_id_66"><div><span>Statistikk</span></div></li>
</ul>
<!-- Top menu content: END -->
</div>
UPDATE
I know how to center align the ul within the div. That can be accomplished using Sarfraz's suggestion.
But the list items are still floated left within the ul.
Do I need Javascript to accomplish this?
From http://pmob.co.uk/pob/centred-float.htm:
The premise is simple and basically just involves a widthless float wrapper that is floated to the left and then shifted off screen to the left width position:relative; left:-50%. Next the nested inner element is reversed and a relative position of +50% is applied. This has the effect of placing the element dead in the center. Relative positioning maintains the flow and allows other content to flow underneath.
Code
#buttons{
float:right;
position:relative;
left:-50%;
text-align:left;
}
#buttons ul{
list-style:none;
position:relative;
left:50%;
}
#buttons li{float:left;position:relative;}/* ie needs position:relative here*/
#buttons a{
text-decoration:none;
margin:10px;
background:red;
float:left;
border:2px outset blue;
color:#fff;
padding:2px 5px;
text-align:center;
white-space:nowrap;
}
#buttons a:hover{ border:2px inset blue;color:red;background:#f2f2f2;}
#content{overflow:hidden}/* hide horizontal scrollbar*/
<div id="buttons">
<ul>
<li>Button 1</li>
<li>Button 2's a bit longer</li>
<li>Butt 3</li>
<li>Button 4</li>
</ul>
</div>
This works for me. If I haven't misconstrued your question, you might give it a try.
div#centerDiv {
width: 100%;
text-align: center;
border: 1px solid red;
}
ul.centerUL {
margin: 2px auto;
line-height: 1.4;
padding-left: 0;
}
.centerUL li {
display: inline;
text-align: center;
}
<div id="centerDiv">
<ul class="centerUL">
<li>Amazon 1 </li>
<li>Amazon 2 </li>
<li>Amazon 3</li>
</ul>
</div>
With CSS3 flexbox. Simple.
ul {
display: flex;
justify-content: center;
}
ul li {
padding: 0 8px;
}
This is the simplest way I found. I used your html. The padding is just to reset browser defaults.
ul {
text-align: center;
padding: 0;
}
li {
display: inline-block;
}
<div class="topmenu-design">
<!-- Top menu content: START -->
<ul id="topmenu firstlevel">
<li class="firstli" id="node_id_64">
<div><span>Om kampanjen</span>
</div>
</li>
<li id="node_id_65">
<div><span>Fakta om inneklima</span>
</div>
</li>
<li class="lastli" id="node_id_66">
<div><span>Statistikk</span>
</div>
</li>
</ul>
<!-- Top menu content: END -->
</div>
Here's a good article on how to do it in a pretty rock-solid way, without any hacks and full cross-browser support. Works for me:
--> http://matthewjamestaylor.com/blog/beautiful-css-centered-menus-no-hacks-full-cross-browser-support
Try this:
div.topmenu-design ul
{
display:block;
width:600px; /* or whatever width value */
margin:0px auto;
}
Do it like this :
<div id="footer">
<ul>
<li>Link 1</li>
<li>Link 2</li>
<li>Link 3</li>
<li>Link 4</li>
<li>Link 5</li>
</ul>
</div>
And the CSS:
#footer {
background-color:#ccc;
height:39px;
line-height:36px;
margin:0 auto;
text-align:center;
width:950px;
}
#footer ul li {
display:inline;
font-family:Arial,sans-serif;
font-size:1em;
padding:0 2px;
text-decoration:none;
}
Like so many of you, I've been struggling with this for a while. The solution ultimately had to do with the div containing the UL. All suggestions on altering padding, width, etc. of the UL had no effect, but the following did.
It's all about the margin:0 auto; on the containing div. I hope this helps some people, and thanks to everyone else who already suggested this in combination with other things.
.divNav
{
width: 99%;
text-align:center;
margin:0 auto;
}
.divNav ul
{
display:inline-block;
list-style:none;
zoom: 1;
}
.divNav ul li
{
float:left;
margin-right: .8em;
padding: 0;
}
.divNav a, #divNav a:visited
{
width: 7.5em;
display: block;
border: 1px solid #000;
border-bottom:none;
padding: 5px;
background-color:#F90;
text-decoration: none;
color:#FFF;
text-align: center;
font-family:Verdana, Geneva, sans-serif;
font-size:1em;
}
Demo - http://codepen.io/grantex/pen/InLmJ
<div class="navigation">
<ul>
<li>Home</li>
<li>About</li>
<li>Contact</li>
<li>Menu</li>
<li>Others</li>
</ul>
</div>
.navigation {
max-width: 700px;
margin: 0 auto;
}
.navigation ul {
list-style: none;
display: table;
width: 100%;
}
.navigation ul li {
display: table-cell;
text-align: center;
}
.navigation ul li a {
padding: 5px 10px;
width: 100%;
}
Omg so much cleaner.
Generally speaking the way to center a black level element (like a <ul>) is using the margin:auto; property.
To align text and inline level elements within a block level element use text-align:center;. So all together something like...
ul {
margin:auto;
}
ul li {
text-align:center;
list-style-position:inside; /* so that the bullet points are also centered */
}
ul li div {
display:inline; /* so that the bullet points aren't above the content */
}
... should work.
The fringe case is Internet Explorer6... or even other IEs when not using a <!DOCTYPE>. IE6 incorrectly aligns block level elemnts using text-align. So if you're looking to support IE6 (or not using a <!DOCTYPE>) your full solution is...
div.topmenu-design {
text-align:center;
}
div.topmenu-design ul {
margin:auto;
}
div.topmenu-design ul li {
text-align:center;
list-style-position:inside; /* so that the bullet points are also centered */
}
div.topmenu-design ul li div {
display:inline; /* so that the bullet points aren't above the content */
}
As a footnote, I think id="topmenu firstlevel" is invalid as an id attribute can't contain spaces... ? Indeed the w3c recommendation defines the id attribute as a 'name' type...
ID and NAME tokens must begin with a
letter ([A-Za-z]) and may be followed
by any number of letters, digits
([0-9]), hyphens ("-"), underscores
("_"), colons (":"), and periods
(".").
I used the display:inline-block property: the solution consist in use a wrapper with fixed width. Inside, the ul block with the inline-block for display. Using this, the ul just take the width for the real content! and finally margin: 0 auto, to center this inline-block =)
/*ul wrapper*/
.gallery_wrapper{
width: 958px;
margin: 0 auto;
}
/*ul list*/
ul.gallery_carrousel{
display: inline-block;
margin: 0 auto;
}
.contenido_secundario li{
float: left;
}
i use jquery code for this. (Alternative solution)
$(document).ready(function() {
var margin = $(".topmenu-design").width()-$("#topmenu").width();
$("#topmenu").css('margin-left',margin/2);
});
div {
text-align: center;
}
div ul {
display: inline-table;
}
ul as inline-table fixes the with issue. I used the parent div to align the text to center.
this way it looks good even in other languages (translation, different width)
#Robusto's solution was the simplest for what I was trying to do, I suggest you use it. I was trying to do the same thing for images in an unordered list to make a gallery... I made a js fiddle to fool around with it. Feel free to try it here.
[it was set up using robusto's sample code]
HTML:
<div id="centerDiv">
<ul class="centerUL">
<li><img src="http://placehold.it/200x150> </li>
<li><img src="http://placehold.it/200x150"> </li>
<li><img src="http://placehold.it/200x150"></li>
</ul>
</div>
CSS:
div#centerDiv {
width: 700px;
text-align: center;
border: 1px solid red;
}
ul.centerUL {
margin: 2px auto;
line-height: 1.4;
}
.centerUL li {
display: inline;
text-align: center;
}
ul{margin-left:33%}
Is a decent approximation on big screens. Its not good, but a good dirty fix.
What worked for me was just setting the li item's display property to inline-flex:
li {
display: inline-flex;
}
<ul>
<li>Item 1</li>
<li>Item 1</li>
</ul>
You may choose to add justify-content: center to the lis, and padding: 0 to the ul to straighten things out.
.topmenu-design
{
display: inline-table;
}
That all!

Resources