常用的CSS居中方式
1.水平居中margin 0 auto;(浮动元素除外)
这个属性在网页制作的过程中是经常被用到的,一般情况下页面的版心你就可以看到它。
<style>
.father {
width: 400px;
height: 400px;
background-color: pink;
}
.son {
width: 200px;
height: 200px;
background-color: skyblue;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="father">
<div class="son"> </div>
</div>
</body>
效果图:

2.position定位实现垂直居中(一);
position具有兼容性好的特点,在网页布局中建议能使用定位,最好不要选择浮动来布局。下面这个方法局限性在于宽高。
<style>
.father {
position: relative;
width: 200px;
height: 200px;
margin: 0 auto; /* 父盒子居中 */
background-color: pink;
}
.son {
position: absolute;
width: 100px;
height: 100px;
top: 50%;
left: 50%;
margin-top: -50px;
margin-left: -50px;
background: skyblue;
}
</style>
</head>
<body>
<div class="father">
<div class="son"> </div>
</div>
</body>
3.position定位实现垂直居中(二);
这个方法不用受到宽高的限制,非常好用,我布局的时候经常用到它
<style>
.father {
position: relative;
width: 200px;
height: 200px;
margin: 0 auto; /* 父盒子居中 */
background-color: pink;
}
.son {
position: absolute;
width: 100px;
height: 100px;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
background: skyblue;
}
</style>
</head>
<body>
<div class="father">
<div class="son"> </div>
</div>
</body>
4.position定位实现垂直居中(三);
在这之前我还不会使用这个方法 ,我是看别人的博客学习到的,在这里感谢他了。
<style>
.father {
position: relative;
width: 200px;
height: 200px;
margin: 0 auto; /* 父盒子居中 */
background-color: pink;
}
.son {
position: absolute;
width: 100px;
height: 100px;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
background: skyblue;
}
</style>
</head>
<body>
<div class="father">
<div class="son"> </div>
</div>
</body>
效果图都是一样的,在这里我就上传一张了啦 。有的朋友可能会问:‘你把盒子里的内容换成图片,内容就不一样了鸭‘。开始我是打算把里面的内容换成图片的,原谅我偷懒了。

5.CSS3新特性居中:
<style>
.father {
display: flex;
justify-content: center;
align-items: center;
width: 200px;
height: 200px;
margin: 0 auto; /*父盒子居中 */
background-color: pink;
}
.son {
width: 100px;
height: 100px; background: skyblue;
}
</style>
</head>
<body>
<div class="father">
<div class="son"> </div>
</div>
</body>
小结:我这里分享的是一些比较常用的CSS居中方式,没有说到的可以到评论区留言 谢谢大家。
常用的CSS居中方式的更多相关文章
- 常用的Css命名方式
常用的Css命名方式: CSS命名规范: 1.文件命名规范 全局样式:global.css: 框架布局:layout.css: 字体样式:font.css: 链接样式:link.css: 打印样式:p ...
- 各种div+css居中方式调整(转载)
盘点8种CSS实现垂直居中水平居中的绝对定位居中技术 分类: 前端开发2013-09-11 21:06 24959人阅读 评论(3) 收藏 举报 绝对居中垂直居中水平居中CSS居中代码 目录(?) ...
- 15种css居中方式
1 水平居中 1.1 内联元素水平居中 利用 text-align: center 可以实现在块级元素内部的内联元素水平居中.此方法对内联元素(inline), 内联块(inline-block), ...
- css居中方式总结
方法一: line-height <div class="vertical" style="width:200px;height:200px;border:2px ...
- CSS居中的常用方式以及优缺点
前言 居中是页面开发中经常遇到的问题. 使用合适的.简单的.兼容性好的居中方式是我们页面仔在整个工作生涯中都要面对的问题. text-align:center 来看这个例子,一张图片和文字进行居中.如 ...
- 23----2013.07.01---Div和Span区别,Css常用属性,选择器,使用css的方式,脱离文档流,div+css布局,盒子模型,框架,js基本介绍
01 复习内容 复习之前的知识点 02演示VS创建元素 03div和span区别 通过display属性进行DIV与Span之间的转换.div->span 设置display:inline ...
- 【基础】这15种CSS居中的方式,你都用过哪几种?
简言 CSS居中是前端工程师经常要面对的问题,也是基本技能之一.今天有时间把CSS居中的方案汇编整理了一下,目前包括水平居中,垂直居中及水平垂直居中方案共15种.如有漏掉的,还会陆续的补充进来,算做是 ...
- css3种引入方式,样式与长度颜色,常用样式,css选择器
# CSS三种引入方式 ## 一.三种方式的书写规范 #### 1.行间式 ```html<div style="width: 100px; height: 100px; backgr ...
- 第212天:15种CSS居中的方式,最全了
CSS居中是前端工程师经常要面对的问题,也是基本技能之一.今天有时间把CSS居中的方案整理了一下,目前包括水平居中,垂直居中及水平垂直居中方案共15种.如有漏掉的,还会陆续的补充进来,算做是一个备忘录 ...
随机推荐
- python读写xlsx
1使用openpyxl库读写excel xlrd和xlwt处理的是xls文件,单个sheet最大行数是65535,如果有更大需要的,建议使用openpyxl函数,最大行数达到1048576. 如果数 ...
- 汇总各个部门当前员工的title类型的分配数目,结果给出部门编号dept_no、dept_name、其当前员工所有的title以及该类型title对应的数目count
CREATE TABLE `departments` (`dept_no` char(4) NOT NULL,`dept_name` varchar(40) NOT NULL,PRIMARY KEY ...
- <!--[if !IE]> 的用法
除IE外都可识别
- Solr 6.7学习笔记(04)-- suggester 遇到的问题
遇到的一些问题: 在前面的Suggest配置完后,我在 “/select” 这个 <requestHandler>里面加上了highlight的配置,可是当我在搜索框里输入字符时,竟然报如 ...
- AGC001 D - Arrays and Palindrome【构造】
把回文串的相等关系连一下,发现最后要求的是一笔画问题 注意到奇数长度的中间有一个单独没有连线的,所以a数组至多有两个奇数值 如果没有奇数,那么b在最前面放一个1,然后把a[1]~a[m-1]放上去,这 ...
- solidity 学习笔记(3) 函数修饰符/继承
修饰符: 函数修饰符有 constant view pure 其中 constant和view的功能是一样的 不会消耗gas 也就是说不会做任何存储 constant在5.0以后的版本中被废弃 ...
- Tyvj P1520 树的直径
P1520 树的直径 http://www.tyvj.cn/p/1520 时间: 1000ms / 空间: 131072KiB / Java类名: Main 描述 树的直径,即这棵树中距离最远的两个结 ...
- 原生js 的ajax封装
/** * 封装ajax函数(包括跨域) * @method ajax * @param option :{type:"post" or "get" 请求方式, ...
- 01分数规划初探?!By cellur925
都要\(NOIp\)了为啥我还在看这种玄学玩意..... \(01\)分数规划:这是一个问题模型\(qwq\),一般是在求\[\frac{\sum_{i=1}^{n} a_i*x_i}{\sum_{i ...
- 2、CreateJS介绍-TweenJS
需要在html5文件中引入的CreateJS库文件是easeljs-0.7.1.min.js和tweenjs-0.5.1.min.js HTML5文件如下: <!DOCTYPE html> ...