HTML layout centered

2019-06-11 08:57发布

I am trying to create a HTML layout and I have created a number of elements and given them a border etc in my CSS. I am trying to have the main 'Wrapper' centered so everything that goes inside this element are also centered.

I have tried everything, margin, align, absolute etc and nothing is working. My stays situated in the top left corner of my page.

This is my index page where the elements are:

<!--#include file ="inc.heads.asp"-->

<html>

<head>
</head>

<body>
 <div id ="divWrapper">
  <div id ="divHeader">
   <img src="Images/title.png">
   <br>
   <div id ="divNavBar">
    <br>
     <div id ="divContent">
     </div>
   </div>
  </div>
 </div>
</body>

</html>

and this is my CSS:

body {
 background-color: #300;
}

#divWrapper {
 margin: 0 auto;
 width: 800px;
}

#divHeader {
 width: 500px;
 border-style: inset;
 border-color: #COCOCO;
 background-color: #707070;
 padding: 5px;
}

#divNavBar {
width: 500px;
border-style: inset;
border-color: #COCOCO;
background-color: #707070;
padding: 5px;
}

#divContent {
 float: left;
 width: 500px;
 border-style: inset;
 border-color: #COCOCO;
 background-color: #707070;
 padding: 5px;
}

If someone could possibly shed some light on why none of the things I have tried work and what a possible solution could be.

Thanks!

标签: css layout html
5条回答
我只想做你的唯一
2楼-- · 2019-06-11 09:30
#divWrapper{
   margin-left:auto;
   margin-right:auto;
}

Should do the trick.

查看更多
Deceive 欺骗
3楼-- · 2019-06-11 09:30

Seriously what is the point of all those wrappers? Just do this:

body {width: 800px; margin: 0 auto; }
查看更多
再贱就再见
4楼-- · 2019-06-11 09:45

Just add margin: 0 auto to the contents of #divWrapper:

For example, add to your CSS:

#divWrapper * {
    margin: 0 auto;
}

If you only want the div elements to be centered, use:

#divWrapper div {
    margin: 0 auto;
}

You can see a working demo here > http://jsfiddle.net/gu7Sr/

Also, as a side note, try to avoid using <br /> for creating your layout. It won't scale well and you'll have a tough time redesigning in the future!

查看更多
迷人小祖宗
5楼-- · 2019-06-11 09:50

More just a comment to BenM's answer, but I don't have the reputation to comment yet. Pretty sure just:

margin:auto;

will do the trick by itself.

查看更多
地球回转人心会变
6楼-- · 2019-06-11 09:53

I think this is what you want. Link : http://codepen.io/anon/pen/Ihzxp

<body>
 <div id="divWrapper">
  <div id="divHeader">
   <img src="http://blog.codepen.io/wp-content/uploads/2012/06/Button-Fill-Black-Small.png">
   <div id="divNavBar">
     <div id="divContent">
       Stuff
     </div>
   </div>
  </div>
 </div>
</body>

and

body {
 background-color: #300;
}

#divWrapper {
 width: 100%;
}

#divHeader {
 width: 600px;
 border-style: inset;
 border-color: #COCOCO;
 background-color: #707070;
 padding: 5px;
margin: 5px auto;
 text-align:center;
}

#divNavBar {
width: 550px;
border-style: inset;
border-color: #COCOCO;
background-color: #707070;
padding: 15px;
margin: 5px auto;
}

#divContent {
 width: 500px;
 border-style: inset;
 border-color: #COCOCO;
 background-color: #707070;
 padding: 5px;
 margin: 5px auto;
}
查看更多
登录 后发表回答