[英]CSS: Won't center div with max-width
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;
我希望outher div
main-container
max-width: 800px;
and the inner div to be wrapped around a table and to have the same with as the table. 和内部div围绕一个表,并与表相同。 Also
<div class="content">
should be centered inside his parent div - main-container
. <div class="content">
应该在他的父div - main-container
居中。 What am I doing wrong here? 我在这做错了什么? jsfiddle
的jsfiddle
I had a similar issue. 我有一个类似的问题。 Setting
设置
margin-left: auto;
margin-right: auto;
on the inner div worked for me. 在内部div为我工作。
If you need the .content
to be inline-block , just set the container text-align: center
and the content text-align: left
. 如果您需要
.content
为内联块 ,只需设置容器text-align: center
和内容text-align: left
。 You won't be able to center inline-block elements using margin: auto
. 您将无法使用
margin: auto
来居中嵌入块元素。
I tried this and it worked 我试过这个并且它有效
.element-container{
width:100%;
max-width: 700px;
margin: 0px auto;
}
.element{
width: 80%;
max-width: 700px;
height: auto;
margin-left: 10%;
}
You could just change display to table
. 您可以将显示更改为
table
。 http://jsfiddle.net/4GMNf/14/ http://jsfiddle.net/4GMNf/14/
.main-container {
max-width: 800px;
margin: auto;
position: relative;
background-color: red;
}
.content {
margin: auto;
max-width: 600px;
height: 300px;
background-color: gray;
display: table;
}
table
{
width: 200px;
}
try this 试试这个
http://jsfiddle.net/4GMNf/10/ http://jsfiddle.net/4GMNf/10/
CSS CSS
.main-container {
max-width: 800px;
margin: auto;
position: relative;
background-color: red;
}
.content {
margin: auto;
max-width: 600px;
height: 300px;
background-color: gray;
}
table {
width: 200px;
}
Actually, an inline-block element not taken width parent it still contents width, Because a inline-block element behive the middle between block and non-block or inline. 实际上,一个内联块元素没有采用宽度为父,它仍然是内容宽度,因为内联块元素在块和非块或内联之间的中间位置。 So, When you talk this element is inline-block then talk the parent text-align center.
所以,当你说这个元素是内联块时,然后谈谈父文本对齐中心。
Here the code: 这里的代码:
Ans-1: ANS-1:
.main-container { max-width: 800px; margin: auto; position: relative; background-color: red; text-align: center; } .content { margin: auto; max-width: 600px; height: 300px; background-color: gray; display: inline-block; text-align:left; } table { width: 200px; }
<div class="main-container"> <div class="content"> <table> <thead> <tr> <h1>Name</h1> </tr> </thead> <tbody> <tr> <input type="text" Text="" id="txt"> </tr> </tbody> </table> </div> </div>
Ans 2: 答案2:
.main-container { max-width: 800px; margin: auto; position: relative; background-color: red; } .content { margin: auto; max-width: 400px; height: 300px; background-color: gray; } table { width: 200px; }
<div class="main-container"> <div class="content"> <table> <thead> <tr> <h1>Name</h1> </tr> </thead> <tbody> <tr> <input type="text" Text="" id="txt"> </tr> </tbody> </table> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.