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/ ...
随机推荐
- python安装包工具pip的安装
官方的安装帮助页面:http://pip.readthedocs.org/en/latest/installing.html windows: 下载代码get-pip.py, 运行该代码, 这个代码会 ...
- DIV实现CSS 的placeholder效果
placeholder是HTML5中input的属性,但该属性并不支持除input以外的元素 但我们可以使用Css before选择器来实现完全相同的效果 <!DOCTYPE html> ...
- HashSet HashTable HashMap的区别 及其Java集合介绍
(1)HashSet是set的一个实现类,hashMap是Map的一个实现类,同时hashMap是hashTable的替代品(为什么后面会讲到). (2)HashSet以对象作为元素,而HashMap ...
- 如何导入大sql文件到虚拟主机mysql数据库
大部分网站虚拟主机为了安全起见,都限制了通过命令或者phpMyAdmin导入大sql文件到mysql数据库,例如godaddy只允许站长通过phpMyAdmin上传不超过2m的sql文件,但实际上我们 ...
- Android Studio开发入门-引用jar及so文件
作者:王先荣 最近初学安卓开发,因为以前从未用过JAVA,连基本的语法都要从头开始,所以不太顺利.在尝试使用百度语音识别引擎时遇到了如何引用jar及so文件的问题.在GOOGLE加多次尝试之后, ...
- c++中的&
变量的前面表示取变量地址赋值给指针, 如:int a = 0; int *pa = &a;类型后面表示引用,引用即变量的替身. int a = 0; int &ref = a;操作re ...
- 找回Win8.1(windows server 2012 R2)的双拼
一.微软拼音的选项,只能在metro界面修改: 鼠标移动到屏幕右下,出现metro的菜单,选"设置"然后选最下面的"更改电脑设置" 时间和设置->区域和语 ...
- bss段和data段的区别
一般情况下,一个程序本质上都是由 bss段.data段.text段三个组成的——本概念是当前的计算机程序设计中是很重要的一个基本概念.而且在嵌入式系统的设计中也非常重要,牵涉到嵌入式系统运行时的内存大 ...
- Android Weak Handler:可以避免内存泄漏的Handler库
这是一个针对技术开发者的一个应用,你可以在掘金上获取最新最优质的技术干货,不仅仅是Android知识.前端.后端以至于产品和设计都有涉猎,想成为全栈工程师的朋友不要错过! android使用java作 ...
- JIT
http://www.cppblog.com/vczh/category/9583.html