December 19, 201312 yr hello all ! 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 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?
December 19, 201312 yr 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.
December 19, 201312 yr 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 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?
December 19, 201312 yr 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>
December 19, 201312 yr 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 December 19, 201312 yr by sash_oo7
December 20, 201312 yr 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).
December 20, 201312 yr 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 . 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?
December 20, 201312 yr 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: " "; }
December 20, 201312 yr 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
December 28, 201312 yr Float will be a better choice as it support many attributes like padding, margin, width etc to make ur menu flexible. By Xee Studios
December 28, 201312 yr well I understand now the use of overflow: hidden. I tried the remove the rule from ul tag and then it collapsed. Thanks Using overflow:auto; may be a better option where floated objects of varying content length are likely
December 29, 201312 yr 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.
December 30, 201312 yr 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.
Create an account or sign in to comment