vertical-align:middle  是依赖div内子元素最高的行高来实现对某元素居中的

-------不存在浮动时可以直接生效垂直居中

HTML

.box1{
display: table-cell;
vertical-align: middle;
text-align: center;
}

CSS

<div class="box box1">
<span>垂直居中</span>
</div>

-----如果存在浮动不生效情况 需要通过元素占位的方法居中可以通过:after 或者直接新建元素 (可能是定位影响子元素默认最高的行高)

.box1{
float:left;
}

  

.box1:after{
content:'';
width:0;
height:100%;
display:inline-block;
vertical-align:middle;
}

  

其他垂直居中方式  浏览器兼容性不能兼容较低版本 很多是HTML5样式

       display:flex

.box1{
display: flex;
justify-content:center;
align-items:Center;
}

   绝对定位和负边距

.box1{position:relative;}
.box1 span{
position: absolute;
width:100px;
height: 50px;
top:50%;
left:50%;
margin-left:-50px;
margin-top:-25px;
text-align: center;
}

  绝对定位和0

.box1 span{
width: 50%;
height: 50%;
background: #000;
overflow: auto;
margin: auto;
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
}

  translate

.box1 span{
position: absolute;
top:50%;
left:50%;
width:100%;
transform:translate(-50%,-50%);
text-align: center;
}

  display:flex和margin:auto

.box1{
display: flex;
text-align: center;
}
.box1 span{margin: auto;}

 display:-webkit-box

.box1{
display: -webkit-box;
-webkit-box-pack:center;
-webkit-box-align:center;
-webkit-box-orient: vertical;
text-align: center
}

 display:-webkit-box

<div class="floater"></div>
<div class="content"> Content here </div>

  

.floater {
float:left;
height:50%;
margin-bottom:-120px;
}
.content {
clear:both;
height:240px;
position:relative;
}

  

原文出处:http://www.cnblogs.com/hutuzhu/p/4450850.html

CSS样式 vertical-align:middle 垂直居中生效情况的更多相关文章

  1. 前端知识点回顾之重点篇——CSS中vertical align属性

    来源:https://www.cnblogs.com/shuiyi/p/5597187.html 行框的概念 红色(line-height)为行框的顶部和底部,绿色(font-size)为字体的高度, ...

  2. Chrome开发工具Elements面板(编辑DOM和CSS样式)详解

    Element 译为“元素”,Element 面板可以让我们动态查看和编辑DOM节点和CSS样式表,并且立即生效,避免了频繁切换浏览器和编辑器的麻烦. 我们可以使用Element面板来查看源代码,它不 ...

  3. css样式让input垂直居中

    css样式让input垂直居中 css代码: .div1{ border: 1px solid #CCC; width:1120px; height:40px; margin:auto; displa ...

  4. css样式之垂直居中

    1.div的水平居中 margin:0 auto 2.table-cell实现垂直居中 样式:.box{ width: 200px; height: 200px; background: red; } ...

  5. css样式之vertical-align垂直居中的应用

    css样式之vertical-align垂直居中的应用:将图片垂直左右居中 元素垂直居中 1:必须给容器父元素加上text-align:center 2:必须给当前元素转换成行内块元素,display ...

  6. 文字在div中水平和垂直居中的的css样式

    文字在div中水平和垂直居中的的css样式 text-align:center; /*水平居中*/ line-height: 20px; /*行距设为与div高度一致*/ 示例如下: HTML元素 & ...

  7. 使文字在div中水平和垂直居中的的css样式为,四个边分别设置阴影样式

    text-align:center; /*水平居中*/ line-height: 20px; /*行距设为与div高度一致*/ HTML元素 <div>水平垂直居中</div> ...

  8. css样式,媒体查询,垂直居中,js对象

    下面是一些截图,有关查询效率,css样式,媒体查询,垂直居中,js基本类型.  

  9. css样式和定义的class都没问题,但样式却没生效

    今天开发遇到过这样的问题,主要原因是 css 文件格式有问题导致的.有问题的 css 样式的那一行下面的 css 样式不能生效

随机推荐

  1. 【转载】为什么不常见include .c文件

    备:对于#include  <filename.h> ,编译器从标准库路径开始搜索 filename.h       对于#include  “filename.h” ,编译器从用户的工作 ...

  2. Storm-源码分析- timer (backtype.storm.timer)

    mk-timer timer是基于PriorityQueue实现的(和PriorityBlockingQueue区别, 在于没有阻塞机制, 不是线程安全的), 优先级队列是堆数据结构的典型应用 默认情 ...

  3. SSH整合中,使用父action重构子类action类.(在父类中获取子类中的泛型对象)

    import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import com.opensymphony.x ...

  4. 【转】Keepalived无法绑定VIP故障排查经历

    一 故障描述 我在台湾合作方给定的两台虚拟机上部署HAProxy+Keepalived负载均衡高可用方案.在配置完Keepalived后,重新启动Keepalived,Keepalived没有绑定VI ...

  5. react setstate

    1.prevstate参数 https://segmentfault.com/q/1010000008177874 2.不是实时渲染 http://bbs.reactnative.cn/topic/3 ...

  6. 【opencv】相机标定程序内存溢出

    运行相机内参标定程序出现内存溢出的错误 opencv的alloc.cpp报cv::OutOfMemoryError 因为同时开了多个线程,每个线程标定一台相机,每个线程都会imread读入所有标定图片 ...

  7. VMwareWorkstations中安装ubuntu,apt install报E: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable)

    ubuntu中apt安装软件python时报: E: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily un ...

  8. Unity3D优化技巧系列七

    笔者介绍:姜雪伟,IT公司技术合伙人.IT高级讲师,CSDN社区专家,特邀编辑.畅销书作者,国家专利发明人;已出版书籍:<手把手教你架构3D游戏引擎>电子工业出版社和<Unity3D ...

  9. mysql 下的命令

    1.查看mysql日志vim /var/log/mysqld.log

  10. UVA10100:Longest Match(最长公共子序列)&&HDU1458Common Subsequence ( LCS)

    题目链接:http://blog.csdn.net/u014361775/article/details/42873875 题目解析: 给定两行字符串序列,输出它们之间最大公共子单词的个数 对于给的两 ...