如何使用 CSS 让 Div 整体居中
使用 CSS 让 Div 整体居中有两种主要方法:使用 text-align 属性和使用 margin 属性。
使用 text-align 属性
- 为父容器设置
text-align: center。这将使子元素在其父容器内水平居中对齐。
使用 margin 属性
- 为 Div 设置
margin: auto。这将设置 Div 的左右外边距为自动,使其在水平方向上居中。
示例
<code class="<a style=\'color:#f60; text-decoration:underline;\' href=" https: target="_blank">css">/* 使用 text-align 属性 */
.parent-container {
text-align: center;
}
.child-div {
/* 无需设置任何属性,已由父容器居中 */
}
/* 使用 margin 属性 */
.div-with-margin {
margin: auto;
/* 无需设置任何其他属性 */
}</code>



腾讯云 12-20 广告

