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/ ...
随机推荐
- redis一句话
- OpenCV仿射变换+投射变换+单应性矩阵
本来想用单应性求解小规模运动的物体的位移,但是后来发现即使是很微小的位移也会带来超级大的误差甚至错误求解,看起来这个方法各种行不通,还是要匹配知道深度了以后才能从三维仿射变换来入手了,纠结~ esti ...
- Enclosure POJ
0:Enclosure http://poj.openjudge.cn/challenge3/0/ 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 131072kB 描述 为了防止 ...
- OpenSSL命令---pkcs8
用途: pkcs8格式的私钥转换工具.它处理在PKCS#8格式中的私钥文件.它可以用多样的PKCS#5 (v1.5 and v2.0)和 PKCS#12算法来处理没有解密的PKCS#8 Private ...
- jquery.pjax.js bug问题解决集锦
jquery.pjax 是一个很好的局部刷新插件,但实际应用过程是还是会有很多小问题,部分问题解决如下: 1.pjax 局部加载时候,IE 存在缓存问题,很容易理解,pjax是通过jquery的aja ...
- Redrain个人维护并使用的DuiLib和UiLib库源代码下载地址
转载请说明原出处:http://blog.csdn.net/zhuhongshu/article/details/40740353,谢谢~~ 首先说明一下Duilib和Uilib的差别:UiLIb是D ...
- 【Cocos2d-Js基础教学 入门目录】
本教程视地址频在: 九秒课堂 完全免费 从接触Cocos2dx-Js以来,它的绽放的绚丽让我无法不对它喜欢.我觉得Js在不断带给我们惊喜:在开发过程中,会大大提升我们对原型开发的利用率,使用Js语言做 ...
- [Under the hood]---Matt Pietrek October 1996 MSJ
Matt Pietrek October 1996 MSJ Matt Pietrek is the author of Windows 95 System Programming Secrets (I ...
- 三种ViewController跳转的异同
- (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag comple ...
- sloth——算法工程师标注数据的福音
一般算法工程师做标注,都要先开发个标注工具,无非下面几个选项: 1.mfc,C#,优点是交互界面友好,开发难度适中,缺点是没法跨平台 2.matlab,优点是可以跨平台,开发难度非常低,缺点是速度慢. ...