html - 如何将这两个 div 与显示在行内 block 中的容器居中?

标签 html css

我无法将这两个 float 的左侧 div 居中放置在高度为自动且显示设置为 inline-block 的容器中。我已将容器的最大宽度设置为 1600 像素,当窗口拉伸(stretch)宽度超过 1600 像素时,容器将固定在左侧。我在容器上尝试了文本 align:centermargin:0 auto,但我没有得到想要的结果。

这是我的代码。

.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>

最佳答案

查看此工作示例:https://jsfiddle.net/s8rL8e8v/ 如果您的内容有高度,则无需在容器 div 上设置高度。只需放置 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;
}

关于html - 如何将这两个 div 与显示在行内 block 中的容器居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39969254/

相关文章:

css - 我该如何解决这个 IE CSS 问题?

javascript - 为什么 safari 不能使用 css。甚至显示 css 类名?

html - IE 7-8 中的 jqGrid 在编辑/添加时在内容之上创建灰色区域而不是在其之上

css - html5进度条中的Webkit渐变背景渐变?

html - 右边距上的差距不会消失(Bootstrap)

javascript - 操作网页中的部分 - 创建、扩展

javascript - 悬停在不工作时的动画对象

javascript - 如何使用 jquery 从选择框中删除多个选项并将其放置到另一个 div?

jquery - 简单的 jquery (DOM).click() 函数不起作用

javascript - 如何使用分配给表单的 ID 使用 JavaScript 验证空/空字段的 html 表单