Is Float For Layout Bad? What Should Be Used in Its Place

Is float for layout bad? What should be used in its place?

Floats were never meant for layout.

They’re simply meant to take an element, put it to one side, and let other content flow around it. That’s all.

Eric A. Meyer, in Floats Don’t Suck If You Use Them Right

The early web was influenced by print/academic publications where floats are used to control the flow of text around figures and tables.

So why did we use them for layout?

Because you can clear a footer below two floated columns, float layout
came into being. If there had ever been a way to “clear” elements
below positioned elements, we’d never have bothered to use floats for
layout.

Today, the CSS Flexible Box Layout Module flex and the CSS Grid Layout Module grid are optimized for user interface design and complex layouts and are expected to complement each other.

Grid enforces 2-dimensional alignment, uses a top-down approach to layout, allows explicit overlapping of items, and has more powerful spanning capabilities. Flexbox focuses on space distribution within an axis, uses a simpler bottom-up approach to layout, can use a content-size–based line-wrapping system to control its secondary axis, and relies on the underlying markup hierarchy to build more complex layouts.

Flexbox and Grid are—as of this writing—W3C candidate recommendation and candidate recommendation draft, respectively. Flexbox is supported by all major browsers and has known issues in IE11. Grid is supported by all major browsers but IE11 supports an older version of the spec.

Is the float property deprecated in CSS?

I ran your code and it works well as far as I think.

and no, the float property is not deprecated yet in CSS, you can still use it.
but for an advanced and more clean easy code, you better use flexbox

your code can be then something like that:

body {
margin: 0;
padding: 0;
}

#security {
position: relative;
background: url('https://zupimages.net/up/20/21/9zj3.jpg') no-repeat;
background-size: cover;
background-attachment: fixed;
width: 100%;
min-height: 500px;
display: flex; /* displaying it as a flexbox */
justify-content: end; /* shifting its content to the end "right" */
}

#security .security-wrapper {
background-color: rgba(255, 255, 255, 0.7);
width: 50%;
}

#security .security-title {
text-transform: uppercase;
font-size: 32px;
color: #c22312;
padding-top: 40px;
padding-bottom: 40px;
padding-left: 25px;
}
<div id="security">
<div class="security-wrapper">
<div class="security-title">Security investment solutions</div>
</div>
</div>

Is it a good practice to use float to position element?

Fundamentally? Sure, that's what it's there for.

Whether it's the right choice depends on what exactly you want to do.

You just need to take care of the consequences - for example, the need for using the clear property when using multiple subsequent floated elements to avoid undesired effects.

Disadvantage to floating everything in a layout?

@kevin; float is not a bad practice; it depends on how you are using it & what the needs of the design are. There is no need to use it on everything when there is no need & it comes from experience.

Every browser renders float correctly.
yes if you use clear:both in your markup like this

<div style="clear:both"></div>

it's increase your markup which increase your page loading time.
. SO, use overflow:hidden in your css to clear it.

Why Float is better than position:relative and absolute while we can make layout quickly with position?

float will not break the document flow -- also, it will position any element it uses the best it can fit in the container width -- say I have 5 x 200px divs in a 800px width container, the last 5th will go in a "new line" below the other ones -- using position:relative will make you need to calculate when it needs to break yourself, and it won't break correctly since the display will either be block and go over the whole width or it will be inline-block or inline which won't render the same way for divs as block would and would pretty much mess up the document flow & layout.

It depends on what you want to do: position:relative is used to move the element a bit aside from it's natural location, whereas float will make it pop to the left-most or right-most position in the parent element. position:absolute will let you position it relative to the nearest positioned ancestor (instead of positioned relative to the viewport, like fixed).
However; if an absolute positioned element has no positioned ancestors, it uses the document body, and moves along with page scrolling.

CSS Float Positioning

It's possible if you also float the paragraph in the opposite direction. However, you'll probably end up with a lot of gotchas with floats (e.g. "clearfix").

Look into inline-block or flex layouts for better approaches (or even a grid layout, but browser support is likely still spotty).

Avoid tables for laying out content, they also can come with a lot of unintended, hard-to-debug side-effects.

.text {  float: left;}
iframe { float: right;}
<div class="text"><p>This is a sentence.</p></div><iframe src="https://www.youtube.com/embed/YbJOTdZBX1g"></iframe>

Is it that wrong to use table instead of css' float?

6 years later (we're now in 2017) and CSS now has grids and flexbox which should be the easiest way to build a layout without using ugly hacks and without using tables.

CSS: Fixed or Float Layout?

What about revealing more or less of the image as the browser is resized, rather than scaling the image? It's not quite the same effect, but it's an easy way to fill an entire space with an image.

Let's assume, for the sake of the example, that your masthead's background image contains a logo of some sort on top of, say, a photograph of a city skyline. This is, overall, 1600px wide. The logo sits to the left of the image while the cityscape extends far right. And we'll assume your markup looks roughly like this:

<div id="page">
<div id="masthead">...</div>
<div id="navigation">...</div>
...
</div>

We can set the #page element to an elastic width and apply a background image to the #masthead element:

#page {
max-width: 1600px;
min-width: 800px;
width: 80%;
}

#masthead {
background: url('path/to/image.jpg') no-repeat left top;
height: 100px;
width: auto;
}

What happens here is that the #masthead element will expand to the width of the #page element, which will be somewhere between 800px and 1600px wide (inclusive) depending on how wide the browser window is. When the #page element is 800px wide, you see only the left-most 800px of the skyline; when it's 1600px wide, you see the entire skyline. That way your logo is always visible and when the browser is resized, more of the cityscape is revealed.

This does require having a larger image to start with (at least as wide as your max-width, if you go elastic), but the result is a masthead that will look good no matter what size it is--without relying on, as strager mentioned, browsers' image resizing algorithms.



Related Topics



Leave a reply



Submit