CSS基础学习 21.CSS居中总结
注意:*在IE中并不代表通配符的意思,是代表根元素的意思,所以为了匹配适应各种浏览器,进行页面初始化
- <style>
- *{
- margin:0;
- padding:0;
- }
- </style>
用以下形式代替
- <style>
- html,body{
- margin:0;
- padding:0;
- }
- </style>
1.盒子居中 margin:0 auto;
- <!doctype html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Document</title>
- <style type="text/css">
- html,body{
- margin: 0;
- padding: 0;
- }
- .container{
- width: 100%;
- min-width: 996px;/*设置最小宽度,缩小到最小宽度996px的时候就不再压缩内容了*/
- height: 70px;
- background-color: aquamarine;
- }
- .header{
- width: 80%;
- height: 70px;
- background-color: blueviolet;
- min-width: 996px;
- }
- </style>
- </head>
- <body>
- <div class="container">
- <div class="header"></div>
- </div>
- </body>
- </html>
设置margin:0 auto;让盒子居中
- <!doctype html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Document</title>
- <style type="text/css">
- html,body,ul{
- margin: 0;
- padding: 0;
- }
- .container{
- width: 100%;
- min-width: 996px;/*设置最小宽度,缩小到最小宽度996px的时候就不再压缩内容了*/
- height: 70px;
- background-color: aquamarine;
- }
- .header{
- width: 80%;
- height: 70px;
- background-color: blueviolet;
- min-width: 996px;
- margin:0 auto; /*上下为0,左右为自适应*/
- }
- </style>
- </head>
- <body>
- <div class="container">
- <div class="header">
- </div>
- </div>
- </body>
- </html>
2.文字居中 line-height;text-align:center;
- <!doctype html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Document</title>
- <style type="text/css">
- html,body,ul{
- margin: 0;
- padding: 0;
- }
- .container{
- width: 100%;
- min-width: 996px;/*设置最小宽度,缩小到最小宽度996px的时候就不再压缩内容了*/
- height: 70px;
- background-color: aquamarine;
- }
- .header{
- width: 80%;
- height: 70px;
- background-color: blueviolet;
- min-width: 996px;
- margin:0 auto; /*上下为0,左右为自适应*/
- }
- ul li{
- display: inline-block;/*内联块级元素和其他元素都在一行上*/
- list-style-type: none;
- }
- </style>
- </head>
- <body>
- <div class="container">
- <div class="header">
- <ul>
- <li>列表项目</li>
- <li>列表项目</li>
- <li>列表项目</li>
- <li>列表项目</li>
- </ul>
- </div>
- </div>
- </body>
- </html>
line-height;text-align:center;设置文字居中
- <!doctype html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Document</title>
- <style type="text/css">
- html,body,ul{
- margin: 0;
- padding: 0;
- }
- .container{
- width: 100%;
- min-width: 996px;/*设置最小宽度,缩小到最小宽度996px的时候就不再压缩内容了*/
- height: 70px;
- background-color: aquamarine;
- }
- .header{
- width: 80%;
- height: 70px;
- background-color: blueviolet;
- min-width: 996px;
- margin:0 auto; /*上下为0,左右为自适应*/
- text-align: center;/*水平居中*/
- }
- ul{
- line-height: 70px;/*垂直居中*/
- }
- ul li{
- /*float: left;*//*会脱离文档流,这时不能用text-align: center;设置水平居中*/
- display: inline-block;/*内联块级元素和其他元素都在一行上*/
- list-style-type: none;
- }
- </style>
- </head>
- <body>
- <div class="container">
- <div class="header">
- <ul>
- <li>列表项目</li>
- <li>列表项目</li>
- <li>列表项目</li>
- <li>列表项目</li>
- </ul>
- </div>
- </div>
- </body>
- </html>
3.由table演变来的一种居中
- <!doctype html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Document</title>
- <style type="text/css">
- .t{
- width: 200px;
- height: 200px;
- background-color: aquamarine;
- }
- </style>
- </head>
- <body>
- <div class="t">
- <p>哈哈</p>
- </div>
- </body>
- </html>
用table设置水平垂直居中
- <!doctype html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Document</title>
- <style type="text/css">
- .t{
- display: table;/*外层div变为table*/
- width: 200px;
- height: 200px;
- background-color: aquamarine;
- }
- p{
- display: table-cell;/*设置为单元格*/
- text-align: center;/*水平居中*/
- vertical-align: middle;/*垂直居中*/
- }
- </style>
- </head>
- <body>
- <div class="t">
- <p>哈哈</p>
- </div>
- </body>
- </html>
4.利用伸缩盒居中
- <!doctype html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Document</title>
- <style type="text/css">
- .outer{
- width: 200px;
- height: 200px;
- background-color: aquamarine;
- }
- .inner{
- width: 100px;
- height: 100px;
- background-color: antiquewhite;
- }
- </style>
- </head>
- <body>
- <div class="outer">
- <div class="inner">
- 哈哈
- </div>
- </div>
- </body>
- </html>
- <!doctype html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Document</title>
- <style type="text/css">
- .outer{
- display: -webkit-box;/*设置为盒子*/
- width: 200px;
- height: 200px;
- background-color: aquamarine;
- -webkit-box-pack:center;/*水平居中*/
- -webkit-box-align:center;/*垂直居中*/
- }
- .inner{
- width: 100px;
- height: 100px;
- background-color: antiquewhite;
- }
- </style>
- </head>
- <body>
- <div class="outer">
- <div class="inner">
- 哈哈
- </div>
- </div>
- </body>
- </html>
接下来设置文字居中
- <!doctype html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Document</title>
- <style type="text/css">
- .outer{
- display: -webkit-box;/*设置为盒子*/
- width: 200px;
- height: 200px;
- background-color: aquamarine;
- -webkit-box-pack:center;/*水平居中*/
- -webkit-box-align:center;/*垂直居中*/
- }
- .inner{
- display: -webkit-box;/*设置为盒子*/
- -webkit-box-pack:center;/*水平居中*/
- -webkit-box-align:center;/*垂直居中*/
- width: 100px;
- height: 100px;
- background-color: antiquewhite;
- }
- </style>
- </head>
- <body>
- <div class="outer">
- <div class="inner">
- 哈哈
- </div>
- </div>
- </body>
- </html>
CSS基础学习 21.CSS居中总结的更多相关文章
- CSS基础学习 20.CSS媒体查询
- CSS基础学习 19.CSS hack
- CSS基础学习 18.CSS多列
四种常见的浏览器内核:
- CSS基础学习 17.CSS动画
- CSS基础学习 16.CSS过渡
- CSS基础学习-15-1.CSS 浏览器内核
- CSS基础学习-14 CSS visibility与overflow属性
- CSS基础学习-13.CSS 浮动
如果前一个元素设置浮动属性,则之后的元素也会继承float属性,我觉得这里说是继承不太对,可以理解为会影响到之后的元素,所以在设置浮动元素之后的元素要想不被影响就需要清除浮动.元素设置左浮动,则清除左 ...
- CSS基础学习-12.CSS position
绝对定位 position:absolute,元素脱离文档流,然后使用left.right.top.bottom属性相对于其最接近的一个具有定位属性的祖先元素进行绝对定位.如果不存在这样的祖先元素,则 ...
随机推荐
- 【并行计算与CUDA开发】基于NVIDIA显卡的硬编解码的一点心得 (完结)
原文:基于NVIDIA显卡的硬编解码的一点心得 (完结) 1.硬解码软编码方法:大体流程,先用ffmpeg来读取视频文件的包,接着开启两个线程,一个用于硬解码,一个用于软编码,然后将读取的包传给解码器 ...
- 「JOISC 2019 Day3」穿越时空 Bitaro
「JOISC 2019 Day3」穿越时空 Bitaro 题解: 不会处理时间流逝,我去看了一眼题解的图,最重要的转换就是把(X,Y)改成(X,Y-X)这样就不会斜着走了. 问题变成二维平面上 ...
- SpringBoot异步编程
异步调用:当我们执行一个方法时,假如这个方法中有多个耗时的任务需要同时去做,而且又不着急等待这个结果时可以让客户端立即返回然后,后台慢慢去计算任务.当然你也可以选择等这些任务都执行完了,再返回给客户端 ...
- elasticsearch 的post put 方式的对比 setting mapping设置 - 添加查询数据
1.POST和PUT都可以用于创建 2.PUT是幂等方法,POST不是.所以post用户更新,put用于新增比较合适. 参考:https://yq.aliyun.com/articles/366099 ...
- [CF429E]Points ans Segments_欧拉回路
Points and Segments 题目链接:www.codeforces.com/contest/429/problem/E 注释:略. 题解: 先离散化. 发现每个位置如果被偶数条线段覆盖的话 ...
- HDU 4417-Super Mario-线段树+离线
Description Mario is world-famous plumber. His "burly" figure and amazing jumping ability ...
- VNote: 一个舒适的Markdown笔记软件
Update: 支持macOSYunpan Update 2: 写在VNote半周岁 QQ群(487756074) Markdown标记语言一直是许多程序员的最爱.目前,有许多优秀的Markdown编 ...
- C#向远程地址发送数据
static string proxyIpAddress = AppConfig.GetProxyIpAddress; static string proxyUserName = AppConfig. ...
- 怎样限制第三方Cookie
使用Cookie的 SameSite 属性. 1. SameSite=Strict; 这个模式下, 服务器将会完全禁止第三方Cookie, 在跨站点时, 任何情况下都不会发送Cookie, 也就是说, ...
- JDK + Tomcat 安装 + 制作自定义镜像【第 2 篇 Tomcat】
[第 1 篇 JDK]:https://www.cnblogs.com/del88/p/11842387.html[第 2 篇 Tomcat]:https://www.cnblogs.com/del8 ...