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.

float or display inline?

Featured Replies

hello all :santa_smiley: ! I have seen some people using float and other people using display : inline to lay out a set of list items horizontally. I want to make navigation links using unordered lists display horizontally but I am very confused about which method should I use :santa_sad: Can you guys please tell me which method should I use? Or which method is best suited for a certain situation? their advantages and disadvantages?

I would say float is better

 

check the following example

 

ul { list-style-type: none; overflow: hidden; width:200px; }
ul li { float:left; width: 100px; }
ul li a { display: block; padding: 10px; width:80px; }
ul li a:hover { background: black; }
<ul>
  <li><a href="http://www.facebook.com">Facebook</a></li>
  <li><a href="httpt://www.google.com">Google</a></li>
</ul>

here u can see when u use display:inline u cannot set properties like width, padding (top and bottom), margin etc... which is an handicap for layout purposes.

  • Author

I would say float is better

 

check the following example

 

ul { list-style-type: none; overflow: hidden; width:200px; }
ul li { float:left; width: 100px; }
ul li a { display: block; padding: 10px; width:80px; }
ul li a:hover { background: black; }
<ul>
  <li><a href="http://www.facebook.com">Facebook</a></li>
  <li><a href="httpt://www.google.com">Google</a></li>
</ul>

here u can see when u use display:inline u cannot set properties like width, padding (top and bottom), margin etc... which is an handicap for layout purposes.

That was really helpfull. thanks :santa_smiley: but I gor curious about one thing from your answer. Is it possible to first set display inline to li tag and then set display block to a tag?

You could use display: inline-block for the li tag, but float is better because it's better for positioning (like margins). Use this code to see the difference:

 

 

<!DOCTYPE html>
<html>       
<meta charset="UTF-8" />           
<title>test</title>
    
<style type="text/css">
ul { list-style-type: none; overflow: hidden;  background: pink; }
ul li { display: inline-block; background: lime; }
ul li a { display: block; padding: 10px; width:80px; background: #aaa; }
ul li a:hover { background: skyblue; }

ul.b { list-style-type: none; overflow: hidden; width:200px; background: pink; }
ul.b li { float:left; width: 100px; background: lime; }
ul.b li a { display: block; padding: 10px; width:80px; background: #aaa; }
ul.b li a:hover { background: skyblue; }
</style>   
    
</head>

<body>
    
<ul>
  <li><a href="http://www.facebook.com">Facebook</a></li>
  <li><a href="httpt://www.google.com">Google</a></li>
</ul>

<ul class="b">
  <li><a href="http://www.facebook.com">Facebook</a></li>
  <li><a href="httpt://www.google.com">Google</a></li>
</ul>

</body>
</html>
 

u can use display:inline-block; to li and

 

 

Apply height and width to the parent li elements, and then for a use this

 

 

a {
display: inline-block;
height: 100%;
width: 100%;
}

Edited by sash_oo7

you can use either, I personally use floats, reason being is that display-inline block adds a tiny bit of margin to the element (The size of the margin being related to the font size either specified to the element or inherited from a parent / ancestor).

  • Author

 

You could use display: inline-block for the li tag, but float is better because it's better for positioning (like margins). Use this code to see the difference:

 

 

<!DOCTYPE html>
<html>       
<meta charset="UTF-8" />           
<title>test</title>
    
<style type="text/css">
ul { list-style-type: none; overflow: hidden;  background: pink; }
ul li { display: inline-block; background: lime; }
ul li a { display: block; padding: 10px; width:80px; background: #aaa; }
ul li a:hover { background: skyblue; }

ul.b { list-style-type: none; overflow: hidden; width:200px; background: pink; }
ul.b li { float:left; width: 100px; background: lime; }
ul.b li a { display: block; padding: 10px; width:80px; background: #aaa; }
ul.b li a:hover { background: skyblue; }
</style>   
    
</head>

<body>
    
<ul>
  <li><a href="http://www.facebook.com">Facebook</a></li>
  <li><a href="httpt://www.google.com">Google</a></li>
</ul>

<ul class="b">
  <li><a href="http://www.facebook.com">Facebook</a></li>
  <li><a href="httpt://www.google.com">Google</a></li>
</ul>

</body>
</html>
 

that was a great example. I got the difference now. thanks a lot :santa_smiley: . But could you tell me why have u used overflow : hidden rule to the ul tag? As far as i know, that rule clips the overflowed content of a container. But what is the function of that rule here in the ul tag?

He has used it as a clearfix solution - stopping the parent container collapsing when it has floated children, floated elements are taken out of the normal document flow causing the parent to collapse.

 

I prefer to use this clearfix solution because at times you do want content to overflow - such as a drop down have on hover or similar. Give the ul a class of clearfix

then

.clearfix:before {
  display: table;
  clear: both;
  content: " ";
}
  • Author

 

He has used it as a clearfix solution - stopping the parent container collapsing when it has floated children, floated elements are taken out of the normal document flow causing the parent to collapse.

 

I prefer to use this clearfix solution because at times you do want content to overflow - such as a drop down have on hover or similar. Give the ul a class of clearfix

then

.clearfix:before {
  display: table;
  clear: both;
  content: " ";
}

well I understand now the use of overflow: hidden. I tried the remove the rule from ul tag and then it collapsed. Thanks :santa_smiley:

  • 2 weeks later...

well I understand now the use of overflow: hidden. I tried the remove the rule from ul tag and then it collapsed. Thanks :santa_smiley:

 

Using overflow:auto; may be a better option where floated objects of varying content length are likely

Float is the simplest option, and keeping things simple is a good idea. However; there is likely to come a time when display: inline-block is the way forward.

 

This article on Codrops discusses such a situation when working with responsive grid designs, I found it quite logical and informative.

 

One point that it does discuss is how you can address the margin issues with 'white-space' and other properties so that all styling can still be applied.

Float will be a better choice as it support many attributes like padding, margin, width etc to make ur menu flexible.

 

 

By Xee Studios

 

Inline blocks support all of those attributes just the same as a float block level element.

In a responsive grid it's best to go along with floats as inline-block adds a tiny bit of margin gets added between inline blocks that can cause problems. If you are just stacking items side by side, like in a tabular fashion for example than inline-block or floats will work well there.

  • 3 weeks later...

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.