Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I avoid float elements shifting when I resize my browser?

I am new to the world of coding as well as CSS and have noticed that when I resize my browser, all my float elements move to say the right and the user is forced to scroll horizontally to view say the menu.

Firstly is there a need to prevent float elements not changing their position?

Secondly, is there a way I can avoid this if I should be preventing it?

like image 341
PeanutsMonkey Avatar asked May 24 '11 19:05

PeanutsMonkey


People also ask

How do I make DIV not move when resized?

first add a parent div/wrap and put those two divs of yours into it. Overall, whatever size you add to the min-width itll scale the parent div down to the size specified. once the window is sized down past your specified size, itll behave like any other window. this is my way of doing it.

What property will tell the browser to stop floating elements?

Definition and Usage Note: Elements next to a floating element will flow around it. To avoid this, use the clear property or the clearfix hack (see example at the bottom of this page).

What property can be used to help position floating elements properly on a Web page?

One way to position elements on a page is with the float property. The float property is pretty versatile and can be used in a number of different ways. Essentially, the float property allows us to take an element, remove it from the normal flow of a page, and position it to the left or right of its parent element.

How do you stop a floating element?

To clear a float, add the clear property to the element that needs to clear the float. This is usually the element after the floated element. The clear property can take the values of left , right , and both . Usually you'll want to use both.


1 Answers

Floats are a pain in the butt. Primarily because of the one browser that refuses to follow the universal web standards. Yeah, that one.

There are a million and one tricks to avoid these problems. I tell my team to follow these steps:

  1. Use absolutely positioned elements sparingly. If you position based on another element, things can "auto-adjust" but not so much when things are mathematically glued to one spot. They have their place, just not on every element on a page.
  2. Consider a "reset" or grid-based CSS layout. I personally prefer 960 Grids They take the guesswork out of the various different browsers. Will IE push your padding out? Will the IE-specific padding be too much or too little with Firefox? With a reset and/or a grid, it's not as big a deal.
  3. When doing percentages (which I personally really like) remember that browser generally don't use math where 1 + 1 = 2. That is to say, if you have a 1000 pixel space and you do two 50% columns side by side, chances are very good one is going to push itself below the other. Why? Because floats, margins, borders, etc. add up, and all differently in different browsers. So, think that .9 + .9 = 2 when you design. Does it work all the time? Nope. But it's a start.
  4. Test, test, test. Things like browsershots, Adobe Browser Labs, etc are your friend. Use them constantly to ensure you're putting out good quality code that looks consistent across all platforms.
  5. Keep it simple. You might be working on a project that dictates 100 different style sheets covering every single version of every browser. Most of us work on real-world projects with real-world budgets...that means you have to deploy quickly and efficiently. Use good quality w3 compliant code and quality CSS and you can completely avoid hacks.
like image 156
bpeterson76 Avatar answered Oct 03 '22 01:10

bpeterson76