HTML-子div在父div中垂直居中

本文介绍了四种不同的CSS方法来实现一个内部元素在其父容器内水平和垂直居中。这些方法包括使用绝对定位、表格单元格、Flexbox布局以及另一种绝对定位技巧。

转自:http://www.cnblogs.com/gwcyulong/p/6251342.html
方法一:

1、代码:

 <div style="width:200px;height:200px;border:solid blue;position:relative;">
     <div style="width:100px;height:100px;margin: auto;  position: absolute;  top: 0; left: 0; bottom: 0; right: 0; background: red;"></div>
</div>

2、效果图:
这里写图片描述
方法二:

1、代码

<div style="width:200px;height:200px;border:2px solid #000;display:table-cell;vertical-align:middle;text-align: center;">
     <div style="width:100px;height:100px;display:inline-block;background-color: red;"></div>
 </div>

2、效果图:
这里写图片描述
方法三:

1、代码:

<div style="width:200px;height:200px; border:2px solid #000;display:flex;justify-content:center;align-items:center;">
     <div style="width:100px;height:100px;background-color: red;"></div>
 </div>

2、效果图:
这里写图片描述
方法四:

1、代码:

<div style="width:200px;height:200px; border:2px solid #000;position:relative;">
     <div style="width:100px;height:100px;margin:auto;position:absolute;left:50%;top:50%;margin-left: -50px;margin-top:-50px;background-color: red;"></div>
 </div>

2、效果图:
这里写图片描述

个人还看了个比较详细的文章:http://blog.liuxianan.com/css-align-center.html

### 使用 CSS 让 `div` 在 `div` 中水平垂直居中 实现 `div` 在 `div` 中水平垂直居中有多种方法,以下是几种常见的方案及其适用场景: #### 方法一:Flexbox 布局 通过 Flexbox 可以轻松实现元素在容器中的水平垂直居中。这种方法兼容现代浏览器。 ```css .far { display: flex; /* 启用 Flexbox */ justify-content: center; /* 主轴方向水平)居中 */ align-items: center; /* 交叉轴方向(垂直)居中 */ } ``` 此方法适用于任何类型的元素[^1]。 --- #### 方法二:Line Height Inline Block 利用 `line-height` `inline-block` 的特性可以实现简单的水平垂直居中效果,但该方法仅适合单行文本或固定大小的内容。 ```css .box { width: 300px; height: 300px; line-height: 300px; /* 设置与高度相同的行高 */ text-align: center; /* 文本水平居中 */ } .box1 { display: inline-block; vertical-align: middle; width: 100px; height: 100px; background-color: yellow; } ``` 需要注意的是,如果元素的高度发生变化,则需要调整元素的 `line-height` 属性来保持一致[^2]。 --- #### 方法三:Table Cell 定位 借助 `display: table-cell` `vertical-align: middle`,可以让元素像表格单元格一样行为。 ```html <div class="father"> <div class="son"></div> </div> ``` ```css .father { background: red; height: 200px; width: 200px; display: table-cell; /* 将元素设置为表格单元格 */ vertical-align: middle; /* 垂直居中 */ } .son { background: green; width: 80%; height: 100px; margin: 0 auto; /* 水平居中 */ } ``` 这种方式的优点在于无需额外计算位置偏移量,缺点是可能不支持某些复杂的布局需求[^3]。 --- #### 方法四:Absolute Positioning 配合 Transform 对于更灵活的布局需求,可以通过绝对定位配合 `transform` 来完成精确控制。 ```css .parent { position: relative; /* 级相对定位 */ width: 300px; height: 300px; border: 1px solid black; } .child { position: absolute; /* 级绝对定位 */ top: 50%; /* 距离顶部 50% */ left: 50%; /* 距离左侧 50% */ transform: translate(-50%, -50%); /* 移动到中心点 */ width: 100px; height: 100px; background-color: blue; } ``` 这种技术广泛应用于响应式设计以及动态尺寸未知的情况下[^3]。 --- ### 总结 以上四种方式各有优劣: - **Flexbox** 是最推荐的方式之一,简单直观且功能强大; - **Line Height** 方便快捷但局限性强; - **Table Cell** 提供了良好的语义化支持; - **Absolute Positioning with Transform** 则提供了最大的灵活性。 具体选择取决于实际项目需求技术栈的支持情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值