Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

centering horizontal list in fluid layout

Featured Replies

HI gadgetgirl,

 

The float:left; in your CSS (nav > ul li) is causing this.

 

Modify your CSS by removing the float and adding a text-align:center; to your nav>ul selector.

 

Your CSS should look like this:

 

nav > ul {
display:block;
color:#f4847f;
list-style:none;
overflow:hidden;
width:87.5%;
margin:auto;
	text-align:center;
}


nav > ul li {
display:inline;	
margin:10px 5% 0 0;
font-size:2.25em;
letter-spacing:4px;
line-height:1.5em;
}

 

I've tested it in Firefox and it works OK. Let me know if it's good.

 

Lyndsey.

Edited by Renaissance-Design
Please use the code button or tags to format your code.

Using text-align:center for alignment of elements is unsemantic. Text-align is for aligning text, not elements.

 

Fix up your nav:

 

width:90%;
margin:0 auto;

 

On your UL element, remove the left and right margins and give it a 100% width.

 

Then give your LI elements a 33.3% width, keep them floated, and center align the text so the text is in the middle of the element. Also remove left and right margins.

 

If you change the number of items in your nav, just change the % width of your LI. For example, if you have four items, it will be 25%, 5 items 20% and so on. You should end up with something like the menu on my website, the link is in the signature.

Edited by brightonmike

Using text-align:center for alignment of elements is unsemantic. Text-align is for aligning text, not elements.

 

Fix up your nav:

 

width:90%;
margin:0 auto;

 

On your UL element, remove the left and right margins and give it a 100% width.

 

Then give your LI elements a 33.3% width, keep them floated, and center align the text so the text is in the middle of the element. Also remove left and right margins.

 

If you change the number of items in your nav, just change the % width of your LI. For example, if you have four items, it will be 25%, 5 items 20% and so on. You should end up with something like the menu on my website, the link is in the signature.

 

Thanks for the tip BrightonMike.

 

  • Author

@Lyndsey - thanks for the reply, it works nicely.

 

@BrightonMike - thanks for the reply and what you say makes sense. However, I don't want the menu to stretch across the whole of the containing div which is why I originally coded it with a smaller percentage and some margins. Also with your approach when I decrease the window width, the nav elements overlap each other on the same line. In the original version they would jump down to the next line and centre themselves. How would I achieve that same affect using your approach?

Give this a try

 

<nav>
<ul class="topnav">
<li><a href="#portfolioLink">portfolio</a></li>
<li><a href="#aboutLink">about</a></li>
<li><a href="#" id="emailLink">contact</a></li>
</ul>
</nav>

 

nav {
position:relative;
float:right;
right:50%
}
.topnav{
color:#f4847f;
list-style:none;
position:relative;
float:left;
left:50%
}
.topnav li {
float:left;
margin:10px 30px 0 0;
font-size:2.25em;
letter-spacing:4px;
line-height:1.5em;
}

 

#objective {
margin:10px auto 0;
width:66.666667%;
text-align:center;
clear:both /*added a clear*/}

Edited by mteam

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.