Firefox renders a 100px margin at the top of div.p. It seems to be a margin collapse. But the computed height of div.p is 100px. According to the spec it should not do margin collapse. Is it a bug, or am I wrong here? Chrome renders as expect.
codepen
.s {
height: 100px;
width: 200px;
background: yellow;
float: left;
}
.p {
margin-top: 20px;
margin-bottom: 100px;
zoom: 1;
}
.p:after {
content: ' ';
display: block;
height: 0;
clear: both;
}
<div class="p">
<div class="s"></div>
</div>
I'm not sure why the traditional clearfix is not working, but here are three ways that do:
Like Kaiido said a *{overflow:auto;} works just fine to fix the issue. This also works as *{overflow:hidden;}.
If you don't want a blanket overflow, you can be more precise by putting overflow:hidden or overflow:auto to just .p. This also fixes the issue.
Finally, if you change display: block to display: table in your clearfix (.p:after), the issue also goes away.
I'm really not sure why the traditional clearfix is not working, but it isn't too hard to work around.
Related
so for some reason there is some white space between my elements (using chrome browser). I tried inspecting it and couldn't solve the issue. Not sure what could be causing it at all. I hosted a demo page here so you can see what I am talking about.
So any idea what would be causing this? I'll provide my CSS below for the elements in question. As you can see the blue element to the left does not have this same issue so I assume it has something to do with the gear elements.
This is the only code I have on it right now:
.gear-item {
position: relative;
height: 320px;
width: 320px;
padding: 80px 25px 0px 55px;
display: inline-block;
clear: none;
}
.gear-item:nth-child(even) {
background: #252627;
}
the whitespace you see is the actual whitespace in the html, inline elements will show whitespace between then, the easiest solution is to float the elements
.gear-item {
position: relative;
height: 320px;
width: 320px;
padding: 80px 25px 0px 55px;
display: block;
float:left;
clear: none;
}
.gear-item:nth-child(even) {
background: #252627;
}
Inline elements are sensitive to the white space in your code. Remove the white space in your code between your divs and the space goes away.
Its caused by using inline-block. Apparently the same problem described here: https://css-tricks.com/fighting-the-space-between-inline-block-elements/. So it's actually the whitespace in your markup!
A quick fix would be using floats instead for gear-item:
display: block;
float: left;
(or some of the other solutions outlined in the link I mentioned)
I am trying to make a div with text and a div with a button fit side by side. It works fine until you make the screen really narrow. Is there a way to force them to be on the same line and for the first div to shrink to accommodate the min-width of the second?
http://jsfiddle.net/C3877/9/
To see what I mean, resize the window, reducing the width, until the div with the button is forced onto the second line. That is what I'd like to prevent.
Note: I only care if a suggested fix works properly in Chrome.
Instead of floats, you could use display: inline-block. This will keep things all on one line, and respect the min-width as well.
Inline-block fiddle: http://jsfiddle.net/C3877/8/
In addition, since you only care about Chrome, you could look into flexible boxes
A (quick) flex fiddle here: http://jsfiddle.net/C3877/11/
You can use negative margin-left for the floated right element. Note that this solution keeps using float for both the left and right divs, without using float, you have dozens of solutions (as some of other answers pointed out).
#right_div {
...
margin-left:-100%;
}
Note that all the next content should be wrapped in a block element and use clear:both. I also added a sample of such an element with background:green in this DEMO.
Appending this does the trick I suppose:
#media (max-width:515px) {
#left_div { width: 100%; margin-right: -100px }
}
UPDATED
You could use margin and absolute positioning:
CSS
#parent_div {
width: 100%;
height: 10%;
position: relative;
min-width: 40px;
}
#left_div {
width: 80%;
min-width: 100px;
height: 80%;
float: left;
background-color: #000;
color: #FFF;
}
#right_div {
width: 15%;
min-width: 100px;
float: right;
background-color: blue;
position:absolute;
right: 0px;
}
input[type=button] {
font-size: 2rem;
}
SEE DEMO: http://jsfiddle.net/C3877/19/
You will have to play with some of the css to get it just right when you move it on your website. But this is a sure quick fix.
sorry if the question title is weak, i can't quite sum my problem up into one snappy tagline...
I'm working on a website (using Joomla) and i've had to insert a DIV serving as a sidebar on the right side of the page. in order for it to be displayed "above" (or "over", i mean on the z-axis) the regular page content, i'm using a negative margin on the left side of it, covering the whole width of it, so it will simply float to the right and sit there, which works fine in ff and IE.
Since i've rarely ever run into issues with Chrome that were fine in IE, i didn't bother to check until quite late:
Now i see that in Chrome, the div is just sitting below (at the bottom of) the regular content; despite the "inline" display-types and the negative margin.
Now I've tried ridiculous things to make it work, but for some reason it just won't.
Can someone tell me how i can get it to work in Chrome?
HTML:
<div class="cframe">
<div class="content">
...
</div>
<div class="sideright">
...
</div>
</div>
CSS:
div.cframe {
display: table;
vertical-align: top;
}
div.content {
display: inline-table;
width: 751px;
padding: 60px;
}
DIV.sideright {
width: 200px;
float: right;
display: block;
position: relative;
top: 320px;
margin: 0px 0px 0px -200px;
}
...this is what i'm stuck with right now, it's all quite ugly.
[link to live-page removed as the solution has already been applied]
(The sidebar is the div classed sideright, and contains a module titled Archiv)
Thank you in advance
Change the div.content css to:
div.content {
display: inline;
float: left;
}
You're using float, but then setting the position to relative. You should remove the relative part of your css for the siderright and it should fix the issue
Edit: even better you should change the position to absolute.
Set your container div to position:relative and then position:absolute your sidebar in relation to that.
.cframe {
display: table;
vertical-align: top;
position: relative;
}
.sideright {
width: 200px;
position: absolute;
top: 320px;
right: 0;
}
I didn't test the answers above but I take their word that they worked. However, your question caught my eye, because I thought you were looking for a browser hack.
There are ways that you can tell an element to behave differently on a specific browser. This happens sometimes across browsers and the best way is to hack each individual browser and give them specific instructions. For chrome, of course you'll have to use a webkit.
This would be an easy example of the syntax to follow:
<p>TEST</p>
p {color:green;}
#media screen and (-webkit-min-device-pixel-ratio:0) {
p {color:red;}
}
Try the DEMO in several browsers and notice how only chrome will display it in red
I stumbled upon a difference in layout rendering between Safari and Chrome/Firefox and I don't know which one is "right".
You can check the jsfiddle here
On Firefox/Chrome the layout is as expected, the yellow div is right after the red ones. But on Safari, the yellow div is positioned under the red ones.
After investigating what I did wrong I found out the bug comes from the CSS class E whose property margin-right (value: -11px) is bigger than the width property (value: 10px) for my div.
I think I understand why Safari renders it this way. The width of div of class B is computed as being the sum of the widths of its children as they have the property float: left;.
Here it is widthB = widthB2*2 + widthE + marginRightE + widthC or marginRightE < -widthE so widthB is not large enough to contain each div next to each other.
So my questions are:
Am I right in my understanding of what Safari does?
Why do Chrome and Firefox render differently? Are they just not decreasing the width of the parent div based on a negative margin-right?
Would the proper correction to always have a margin-right lesser or equal to the width of a div in this case?
Thank you!
HTML:
<div class="A">
<div class="C">
<div class="B">
<div class="B2"></div>
<div class="B2"></div>
<div class="E"></div>
<div class="C">
<div class="D"></div>
</div>
</div>
</div>
</div>
CSS:
.A {
background-color: blue;
height: 200px;
}
.B {
height:100px;
}
.B2 {
background-color: red;
height: 100px;
width: 100px;
float: left;
}
.C {
float: left;
}
.D {
height: 40px;
width: 40px;
float:left;
background-color: yellow;
}
.E {
height: 50px;
width: 10px;
position: relative;
left: -10px;
margin-right: -11px;
background-color: black;
float: left;
}
I'm not sure what you expect to happen with the CSS in the JS fiddle. You are delving into undefined behaviour. I say this because:
'C' is floated but does not have a defined width. This leads to issues in various browsers depending on the complexity of the layout.
None of the floated elements are ever cleared. When floating it is imperative that a clearfix of some description is used, whether it is clear:both, etc.
If you tweak the mark-up and add a clear-fix, you see that the content is always 239px. See http://jsfiddle.net/eaFn9/
However, it seems like the relatively positioned item 'E' and margin is having a negative impact on the width calculation, as Chrome's web inspector seems to always report oddly for the negative margin on this element.
If you play around with this in web inspector you can see it's almost as if the negative margin is the cause of the drop. I think it may be due to a container that does not have a width, and isn't position relative in itself.
How to fix?
Personally, I would want to re-write your layout to include fixed widths on all floats, reduce nesting of floats and clear where possible. It seems overly complex but without a real world use case it's hard to rewrite.
However, It seemed to me that you can wrap 'B2' + 'E' elements in a wrapper that is floated and relatively positioned, then use absolute positioning on 'E' to give the same affect and remove the negative margin.
This is the JSFiddle I came up with: http://jsfiddle.net/jV3Ub/
Sorry, this is not really an answer but it's too long to make it a comment...
Anyway, it took me a minute to figure this out.
I used Firefox 19 on Mac OS X 10.8.2, Chrome 24.0 (Mac) and Safari 6.0.2 (Mac as well). Using the web inspector tools, I realized the divs are not computed the same way indeed. I suck at calculations, but I took the time to sit down and look at this thoroughly, and I do understand Safari's calculations the same way you do.
In Safari, it seems that div B isn't wide enough to contain the yellow div (C) so it seems to reject it to the bottom. For the record, in my tests, I see the yellow div to the right of the red div in FF and Chrome, while Safari shows it right underneath the red, and to the upper left. I am not sure this will help, but I can only recommend you to use the web inspector tools now integrated to all modern browsers to debug this.
I'm not sure about why this happens, all I know is that by only changing the width of E by 1px, like so:
.E {
height: 50px;
width: 11px; /* added 1px to this property */
position: relative;
left: -10px;
margin-right: -11px;
background-color: black;
float: left;
}
it displays correctly in Safari.
Make the following changes to classes .D and .E:
.D {
float:left;
height: 40px;
width: 40px;
background-color: yellow;
margin-left: -11px;
}
.E{
height: 50px;
width: 10px;
position: relative;
left: -10px;
background-color: black;
float: left;
}
DEMO: http://jsfiddle.net/uryJJ/22/
I hope this helps!
SECOND EDIT:
I think we should link these two questions: https://stackoverflow.com/questions/4989930/css-negative-margin and why use negative margins? to this one.
Also See the W3C spec on margin: http://www.w3.org/TR/CSS2/box.html#margin-properties.
Section 8.3.1 Might actually explain what is going on with your sample. A collapsing margin issue not rendering correctly in Safari.
ORIGINAL POSTING:
So my questions are:
1) Am I right in my understanding of what Safari does. Why do Chrome and Firefox render differently? Sounds like that might be it, but, really, who cares? You are not getting the results you want. You should change your code unless you don't care about Safari users.
2) Are they just not decreasing the width of the parent div based on a negative margin-right?
Probably, but again, not really important.
3) Would the proper correction to always have a margin-right lesser or equal to the width of a div in this case? I would say yes. To fix the issue and get the results you want I would move the div with class E inside the right most div with class B2. Then float E to the right and remove the position, left and margin-right attributes.
.E {
height: 50px;
width: 10px;
background-color: black;
float: right;
}
http://jsfiddle.net/uryJJ/32/
FIRST EDIT
.D {
height: 40px;
width: 40px;
float:left;
background-color: yellow;
position:relative;
left: -10px;
}
.E {
height: 50px;
width: 10px;
position: relative;
left: -10px;
background-color: black;
float: left;
}
http://jsfiddle.net/uryJJ/33/
Sorry, I might be beating this to death but this fixes it:
.E {
height: 50px;
width: 10px;
margin-left: -10px;
background-color: black;
float: left;
}
http://jsfiddle.net/uryJJ/35/
I was not a fan of negative margin values until just now.
i've got a very simple CSS example detailed below. The .entry class generates a simple box with a 1px black border. the example below, will render "123" within the box and "abc" and "xyz" outside the box because those elements are tagged with float left and right. My goal is to have .box1 inside .entry aligned left, and .box2 inside .entry aligned right.
<html>
<head>
<style type="text/css">
.entry
{
width: 400px;
border-style: solid;
border-width: 1px;
border-color: #000;
}
.box1
{
width: 75px;
float: left;
}
.box2
{
width: 300px;
float: right;
}
</style>
</head>
<body>
<div class="entry">
123
<div class="box1">abc</div>
<div class="box2">xyz</div>
</div>
</body>
</html>
it renders fine in IE but not firefox
Since floats are taken out of the normal flow, their surrounding boxes will collapse.
I'll first explain why it is working in IE. You have given it "layout" by setting a width or height on each element. It's width in this case. See hasLayout for more about this. This is very handy as it will also solve most of any other IE bugs you come across.
The magic bullets are:
height: 1%; /* or any other value. IE6 wrongly sees height as min-height. */
or if you aren't in a position to set a specific size, use
zoom: 1; /* A IE only property. Doesn't validate, but doesn't cause any harm either */
So, to your problem, making it working in a proper browser, there are a couple of solutions. If you set the parent of the floated elements to have float as well, the box will stretch around the child floats. Although this might cause trouble further up the DOM, I don't know how the rest of your markup is.
An easier way can then be to set overflow:hidden; which also fixes it. But now you have to watch out for long url's, code-snippets, or anything else that might accidentally push out of the div. If you're doing the whole site yourself this is normally not a big problem, if you're handing it over to someone to use in a CMS, chances are slightly larger. Still worth taking the risk though, if you can't solve a myriad of floats around your layout.
Adding an element at the end to clear the floats is also a way of doing it, but I find the former ways easier.
I better stop blabbering, you probably want to see the code:
.entry {
width: 400px;
border: 1px solid #000;
float: left; /* Option 1 */
overflow: hidden; /* Option 2 */
}
.box1 {
width: 75px;
float: left;
}
.box2 {
width: 300px;
float: right;
}
Since you have no Doctype, you are triggering Quirks mode. This renders browsers significantly more buggy then Standards (AKA Strict) mode. Internet Explorer is the worst offender here.
I suspect your problem is that you want the border of .entry to extend around .box1 and .box2. See containing floats for more detail and Methods for Containing Floats for some nicer solutions..
Of course, your problem might be something different. You were rather vague.
You need to clear your floats for FF:
.entry:after {
content: ".";
display: block;
height: 0;
clear: both;
visibility: hidden;
}
I would do it for IE as well:
.entry { min-height: 10px;}
Otherwise, I don't see why it wouldn't work...
Try this; I've tested it and it works in FF 3, Safari 3.2, Opera 9.6 and IE 7.
(The reason I added the extra line of text in the .box2 div was to ensure that the .entry div would stretch vertically as extra content was added).
<html>
<head>
<style type="text/css">
.entry
{
width: 400px;
border-style: solid;
border-width: 1px;
border-color: #000;
}
.box1
{
width: 75px;
float: left;
}
.box2
{
width: 300px;
float; left;
margin: 0 0 0 75px;
}
.entry span
{
width: 400px;
float: left;
}
</style>
</head>
<body>
<div class="entry">
<span>123</span>
<div class="box1">abc</div>
<div class="box2">xyz<br>More text</div>
</div>
</body>
</html>
Try adding overflow:hidden (or auto) to .entry{...}