Skip to content Skip to sidebar Skip to footer

Need Help Centering A Div Class On Page

I'm fairly new to HTML and CSS and I've been trying to figure out how to center this div class called .content in the middle of the page that is wrapped around my table data. I hav

Solution 1:

Since your .content does not have a fixed width you cannot use margin:0px auto which would center the content. In your case u can just it on the table element like this:

table{
    margin:0px auto;
}

but you need to remove display:inline-block from .content for this to work.

Solution 2:

Centering means equal margins from left and right. Therefore, side margins should be set to auto, like so:

.content {margin: 0 auto}

...where 0 is the margins from top AND bottom and auto are margins from left AND right

I'd remove inline-block from there as well.

Solution 3:

Update .content as

.content {
 display: inline-block;
 border: 1px;
 border-style:dashed;
 border-color: orange;
 color: red;
 text-align: center;
 margin-left:auto; #make this as auto
 margin-right: auto;
}

However this will align .content at the center horizontally, but if you want .content in center vertically then

.content{
  position: absolute;
  top:0;
  bottom: 0;
  left: 0;
  right: 0;
  margin: auto;
}

Solution 4:

In your case in particular, the best solution is to adjust the two "margin" properties inside your .content class declaration from:

margin-left:500px; margin_right: auto;

To:

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

Be aware that "margin_right" needs to have a hyphen ('-') not an underscore.

Post a Comment for "Need Help Centering A Div Class On Page"