html - 主体中的居中部分

标签 html css

我有一个格式如下的html

   <body>
   <section></section>
   <section></section>
   <section></section>
   </body>

正文是整个页面,我希望每个部分在正文中水平居中。

最佳答案

它们默认居中(宽度为 100%)。如果您指定的宽度 < 100%,请使用 auto 的左/右边距将它们水平居中。

body {
border: 1px solid black;
padding: 1em;
}

section {
width: 80%;
margin: auto;
border: 1px solid red;
height: 1em;
}
<body>
   <section></section>
   <section></section>
   <section></section>
</body>

你也可以使用display: flex;对齐元素:居中; flex-direction: column; 在父级上以水平居中各部分。

body {
border: 1px solid black;
padding: 1em;
display: flex;
align-items: center;
flex-direction: column;
}

section {
width: 80%;
border: 1px solid red;
height: 1em;
}
<body>
   <section></section>
   <section></section>
   <section></section>
</body>

关于html - 主体中的居中部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42941249/

相关文章:

html - div位置问题

html - Div 内容未显示在正确的位置,因为在 div 具有位置 : relative 之前

css - 表td垂直溢出

html - 使 li 背景色跨越整个 ul 宽度 CSS

css - 具有简单形式的 Rails 4 - CSS 问题

php - 表格中的输入文本框不允许在 Firefox 中进行任何输入

html - 使用 jQuery 扩展时, float 到左侧的框会被推到右侧

jquery - 新添加的元素上的 .fadeIn()

javascript - 如何在谷歌可视化中画一条线来指示今天/当前日期

css - 无法使用 CSS 限制图像大小