CSS居中的多种方法
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居中的多种方法的更多相关文章
- CSS 布局实例系列(一)总结CSS居中的多种方法
使用 CSS 让页面元素居中可能是我们页面开发中最常见的拦路虎啦,接下来总结一下常见的几种居中方法吧. 1. 首先来聊聊水平居中: text-align 与 inline-block 的配合 就像这样 ...
- css未知宽高的盒子div居中的多种方法
不知道盒子大小.宽高时,如何让盒子上下左右居中? 应用场景:比如上传图片时,并不知道图片的大小,但要求图片显示在某盒子的正中央. 方法1:让4周的拉力均匀-常用 <!-- Author: Xia ...
- html+css实现图片或元素的垂直、水平同时居中的多种方法
实现元素或图片的上下.左右居中的三种方法 效果图如下: 方法一:利用vertical-align属性实现图片上下居中 先设置父元素样式text-align: center,实现图片左右居中,给图片添加 ...
- 理解CSS居中
我想很多在前端学习或者开发过程中,肯定会遇到如何让你的元素居中的问题,网上google肯定会有很多的解决方法.今天我就个人的项目与学习经验谈谈个人理解css如何让元素居中. 要理解css的居中,首先必 ...
- CSS导航菜单水平居中的多种方法
CSS导航菜单水平居中的多种方法 在网页设计中,水平导航菜单使用是十分广泛的,在CSS样式中,我们一般会用Float元素或是「display:inline-block」来解决.而今天主要讲解如何让未知 ...
- css实现居中的各种方法
css垂直居中有很多种方法,可以参考下这个网站
- CSS居中方法
css居中方法非常多,根据工作的实际情况采用恰当方法,可取到事半功倍的效果. 就常见的一些居中方法整理如下: 代码如下: <div class="con"> <d ...
- css隐藏页面元素的多种方法
在平常的样式排版中,我们经常遇到将某个模块隐藏,下面我整理了一下隐藏元素的多种方法以及对比(有的占据空间,有的不占据空间.有的可以点击,有的不能点击.): ( 一 ) display: none; ...
- css 分割线样式_css实现文章分割线的多种方法总结
这篇文章整理css如何实现文章分割线的多种方式,分割线在页面中可以起到美化作用,那么就来看看使用css实现分割线样式的多种方法.效果如下: 方式一:单个标签实现分隔线: html: <div c ...
随机推荐
- 前端自适应样式(reset.css)
@charset "utf-8"; /* CSS Document */ html, body, ul, li, ol, dl, dd, dt, p, h1, h2, h3, h4 ...
- 2020-07-28:已知sqrt (2)约等于 1.414,要求不用数学库,求sqrt (2)精确到小数点后 10 位。
福哥答案2020-07-28: 1.二分法.2.手算法.3.牛顿迭代法.基础是泰勒级数展开法.4.泰勒级数法.5.平方根倒数速算法,卡马克反转.基础是牛顿迭代法. golang代码如下: packag ...
- 小程序的优化代码的分析Promise方法
代码优化,这里通过了wx.request请求轮播图的API,通过result结果里面的data数据我们可以看到massage里面装着我们的数据 通过图片可以用看到swiperList返回的三个元素的数 ...
- linux下免密登录配置
1.首先大家先开三台虚拟机 2.回到首层. 2.1:编辑文件: vim /etc/ssh/sshd_config 3:在master的linux上生成ssh密钥: ssh-keygen -t r ...
- Ubuntu18.04安装Nautilus-actions自定义文件管理器鼠标右键列表
sudo add-apt-repository ppa:daniel-marynicz/filemanager-actions #需要添加源 sudo apt-get install filemana ...
- python基础 Day8
python Day8 文件操作的识 利用python代码写一个脚本操作文件的过程 文件的路径:path 打开方式:读,写,追加,读写,写读 编码方式:utf-8,gbk,gb2312 简单文件读取( ...
- C++ Templates (1.7 总结 Summary)
返回完整目录 目录 1.7 总结 Summary 1.7 总结 Summary 函数模板定义了一系列不同模板实参的函数 当传递实参给依赖于模板参数的函数参数,函数模板推断模板参数并实例化相应的参数类型 ...
- swagger2配置详解
1.写在controller上的注解 1.1 @Api 代码 @Api(tags = "用户相关接口", description = "提供用户相关的 Rest API& ...
- 修改vsftpd的默认根目录/var/ftp/pub到另一个目录
修改ftp的根目录只要修改/etc/vsftpd/vsftpd.conf文件即可: 加入如下几行: local_root=/var/www/html chroot_local_user=YES ano ...
- psutil 简单使用!
psutil.cpu_percent() cpu 百分比 mem = psutil.virtual_memory()mem.total,mem.used mem.free psutil.cpu_cou ...