css常用居中方式
一、水平居中
1.内联元素
父级元素加 text-align: center 即可
html
<div class="container">
<a>内联元素</a>
</div>
css
.container {
text-align: center;
}
2.块级元素
给定宽度,然后 margin 上下为 0,左右 auto 即可
html
<div class="container">
块级元素
</div>
css
.container {
width: 200px;
margin: 0 auto;
}
3.多个块级元素
第一种方式
子元素设置成内联,父级元素加 text-align:center即可
html
<div class="container">
<div>
第一个块
</div>
<div>
第二个块
</div>
<div>
第三个块
</div>
</div>
css
.container {
text-align: center;
}
.container div {
display: inline-block;
}
第二种方式
利用 flexbox 弹性布局
html
<div class="container">
<div>
第一个块
</div>
<div>
第二个块
</div>
<div>
第三个块
</div>
</div>
css
.container {
display: flex;
justify-content: center;
}
二、垂直居中
1.内联元素
第一种方式
设置 padding
html
<div class="container">
需要垂直居中的内容(内联)
</div>
css
.container {
padding: 40px 40px;
}
第二种方式
按照父级元素的高度,设置子元素的行高
html
<div class="container">
需要垂直居中的内容(内联)
</div>
css
.container {
height: 100px;
line-height: 100px;
}
第三种方式
利用 flexbox,父级元素需给定高度
html
<div class="container">
<a href="">爷要垂直居中</a>
<a href="">爷要垂直居中</a>
<a href="">爷要垂直居中</a>
</div>
css
.container {
width: 200px;
height: 200px;
display: flex;
flex-direction: column;
justify-content: center;
}
2.块级元素
第一种方式
父元素相对定位 position:relative,子元素绝对定位 position: absolute
html
<div class="container">
<div>爷要垂直居中</div>
</div>
css
.container {
position: relative;
width: 200px;
height: 200px;
}
.container div {
position: absolute;
top: 50%;
transform: translateY(-50%);
}
第二种方式
利用 flexbox
html
<div class="container">
<div>爷要垂直居中</div>
</div>
css
.container {
width: 200px;
height: 200px;
display: flex;
flex-direction: column;
justify-content: center;
}
三、水平和垂直居中
第一种方式
父元素相对定位 position:relative,子元素绝对定位 position: absolute
html
<div class="container">
<div>爷要水平和垂直居中</div>
</div>
css
.container {
position: relative;
width: 200px;
height: 200px;
}
.container div {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: red;
}
第二种方式
使用 flexbox
html
<div class="container">
<div>我要垂直居中啊a我要垂直居中啊a我要垂直居中啊</div>
</div>
css
.container {
width: 200px;
height: 200px;
display: flex;
align-items: center;
justify-content: center;
}
.container div {
width: 100px;
height: 100px;
}

最后说一点,如果具体宽高已知,给定具体数值就可以直接实现
The_End
css常用居中方式的更多相关文章
- css常用居中
对一个已知大小的元素上下左右居中(已知大小了,直接margin也就行了): css如下:.parent{height:100px;width:100px;background:grey;positio ...
- CSS常用布局方式-两列布局、三列布局
CSS基础 2.几种布局方式1)table布局 当年主流的布局方式,第一种是通过table tr td布局 示例: <style type="text/css"> ta ...
- 常用的CSS居中方式
1.水平居中margin 0 auto;(浮动元素除外) 这个属性在网页制作的过程中是经常被用到的,一般情况下页面的版心你就可以看到它. <style> .father { width: ...
- 各种div+css居中方式调整(转载)
盘点8种CSS实现垂直居中水平居中的绝对定位居中技术 分类: 前端开发2013-09-11 21:06 24959人阅读 评论(3) 收藏 举报 绝对居中垂直居中水平居中CSS居中代码 目录(?) ...
- 前端(二)—— CSS的引入方式、长度与颜色单位、常用样式、选择器
CSS的引入方式.长度与颜色单位.常用样式.选择器 一.CSS的三种引入方式 1.行间式 <!doctype html> <html> <head> <met ...
- CSS实现居中的方式
在介绍居中方式之前,简单介绍一下行内元素和块级元素. 行内元素 和其他元素都在同一行 高,行高及外边距和内边距部分可以改变 宽度只与内容有关 行内元素只能容纳文本或者其他行内元素 常用内联元素:a,i ...
- css中两种居中方式text-align:center和margin:0 auto 的使用场景
关于使用text-align:center和margin:0 auto 两种居中方式的比较 前言:最近由于要学习后端,需要提前学习一部分前端知识,补了补css知识,发现狂神在讲这一部分讲的不是特别清楚 ...
- CSS常用标签
CSS常用标签 一 CSS文字属性 color : #999999; /*文字颜色*/ font-family : 宋体,sans-serif; /*文字字体*/ font-size : 9pt; / ...
- python 全栈开发,Day46(列表标签,表格标签,表单标签,css的引入方式,css选择器)
一.列表标签 列表标签分为三种. 1.无序列表<ul>,无序列表中的每一项是<li> 英文单词解释如下: ul:unordered list,“无序列表”的意思. li:lis ...
随机推荐
- 记一次由sql注入到拿下域控的渗透测试实战演练(下)
本次渗透使用工具: mimikatz,PsExec 本次渗透网络拓扑图: 开始渗透: 上一篇文章中我们讲到我们成功渗透了目标内网中web服务器并提取其管理员明文密码,通过明文密码又成功登陆了内网中的另 ...
- zjnu1726 STOGOVI (lca)
Description Mirko is playing with stacks. In the beginning of the game, he has an empty stack denote ...
- hdu 1517 Multiplication Game
题意: 用整数p乘以2到9中的一个数字.斯坦总是从p = 1开始,做乘法,然后奥利乘以这个数,然后斯坦,以此类推.游戏开始前,他们画一个整数1 < n < 4294967295,谁先到达p ...
- hdu4770 Lights Against Dudely
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission ...
- Base64 编码原理
什么是 Base64 编码 Base64 编码是最常见的编码方式,基于 64 个可打印字符来表示任意二进制数据的方法,是从二进制转换到可见字符的过程. 使用场景 数据加密或签名通过 Base64 转换 ...
- Java15变量竟然没什么区别,八大基本数据类型你知道吗?
变量是什么? 变量是用来为不同数据类型在内存中分配的空间用来储存该数据. 不同于python这样的弱类型语言,变量声明不需要定义数据类型,就和写数学方程式一般,谁等于谁即可.而Java这个发展了多个版 ...
- codeforces 868C
C. Qualification Rounds time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- cursor CSS属性定义鼠标指针悬浮在元素上时的外观。
1 1 cursor CSS属性定义鼠标指针悬浮在元素上时的外观. https://developer.mozilla.org/zh-CN/docs/Web/CSS/cursor 概述 cursor ...
- 论文 公式 排版 编辑 工具 NoteExpress & MathType
1 1 论文 公式 排版 编辑 工具: NoteExpress http://www.noteexpress.net/index2.html 1 MathType http://www.dessci. ...
- Apple 产品反人类的设计 All In One
Apple 产品反人类的设计 All In One 用户体验 shit rank WTF rank iPhone 更换铃声 WTF, 这么简单的一个功能搞得太复杂了 使用要下载 1.6 G的库乐队 A ...