Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there no float center in CSS?

Tags:

css

css-float

I have found that there is no float center in CSS and I was a little disappointed. However, I can't help but ask myself why. While many people want to use this for centering content I wished to use it to float a bunch of blocks into rows on a dynamic page size. Unfortunately without a float center it looks sloppy as there is extra space (whatever fraction of a full block doesn't fit) on one side. It makes me sad that the intended use of floats is hurt by this property missing.

I can't see a reason why there isn't a float center and was wondering if anyone had reasons, either technical or otherwise why a float center was not included in the standard.

like image 854
Kevin Cox Avatar asked Mar 11 '13 03:03

Kevin Cox


People also ask

Does Float Center exist?

float: center does not actually exist, for reasons I will explain shortly. But it is possible to fake it. This is such a neat trick that I wish I had come up with it, but credit goes to Chris Coyier; what is shown here is merely my responsive variation on his technique.

How do I center a floated div?

First, remove the float attribute on the inner div s. Then, put text-align: center on the main outer div . And for the inner div s, use display: inline-block . Might also be wise to give them explicit widths too.

How do you float in the middle in HTML?

To center the text using float we can use margin-left or margin-right and make it 50% , like below.

How do I force a center in CSS?

Simply add text-align center to parent div and set the child div display to inline-block. This will force our div to behave like inline element and therefore subjected text-align center. The difference between this method and margin: 0 auto is we don't need the width to be specified.


3 Answers

Instead of using float: left, use display: inline-block on the individual elements and center their container.

http://jsfiddle.net/ExplosionPIlls/rAkNY/5/

like image 86
Explosion Pills Avatar answered Oct 18 '22 22:10

Explosion Pills


Yes, There is not Float center/middle and may the W3C having the answer.

There is <center> tag but no-longer.

The <center> tag is deprecated as of HTML 4, and using it creates a few different issues. HTML centered elements can display differently in different browsers, and using the tag can increase page load time. Also, heavy use of will complicate your site redesigns — removing hundreds of tags takes a lot longer than changing one style rule in a stylesheet.

The tag was officially deprecated many, many years ago, but it is still recognized by most browsers through their backward-compatibility features. So yeah, if you something, it'll be centered. However, in the interest of future-proofing you should use modern CSS centering methods instead.

Use margin:0 auto;

If you're centering something else, margin: 0 auto; will get your element centered most of the time. (Quick note: your element must have a declared width for this to work.)

The margin: 0 auto; rule is shorthand for 0 top and bottom margin, and automatic left and right margins. Automatic left and right margins work together to push the element into the center of its container.

like image 4
Krish Avatar answered Oct 18 '22 22:10

Krish


there is no float center because floats take elements out of the content flow and position them as far left/right as possible. floats by themselves only move things sideways. not 100% on this last part, but i reckon it has something to do with print. i know the idea of floats was taken from the print industry.

like image 3
albert Avatar answered Oct 19 '22 00:10

albert