Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS: Won't center div with max-width

Tags:

html

css

center

I've got the following page structure:

<div class="main-container">     <div class="content">         <table>          </table>     </div> </div> 

I want the outher div main-container to have max-width: 800px; and the inner div to be wrapped around a table and to have the same with as the table. Also <div class="content"> should be centered inside his parent div - main-container. What am I doing wrong here? jsfiddle

Working example: jsfiddle

like image 951
Anton Belev Avatar asked Aug 01 '13 11:08

Anton Belev


People also ask

How do I center a div with a max width?

Technique 1The max-width does exactly what it says, it sets a maximum width for the center content. The margin declaration is what centers the content in the browser window. The "0" sets the top and bottom margin to "0" and the "auto" sets the right and left margin to "auto" which is what actually centers the content.

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.

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.

Why is my div not centering vertically?

To center a div vertically on a page, you can use the CSS position property, top property, and transform property. Start by setting the position of the div to absolute so that it's taken out of the normal document flow. Then set the top property to 50%.


2 Answers

I had a similar issue. Setting

margin-left: auto; margin-right: auto; 

on the inner div worked for me.

like image 103
Gideon Rosenthal Avatar answered Sep 30 '22 20:09

Gideon Rosenthal


If you need the .content to be inline-block, just set the container text-align: center and the content text-align: left. You won't be able to center inline-block elements using margin: auto.

like image 36
Adi Ulici Avatar answered Sep 30 '22 20:09

Adi Ulici