Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS: Responsive way to center a fluid div (without px width) while limiting the maximum width?

I've seen a million questions about how to center a block element and there seem to be a couple popular ways to do it, but they all rely on fixed pixels widths. Then either margin:0 auto or with absolute/relative positioning and left:50%; margin-left:[-1/2 width]; etc. We all know this can't work if the element has a width set in %.

Is there really no way to do this in a way that is truly fluid? I'm talking about using % for width (not setting a dozen media queries with increasingly small fixed widths).

Beware: there are tons of solutions out there which use the buzz word "responsive" but don't answer my question (because they use fixed widths anyway). Here's an example.

Update: I almost forgot: how do you handle limiting the max-width of the centered element and still keep it in the center? See my comment under @smdrager's answer. Real-life example. I want a pop-up message or a light box effect containing a paragraph of text to appear centered in the window and the text to wrap fluidly depending on the width. But I don't want the text box to stretch out toooo far where the text would get difficult to read (imagine a 4ft screen with three paragraphs stretched out onto a single line of text). If you add a max-width to most approaches, the centered box will stop centering when the max-width is reached.

like image 683
emersonthis Avatar asked Jul 09 '13 13:07

emersonthis


People also ask

How do I center a div in CSS responsive?

You can use margin:0 auto; to center a div horizontally as long as its width is less than that of the container div.

How do I center a div without the width?

If the target element is absolutely positioned, you can center it by moving it 50% in one direction ( left: 50% ) and then transforming it 50% in the opposition direction ( transform:translateX(-50%) ). This works without defining the target element's width (or with width:auto ).

How do I center a div perfectly?

You can do this by setting the display property to "flex." Then define the align-items and justify-content property to “center.” This will tell the browser to center the flex item (the div within the div) vertically and horizontally.

How do I center a div according to my screen size?

Easiest way to center something regardless of page width is margin: auto; in your CSS, with height and width defined. Show activity on this post. This will center your DIV with class center-div horizontally AND vertically. The margin-left must be negative half of what your width is.


2 Answers

Centering both horizontally and vertically

Actually, having the height and width in percents makes centering it even easier. You just offset the left and top by half of the area not occupied by the div.

So if you height is 40%, 100% - 40% = 60%. So you want 30% above and below. Then top: 30% does the trick.

See the example here: http://dabblet.com/gist/5957545

Centering only horizontally

Use inline-block. The other answer here will not work for IE 8 and below, however. You must use a CSS hack or conditional styles for that. Here is the hack version:

See the example here: http://dabblet.com/gist/5957591

.inlineblock {      display: inline-block;     zoom: 1;     display*: inline; /* ie hack */ } 

EDIT

By using media queries you can combine two techniques to achive the effect you want. The only complication is height. You use a nested div to switch between % width and

http://dabblet.com/gist/5957676

@media (max-width: 1000px) {     .center{}     .center-inner{left:25%;top:25%;position:absolute;width:50%;height:300px;background:#f0f;text-align:center;max-width:500px;max-height:500px;} } @media (min-width: 1000px) {     .center{left:50%;top:25%;position:absolute;}     .center-inner{width:500px;height:100%;margin-left:-250px;height:300px;background:#f0f;text-align:center;max-width:500px;max-height:500px;} } 
like image 184
smdrager Avatar answered Sep 30 '22 13:09

smdrager


From Chris Coyier's article on centering percentage width elements:

Instead of using negative margins, you use negative translate() transforms.

.center {   position: absolute;   left: 50%;   top: 50%;    /*   Nope =(   margin-left: -25%;   margin-top: -25%;   */    /*    Yep!   */   transform: translate(-50%, -50%);    /*   Not even necessary really.    e.g. Height could be left out!   */   width: 40%;   height: 50%; } 

Codepen

like image 26
Brian Phillips Avatar answered Sep 30 '22 14:09

Brian Phillips