CSS: on floating divs and negative margins - css

So I wanted a centered, constant width, three-column layout that was compatible with IE and whose columns would stretch to all be of equal height (equal to the height of whichever column had the tallest content). I know- keep dreaming, right?
Well I almost figured it out. After combining the techniques I found here on Stack Overflow with a nifty CSS hack for fixing width issues and the text-align trick for centering, as well as experimenting with different margin/padding values, I have a nearly perfect layout. I'm doing this for a friend's aunt and you can see what I have so far at www.allfourseasonslawncare.com/index.php (The index.html is her original site which she paid someone $500 to make for her. I'm re-creating the page using CSS)
The only bug I'm getting now is that in IE the left column has about a 30 pixel left margin, and all elements inside of the left column have a second 30 pixel left margin.
I can easily fix this using the html>body trick to give a larger negative margin to IE, but I'm trying to figure out where this magical margin is coming from so I can look for another solution. Any ideas?

It might be because your ul elements have a 30px margin. Try adding a span to each list item and then add the padding.
Also, about the layout, you shouldn't need to add margin to each column (I see you have margin on .leftRail, .rightRail, etc). I noticed your .columnWrapper element is only 615px wide, thus you're using negative margin to move these elements.
Instead, you can use a clearfix hack to effectively float elements. (Link for the CSS is below, paste it into the bottom of your CSS file)
After you placed the clearfix in your css, then you go to your wrapper div that is wrapping all of the columns and simply place a class="clearfix" on it.
Then, you just float each column left (float the .rightRail right), specify widths and voila! It should be a 100% working, less-hacky solution.
When you hack too much, IE tends to bug out. Comment if you need any help.
Clearfix: http://gist.github.com/550114

So I spent an hour or two yesterday wrestling with it and I spent another hour this morning (posting updates as I started to get closer as comments to my own question) and I've finally figured it out.
In Internet Explorer, the "width: 100%" command is interpreted as the entire width of the parent element, not the width minus padding. This was giving the table a width of 615 pixels (the width of the center column). Seeing that this would stretch beyond the container, Internet Explorer respected only the left padding and the table stretched 15 pixels (the size of the left padding) beyond the right side of the column.
Since it stretched outside of the column, this re-arranged all floating elements in a way that to Internet Explorer seemed logical. That being, for some ungodly reason, moving the left rail the distance of the right padding (15 pixels) to the right of the left padding (15 pixels into the center column), thus moving it a total of 30 pixels into the center column, or the magical 30 pixel margin.
The solution? I'm still working on this. I moved the padding from the #content div to the #center div as an experiment and this caused the issue to arise in Chrome while simultaneously resolving it in Internet Explorer. At least the primary question was answered, though. We now know where the magical 30 pixel margin was coming from. As a quick solution I've set a constant width on the #content div of 585 pixels and given it margins instead of padding.
#center{
float:left;
width:615px;
margin-right:-615px;
}
#content{
width:585px;
margin:10px 15px 10px 15px;
}

Related

why do grid systems float the last column right

i have been looking at many responsive grid systems(best example is foundation) and they all seem to float the last child column right this annoys me especially when creating pages with dynamic content is there a good reason they do this?
if so is there any good workarounds?
The last column is floated to the right in fluid grid systems to work around rounding errors in different browsers, Safari seems to be the worst.
If you have X amount of columns set to a percentage width (with or without gutters), its rare that the total width will equal 100%. Due to subpixel rounding errors it may be 98%, 99.327%, etc. Usually its under 100% by just a bit, but a noticeable bit.
So if you have a row of say 2 or more columns and right after have a element that is 100% wide, the right edges will not line up if all the grid columns are floated left.
Due to the subpixel rounding issue, the grid columns would be just a bit less wide than the 100% element. To get around this, people float the last column to the right. This hides the subpixel rounding space between the last column and second to last column instead of after the last column making it less noticeable.
.span-last { float: right }
Here's a demo: http://codepen.io/bjankord/pen/mvKkn
Resize the page and look at how the last column lines up on the right.
Not an ideal solution (the !important) but for the most part you should be able to override it with something along the lines of
tr td:last-child{
float:left !important;
}
or jQuery if it's added with something after CSS has been loaded
$('tr td').last().css('float','left');

CSS: Floating IMGs inside floating divs

I encountered a problem and didn't find a solution for this. I'm pretty confused because I thought this was a very simple requirement.
There are the following elements:
a surrounding div#wrapper
div#A, floating left and fixed width
div#B, floating left (right of #A) and dynamic width
Inside of div#B there are plenty of images, floating left and fixed width (and height).
Depending on the screen resolution there should be 1, 2, 3, n columns of images on the right part of the screen (next to div#A). Instead of this, container #B is aligned below container #A and uses the full window width.
My alternative attempt was giving #B a float:right and a margin-left (which was greater than the width of #A), but that also didn't work.
I would like to avoid absolute positioning because the height of the surrounding wrapper should increase with its content.
To visualize what I'm talking about, I made the following diagram:
http://abload.de/img/rezeptbilder1k8lsr.png
Many thanks in advance!
this is happening because..you are having dynamic width for your div#B...ans as there are plenty of images and they are aligned next to each other...so eventually div#B grows to 100% width...when it has 100% width then it arrives under the div#A.because 100% + div#A's width cant fit together in a 100% wide screen..you understand???
1st solution :: you may use width:calc(100% - div#a's width).it will give div#b a width equals to remaining free space besides div#a
or you may use overflow:hidden in your div#B...now at first this div will grow eventually to take the width of remaining free space with a single row of images and once it has 100% width it will create another row of images..but for this your wrapper must have fixed with with **overflow:auto;** also..overflow:auto; in wrapper will introduce scroll bar when combined with of div A and B is greater than 100%
EDIT :: CHECK BROWSER SUPPORTS FOR CALC() HERE
note::if you provide us your try our suggestion will be more appropriate
I can't really provide a solution because you haven't provided any code. (please do so)
That said... Based on your description and your image, your floats on div#A and div#B are breaking. The most common reason for this is that your width is adding up to be more than 100% of the width of your div#wrapper (the containing or parent element of the floated elements)
Your floats should always end up equally 100% (less than 100% works but then you have some extra space somewhere etc).
For example... If you have #wrapper which is 100px wide, then you can have two div's inside which are 50px and 50px wide, or 30px and 70px etc. as adding those values = 100px which is 100%.
So... Why is yours breaking? I suspect it is because you are trrying to mix a dynamic width element, with a static width element. Float was never designed do something like this. There are various ways to achieve it, but which way depends on your code which I don't have.
Other possible reasons why is is breaking is because of too much margin space, too much padding, or even a border on your divs. The width of an element, by default includes it's padding, margin, and border widths. If you have a 100px wide div, with 10px of padding on the left and right and a 2 px border the whole way around (2px on each side). Then you have a div with a total width of 124px.

Left and right floated boxes wider than their container

Let's see:
Container box is 920px, left box is 600px, right box width dinamically changing between 200px and 400px. As we know in basic situation if two floated box wider than their container the lastest one breaks into a new line.
I tried to solve it with negative leftmargin on right box and it works fine. (buggy on IE6 but it does not matter.)
Is negative margin good or can I avoid annoying float box breaking in other way? (i don't want to use absolute positioning.)
Negative left margin is great. I use it lots of time to align to the center left:50%; margin-left: -250px; // width == 500px; so don't worry you can use it without a problem.
Also ppl use it for lot's of small things. It's not uncommon for sites to have elements with negative margins.
Even here in Stackoverflow if you look at the CSS you can find negative margins.

why use negative margins?

Just looking at some CSS here, and I noticed:
.head{position:relative;overflow:hidden;margin:-30px 0 0 -25px;width:820px;padding:20px 25px 0 25px;background:url(/images/bkg.gif) 0 0 no-repeat;}
Why would you put -30 and -25px margins?
I started typing an answer, and then found a much better one here (Wayback Machine backup). Some salient points:
Negative margins:
are valid CSS
don't break page flow
have high levels of cross-browser compatibility (although if they break your link or floated image, then try adding position: relative; that should fix it)
Their effect on unfloated elements:
applying them to the top or left of an element "pulls" that element in the appropriate direction(s)
HOWEVER, applying them to the bottom or right of an element "pulls" immediately subsequent elements into them, making them overlap
Their effect on floated elements:
this is more complex and I can't summarise it better than the article. Have a play around in Firebug to get a feel for them.
There are some brilliant examples of negative margin use in that article (especially the 3-column layout! Magic. I've used a similar technique for page layout before.) The most common use for them I've found is just to move an element a small amount to correct its position, and to make one element overlap another for visual effect.
A lot of tricks and nice effects use negative margins:
Image Replacement Trick - when you want to use that particular font and you just can't tear away from it, image replacement is the trick. Uses negative margins to push out the regular font and replace it with the "picture" font.
Image Rollovers with borders - giving a negative margin to the image the same size in pixels as the border size will keep the image, and therefore the layout, from shifting on a rollover.
Center screen positioning - using negative margins the same dimension as the height and width of the object you want to center, you can center an object in the middle of the browser.
Negative margins can be helpful when you have other element "around" that you want to e.g. have a padding for all other elements. I use it very often, read here, why:
http://www.simplebits.com/notebook/2005/05/23/negative.html
Actually i think there is an use case where negative margins are the only right thing to do:
You want an part of a box to extend over the whole parent, even over the padding. So instead of removing the padding of the parent element an apply it to all children exept for your special case, you give your special case a negative margin. Also no haggling with positioning. Works great and is very readable.
Example: http://codepen.io/anon/pen/DpHvu

How to use very large font sizes in Internet Explorer with CSS that won't affect design?

The font size I need to match the design I have is 85pt, which is extremely large. In IE6 and IE7, my design is affected because the divs that contain these elements become larger than they normally are, and as a result, elements under these are pushed further down, somewhat breaking the design. I have the height defined for these elements and when I decrease the font size, the elements begin to shrink to the correct size. I've added line-height: 0; to the element and this works in all modern browsers.
Unfortunately, the design I'm working on cannot be shown publicly, but I was hoping to get some insight into other possible techniques that I could try to get the design to render correctly. The height of the parent element is 144px, which includes 10px padding on top and bottom and a top and bottom 1px border.
Unfortunately there's not a lot more that I can add to this, but I'll include whatever info I can if asked.
line-height:0 is a great start. However, I'm a little concerned about the 10px padding on the parent element. Whenever you mix padding with IE, you start to lose control over width & height.
I'd start by removing the padding-top on the parent and convert that into a margin-top:10px on the actual child element. If that still gives you trouble, remove the margin and try a position:relative on the child with a top:10px.
Finally, try adding a overflow:hidden to your parent element to force it to not budge when the font-size gets larger.
All this depends on what your child element actually is. If you convert it to an inline element (like a span, em, or strong) it might help alleviate some rendering issues, depending on your predefined styles.
Another thing to consider - are you using floats? Sometimes you'll get a double-float issue with IE and floats. A quick google for "IE double float" will show you why.
Does that help?
Convert the font-sizes to pixels and use px instead of pt. Make sure there that padding, margin and border is 0. Verify that there are no whitespace in your HTML except for between words. Whitespace can end up being displayed as a newline or space, making elements bigger than intended. Also don't set line-height to 0, set it to either auto or the same as font-size.
Thank you all for your input. Originally I needed absolute positioning on the element in question, while the parent element had relative positioning. However, using this with line-height: 0 caused the text to disappear in IE6 and 7; after trying to figure out where the text was initially, I removed absolute positioning and decided to leave the text left aligned in IE6 and 7, which affected the position of other elements as a result. I revisited the original absolute positioning and added border to the element to reveal its location. Doing this showed that it was exactly as I defined it: an element with a line-height of 0px, so the top and bottom borders were next to each other. For IE6 and 7, I defined line-height: 100%; and my text was almost where I needed it. I added top and the needed pixels and now my element is in the correct position with its line-height not affecting any of the other elements because of the positioning.
Thank you all again for your assistance.
My first thought when reading your post was to adjust the line-height, but since you've already done that, I'm not sure how much more can be done. From your summary, I gather that the design cannot be modified to account for the large font sizes.
Another answerer recommended using pixel sizes, but I would recommend using ems as they are percentage dimensions and will be more consistent across browsers, screens, and resolutions.
Line-height can be left as 0 (or set it to the height of the parent element), but you will likely see the text floating over other elements if the text's height surpasses the line-height.
Any possible way you could use an image for the text instead? That's really the only fool-proof method for getting all browsers to look consistent.

Resources