方法一:使用flex布局,父级元素设置justify-content和align-items

 <div class="cont">
<div class="item"></div>
</div> .cont {
width: 100px;
height: 100px;
background: red;
display: flex;
justify
-content: center;
align-
items: center;
} .item {
width: 50px;
height: 50px;
background: yellow
}

方法二:使用position和transform属性:这是css3里的样式;缺点:兼容性不好,只支持IE9+的浏览器

 <div class="cont">
<div class="item"></div>
</div> .cont {
width: 100px;
height: 100px;
background: red;
position: relative;
} .item {
width: 50px;
height: 50px;
background: yellow;
position: absolute;
left:
50%;
top: 50%;
transform: translate(-50%, -50%
);
}

方法三:使用position和margin属性:兼容性好。缺点:必须知道元素的宽高

 <div class="cont">
<div class="item"></div>
</div> .cont {
width: 100px;
height: 100px;
background: red;
position: relative;
} .item {
width: 50px;
height: 50px;
background: yellow;
position: absolute;
left:
50%;
top: 50%;
margin-left: -25px;
margin-top: -
25px;
}

方法四:使用position和margin属性:兼容性好

 <div class="cont">
<div class="item"></div>
</div> .cont {
width: 100px;
height: 100px;
background: red;
position: relative;
} .item {
width: 50px;
height: 50px;
background: yellow;
position: absolute;
left:
0;
right: 0;
top: 0;
bottom: 0
;
margin: auto;

}
 
 
 
 

CSS中垂直水平居中的更多相关文章

  1. js实现未知宽高的元素在指定元素中垂直水平居中

    js实现未知宽高的元素在指定元素中垂直水平居中:本章节介绍一下如何实现未知宽高的元素在指定元素下实现垂直水平居中效果,下面就以span元素为例子,介绍一下如何实现span元素在div中实现水平垂直居中 ...

  2. css实现垂直水平居中的5种方法

    css实现垂直水平居中的5种方法 给父元素设置table-cell,text-align,vertical-align #big{ width: 200px; height: 200px; borde ...

  3. css实现垂直水平居中的方法(个数不限)?

    方法一:使用绝对定位 大家都知道margin:0 auto;能够实现水平居中,但却不知道margin:0 auto;也是可以实现垂直居中的: 给居中元素添加如下样式: .Absolute-Center ...

  4. CSS之垂直水平居中的背后

    最开始,我想说,这个体系有点大,我写的并不好.就当作是一个思路吧,虽然这个思路有点乱.几乎每一个实现方案的背后都是该属性及其组合的原理,每一个都要剖析其规范细节的话,这篇文章绝不会是这样的篇幅,所以每 ...

  5. CSS中有关水平居中和垂直居中的解决办法

    CCS中让div等块级元素在父级元素中居中的方法: (1)div{  margin:0 auto   } 该方法只能实现水平的居中,无法实现元素的垂直居中 (2)当div元素的宽高是固定的,然后设置位 ...

  6. css图片垂直水平居中及放大(实现水平垂直居中的效果有哪些方法?)

    实现水平垂直居中方法有很多种: 一.万能法: 1.已知高度宽度元素的水平垂直居中,利用绝对定位和负边距实现. <style type="text/css"> .wrap ...

  7. CSS中元素水平居中和垂直居中的方法

    #CSS中水平居中和垂直居中的方法 一. 水平居中 1.行内元素(文本,图片等) 如果被设置元素为文本.图片等行内元素时,可以通过给父元素设置` text-align:center;` 来实现 2.定 ...

  8. css文本垂直水平居中

    一.单行文本居中 .content{ height:100px; line-height:100px; text-align:center; border:1px solid red; } 效果图 二 ...

  9. CSS实现垂直水平居中

    HTML结构: <div class="wrapper"> <div class="content"></div> < ...

随机推荐

  1. codevs 1144 守望者的逃离

    传送门 1144 守望者的逃离 2007年NOIP全国联赛普及组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解   题目描述 Description 恶 ...

  2. 【LeetCode】042 Trapping Rain Water

    题目: Given n non-negative integers representing an elevation map where the width of each bar is 1, co ...

  3. DataGrid当列宽超出当前宽度时,没有数据也恒有滚动条

    附件是DataGrid支持滚动条的文件. 具体使用如下: 1)DataGrid使用控件模板 <Setter Property="Template" Value="{ ...

  4. InformationSecurity:template

    ylbtech-InformationSecurity: 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部     6.返回顶部   作者:ylbtech出处:ht ...

  5. Oracle的rowid

    ROWID是数据的详细地址,通过rowid,oracle可以快速的定位某行具体的数据的位置. ROWID可以分为物理rowid和逻辑rowid两种.普通的堆表中的rowid是物理rowid,索引组织表 ...

  6. Maven&nbsp;3&nbsp;入门&nbsp;--&nbsp;安装与配置

    Maven 3 入门 -- 安装与配置 Maven以及其Eclipse插件m2eclipse的安装 (本文参考了Maven实战) 检查JDK的安装以及环境变量的配置 打开cmd echo %Java_ ...

  7. Oracle tns 协议

    下面是翻译国外的一篇博客,原文连接如下: https://thesprawl.org/research/oracle-tns-protocol/ 简介 TNS(Transparent Network ...

  8. js用"."和"[]"获取属性的区别

    在JavaScript中通常使用”."运算符来存取对象的属性的值.或者使用[]作为一个关联数组来存取对象的属性. 对象的属性和方法统称为对象的成员. 访问对象的属性 在JavaScript中 ...

  9. 解决错误---undefined reference to `pthread_create‘

    今天试着敲了一下APUE的小例子,遇到了个错误 -----  undefined reference to `pthread_create.(为自己这么晚接触多线程惭愧). 上网上查了一下,借人经验. ...

  10. python中出现 IndentationError:unindent does not match any outer indentation level

    python中出现IndentationError:unindent does not match any outer indentation level 今天在网上copy的一段代码,代码很简单,每 ...