css实现垂直水平居中的方法(个数不限)?
方法一:使用绝对定位
大家都知道margin:0 auto;能够实现水平居中,但却不知道margin:0 auto;也是可以实现垂直居中的;
给居中元素添加如下样式:
.Absolute-Center {
border:2px solid red;
height: 200px;
margin: auto;
position: absolute;
top:; left:; bottom:; right:;
}
缺点:内容容易溢出,建议使用overflow:auto;
方法二:负margin方法
给居中的div设置样式:
<style>
* {
margin: 0;
padding: 0;
} .Absolute-Center {
width: 400px;
height: 200px;
background-color: pink;
position: absolute;
top: 50%;
left: 50%;
margin-top: -100px;
margin-left: -200px;
}
</style>
<body>
<div class="Absolute-Center">
</div>
</body>
方法三:css3的 transform 来实现
<style>
* {
margin: 0;
padding: 0;
}
.mydiv {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 300px;
height: 200px;
}
</style>
<body>
<div class="mydiv" style="border: 2px solid red;"></div>
</body>
http://blog.csdn.net/freshlover/article/details/11579669
重新整理部分如下:
CSS实现垂直水平居中的方法(至少写2种)
(1) 文本的水平垂直居中
使用:line-height + text-align: center
<div class=”wrap”>
文本的垂直水平居中
</div>
html,body{
margin: 0;
}
.wrap{
line-height: 400px;
text-align: center;
height: 400px;
font-size:36px;
}
(2) 使用margin: auto 方法实现div水平垂直居中
<div class=”wrap”>
<div class=”content”></div>
</div>
.wrap{
Position: relative;
width: 200px;
height: 300px;
border: 1px solid red;
}
.content{
Position: absolute;
width: 50%;
height: 50%;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
border: 1px solid red;
}
(3) 负margin方法
<div class=”wrap”>
<div class=”content”></div>
</div>
.wrap{
Position: relative;
width: 200px;
height: 300px;
border: 1px solid red;
}
.content{
Position: absolute;
width: 180px;
height: 260px;
top: 50%;
left: 50%;
margin-top: -130px;
margin-left: -90px;
border: 1px solid red;
}
(4) 让图片水平垂直居中,使用table-cell方法
<div class=”wrap”>
<img src=”tupian.png”/>
</div>
.wrap{
width: 200px;
height: 300px;
border: 1px solid red;
display:table-cell;
vertical-align: middle;
text-align:center;
}
img{
vertical-align:middle;
border: 1px solid red;
}
(5) 弹性盒子方法
<div class=”wrap”>
<div class=”content”></div>
</div>
.wrap{
width: 300px;
height: 200px;
border: 1px solid red;
display: -webkit-flex;
display: flex;
-webkit-align-items: center;
align-items: center;
-webkit-justify-content: center;
justify-content: center;
}
.content{
padding: 20px;
border: 1px solid red;
}
(6) 使用CSS3的transform:translate(-50%,-50%); 或者
transform:translate3d(-50%,-50%,0);
<div class=”wrap”>
<div class=”content”></div>
</div>
.wrap{
width: 300px;
height: 200px;
border: 1px solid red;
position: relative;
}
.content{
Position: absolute;
width: 160px;
height: 100px;
left: 50%;
right: 50%;
transform: translate(-50%,-50%);或者用3d旋转的也可以
}
css实现垂直水平居中的方法(个数不限)?的更多相关文章
- css实现垂直水平居中的方法
html结构: <div class="box"> <div>垂直居中</div> </div> 方法1:display:flex ...
- css实现垂直水平居中的5种方法
css实现垂直水平居中的5种方法 给父元素设置table-cell,text-align,vertical-align #big{ width: 200px; height: 200px; borde ...
- css图片垂直水平居中及放大(实现水平垂直居中的效果有哪些方法?)
实现水平垂直居中方法有很多种: 一.万能法: 1.已知高度宽度元素的水平垂直居中,利用绝对定位和负边距实现. <style type="text/css"> .wrap ...
- CSS之垂直水平居中的背后
最开始,我想说,这个体系有点大,我写的并不好.就当作是一个思路吧,虽然这个思路有点乱.几乎每一个实现方案的背后都是该属性及其组合的原理,每一个都要剖析其规范细节的话,这篇文章绝不会是这样的篇幅,所以每 ...
- css文本垂直水平居中
一.单行文本居中 .content{ height:100px; line-height:100px; text-align:center; border:1px solid red; } 效果图 二 ...
- Css实现垂直水平居中的六种方法
经常在项目中用到,今天总结了一下: 演示地址:http://codepen.io/anon/pen/xGdpOa 以下两个类为公共类,便于更好的显示效果,非核心代码 .common{ width: 6 ...
- css中设置div垂直水平居中的方法
设置要水平垂直居中的div的position为absolute,left:50%;margin-left为负的这个元素宽度的一半,同理,top:50%;margin-top为负的这个元素的高度的一半. ...
- CSS之垂直水平居中方法
//居中方法1 position: absolute; top: 0; left: 0; right: 0; bottom: 0; margin: auto; width: 346px; height ...
- CSS实现垂直水平居中
HTML结构: <div class="wrapper"> <div class="content"></div> < ...
随机推荐
- Jenkins - ERROR: Exception when publishing, exception message [Failure] Build step 'Send build artifacts over SSH' changed build result to UNSTABLE
今天在处理Jenkins的时候出现了一些异常,看着控制台,编译都是通过的,只是没有部署上来,查看了控制台日志,如下: 刚开始还以为是权限通道什么的,后来才发现是执行脚本根本不让执行,以前也遇到过,都是 ...
- springboot自动生成mysql的DAO层代码
springboot提供了强大丰富的整合内容,但是每次要写一堆dao层的xml或者数据库相关的配置代码的时候,还是挺繁琐又容易出错的. 可以用mybatis-generator自动生成代码: 只需要加 ...
- 线段树 HDU-1754 I Hate It
附上原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754 Problem Description 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某 ...
- 软件工程实践助教每周小结 < 福州大学 | 傅明建 >
第一周助教小结 1. 助教博客链接: http://www.cnblogs.com/sinceway/ 2. 本周点评的作业数量:约22份,有多次交互 3. 本周点评有困难的作业链接: https:/ ...
- http协议中的响应代码从 1xx ~ 5xx,一共有41种
http协议中的响应代码从 1xx ~ 5xx,一共有41种 http://how2j.cn/k/http/http-response-code/572.html
- C# 解析html中筛选class的问题
C# 解析html中筛选class的问题 C# html解析 class 类 当我们用C#的.net解析html的时候,当html的元素没有id,并且没有过多的属性供筛选,只能通过class=&qu ...
- flutter packages.
connectivity This plugin allows Flutter apps to discover network connectivity and configure themselv ...
- SpringMVC,SpringBoot利用ajax上传文件到后台
1.传递单文件 首先html文件中有个<input type=”file” name=”file” id=”file”/>元素. 前台js写法: var formData=new Form ...
- IDEA 初建Spring项目(Hello Spring)
新建项目 在文件夹中建立一个项目文件 打开项目 打开IDEA,点击Open,根据所建项目路径找到该项目 依赖注入 点击项目名右键,点击new,点击file,创建pom.xml 内容为: <pro ...
- redis搭建主从复用-读写分离
1:安装redis5.0.3 2:解压到/usr/local/redis 3:在/opt/redis/下创建三个文件夹 data,存放数据的目录 log,存放日志的目录 conf,存放配置的目录 co ...