居中(整理)
来源:互联网 发布:汤臣倍健 鱼油 知乎 编辑:程序博客网 时间:2024/05/29 13:48
一、在一个div里的div左右垂直居中
条件1:已知box1,box2宽高
条件2:已知box1宽高,box2宽高未知(即box2宽高是可变的)
<div class="box1"><div class="box2"></div></div>通用method1:
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Document</title><style type="text/css">.box1{width: 500px;height: 500px;border: 1px solid red;text-align: center;}.box2{width: 200px;height: 200px;border:1px solid green;display:inline-block;vertical-align: middle;}span{width: 0;height: 100%;display: inline-block;vertical-align: middle;}</style></head><body><div class="box1"><div class="box2"></div><span></span></div></body></html>
通用method2:
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Document</title><style type="text/css">.box1{width: 500px;height: 500px;border: 1px solid red;display: table-cell;vertical-align: middle;}.box2{width: 200px;height: 200px;border:1px solid green;margin: 0 auto;}</style></head><body><div class="box1"><div class="box2"></div></div></body></html>
通用method3:
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Document</title><style type="text/css">.box1{width: 500px;height: 500px;border: 1px solid red;position: relative;}.box2{width: 200px;height: 200px;border:1px solid green;position: absolute;top: 0;right: 0;bottom: 0;left: 0;margin: auto;}</style></head><body><div class="box1"><div class="box2"></div></div></body></html>
通用method4:
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Document</title><style type="text/css">.box1{width: 500px;height: 500px;border: 1px solid red;position: relative;}.box2{width: 200px;height: 200px;border:1px solid green;position: absolute;top: 50%;left: 50%;transform:translate(-50%,-50%);}//使用css3属性</style></head><body><div class="box1"><div class="box2"></div></div></body></html>
box2宽高已知时:(或不用定位,计算后用margin来实现)
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Document</title><style type="text/css">.box1{width: 500px;height: 500px;border: 1px solid red;position: relative;}.box2{width: 200px;height: 200px;border:1px solid green;position: absolute;top: 50%;left: 50%;margin: -100px 0 0 -100px;}</style></head><body><div class="box1"><div class="box2"></div></div></body></html>
当需要文字(inline)居中时:line-height:容器高度; text-align:center;(父元素)
当需要图片居中时:可用method1或给父元素line-height:容器高度;img vertical-align:middle;
阅读全文
0 0
- 居中(整理)
- css居中思路整理
- CSS 6种完全居中最佳实践(整理)
- css 垂直居中方法整理
- 居中大全【虐心整理】
- 居中(水平居中+垂直居中)
- CSS图片垂直居中方法整理集合
- CSS图片垂直居中方法整理集合 !
- css垂直水平居中的整理
- CSS图片垂直居中方法整理集合 !
- css垂直水平居中的整理
- css兼容性整理,水平、垂直居中等
- CSS图片垂直居中方法整理集合
- 整理div左右垂直居中解决方案
- LinearLayout、RelativeLayout、FrameLayout居中显示整理
- CSS中的几种居中方式整理
- css遇到的居中问题整理
- Android笔记:Button居中|水平居中|垂直居中(总结)
- 详解浮点运算的定点编程
- Hibernate学习之---映射组件属性
- bayes python 源代码
- 473. Matchsticks to Square
- Microsoft.AspNet.Identity 剖析(一)—— 初识 Identity
- 居中(整理)
- 基于Swift的iOS应用程序开发:使用NSMutableURLRequest向服务器发送请求
- 用Python爬取用户虾米音乐的歌单
- 光头李
- Java SE 6.0新特性体验(上篇)
- [JS]小数部分处理
- .hex 文件详解 stm32(stm32深入理解机器码)
- C#中为啥会有大小写两种类型,比如string和String
- 智能指针之make_unique与make_shared