一、水平居中方法

1.行内元素、字体的水平居中

1.对于行内元素(display值为inline或inline-block都可以)或者字体:父元素添加css规则:text-align:center;

<style>
p{
/*关键*/
text-align:center;
} ul{
/*关键*/
text-align:center:
}
/*这里li设置inline-block*/
.item{
/*关键*/
display:inline-block;
} </style>
<!--字体-->
<p>I am ry</p> <!--行内元素-->
<ul>
<li class="item">1</li>
<li class="item">2</li>
<li class="item">3</li>
<li class="item">4</li>
</ul>

2.块元素的水平居中

1.使用margin实现水平居中

将margin-left 和 margin-right 设置为auto,块元素将会自动匹配适应,实现水平居中

<style>
*{
margin:0;
padding:0;
}
.box1{
height:300px;
background:blue;
}
.item1{
/*关键,margin-left,margin-right设置为auto*/
margin: 0 auto;
width: 100px;
height: 100px;
background:red;
}
</style>
<body>
<div class="box1">
<div class="item1"></div>
</div>
</body>

2.使用position+margin-left实现水平居中

定位后,设置left先整体偏移父容器的一半,再使用负margin-left自己一半的宽度,从而子元素中心对准父元素中心。

<style>
.box2{
position:relative;
height:300px;
background:blue;
}
.item2{
/*关键 相对定位*/
position: relative;
/*关键*/
left: 50%;
/*关键*/
margin-left:-50px;
width:100px;
height:100px;
background:red;
}
</style>
<body>
<div class="box2">
<div class="item2"></div>
</div>
</body>

3.如果是多个块元素在同一水平线居中排列,则将其display设置成inline-block,还有一种是使用flexbox的布局方式来实现。

块元素设置了inline-block后,拥有行内元素并排特点,只要父元素设置text-align即可使其水平居中。

<style>
.box3{
/* 关键,对于行内元素水平居中 */
text-align: center;
} .item3{
/* 关键,将块元素设置成行内块接元素 */
display:inline-block;
width:100px;
height: 100px;
background:red;
}
</style>
<body>
<div class="box3">
<div class="item3"></div>
<div class="item3"></div>
<div class="item3"></div>
<div class="item3"></div>
</div>
</body>

二、垂直居中

1.行内元素或者文字(单行情况)

1.可以直接使用line-height属性来设置: 将line-height设置成和height一样即可

<style>
.text{
height:100px;
line-height:100px;
border:1px solid red;
}
</style> <body>
<div class="text">
we dont talk anymore
</div>
</body>

2.使用padding(top,bottom)通过增加内边距来实现垂直的居中

<style>
.ctext{
/*关键*/
padding-top:30px;
padding-bottom:30px;
border:1px solid red;
}
</style>
<body>
<div class="ctext">确认过眼神,我遇上对的人</div>
</body>

2.行内元素或者文字(多行情况)

1.照样可以使用padding(top 和 bottom)

2.对父元素设置display:table-cell 和 vertical-align:middle

<style>
.box{
/* 将其变成单元格的形式 */
display: table-cell;
/* width:100px; */
height:300px;
border:1px solid red;
/* 设置内联元素的垂直对齐的方式 */
vertical-align: middle;
} </style>
<body>
<div class="box">
<span>how to love</span><br>
<span>I knoe I need somebody</span><br>
<span>I know I need somebody</span><br>
<span>pary for me</span>
</div>
</body>

3.块元素垂直居中

1.确定宽高的情况

使用position 和 margin-top配合

<style>
*{
margin:0;
padding:0;
}
/* 父元素 */
.parent{
position:relative;
height:400px;
border: 1px solid blue;
}
/* 子元素 */
.child{
/* position */
position: relative;
/* 距离顶部50% */
top:50%;
/* 再用margin-top 向上调子容器一半高度,则刚好子元素中心对准父元素中心 */
margin-top:-100px;
height:200px;
border:1px solid red;
}
</style>
<body>
<div class="parent">
parent
<div class="child">
child
</div>
</div>
</body>

2.对于未知宽高的

使用transform 的 translateY(-50%) 向上平移自身一半

<style>
.parent2{
position: relative;
background:blue;
height:400px;
}
.child2{
position: relative;
top:50%;
transform: translateY(-50%);
background: red;
}
</style>
<div class="parent2">
parent2
<div class="child2">child2</div>
</div>

3.使用flex布局

<style>
.parent3{
/*关键flex*/
display:flex;
display: -webkit-flex;
/* 对齐排列居中 */
justify-content:center;
/* 排列方向垂直 */
flex-direction:column;
height:400px;
background:yellow;
}
.child3{
height:100px;
}
</style>
<body>
<!-- flexbox -->
<div class="parent3">
<div class="child3"></div>
</div>
</body>

三、水平且垂直居中

1.position 和 margin 配合

<style>
*{
margin: 0 ;
padding: 0 ;
}
.box1{
position:relative;
height:400px;
background:blue;
}
.item1{
/*关键*/
position: absolute;
top: 50%;
left: 50%;
margin-top:-50px;
margin-left:-50px;
width:100px;
height:100px;
background: red;
}
</style>
<body>
<div class="box1">
<div class="item1"></div>
</div>
</body>

2.使用flex布局

同时设置两条居中属性 justify-content 和 align-items

<style>
.box2{
display: flex;
/*关键*/
justify-content: center;
/*关键*/
align-items: center;
height:300px;
background:yellow;
}
.item2{
width:50px;
height:50px;
background:red;
}
</style>
<body>
<div class="box1">
<div class="item1"></div>
</div> <div class="box2">
<div class="item2"></div>
</div>
</body>

本篇是个人笔记,可经常看看。向前走,也别忘记要多回顾

确认过眼神,我遇上对的人

Ry(元)

CSS--居中方式总结的更多相关文章

  1. 常用的CSS居中方式

    1.水平居中margin 0 auto;(浮动元素除外) 这个属性在网页制作的过程中是经常被用到的,一般情况下页面的版心你就可以看到它. <style> .father { width: ...

  2. 各种div+css居中方式调整(转载)

    盘点8种CSS实现垂直居中水平居中的绝对定位居中技术 分类: 前端开发2013-09-11 21:06 24959人阅读 评论(3) 收藏 举报 绝对居中垂直居中水平居中CSS居中代码   目录(?) ...

  3. 15种css居中方式

    1 水平居中 1.1 内联元素水平居中 利用 text-align: center 可以实现在块级元素内部的内联元素水平居中.此方法对内联元素(inline), 内联块(inline-block), ...

  4. css居中方式总结

    方法一: line-height <div class="vertical" style="width:200px;height:200px;border:2px ...

  5. 【基础】这15种CSS居中的方式,你都用过哪几种?

    简言 CSS居中是前端工程师经常要面对的问题,也是基本技能之一.今天有时间把CSS居中的方案汇编整理了一下,目前包括水平居中,垂直居中及水平垂直居中方案共15种.如有漏掉的,还会陆续的补充进来,算做是 ...

  6. 第212天:15种CSS居中的方式,最全了

    CSS居中是前端工程师经常要面对的问题,也是基本技能之一.今天有时间把CSS居中的方案整理了一下,目前包括水平居中,垂直居中及水平垂直居中方案共15种.如有漏掉的,还会陆续的补充进来,算做是一个备忘录 ...

  7. CSS居中的常用方式以及优缺点

    前言 居中是页面开发中经常遇到的问题. 使用合适的.简单的.兼容性好的居中方式是我们页面仔在整个工作生涯中都要面对的问题. text-align:center 来看这个例子,一张图片和文字进行居中.如 ...

  8. css中两种居中方式text-align:center和margin:0 auto 的使用场景

    关于使用text-align:center和margin:0 auto 两种居中方式的比较 前言:最近由于要学习后端,需要提前学习一部分前端知识,补了补css知识,发现狂神在讲这一部分讲的不是特别清楚 ...

  9. css居中那点事儿

    css居中那点事儿 在css中对元素进行水平居中是非常简单的,然而使元素垂直居中就不是一件简单的事情了,多年以来,垂直居中已经成为了CSS领域的圣杯,因为它是极其常见的需求,但是在实践中却不是一件简单 ...

  10. 23----2013.07.01---Div和Span区别,Css常用属性,选择器,使用css的方式,脱离文档流,div+css布局,盒子模型,框架,js基本介绍

    01 复习内容 复习之前的知识点 02演示VS创建元素 03div和span区别 通过display属性进行DIV与Span之间的转换.div->span 设置display:inline   ...

随机推荐

  1. 优化以及bug

    优化1:节流函数2:城市查询时,之前用事件(拿到DOM中innerHTML,后触发事件),后改用v-model双向绑定:应该是更符合数据驱动.3:使用localstorage等本地存储,如果用户关闭本 ...

  2. OpenStack-Ocata版+CentOS7.6 云平台环境搭建 —7.网络服务Neutron配置

    网络服务Neutron本章节结束如何安装并配置网络服务(neutron)采用:ref:`provider networks <network1>`或:ref:`self-service n ...

  3. Typescript 学习笔记三:函数

    中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...

  4. asp.net mvc 安全测试漏洞 " HTTP 动词篡改的认证旁路" 问题解决

    IBM Security Appscan漏洞筛查-HTTP 动词篡改的认证旁路漏洞,具体解决方案: 在Web.Config中system.webServer节点增加配置security: <se ...

  5. python ---解决高并发超卖问题

    使用redis 解决美多商城超卖的问题 import redis r = redis.Redis(host='localhost', port=6379) #定义过载 def limit_handle ...

  6. numpy中pad函数的常用方法

    一.参数解释 ndarray = numpy.pad(array, pad_width, mode, **kwargs) array为要填补的数组 pad_width是在各维度的各个方向上想要填补的长 ...

  7. PowerShell 连接远程服务器

    >>服务端Enable-PSRemoting winrm quickconfig ————这个可能不需要 >>客户端Set-Item wsman:\localhost\Clie ...

  8. 利用jquery操作隐藏table某一列

    本文版权归 远方的风lyh和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作. //隐藏表格第一列 $('tr').find('th:eq(0)').hide(); $('tr').f ...

  9. 【JAVA】ThreadLocal源码分析

    ThreadLocal内部是用一张哈希表来存储: static class ThreadLocalMap { static class Entry extends WeakReference<T ...

  10. win10 HTTP 错误 500.21 - Internal Server Error

    错误描述: HTTP 错误 500.21 - Internal Server Error 处理程序“ExtensionlessUrlHandler-Integrated-4.0”在其模块列表中有一个错 ...