[英]How do I center these two divs with the container being displayed inline-block?
I'm having trouble centering these two floated left divs into a container which has a height of auto and a display set to inline-block
. 我在将这两个浮动的div居中时遇到麻烦,该容器的高度为auto且显示设置为
inline-block
。 I've given my container a max-width of 1600px and when the window is stretched wider than 1600px, the container stays pinned to the left. 我给容器设置了最大宽度1600px,当窗口拉伸到大于1600px时,容器保持固定在左侧。 I tried text
align:center
, and margin:0 auto
, on the container but I'm not getting the result I'm looking for. 我在容器上尝试了文本
align:center
和margin:0 auto
,但是没有得到想要的结果。
Here is my code. 这是我的代码。
.keyfacts { width: 94%; height: auto; max-width: 1600px; padding: 60px 3% 60px 3%; background-color: #634A4B; display: inline-block; } .keyfactsleft { float: left; width: 47%; height: auto; padding-right: 3%; } .keyfactsleft p { font-family: 'Raleway', san-serif; font-size: 24px; line-height: 32px; font-weight: 100; color: #58595b; margin-bottom: 35px; } .keyfactsright { float: left; width: 50%; height: 465px; background-image: url(_images/meredith-manor-aerial.jpg); background-size: cover; background-position: center center; background-color: #A05B5C; }
<section class="keyfacts"> <div class="keyfactsleft"> <h3 class="red">When</h3> <p>Saturday, October 14, 2017 at Five in the Evening</p> <h3 class="red">Where</h3> <p>Meredith Manor<br>2270 Pottstown Pike, Pottstown, PA 19465</p> <p>Our convenient venue will host both our ceremony and our reception. Its location is only a few miles down the road from the Hilton Garden Inn, where you'll be sleeping if you plan on getting wild and staying over.</p> </div> <div class="keyfactsright"></div> </section>
see this working sample: https://jsfiddle.net/s8rL8e8v/ if your content has a height, you dont need to put height on your container div. 请参阅此工作示例: https : //jsfiddle.net/s8rL8e8v/如果您的内容具有高度,则无需在容器div上放置高度。 just put
overflow:hidden
and it will cope all the content inside it and put some padding
. 只需将
overflow:hidden
放进去,它将处理其中的所有内容并放置一些padding
。
.keyfacts {
width: 94%;
max-width: 1600px;
margin: 0 auto;
overflow: hidden;
padding: 60px 3% 60px 3%;
background: #634A4B
}
.keyfactsleft {
float: left;
width: 47%;
height: auto;
padding-right: 3%;
}
.keyfactsleft p {
font-family: 'Raleway', san-serif;
font-size: 24px;
line-height: 32px;
font-weight: 100;
color: #58595b;
margin-bottom: 35px;
}
.keyfactsright {
float: left;
width: 50%;
height: 465px;
background-image: url(_images/meredith-manor-aerial.jpg);
background-size: cover;
background-position: center center;
background-color: #A05B5C;
}
If you want the parent container to be centered, then div.keyfacts
must be a block level element, for example, display: block
or display: table
. 如果要使父容器居中,则
div.keyfacts
必须是块级元素,例如display: block
或display: table
。
In both cases, margin: 0 auto
will center the container. 在这两种情况下,
margin: 0 auto
都会使容器居中。
Since you are specifying the width for div.keyfacts
, then either choice will work. 由于您正在指定
div.keyfacts
的宽度,因此任何一种选择都将起作用。
(I adjusted the width
and max-width
values for demonstration only.) (我调整了
width
和max-width
值仅用于演示。)
.keyfacts { width: 70%; height: auto; max-width: 450px; padding: 60px 3% 60px 3%; background-color: #634A4B; display: table; margin: 0 auto; } .keyfactsleft { float: left; width: 47%; height: auto; padding-right: 3%; } .keyfactsleft p { font-family: 'Raleway', san-serif; font-size: 24px; line-height: 32px; font-weight: 100; color: #58595b; margin-bottom: 35px; } .keyfactsright { float: left; width: 50%; height: 465px; background-image: url(_images/meredith-manor-aerial.jpg); background-size: cover; background-position: center center; background-color: #A05B5C; }
<section class="keyfacts"> <div class="keyfactsleft"> <h3 class="red">When</h3> <p>Saturday, October 14, 2017 at Five in the Evening</p> <h3 class="red">Where</h3> <p>Meredith Manor <br>2270 Pottstown Pike, Pottstown, PA 19465</p> <p>Our convenient venue will host both our ceremony and our reception. Its location is only a few miles down the road from the Hilton Garden Inn, where you'll be sleeping if you plan on getting wild and staying over.</p> </div> <div class="keyfactsright"></div> </section>
You can either do: 您可以执行以下操作:
body { text-align: center }
to center the .keyfacts
, but this will require you to do .keyfacts { text-align: left }
body { text-align: center }
以使.keyfacts
,但这需要您执行.keyfacts { text-align: left }
I would go with .keyfacts { display: block, margin: 0 auto }
instead. 我会
.keyfacts { display: block, margin: 0 auto }
代替。
If an element is display: inline-block
, then the parent will need a text-align: center
to center the element, but this affects all children elements within the container. 如果
display: inline-block
元素display: inline-block
,那么父元素将需要text-align: center
来使元素居中,但这会影响容器中的所有子元素。
If an element is display: block
, then applying margin: 0 auto
on the element itself (NOT THE PARENT) will center the element. 如果元素
display: block
,则在元素本身上应用margin: 0 auto
(没有父级)将使元素居中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.