如何将网页内容居中
我似乎无法解决为什么我的两个 div 没有在页面上居中的问题。我知道这是因为 float: left 。但对于我的一生,我不知道如何解决这个问题。有人可以帮忙吗?
这是我的 HTML:
<body>
<div align="center">
<div id="left" class="left">
Left box needs to be on left of of white box
</div>
<div id="content" class="content">
Right box needs to be on the right side of red box
</div>
</div>
</body>
这是我的 CSS:
body, div, h1, h2, h3, h4, h5, h6, p, ul, img {margin:0px; padding:0px; }
body {
font-family: Arial, sans-serif;
background: #006699;
}
.content{
width: 300px;
float: left;
background: #ffffff;
height: 300px;
}
.left{
background: none repeat scroll 0 0 red;
float: left;
height: 300px;
width: 300px;
}
我知道 float: left;导致了问题,但我不知道如何解决它。
谢谢。
I can't seem to solve the issue on why my two divs are not centering on the page. I know it is because of float: left. But for the life of me, I can't figure out how to solve the problem. Can anyone help?
Here is my HTML:
<body>
<div align="center">
<div id="left" class="left">
Left box needs to be on left of of white box
</div>
<div id="content" class="content">
Right box needs to be on the right side of red box
</div>
</div>
</body>
Here is my CSS:
body, div, h1, h2, h3, h4, h5, h6, p, ul, img {margin:0px; padding:0px; }
body {
font-family: Arial, sans-serif;
background: #006699;
}
.content{
width: 300px;
float: left;
background: #ffffff;
height: 300px;
}
.left{
background: none repeat scroll 0 0 red;
float: left;
height: 300px;
width: 300px;
}
I know the float: left; is causing the problem but I don't know how to solve it.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将它们包装在具有设定宽度和
margin: 0 auto;
的 div 中...例如,将顶级 div 从align="center"
更改为class ="center"
(或 id 而不是 class),然后执行以下操作:align 属性不会影响块级元素的对齐方式,而是影响其中内联元素的对齐方式。你的 div 不是。
wrap them in a div with a set width and
margin: 0 auto;
... for example change your top level div fromalign="center"
toclass="center"
(or id instead of class) and then do:the align attribute wont effect the alignment of a block level element but rather the alignment of inline elements inside it. which your div's are not.