How do i set margins for floating divs? - css

I have these div, one floats left and the other floats right but they are so spaced apart i want them to be a little closer, i tried setting margins but that didn't work. Can anyone help
here is the css
.right{float:right; margin-left:-200px;}
.left{float:left;}
.date{
width:80px;
margin: 50px 90px 0px 200px;
padding: 0px 0px 0px 0px;
}
.entry {
margin: 0px 0px 0px 0px;
padding: 20px 0px 0px 0px;
border-bottom: 0px solid #cccccc;
font-family:eurofurence;
font-size:17px;
width:610px;
background:#000;
}

set
margin-right:200;
for .right
or you can use
position and left or right attribute for style like this
.left{
position:relative;
float:left;
left:100px;
}
DEMO

Try this css :
.left{ float:left;}
.right{float:left; margin-left:200px;}

Note that there's a bug in the current specifications for left-/right-floatting elements with their vertical margins: these vertical margins DO NOT collapse into the vertical margins of non-floatting elements just above them, only with those floatting below them.
It is incoherent... And sometimes the behavior of these vertical margins are not the same across browsers (some will collapse normally).
This creates unbalanced vertical alignment of these floatting elements with non-floatting elements just beside them:
<div style="border:1px solid black;margin:10px">Non-floatting above.</div>
<div style="border:1px solid black;margin:10px;float:left">Floatting on the left.</div>
<div style="border:1px solid black;margin:10px;float:right">Floatting on the right.</div>
<div style="border:1px solid black;margin:10px">Non-floatting in the middle.</div>
<div style="border:1px solid black;margin:10px;clear:both">Non-floatting below.</div>
You'll note that the 3 blocks on the middle are not aligned: the floatting blocks on the left and right are unexpectedly positioned 10px below the central non-floatting block...
But then the block below (with "clear:both") may be shifted down (but this clearing will ignore the bottom margins of the floats before, these floatting bottom margins may still participate to the collapse of the top margin for the element below.
So top margins of floats are not working as expected: it's not possible to create a deisgn where floats will adopt a correct top margin, coherent with non-floatting elements designed to be just beside them if these non-floatting elements also need the same margins.
A work around is to encapsulate all blocks in the middle row in a parent blocks with its own vertical margin, and all blocks in in the middle must not have any vertical margin. And then this creates problems if all these floats do not fit on the row, some will "wrap" and won't have any margin!
The only work around is then to encapsulate all central elements inside a parent block with NO margin, not participating to the collapse of vertical margins with contents above or below them, but then all elements in the middle need to set their margin. In that case there's no way to allow the correct collapse of vertical margins of the central row with vertical margins of rows that are just above or below the middle row.
<div style="border:1px solid black;margin:10px">Non-floatting above.</div>
<div style="position:relative">
<div style="border:1px solid black;margin:10px;float:left">Floatting on the left.</div>
<div style="border:1px solid black;margin:10px;float:right">Floatting on the right.</div>
<div style="border:1px solid black;margin:10px">Non-floatting in the middle.</div>
<div style="clear:both"></div>
</div>
<div style="border:1px solid black;margin:10px">Non-floatting below.</div>

Related

Margin collapse issue between div

I'm expecting the vertical gap between bottom border of first div and top border of second div to be 45px in this case but browser collapse bottom and top margin.
Effective gap b/w bottom border of first div and top border of second div in browser display is 25px.
Ideally border should prevent margin collapse. What is the mistake here?
I have not applied any positioning or float.
jsfiddle Code
HTML
<body>
<div><p>A</p></div>
<div><p>B</p></div>
</body>
CSS
div{
width:200px;
height:200px;
}
div:nth-child(1){
background-color: #F52C6F;
border-bottom: 10px solid black;
margin-bottom: 20px;
}
div:nth-child(2){
background-color: #0ECCCC;
border-top: 10px solid yellow;
margin-top: 25px;
}
Ideally border should prevent margin collapse. What is the mistake here? I have not applied any positioning or float.
Borders do not block margin collapse between siblings — they only block margin collapse between a parent and a child, or wherever the borders would otherwise intersect the margins.
From the spec:
Two margins are adjoining if and only if:
...
no line boxes, no clearance, no padding and no border separate them
...
Since the borders aren't actually separating or intersecting the margins between your two div elements, the margins are considered adjoining, and therefore margin collapse occurs between them as usual. The borders on your div elements would however prevent margins of your divs collapsing with those of their p children.
This behaviour is actually documented in the W3C Specification on the box model: Collapsing Margins.
Basically, adjoining vertical margins collapse into one margin, where the larger margin value is used and the lower value is discarded.
Use one higher margin value instead of two lower. :-)
Try somthing like this:
Html:
<body>
<div id="parent">
<div><p>A</p></div>
<div><p>B</p></div>
</div>
</body>
CSS:
#parent
{
width:200px;
height:200px;
}
#parent div:nth-child(1){
background-color: blue;
border-bottom: 10px solid black;
margin-bottom: 20px;
}
#parent div:nth-child(2){
background-color: green;
border-top: 10px solid yellow;
}
And here is a working jsFiddle: http://jsfiddle.net/hEDR3/

nested DIV tags and height

I'm trying to get some nested div tags to auto grow in height depending on the content inside them. A sample code is given here. The middle for example has some more content, but the height doesn't seem to grow. What is the trick to make it auto grow? I took out all the floating elements from inside the parents thinking it might be the CSS clear rule. But that didn't help either. Appreciate any help.
<div id="editmain">
<div class="ticker">
some content here
</div>
<div class="ticker">
some longer content content here
</div>
<div class="ticker">
some content here
</div>
</div>
#editmain
{
position:relative;
min-height:480px;
background-color:#84d;
overflow:hidden;
padding: 20px 0px 30px 0px;
}
.ticker
{
position:relative;
border-bottom: solid 2px #ddd;
margin:10px;
background-color:white;
overflow:hidden;
height:auto;
min-height:100px;
}
In the absolute positioning model, a box is removed from the normal
flow entirely (it has no impact on later siblings) and assigned a
position with respect to a containing block.
w3.org
Remove the absolute positioning and find another way to format your labels and inputs using width and margin. Fiddle
.tickertxtlabel
{
display:inline-block;
margin: 10px 10px 10px 10px;
width:90px;
}

Extra White Space Top of Webpage

I created a wrapper div for my site to contain the entire contents of the body, but for some reason, I have extra white space above the wrapper div. I can't exactly pinpoint why this is happening; I put zero padding and zero margins for both the body and the wrapper div:
<body onload="init()" style="margin:0px; padding:0px;">
<div id="wrapper" style="background-color:#000000; margin:0px; padding:0px;">
<div id="pagecontent">
<nav id="navlogo" style="margin:10px;">Some Navigation Stuff
</nav></div></div></body>
(The Page Content Div holds all of the webpage's elements and centers them.)
I also have this CSS for the page content:
#pagecontent
{
margin: 0px auto;
width: 1044px;
box-shadow:inset 0px 0px 15px black;
background-color:#000000;
}
I tried using negative margins of -10px on the wrapper div, and that worked out; is that the only way to get rid of this white space?
<nav id="navlogo" style="margin:10px;">
This margin includes margin-top:10px - that is the issue.
margin:10px; = margin-top:10px;margin-right:10px;margin-bottom:10px;margin-left:10px;
to get around this you could change it to:
margin:10px;margin-top:0;
or to make the navbar go to the top just change margin:10px; to padding:10px;

Using border-right with floats displays incorrectly

I am floating a couple divs inside a container div & the first div has a border on the right. It works correctly WITHOUT the border, but when I add the border it all messes up & the text inside the container on the right displays itself under the border from the other div.
To show you what I mean here is a picture:
Here is my code:
<div style="margin: 0px auto; width: 500px; border: 1px solid #000;">
<div style="width: 500px; border-bottom: 1px solid #000;">
<div style="float: left; width: 250px;">Resolution/Megapixels</div>
<div style="float: right; width: 250px;">Average Quality Size/Best Quality Size</div>
<div style="clear: both;"></div>
</div>
<div style="width: 500px; border-bottom: 1px solid #000;">
<div style="float: left; width: 250px; border-right: 1px solid #000;">0.5 megapixels</div>
<div style="float: right; width: 250px;">3x5 inches/NA</div>
<div style="clear: both;"></div>
</div>
</div>
Edit:
Please disregard. Worked it out as soon as I posted this.
You're border is making the box too wide. Need to set the left div (with the border) to 249 so that it adds up to 250px with the border.
it is because adding a boarder to an element will add the border width to the elements width so your border is making the "3x5 inches" is actually 251px wide forcing it down as it can't fit next to a 250px width element in a 500px container, just reduce one of the 250px divs by 1px to 249px
NVM... I'm a fool. Realized right after I posted this I had to decrease the first div's size by 1 because of the border size.

xHTML/CSS: How to make inner div get 100% width - margins

I have 2 nested divs and outer one has width:100%
<div id="#outer" style="width:100%; border:1px">
<div id="#inner" style="width:100%; border:1px; margin:4px">
something inside ...
</div>
</div>
But in this case inner div exceeds width of outer by 8px (margins).
How to make inner div to get width of outer div minus 8px margin?
P.S. All styles are in separate classes in my case, here I putted CSS into style attributes just for simplification.
Taking away the width on the inner div should work, width: auto; will work with margins, and expand to the maximum horizontal area:
<div id="#outer" style="width:100%; border: solid 1px red;">
<div id="#inner" style="border:solid 1px green; margin:4px">
something inside ...
</div>
</div>
Here are some styles that work if you remove the ones directly on the elements. I used auto on the inner CSS and a margin-right = 8px. To make it easier to see I made the inner green and the outer black.
#outer
{
width: 100%;
border: 1px solid black;
}
#inner
{
width: auto;
border: 1px solid green;
margin-right: 8px;
}

Resources