Div collapse after float css - html

I have a div called NAV and inside of NAV I have an UL with 5 li which I float to the left, the li's that is but when I do that the NAV collapses. I know this because I put a border around NAV to see if it collapses and it does. Here is the example.
collapsed http://img401.imageshack.us/img401/8867/collapsedze4.png
no collapsed http://img71.imageshack.us/img71/879/nocollapsedkx7.png
as you can see in the first image, the links in the NAV div are floated left and that
black border ontop is the actual div called NAV.
in this image you can see how it has top and bottom border and it not collapsed.
here is some of the html and css I used.
alt text http://img301.imageshack.us/img301/5514/codejc8.png
#nav #ulListNavi a {
float: left;
}

Add any overflow value other than visible to your container:
div#nav { overflow:auto; }
Then add width to restore the width
div#nav { width: 100%; overflow:auto; }

One solution is to add a "clear:both" style to an element after the last floated anchor, for instance:
<div id="nav">
<ul id="ulListNavi">
<li>Home</li>
<li>Search</li>
<li>Flowers</li>
<li>My Account</li>
<li>Contact Us</li>
</ul>
<div style="clear:both;"></div>
</div>
This causes the containing element to clear all floating elements before closing the containing box.

A few other options for clearing floats here:
http://www.quirksmode.org/css/clearing.html
http://www.sitepoint.com/blogs/2005/02/26/simple-clearing-of-floats/
As to the best way of doing it, that's almost a holy war, the purists would freak about the extra div, if you are not fussed by a little extra markup, the addition of the cleared div as suggested by Joshua and AJ will work fine, and is a reliable technique, but there are at least 17 other ways of doing it...

add this code after your ul:
<div style="clear: both"></div>

Try floating the containing element to the left too.

Don't bother with clearing elements or overflow. Add this:
#nav {
float: left;
}
When you float the LI's, the #nav no longer "contains" anything so it collapses. But if the #nav is floated also, it contains anything floated inside it, so it expands again.
(Also consider removing the #nav div and just applying the same styles to the UL.)

Your problem is because you are floating the <A> elements, but each of them is inside an <LI> element. LIs display as blocks by default, so each <LI> is forcing it's child <A> to begin on a new line.
If you float the <LI>s, I think you'll solve your problem.
#nav #ulListNavi li {
float: left;
}

The quickest solution would be to add overflow:hidden to clear the float on the parent element:
#nav{overflow:hidden;}

Without changing your HTML:
#nav
{
width: 100%;
overflow: auto;
border: solid 1px red;
}
#ulListNavi
{
margin: 0;
padding: 0;
list-style: none;
}
#nav #ulListNavi li
{
float: left;
}
#nav #ulListNavi li a
{
margin-left: 5px;
}
Works in IE8 and FF 3.5

Related

How do you align text of one item in nav?

I am building a navigation bar using <nav> and I am aligning the text to the right but I want to get the logo in the navbar to be on the left I have tried many ways to move it like giving it a id and using !important but it still wont move
Here is my relevant HTML
<div id="navDiv">
<nav>
Name
Home
About
Contact
</nav>
</div>
And here is my CSS
#logo
{
text-align:left !important;
}
nav
{
text-align:right;
}
Why is the logo element still on the right when !important is telling it to go to the left and how can I fix this?
add float:left;
#logo
{
float:left;
}
nav
{
text-align:right;
}
Live demo: JSFiddle
If I understand you correctly, you want to the "Name" to appear to the left while everything else is to the right? So, like this: http://jsbin.com/gejefiji/1/edit
The only thing I did there is add a float: left; to the #logo selector.
The reason the nav rule overrides the #logo rule even with the !important is due to nesting. The nav itself is a block element, meaning it expands to fill its container. The a tags are not block elements, they are inline, meaning they follow the alignment rules for the containing element, which means they get text-aligned right. If you inspect the elements in the layout section of your browser's development tools, you can see that the width of the a elements shrinks-to-fit the content, like so:
The text inside the a#logo is left-aligned, but the elements themselves are right-aligned, so it appears to not follow what you want.
If you do use the float: left, I strongly recommend you look into adding where and when to use a clear: left element after your nav, just to prevent any float properties form interfering with the rest of the page's layout. Emre's nav:after trick is handy, but is not supported on all browsers, so you might need a separate element with a specific class assigned to it, like this:
// CSS
.clear {
clear: left;
}
// HTML
</nav> <!-- after the nav -->
</div> <!-- or after the div -->
<div class="clear"></div>
Basically, you want to put the clear before anything else that you don't want floated.
The text-align property can only be applied to box-level item that contains items to be aliged either left or right -- not on the items themselves. So you can't have some items inside nav text-aligned left and some text-aligned right this way.
You can, however, float the logo left:
#logo{float:left;}
here it is :) that should work well [tested]
<style>
nav
{
text-align:right;
}
#logo
{
float:left;
}
nav:after { /* fix float */
visibility: hidden;
display: block;
font-size: 0;
content: " ";
clear: both;
height: 0;
}
</style>
<div id="navDiv">
<nav>
Name
Home
About
Contact
</nav>
</div>
Why you do not use float?
#logo {
float:left;
}

CSS Make list-items totally responsive

I am facing a weird problem. I have an un-ordered list of images. List has 4 Items. I want to include it in responsive page and all I want is that all items should have a 25% width and the list itself should span the width of whole screen. Well it all sounds easy and straight-forward but for some odd reason I can't assign 25% width to the list-items and only three items accommodate in one line if I assign width:25%.
If I reduce the width to 24.4% the items are in line. But then again weirdly when I resize browser one of the item goes into 2nd line.
I am using Google chrome. Here is the HTML:
<ul class="imgs">
<li>aaaa</li>
<li>bbbb</li>
<li>cccc</li>
<li>dddd</li>
</ul>
Here is the CSS:
.imgs {
width: 100%;
list-style: none;
margin: auto;
padding:0px;
}
.imgs li {
width: 24.4%;
margin: 0px !important;
padding: 0px !important;
display: inline-block;
}
And here is the Fiddle Demo:
http://jsfiddle.net/7HXw7/2/
WIth inline-bock elements, the browser will interpret the space between the li's as actual spaces and that's why your elements aren't lining up. If you just remove the space, it'll line up. See code:
<ul class="imgs"><li>aaaa</li><li>bbbb</li><li>cccc</li><li>dddd</li></ul>
Or the fiddle, here: http://jsfiddle.net/dgvc9/
Alternately, if you find this as annoying as would think you do, you can make the li's block elements and float them left.
See alternate fiddle: http://jsfiddle.net/8Gxvd/
Try adding:
float: left;
to .imgs li
Fiddle

Chrome Float Left Issue

My chrome is the latest 31.0.1650.48 version.
Today I just notice that the float:left seems not working properly. (IE & FF are fine and Chrome used to be fine as well.)
Html is like:
<div class="listWrapper collapsed clearfix">
<ul>
<li></li>
<li></li>
<li></li>
</ul>
<div class="ui-checkbox" role="checkbox" tabindex="0" style="display:inline-block;"></div>
</div>
Css is like:
.ui-checkbox {
background: url("../images/elements/form.png") no-repeat -183px -331px;
width: 37px;
height: 31px;
float: left;
}
What happened now is the inner checkbox div cannot float next to ul & li at very beginning. But if I right-click on that div --> Inspect Element --> Untick 'float: left' and then re-tick it on, it becomes correct.
Who has any idea about this?
ADD:
ul is float:left and each li actually has 1 background image and float:left as well. In that case, 3 (li) images show together in 1 line, and next div (checkbox shape) should display in the same line. But now div display to next line unless I untick & re-tick its float:left css.
ADD AGAIN
Please see the screen shots for 2 result:
The WHOLE div is float:right, and inner ul,li and div are float left. But the green "checkbox like" div at very beginning display under all these li. When I untick and re-tick float:left, it becomes OK.
Thanks!
Check whether you have given an explicit width to <ul>. And check whether your <ul> 's and the checkbox div 's widths' addition are less than or equal to the width of div listWrapper collapsed. If there is no width to the mother div then it is ok and try this
.ui-checkbox {
background-color:green;
width: 37px;
height: 31px;
float: left;
}
ul{
float: left;
width:50px;
}
Check this Fiddle
Or the update Fiddle.
Not sure why you're getting different results from different browsers, but you need to float the ul element as well to position the checkbox alongside it. Also, as someone mentioned in your post comments, you have an inline style changing the display property to inline-block, which obviously negates the float property.
Here's a codepen. I replaced the background image url with a color just to show the behavior.
.ui-checkbox {
background: url("../images/elements/form.png") no-repeat -183px -331px;
width: 37px;
height: 31px;
float: left;
display:inline-block
}

Unordered list pushing the parent element down

I have two divs, one nested in the other, i.e. the #messages div is inside the #mainContent div. The #messages div should be 0px from the top of its parent. Everything is fine until i put a unordered list inside it, then it pushes the whole div down from the whole #messages div down by a few pixels.
If i put margin-top:0px; on the ul element, everything is fine again, but i want the ul to be margin-top:10px; from it's #messages parent. If i put margin-top:10px; it again pushes #messages 10px from #mainContent.
Can someone please explain why this is happening, and can someone provide a clean solution for this?
Thank you, it's jsfiddle is:
http://jsfiddle.net/wtKuP/4/
"Can someone please explain why this is happening, and can someone provide a clean solution for this?"
The top margins of a block-level element and its first in-flow block-level child will always collapse. There are many ways to avoid this (read the above link for a complete list) - one is to just add overflow: hidden to the parent of the <ul> as this creates a new Block Formatting Context and prevents the standard margin collapsing behaviour.
http://jsfiddle.net/wtKuP/22/
From the 2.1 Spec, Section 8.3.1:
Margins of elements that establish new block formatting contexts (such as floats and elements with 'overflow' other than 'visible') do not collapse with their in-flow children.
Put
margin:0;
padding:0;
on both divs. Then ul will have a 10px margin from messages div, but messages div will have no margin with maincontent.
Add ul:{margin:0;}. Better use eric myers reset.css which will remove browser inconsistence
Try this
JSFiddle updated with what you were asking for
<div id="mainContent">
<div id="messages">
<ul>
<li>Remove the UL and LI, and the #messages div will be 0px from the top of the parent div #mainContent. Why?</li>
</ul>
</div>
</div>
And then...
div#mainContent {
border:1px solid black;
position:absolute;
width:500px;
height:600px;
margin-top:50px;
margin-left:-250px;
left:50%;
}
#mainContent > #messages {
margin: 0;
width:499px;
height:525px;
background-color:red;
}
#messages ul {
display: inline-block;
margin-top: 10px;
}

Li over floated div

I have div.content that holds div.images which is left floated and next to it is a list with couple of items.
The problem is that li's apear over div.images element. I've tried to make list inline-block but in this case items that appear under the div.images are positioned next to the right edge of div.images.
http://jsfiddle.net/kmZ3X/
Any idea?
Try list-style-position: inside on the li elements.
For example: http://jsfiddle.net/mqchen/psJyh/
add
ul{
float: left
}
That should do it
this will make it appear below the images
css:
.content p, .content ul {clear:both}
<div class="content">
<img alt="Desert" src="images/Desert.jpg"/>
<p>sth.</p>
</div>
div.content img
{display: block;float: left; margin-right: 10px;border: solid 1px #999;padding: 5px; }
if you have a list instead of paragraph it will be same. if you have any problem you can ask me again. thank you.