css居中学习笔记
css居中学习笔记
一、水平居中
以下面的代码为例:
<body> <div class="parent"> <div class="child"> hello world ! </div> </div> </body>
方法一:inline-block + text-align
<style type="text/css">
body{
margin: 0px;
padding: 0px;
}
.parent{
background-color: antiquewhite;
text-align: center;
}
.child{
background-color: #000000;
color: white;
display: inline-block;
}
</style>
评价:优点:兼容性好;缺点:元素居中且元素里面的文字也居中
方法二:display:table
<style type="text/css">
body{
margin: 0px;
padding: 0px;
}
.parent{
background-color: antiquewhite;
}
.child{
background-color: #000000;
color: white;
display: table;
margin: 0px auto;
}
</style>
评价:优点:只需要调用child里面的;缺点:兼容性
方法三:通过定位的方法
<style type="text/css">
body{
margin: 0px;
padding: 0px;
}
.parent{
background-color: red;
position: relative;
}
.child{
background-color: #000000;
color: white;
position: absolute;
left: 50%;
transform: translateX(-50%);
}
</style>
评价:优点:不会对其他元素产生影响;缺点:兼容性
方法四:
<style type="text/css">
body{
margin: 0px;
padding: 0px;
}
.parent{
background-color: red;
display: flex;
justify-content: center;
}
.child{
background-color: #000000;
color: white;
}
</style>
评价:优点:只需要设置parent就可以了;缺点:兼容性
方法五:
<style type="text/css">
body{
margin: 0px;
padding: 0px;
}
.parent{
background-color: red;
display: flex;
}
.child{
background-color: #000000;
color: white;
margin: 0px auto;
}
</style>
评价:优点:简单易操作;缺点:兼容性
二、垂直居中问题
方法一:table-cel + vertical-align
<style type="text/css">
body{
margin: 0px;
padding: 0px;
}
.parent{
background-color: red;
height: 200px;
display: table-cell;
vertical-align: middle;
}
.child{
background-color: #000000;
color: white;
}
</style>
评价:优点:兼容性好
方法二:
<style type="text/css">
body{
margin: 0px;
padding: 0px;
}
.parent{
background-color: red;
height: 400px;
position: relative;
}
.child{
background-color: #000000;
color: white;
position:absolute;
top:50%;
transform: translateY(-50%);
}
</style>
方法三:
<style type="text/css">
body{
margin: 0px;
padding: 0px;
}
.parent{
background-color: red;
height: 400px;
display: flex;
align-items: center;
}
.child{
background-color: #000000;
color: white;
}
</style>
三、水平和垂直都居中
方法一:
<style type="text/css">
body{
margin: 0px;
padding: 0px;
}
.parent{
background-color: red;
width: 500px;
height: 500px;
text-align: center;
display: table-cell;
vertical-align: middle;
}
.child{
background-color: #000000;
color: white;
display: inline-block;
}
</style>
方法二:
<style type="text/css">
body{
margin: 0px;
padding: 0px;
}
.parent{
background-color: red;
width: 600px;
height: 600px;
position: relative;
}
.child{
background-color: #000000;
color: white;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%,-50%);
}
</style>
方法三:
<style type="text/css">
body{
margin: 0px;
padding: 0px;
}
.parent{
background-color: red;
width: 100%;
height: 800px;
display: flex;
justify-content: center;
align-items: center;
}
.child{
background-color: #000000;
color: white;
}
</style>
css居中学习笔记的更多相关文章
- CSS 3 学习笔记
css 3 学习笔记 文本: word-wrap : normal | break-word取值:normal: 控制连续文本换行.break-word: 内容将在边界内换行.如果需要,词 ...
- (2)《Head First HTML与CSS》学习笔记---img与基于标准的HTML5
1.浏览器处理图像的过程: 1.服务器获取文件,显示出文本结构,以及预留默认的大小给<img>(如果该<img>有width-1值和height-1值,则根据这个值提前设好页面 ...
- 【干货】Html与CSS入门学习笔记1-3
从23号开始用了4天时间看完了<Head First Html与CSS>这本书,本书讲解方式深入浅出,便于理解,结合习题,便于记忆,是一本不错的入门书.下面是本书的学习笔记: 一.认识HT ...
- (6)《Head First HTML与CSS》学习笔记---结尾、《HTML5权威指南》读书笔记
1.内联元素的外边距.内边距与块元素稍有不同. 如果一个内联元素四周都增加外边距,只能看到左边和右边会增加空间:你也可以对内联元素的上下增加内边距,不过这个内边距不会影响包围它的其他内联元素的间距—— ...
- HTML&CSS基础学习笔记1.16-单元格间距和表格主体
上一篇讲html学习笔记,讲过了合并单元格,那么今天就来介绍下如何控制单元格的间距,以及表格主体的相关知识. 单元格间距 在上个知识点的显示结果中你可能发现了,单元格与单元格之间有一小段空白.这是由& ...
- HTML&CSS基础学习笔记—创建列表
创建一张表格 很多时候我们需要在网页上展示一些数据,使用表格可以很好的来展示数据. 在HTML中<table>标签定义 表格. <table> </table> 添 ...
- HTML&CSS基础学习笔记1.32-选择器是什么
选择器是什么 选择器是CSS样式为了定位页面上的任意元素的一种方法. 选择器主要分为:元素标签选择器.通用选择器.类选择器.ID选择器.属性选择器.组合选择器.伪类选择器.伪元素选择器. 先做个了解, ...
- HTML&CSS基础学习笔记1.31-像素和相对长度
像素和相对长度 之前的笔记中,我们提到过用属性width.height来设置图片的尺寸,它们的单元都是”px(像素)”.长度单位总结一下,目前比较常用到px(像素).em.% 百分比,要注意其实这三种 ...
- CSS简易学习笔记
学习地址:http://www.w3school.com.cn/css/index.asp cnblog不能把格式复制上来,有格式文字版:https://github.com/songzhenhua/ ...
随机推荐
- nginx 重写 rewrite 基础及实例
nginx rewrite 正则表达式匹配 大小写匹配 ~ 为区分大小写匹配 ~* 为不区分大小写匹配 !~和!~*分别为区分大小写不匹配及不区分大小写不匹配 文件及目录匹配 -f和!-f用来判断是否 ...
- GBK、UTF8、UNICODE编码转换
string GBKToUTF8(const std::string& strGBK) { , strGBK.c_str(), -, NULL, ); WCHAR * wszUTF8 = ne ...
- 深入学习golang(3)—类型方法
类型方法 1. 给类型定义方法 在Go语言中,我们可以给任何类型(包括内置类型,但不包括指针和接口)定义方法.例如,在实际编程中,我们经常使用[ ]byte的切片,我们可以定义一个新的类型: type ...
- 记一次苦逼的SQL查询优化
最近在维护公司项目时,需要加载某页面,总共加载也就4000多条数据,竟然需要35秒钟,要是数据增长到40000条,我估计好几分钟都搞不定.卧槽,要我是用户的话估计受不了,趁闲着没事,就想把它优化一下, ...
- likwid工具尝鲜
likwid: like I knew what I am doing. 是一系列工具的集合,用于针对多线程程序的指标分析,方便程序员进行性能调优工作.可以深入到控制cpu等硬件的频率. 主要包括以下 ...
- gem install 出现Errno::ECONNRESET: Connection reset by peer - SSL_connect (https://api.rubygems.org
在安装了rvm来管理多版本的ruby之后,想在不同环境下安装一些gems,结果gem install puma 之后,发现一次又一次失败. gem install 出现Errno::ECONNRESE ...
- "无法启动应用程序,工作组信息文件丢失,或是已被其他用户已独占方式打开"解决办法
当程序用ado的jet4.0方式连接mdb数据库的时候,对于设有access数据库密码的mdb的访问居然报错“无法启动应用程序,工作组信息文件丢失,或是已被其他用户已独占方式打开”. ADOConne ...
- 那些一目了然的3D地质模型 【转】
http://www.360doc.com/content/16/0830/09/14719766_586950902.shtml
- 构造函数和:this()的应用
一.构造函数和:this()的应用 //本实例演示构造函数和:this()的应用 public class ClsA { public string A{set;get;} public string ...
- android 左右翻页
布局: <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android ...