css--元素居中常用方法总结
前言
元素居中是日常开发和学习中最常见的问题,同时也是面试中经常考察的知识点,本文来总结一下这方面的知识点。
正文
1、水平居中
(1)子父元素宽度固定,子元素设置 margin:auto,并且子元素不能设置浮动,否则居中失效。
<div class="wrap">
<div class="inner"></div>
</div>
<style>
.wrap {
width: 500px;
background-color: skyblue;
}
.inner {
width: 200px;
height: 100px;
background-color: sandybrown;
margin: auto;
}
</style>

(2)子父元素宽度固定,父元素设置 text-align:center,子元素设置display:inline-block,并且子元素不能设置浮动,否则居中失效。
<div class="wrap">
<div class="inner"></div>
</div>
<style>
.wrap {
width: 500px;
background-color: skyblue;
text-align: center;
} .inner {
width: 200px;
height: 100px;
display: inline-block;
background-color: sandybrown;
}
</style>

2、水平垂直居中
(1)子父元素宽高度固定,子元素相对于父元素绝对定位(子绝父相),子元素 top,left 设置 50%,子元素 margin-top 和 margin-left 减去各自宽高的一半或者 transform :translate(-50%,-50%)。
<div class="wrap">
<div class="inner"></div>
</div>
<style>
.wrap {
width: 500px;
height: 200px;
background-color: skyblue;
position: relative;
} .inner {
width: 200px;
height: 100px;
background-color: sandybrown;
position: absolute;
top: 50%;
left: 50%;
/* transform: translate(-50%, -50%); */
margin-top: -50px;
margin-left: -100px;
}
</style>

(2)子父元素宽高度固定,子元素相对于父元素绝对定位(子绝父相),使用calc达到上面效果。
<div class="wrap">
<div class="inner"></div>
</div>
<style>
.wrap {
width: 500px;
height: 200px;
background-color: skyblue;
position: relative;
}
.inner {
width: 200px;
height: 100px;
background-color: sandybrown;
position: absolute;
top: calc(50% - 50px);
left: calc(50% - 100px); }
</style>

(3)子父元素宽高度固定,子元素相对于父元素绝对定位(子绝父相),子元素上下左右全为 0,然后设置子元素margin:auto。
<div class="wrap">
<div class="inner"></div>
</div>
<style>
.wrap {
width: 500px;
height: 200px;
background-color: skyblue;
position: relative;
}
.inner {
width: 200px;
height: 100px;
background-color: sandybrown;
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
margin: auto;
}
</style>

(4)子元素相对定位,子元素 top,left 值为 50%,transform:translate(-50%,-50%)。
<div class="wrap">
<div class="inner"></div>
</div>
<style>
.wrap {
width: 500px;
height: 200px;
background-color: skyblue;
}
.inner {
width: 200px;
height: 100px;
background-color: sandybrown;
position: relative;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
</style>
(5)文本水平垂直居中 父元素设置text-algin:center使得子元素水平居中,子元素设置line-height为父元素高度,使得子元素垂直居中。
<div class="wrap">
<span class="inner">321311111111111111</span>
</div>
<style>
.wrap {
width: 500px;
height: 200px;
text-align: center;
background-color: skyblue;
} .inner {
line-height: 200px;
background-color: sandybrown;
}
</style>

(6)利用line-height,vertical-align实现元素水平垂直居中。
<div class="wrap">
<div class="inner">321</div>
</div>
<style>
.wrap {
width: 500px;
height: 200px;
background-color: skyblue;
line-height: 200px;
text-align: center;
}
.inner {
width: 200px;
height: 100px;
background-color: sandybrown;
display: inline-block;/* 将子元素设置为行内块级元素 */
vertical-align: middle;/* 垂直居中 */
text-align: left;/* 默认继承了父元素的水平居中,这里需要修正修正文字 */
}
</style>

(7)父元素设置弹性盒子,display:flex; justfy-content:center ;align-item:center。
<div class="wrap">
<div class="inner"></div>
</div>
<style>
.wrap {
width: 500px;
height: 200px;
background-color: skyblue;
display: flex;
justify-content: center;
align-items: center;
} .inner {
width: 200px;
height: 100px;
background-color: sandybrown;
}
</style>

(8)父元素设置 display:table-cell vertical-align:middle,子元素设置 margin:auto。
<div class="wrap">
<div class="inner"></div>
</div>
<style>
.wrap {
width: 500px;
height: 200px;
background-color: skyblue;
display: table-cell;
vertical-align: middle
}
.inner {
width: 200px;
height: 100px;
background-color: sandybrown;
margin: auto;
}
</style>

(9)网格布局实现水平垂直居中display: grid;place-items: center。
<div class="wrap">
<div class="inner"></div>
</div>
<style>
.wrap {
width: 500px;
height: 200px;
background-color: skyblue;
display: grid;
place-items: center;
} .inner {
width: 200px;
height: 100px;
background-color: sandybrown;
}
</style>

写在最后
以上就是本文的全部内容,希望给读者带来些许的帮助和进步,方便的话点个关注,小白的成长之路会持续更新一些工作中常见的问题和技术点。

css--元素居中常用方法总结的更多相关文章
- CSS元素居中的常用方法
只有普通流和绝对定位的元素可以设置居中,浮动元素是不存在居中的. 1.块级元素居中 1) 普通流元素(position : static 或 position : relative) 水平居中:{ m ...
- Css元素居中设置
你对DIV CSS居中的方法是否了解,这里和大家分享一下,用CSS让元素居中显示并不是件很简单的事情,让我们先来看一下CSS中常见的几种让元素水平居中显示的方法. DIV CSS居中 用CSS让元素居 ...
- css 元素居中
css 4种常见实现元素居中的办法: 1.通过 margin 属性调整 : { position: absolute; top: 50%; left: 50%; margin-left: 盒子的一半: ...
- CSS元素居中汇总
总结实现不同类型元素居中的几种方法: 一.把margin值设置为auto(实现水平居中) 可以实现元素水平居中对齐 原理:使 margin-left=margin-right 如果设置 marg ...
- css元素居中
水平居中 若为行内元素,对其父元素用text-align:center即可: 若为块元素(无浮动),则一般有两种方法可实现对其的水平居中,一为margin:0 auto;二为通过css计算函数calc ...
- css 元素居中方法
目前知道有两种方法 方法一:主要适用于元素未设定高度的情况下. 直接上代码 html: <div class="nav-content"> <ul ng-clic ...
- css 元素居中各种办法
一:通过弹性布局<style> #container .box{ width: 80px; height: 80px; position: absolute; background:red ...
- css元素居中方法
几种居中方式,分情况使用: 1.已知父盒子宽度,子盒子宽度: div{ transform: translate(-50%,-50%); //margin-left: - 自身宽度一半: positi ...
- (前端)面试300问之(2)CSS元素居中【水平、垂直、2者同时居中】
一 仅水平居中 1 行内元素 1)给父元素添加 text-align:center 即可 <div class="parent"> <span class=&qu ...
- css元素居中的几种方式
1.水平居中 <div style="width:200px;margin:0 auto;background-color: yellow;">水平居中</div ...
随机推荐
- eval(input())
看到一段代码,判读输入的数字,用的是eval(input()),查了一下,原来input()会把所有输入值,包括数字,视为字符串,而eval()会去掉字符串最外层的引号,然后当做Python语句执行[ ...
- VUE -input输入框字母转大写
示例: 输入自动转--->大写 <input type="text" placeholder="请输入证件号码" maxlength="1 ...
- 利用OpenCV存储一段视频中的每一帧
// vfc.cpp : 定义控制台应用程序的入口点.#include "stdafx.h"#include <opencv2/highgui/highgui.hpp> ...
- 工作日常-SQL不能乱写
前言:刚接手别人的项目没多久,在昨天的一次上线中无故躺坑,且该大兄弟已经离职,不得不帮他填坑,整完后,今天想搞一个总结,结论就是:SQL不能乱写. 搜索关键词:Cause: java.sql.SQLE ...
- Python中的sys.stdin和input、sys.stdout与print--附带讲解剑指offer42-连续子数组的最大和
2020秋招季,终于开始刷第一套真题了,整套试卷就一道编程题,还是剑指offer上的原题,结果答案死活不对,最后干脆直接提交答案算了,看了下别人的答案,原来是输入数据没有获取的原因,不过这个语法sys ...
- ByteCTF2021 double sqli
double sqli easy sqli http://39.105.175.150:30001/?id=1 http://39.105.116.246:30001/?id=1 http://39. ...
- javascript-原生-函数
本节呢讲解js的函数部分,js函数部分总共分为两大类:1.自定义函数.2.系统函数 说白了,系统函数就是js自己内置的函数,其他的都属于自定义函数. 1.自定义函数 函数是完成指定功能的程序段,可以反 ...
- 痞子衡嵌入式:超级下载算法RT-UFL v1.0在IAR EW for Arm下的使用
痞子衡主导的"学术"项目 <RT-UFL - 一个适用全平台i.MXRT的超级下载算法设计> v1.0 版发布近 4 个月了,部分客户已经在实际项目开发调试中用上了这个 ...
- 【数据结构与算法Python版学习笔记】算法分析
什么是算法分析 算法是问题解决的通用的分步的指令的聚合 算法分析主要就是从计算资源的消耗的角度来评判和比较算法. 计算资源指标 存储空间或内存 执行时间 影响算法运行时间的其他因素 分为最好.最差和平 ...
- Noip模拟52 2021.9.13
T1 异或 比较稳的切掉 观察数据范围,无法线性筛啥的,根号复杂度也会死,于是只能考虑$log$级 然后打表 发现当$n$为$2^i$时的答案是一个可递归数列: $1,3,7,15,31,63,127 ...
