Css3元素浮动时居中
2016-07-22 20:46
423 查看
(1)使用margin:auto+position:absolute
Html:代码如下
<span style="font-family:KaiTi_GB2312;font-size:18px;"> <div class="container"> <div class="content"> 使用margin:auto居中 </div> </div></span>
Css:代码如下
<pre name="code" class="html"><span style="font-size:18px;"> <span style="font-family:KaiTi_GB2312;"> .container { position: relative; width: 400px; height: 150px; background: red; } .content{ width: 150px; height: 50px; margin: auto; position: absolute; top: 0; left: 0; bottom: 0; right: 0; background: green; }</span></span><span style="font-size: 18px;"> </span>
(2)使用 top与margin-top;left与margin-left相互矫正
<pre name="code" class="html"><span style="font-family:KaiTi_GB2312;font-size:18px;"></span><pre name="code" class="html"><pre name="code" class="html">.container { position: relative; width: 400px; height: 150px; background: red; } .content { width: 150px; height: 50px; position: absolute; top: 50%; left: 50%; margin-left: -75px; margin-top: -25px; background: green; }
(3)同上原理 使用 transform:translate(-50%,-50%)代替margin矫正
<span style="font-family:KaiTi_GB2312;font-size:18px;"></span><pre name="code" class="html">.container { position: relative; width: 400px; height: 150px; background: red; }
.content {
width: 150px; height: 50px; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); background: green; }
扩展:如果想实现拖出文档流居中,只需把父级变为position:reactive同理如果想实现永久居中则变为position:fixed
相关文章推荐
- css属性书写顺序分析
- 盒子在浏览器中所占空间-width\height\padding\border\margin
- css3 图片圆形显示 如何CSS将正方形图片显示为圆形图片布局
- CSS布局 ——margin重叠
- animate.css高大上的CSS3动画库
- css 选择器
- CSS 文本垂直居中
- CSS布局——BFC
- IE5,IE6,IE7,IE8的css兼容性列表[转自MSDN]
- CSS Sprite图
- css3 动画
- 各种浏览器css hack
- 【转载】CSS3的calc()使用
- Css:背景色透明,内容不透明之终极方法!兼容所有浏览器
- 自学 html + css 用到的相关文档
- CSS3实现0.5px边框
- 用纯css改变下拉列表select框的默认样式
- css3的媒体查询(Media Queries)
- 认识和理解css布局中的BFC
- css3响应式布局