主流 CSS 布局(水平居中、垂直居中、居中 )
什么是布局
- html 页面的整体结构或骨架
- 布局不是某个技术内容 而是一种设计思想
[ 布局方式 ]
- 水平居中布局
- 垂直居中布局
- 居中布局( 水平 + 垂直 )
什么是水平居中布局
水平居中布局 元素相对于页面/元素相对于父元素水平居中
[ 实现方式 ]
- inline-block + text-align 属性配合使用
注:[优点] 浏览器兼容性比较好 [缺点] text-align 属性具有继承性 导致子级元素的文本居中显示
**解决方法:在子级元素重新设置 text-align 属性覆盖掉父级元素的 text-align 属性 **
<style>
*{
margin: 0;
padding: 0;
}
.parent {
width: 100%;
height: 200px;
background-color: #00ffff;
/* 方法一: inline-block + text-align 属性配合使用 为父元素 添加 text-align 属性 为子元素添加 display 属性
- text-align 属性 为文本内容设置对其方式
+ left: 左对齐
+ center: 居中对齐
+ right: 右对齐
*/
text-align: center;
}
.child {
width: 300px;
height: 200px;
background-color: #ff0000;
/* display 属性:
- block: 块级元素
- inline: 内联元素 (text-align 有效)
+ width 和 height 属性无效
- inline-block: 行内块元素 (块级 + 内联 )
*/
display: inline-block;
}
</style>
<body>
<!-- 居中布局 -->
<!-- 方法一: inline-block + text-align 属性配合使用 -->
<div class="parent">
<div class="child"></div>
</div>
</body>
- table + margin 属性配合使用
注:[优点] 只需要对子级元素进行设置就可以实现水平居中 [缺点] 如果子级元素脱离文档流,导致 margin 属性失效
解决方法:考虑第一种或第三种解决方案
[ 拓展 ] CSS 中使元素脱离文档流的方式
- 将元素设置浮动 float
- 将元素设置为绝对定位 position: absolute
- 将元素设置为固定定位 position: fixed
<style>
*{
margin: 0;
padding: 0;
}
.parent {
width: 100%;
height: 200px;
background-color: #00ffff;
}
.child {
width: 300px;
height: 200px;
background-color: #ff0000;
/* 方法二: gtable + margin 属性配合使用 */
/* display的值 为 table 或 block */
display: table;
/* margin 属性: 外边距
- 一个值: 上下左右
- 两个值: 上下,左右
+ auto 根据浏览器自动分配
- 三个值: 上,左右,下
- 四个值: 上,右,下,左
*/
margin: 0 auto;
}
</style>
- absolute + transform 属性配合使用
注:[优点] 无论父级元素是否脱离文档流,不影响子级元素水平居中的效果 [缺点] transform 属性是 CSS 3 中新增的属性 浏览器支持情况不好
**解决方法:考虑第一种或第二种解决方案 **
<style>
* {
margin: 0;
padding: 0;
}
.parent {
width: 100%;
height: 200px;
background-color: #00ffff;
/* 相对定位 */
position: relative;
}
.child {
width: 300px;
height: 200px;
background-color: #ff0000;
/* 当把当前元素设置为绝对定位以后
- 如果父级元素没有设置定位,当前元素是相对于页面定位的
- 如果父级元素设置了定位,当前元素是相对于父级元素定位的
*/
position: absolute;
left: 50%;
/* 水平方向平移 */
transform: translateX(-50%);
/* margin-left: -50%; */
}
</style>
- ... ...
什么是垂直居中布局
垂直居中布局 :当前元素相对于页面/父元素垂直方向是居中显示的
[ 实现方式 ]
- table-cell + vertical-align 属性配合使用
注:[优点] 浏览器的兼容性比较好 [缺点] vertical-align 属性 具有继承性 导致子级元素的文本居中显示
**如果父级元素中包含除子级元素以外的文本内容,此方法不适用 **
<style>
* {
margin: 0;
padding: 0;
}
.parent {
/*方法一: table-cell + vertical-align 属性配合使用 */
width: 200px;
height: 600px;
background-color: #00ffff;
/* display 属性:
- table: 设置当前元素为<table>元素
- table-cell:设置当前元素为<td>元素 单元格
- 设置完成以后 作为子级元素的div就相当于单元格中的内容了,设置对齐方式即可
*/
display: table-cell;
/*
vertical-align 属性: 用于设置文本内容的垂直方向的定对齐方式
- top: 顶部对齐
- middle: 居中对齐
- bottom: 底部对齐
*/
vertical-align: middle;
}
.child {
width: 200px;
height: 300px;
background-color: #ff0000;
}
</style>
<body>
<div class="parent">
<div class="child"></div>
</div>
</body>
- absolute + transform 属性配合使用
注:[优点] 无论父级元素是否脱离文档流,不影响子级元素的垂直居中的效果 [缺点] transform 属性是 CSS 3 中新增的属性 浏览器支持情况不好
**解决方法:考虑第一种解决方案 **
<style>
* {
margin: 0;
padding: 0;
}
.parent {
width: 200px;
height: 600px;
background-color: #00ffff;
position:relative;
}
/* 方法二: absolute + transform 属性配合使用 */
.child {
width: 200px;
height: 300px;
background-color: #ff0000;
position: absolute;
top: 50%;
/* 垂直方向 */
transform: translateY(-50%);
}
</style>
什么是居中布局
居中布局:( 水平 + 垂直 )居中
[ 实现方式 ]
- display:block + margin 属性实现水平方向居中,table-cell + vertical-align 属性实现垂直方向居中
注:[优点] 浏览器兼容性比较好 [缺点] 父元素与子元素都需要增加代码
<style>
* {
margin: 0;
padding: 0;
}
.parent {
width: 1000px;
height: 600px;
background-color: #00ffff;
/* 实现垂直居中 */
/* <td> */
display: table-cell;
vertical-align: middle;
}
.child {
width: 200px;
height: 300px;
background-color: #ff0000;
/* 实现水居中 */
/* <table> */
/* display: table; */
display: block;
margin: 0 auto;
}
</style>
<body>
<div class="parent">
<div class="child"></div>
</div>
</body>
- absolute + transform 属性实现水平和垂直方向的居中
注:[优点] 无论父级元素是否脱离文档流,不影响子级元素的垂直居中的效果,不考虑浏览器兼容性,优于第一中方案 [缺点] transform 属性是 CSS 3 中新增的属性 浏览器支持情况不好同时子父元素都增加了代码
<style>
* {
margin: 0;
padding: 0;
}
.parent {
width: 1000px;
height: 600px;
background-color: #00ffff;
/* 相对定位 不脱离文档流*/
position:relative;
}
.child {
width: 200px;
height: 300px;
background-color: #ff0000;
/* 绝对定位 ———— 子绝父相 */
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
/* transform: translateX(-50%);
transform: translateY(-50%); */
}
</style>
主流 CSS 布局(水平居中、垂直居中、居中 )的更多相关文章
- CSS布局,div居中,文字居中
.main { width: 100%; margin: 0 auto; .banner { img { width: 100%; } } .article { margin-bottom: 100p ...
- CSS布局之--各种居中
居中是我们使用css来布局时常遇到的情况.使用css来进行居中时,有时一个属性就能搞定,有时则需要一定的技巧才能兼容到所有浏览器,本文就居中的一些常用方法做个简单的介绍. 注:本文所讲方法除了特别说明 ...
- CSS布局技巧 -- 各种居中
多行垂直居中 废话少说,直接上例子!!! display:table Html代码: <div class="wrapper"> <div class=" ...
- day07——css布局解决方案之居中布局
转行学开发,代码100天——2018-03-23 1.水平居中 使用inline-block + text-align方法 先将子框由块级元素改为行内块元素,再通过设置行内块元素居中以实现水平居中 ...
- css布局--水平居中
一.水平居中 1. 使用text-align和display:inline-block实现水平居中 html <div class="parent"> <div ...
- css布局--水平垂直居中
1. 使用text-align 和 vertical-align 和 inline-block实现水平垂直居中 html <div class="parent"> &l ...
- div+css 布局下兼容IE6 IE7 FF常见问题
div+css 布局下兼容IE6 IE7 FF常见问题 收藏 所有浏览器 通用 (市面上主要用到的IE6 IE7 FF)height: 100px; IE6 专用 _height: 100px; IE ...
- 典型的DIV+CSS布局——左中右版式
[效果] [HTML] <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Def ...
- HTML+CSS,让div在屏幕中居中(水平居中+垂直居中)方法总结
最近写网页经常需要将div在屏幕中居中显示,遂记录下几个常用的方法,都比较简单. 水平居中直接加上<center>标签即可,或者设置margin:auto;当然也可以用下面的方法 下面说两 ...
随机推荐
- python,json解析字符串时ValueError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
今天写测试工具的时候,去excel取数据,用json解析字符串为字典时报错,后经调试,发现是单引号的原因,将单引号换位双引号即可 def getExcelValue_to_dic(filepath): ...
- GStreamer基础教程09 - Appsrc及Appsink
摘要 在我们前面的文章中,我们的Pipline都是使用GStreamer自带的插件去产生/消费数据.在实际的情况中,我们的数据源可能没有相应的gstreamer插件,但我们又需要将数据发送到GStre ...
- Spring 梳理-处理Multipart 请求
原理讲解 简单的HTTP POST 大家通过HTTP向服务器发送POST请求提交数据,都是通过form表单提交的,代码如下: <form method="post"actio ...
- spring boot 配置访问其他模块包中的mapper和xml
maven项目结构如下,这里只是简单测试demo,使用的springboot版本为2.1.3.RELEASE 1.comm模块主要是一些mybatis的mapper接口和对应的xml文件,以及数据库表 ...
- LINUX系统学习以及初学者系统下载
Linux系统常用命令大全 来源:服务器之家 [博客中所有文章如有不对的地方希望看官们指出,有问题也可以提出来相互交流,相互学习,感谢大家!] 初学者建议安装:sentOS Ubuntu系统下载连接h ...
- gitbook 入门教程之小白都能看懂的 Gitbook 插件开发全流程
什么是插件 Gitbook 插件是扩展 GitBook 功能(电子书和网站)的最佳方式. 只要是 Gitbook 默认没有提供的功能,基于插件机制都可以自行扩展,是插件让 Gitbook 变得更加强大 ...
- 04-numpy读取本地数据和索引
1.numpy读取数据 CSV:Comma-Separated Value,逗号分隔值文件 显示:表格状态 源文件:换行和逗号分隔行列的格式化文本,每一行的数据表示一条记录 由于csv便于展示,读取和 ...
- F#周报2019年第41期
新闻 .NET架构指南 美妙的WebSharper:学术刊物 .NET Core 3.0中Blazor Server的方案与性能 Mono 6.4.0发布说明 CapitolFSharp召集发言人 视 ...
- C# 获取顶级(一级)域名方法
/// <summary> /// 获取域名的顶级域名 /// </summary> /// <param name="domain">< ...
- 主动降噪(Active Noise Control)
智能耳机 人机交互 智能声学终端 智能耳机 智能音箱 智能听力器 喇叭单体 动圈喇叭 新材料 DLC 石墨烯 陶瓷单位 吸音材料 智能芯片 阵列式麦克风 声纹传感器 演算法 降噪算法 智能听力保护 A ...