1、absolute,margin: auto

.container {
position: relative;
}
.content {
position: absolute;
margin: auto;
top:; left:; bottom:; right:;
}

注意: 当没有指定内容块的具体的高度和宽度时,内容块会填满剩余空间。可以通过使用max-height来限制高度,也可以通过 display:table 来使高度由内容来决定,但是浏览器支持不是很好。

2、relative,left top 50%,负margin-left margin-top

.isNegative {
position: relative;
width: 200px;
height: 300px;
left: 50%;
top: 50%;
margin-left: -100px;
margin-top: -150px;
}

缺点:需要知道具体的高度和宽度

3、relative,left top 50%,transform: translate(-50%,-50%)

  .content {
position: relative;
left: 50%;
top: 50%;
-webkit-transform: translate(-50%,-50%);
-ms-transform: translate(-50%,-50%);
transform: translate(-50%,-50%);
}

这里translate的百分比是相对于自身的,所以高度是可变的

4、Table-Cell

<div class="Center-Container is-Table">
<div class="Table-Cell">
<div class="Center-Block">
<!-- CONTENT -->
</div>
</div>
</div> .Center-Container.is-Table { display: table; }
.is-Table .Table-Cell {
display: table-cell;
vertical-align: middle;
}
.is-Table .Center-Block {
width: 50%;
margin: 0 auto;
}

注意: 需要添加最内层的div,并且给div指定宽度和margin:0 auto;来使div居中。

5、Inline-Block

  • html

  

<div class="Center-Container is-Inline">
<div class="Center-Block">
<!-- CONTENT -->
</div>
</div>
  • css
.Center-Container.is-Inline {
text-align: center;
overflow: auto;
} .Center-Container.is-Inline:after,
.is-Inline .Center-Block {
display: inline-block;
vertical-align: middle;
} .Center-Container.is-Inline:after {
content: '';
height: 100%;
margin-left: -0.25em; /* To offset spacing. May vary by font */
} .is-Inline .Center-Block {
max-width: 99%; /* Prevents issues with long content causes the content block to be pushed to the top */
/* max-width: calc(100% - 0.25em) /* Only for IE9+ */
}
  • 空内容也会占据一定空间,需要margin-left:-0.25em;来抵消偏移
  • 内容块的最大宽度不能是100%,否则会把::after的内容挤到下一行

6、Flex

  • html
 <div class="contain">
<div class="content">
// content
</div>
</div>
  • css
.container {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
}

  最方便最简单的方式,但是要注意浏览器的支持

7、display:flex和margin:auto

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

8、display:-webkit-box

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

9、display:-webkit-box

这种方法,在 content 元素外插入一个 div。设置此 divheight:50%; margin-bottom:-contentheight;。
content 清除浮动,并显示在中间。

.floater {
float:left;
height:50%;
margin-bottom:-120px;
}
.content {
clear:both;
height:240px;
position:relative;
}
  • 优点:
    适用于所有浏览器
    没有足够空间时(例如:窗口缩小) content 不会被截断,滚动条出现
  • 缺点:
    唯一我能想到的就是需要额外的空元素了

CSS上下左右居中的几种方法的更多相关文章

  1. 顽石系列:CSS实现垂直居中的五种方法

    顽石系列:CSS实现垂直居中的五种方法 在开发过程中,我们可能沿用或者试探性地去使用某种方法实现元素居中,但是对各种居中方法的以及使用场景很不清晰.参考的内容链接大概如下: 行内元素:https:// ...

  2. [转] 用javascript修改css伪类的几种方法

    用javascript修改css伪类的几种方法: Modify pseudo element styles with JavaScript http://pankajparashar.com/post ...

  3. css隐藏元素的几种方法与区别

    css隐藏元素的几种方法与区别 一:display:none;隐藏不占位 display 除了不能加入 CSS3 动画豪华大餐之外,基本效果卓越,没什么让人诟病的地方. 二:position:abso ...

  4. Qt中的布局浅析与弹簧的使用,以及Qt居中的两种方法

    1. 布局 为什么要布局: 布局之后窗口的排列是有序的 布局之后窗口的大小发生变化, 控件的大小也会对应变化 如果不对控件布局, 窗口显示出来之后有些控件的看不到的 布局是可以嵌套使用 常用的布局方式 ...

  5. css左右居中的几种常见方法

    本人是前端的新人,这是第一次写技术博客,各位大大,本文有错误请指正,手中的板砖尽量轻拍,我怕疼~~ 对于水平居中和垂直居中我也用过很多方法,但是有的时候管用有的时候又嗝屁不好使了.涉及到的情况很多,所 ...

  6. 《Web开发中让盒子居中的几种方法》

    一.记录下几种盒子居中的方法: 1.0.margin固定宽高居中: 2.0.负margin居中: 3.0.绝对定位居中: 4.0.table-cell居中: 5.0.flex居中: 6.0.trans ...

  7. 纯CSS实现垂直居中的几种方法

    垂直居中是布局中十分常见的效果之一,为实现良好的兼容性,PC端实现垂直居中的方法一般是通过绝对定位,table-cell,负边距等方法.有了css3,针对移动端的垂直居中就更加多样化. 方法1:tab ...

  8. CSS上下左右居中对齐

    上下左右居中对齐 display:  inline/inline-block 将父元素(容器)设定 text-align: center: 即可左右置中. display: block 将元素本身的 ...

  9. 用css隐藏元素的5种方法

    .green { width: 100px; height: 100px; background-color: #a0ee00; text-align: center; float: left; ma ...

随机推荐

  1. 对于python爬虫urllib库的一些理解(抽空更新)

    urllib库是Python中一个最基本的网络请求库.可以模拟浏览器的行为,向指定的服务器发送一个请求,并可以保存服务器返回的数据. urlopen函数: 在Python3的urllib库中,所有和网 ...

  2. 使用 MapTiler 进行地图切片

    在GIS开发中接触比较多的就是切图与发布,通常大家使用的是GlobalMapper.ArcGIS.GDAL等. 一般在使用Leaflet.js或其他框架开发时,使用的是TMS切片格式,大佬们基本用GD ...

  3. Util6 MIS2.0 (优六信息化管理系统框架)

    Util6 MIS一套基于ASP.NET MVC5 + Layui 开发的通用信息化管理系统快速开发框架 系统操作界面简洁, 项目结构清晰,功能模块化设计,支撑框架轻量高效,代码层级分离,注释完整,可 ...

  4. scrapy安装失败:error:Microsoft Visual C++ 14.0 is reuired.及同类型安装问题解决办法

    今天在安装scrapy的时候(pip install Scrapy),出现了如下错误: building 'twisted.test.raiser' extensionerror: Microsoft ...

  5. LeetCode算法题-Find Smallest Letter Greater Than Target(Java实现)

    这是悦乐书的第306次更新,第326篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第175题(顺位题号是744).给定一个仅包含小写字母的有序字符数组,并给定目标字母目标 ...

  6. 如何配置jenkins 与代理服务器吗?

    0       我们面临一些问题使用代理服务器(即缓存服务器)和詹金斯是希望有人可以提供如果他们有类似的设置.    Herea€™年代简要描述的设置: 在主站点反向代理,JTS & CCM服 ...

  7. nginx配置文件详细解读

    https://m.w3cschool.cn/nginx/nginx-d1aw28wa.html ######Nginx配置文件nginx.conf中文详解##### #定义Nginx运行的用户和用户 ...

  8. 使用Swagger辅助开发Fabric Application的Web API

    前面的几篇博客,我们已经把Fabric环境搭建好了,也可以使用Go开发ChainCode了,那么我们在ChainCode开发完毕后,可以通过CLI来测试ChainCode的正确性,ChainCode开 ...

  9. openstack 5大组件之间的关系和基本架构思维导图

    云计算是一种按使用量付费的模式,这种模式提供可用的/便捷的/按需的网络访问,进入可配置的计算资源共享池(资源包括网络/服务器/存储/应用软件和服务),这些资源能够被快速提供,只需投入很少的管理工作或与 ...

  10. MVC中使用viewmodel

    新建一个类在类中写 public class DataViewmodel { public List<users> usersmodel { get; set; } public List ...