1.水平居中:text-align 与 inline-block 的配合

<div id = "div_center_align">
<div id = "div_center_test"></div>
</div>
#div_center_align {
text-align: center
}
#div_center_test {
border:1px solid #ccc;
background-color: #ff2c42;
display: inline-block;
height: 10em;
width: 10em;
}
HTML 中在想要居中的元素外面套了一个父元素,然后在 CSS 中将父元素的 text-align 属性设为 center,接下来将子元素的 display 属性设为 inline-block 就可以水平居中了。

2.水平居中:通过 margin 实现

<div id = "div_center_margin"></div>
#div_center_margin {
width: 10em;
height: 10em;
border: 1px solid #ccc;
background-color: #ff2c42;
margin: 0 auto;
}
通过 margin 实现连父元素都不用套了,直接 margin: 0 auto; 搞定,对,就是这么简单快捷,恐怕是居中最常用的方法了吧.

3.垂直居中

<div id="div_center_margin02">
<div id="div_center_test"></div>
</div>
#div_center_margin02 {
position: relative;
background-color: #ff2c42;
height: 20em;
width: 50em;
}
#div_center_test {
border:1px solid #ccc;
background-color: #4053ff;
height: 10em;
width: 10em;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
}
  • 子元素 div 绝对定位
  • 父元素需要被定位
  • 子元素 top、bottom、left、right 四个位置值均为 0
  • 子元素 margin: auto;

4.垂直居中

  .content {

            width: 300px;
height: 300px;
background: orange;
margin: 0 auto; /*水平居中*/
position: relative; /*脱离文档流*/
top: 50%; /*偏移*/
/*
除了可以使用margin-top把div往上偏移之外,CSS3的transform属性也可以实现这个功能,通过设置div的transform: translateY(-50%),意思是使得div向上平移(translate)自身高度的一半(50%)。
*/
transform: translateY(-50%);或margin-top:-150px;

       }

5.水平垂直居中:CSS3新属性FLEX

<div id="div_center_flex">
<div class="div_center_test"></div>
<div class="div_center_test"></div>
</div>
#div_center_flex {
display: flex;
display: -webkit-flex;
align-items: center; /*垂直居中*/
-webkit-align-items: /* center; */
justify-content: center; /*水平居中*/
-webkit-justify-content: center;
height: 20em;
width: 50em;
background-color: #ff2c42;
}
.div_center_test {
border:1px solid #ccc;
background-color: #4053FF;
height: 10em;
width: 10em;
}

使用 flex 容器布局实现水平垂直居中的关键点在于:

父元素 display 属性设为 flex

垂直布局的属性是 align-items,设为 center 时便垂直居中

水平布局的属性是 justify-content,设为 center 时水平居中

子元素弹性居中,增加子元素也不会有影响

6.通过一起使用 box-align 和 box-pack 属性,对 div 框的子元素进行居中:

 

.parent{
width: 300px;
height: 300px;
border: 1px solid black;

/* Firefox */
display:-moz-box;
-moz-box-pack:center;
-moz-box-align:center;


/* Safari, Chrome, and Opera */
display:-webkit-box;
-webkit-box-pack:center;
-webkit-box-align:center;


/* W3C */
display:box;
box-pack:center;
box-align:center;
}
.child
{
width:100px;
height:100px;
border:1px solid black;
}

7.通过一起使用 display:flex 和 margin: auto属性,对 div 框的子元素进行居中:

.parent{

  border: 1px solid #ccc;
  width: 200px;
  height: 200px;
  display: flex;
}
.child{
  border: 1px solid #888;
  width: 100px;
  height: 100px;
  margin: auto;
}

 

CSS居中的多种方法的更多相关文章

  1. CSS 布局实例系列(一)总结CSS居中的多种方法

    使用 CSS 让页面元素居中可能是我们页面开发中最常见的拦路虎啦,接下来总结一下常见的几种居中方法吧. 1. 首先来聊聊水平居中: text-align 与 inline-block 的配合 就像这样 ...

  2. css未知宽高的盒子div居中的多种方法

    不知道盒子大小.宽高时,如何让盒子上下左右居中? 应用场景:比如上传图片时,并不知道图片的大小,但要求图片显示在某盒子的正中央. 方法1:让4周的拉力均匀-常用 <!-- Author: Xia ...

  3. html+css实现图片或元素的垂直、水平同时居中的多种方法

    实现元素或图片的上下.左右居中的三种方法 效果图如下: 方法一:利用vertical-align属性实现图片上下居中 先设置父元素样式text-align: center,实现图片左右居中,给图片添加 ...

  4. 理解CSS居中

    我想很多在前端学习或者开发过程中,肯定会遇到如何让你的元素居中的问题,网上google肯定会有很多的解决方法.今天我就个人的项目与学习经验谈谈个人理解css如何让元素居中. 要理解css的居中,首先必 ...

  5. CSS导航菜单水平居中的多种方法

    CSS导航菜单水平居中的多种方法 在网页设计中,水平导航菜单使用是十分广泛的,在CSS样式中,我们一般会用Float元素或是「display:inline-block」来解决.而今天主要讲解如何让未知 ...

  6. css实现居中的各种方法

    css垂直居中有很多种方法,可以参考下这个网站

  7. CSS居中方法

    css居中方法非常多,根据工作的实际情况采用恰当方法,可取到事半功倍的效果. 就常见的一些居中方法整理如下: 代码如下: <div class="con"> <d ...

  8. css隐藏页面元素的多种方法

    在平常的样式排版中,我们经常遇到将某个模块隐藏,下面我整理了一下隐藏元素的多种方法以及对比(有的占据空间,有的不占据空间.有的可以点击,有的不能点击.): ( 一 )  display:  none; ...

  9. css 分割线样式_css实现文章分割线的多种方法总结

    这篇文章整理css如何实现文章分割线的多种方式,分割线在页面中可以起到美化作用,那么就来看看使用css实现分割线样式的多种方法.效果如下: 方式一:单个标签实现分隔线: html: <div c ...

随机推荐

  1. Win10系统Jmeter+maven+Jenkins接口自动化环境搭建(一)

    Jmeter+maven+Jenkins实现接口自动化,需要使用idea或eclipse配置maven项目,这里我使用的是idea.具体步骤如下: 1.安装jmeter+jdk jmeter安装之前需 ...

  2. day7 地址 名片管理系统

    1 无限循环  (while  True)   break 退出     人为设计的 ,并且有退除的出口      死循环 bug  错误 2.引用   数字型

  3. 【Codeforces】CF Round #592 (Div. 2) - 题解

    Problem - A Tomorrow is a difficult day for Polycarp: he has to attend \(a\) lectures and \(b\) prac ...

  4. 数据 恢复----判断Raid盘序及校验方向

    重组Raid(如何判断校验方向及盘序) 1. 常规左异结构[backward parity(反向奇偶校验--(静态))] 校验块:校验块从最后一块物理盘开始写起,然后依次往前面的盘中写入,当写到第一块 ...

  5. IDEA_Shelve代码搁置与恢复

    日常开发中,经常会遇到在当前分支开发到一半,但是需要Checkout上个版本解决bug或调查问题的情况.这个时候,我们是将代码提到Push远程?还是直接Rollback? 最理想的做法,就是将当前的开 ...

  6. dcoker 小应用(二)

    sudo yum install epel-release   vi /etc/yum.repos.d/epel.repo     use base url instead of mirror url ...

  7. Advances and Open Problems in Federated Learning

    挖个大坑,等有空了再回来填.心心念念的大综述呀(吐血三升)! 郑重声明:原文参见标题,如有侵权,请联系作者,将会撤销发布! 项目地址:https://github.com/open-intellige ...

  8. VMD的相关命令(转载)

    转载自:http://blog.sina.com.cn/s/blog_b48a7ac30102w6xg.html 自我学习总结: 1.打开VMD main上Extensions中的TkConsole这 ...

  9. Spark-shell 报错:Failed to get database default, returning NoSuchObjectException

    Spark-shell 执行sql查询报错: 20/08/24 15:33:59 WARN metastore.ObjectStore: Failed to get database default, ...

  10. javascript正则用法

    一.元字符 .      匹配除了换行符以外的字符. \w   匹配字母或者数字或者下划线 \W  匹配不是字母.数字.下划线 \d   匹配数字,相当于[0-9] \D  匹配不是数字的字符 \s  ...