美文网首页
CSS实现居中样式

CSS实现居中样式

作者: 小q | 来源:发表于2019-03-19 11:31 被阅读0次
  • 水平居中

  • 行内元素水平居中

  • 方法:用text-align:center实现

html代码部分

<div class="box1">
    行内元素水平居中
</div>

css代码部分

.box1{
    width: 200px;
    height: 200px;
    background: tomato;
    text-align: center;
}   
  • 块元素样式水平居中

  • 方法一:子元素利用margin: 0 auto;实现水平居中
    html代码部分
<div class="box2">
    <div class="boy2">块元素水平居中</div>
</div>

css代码部分

.box2{
    width: 200px;
    height: 200px;
    background: tomato;
}
.boy2{
    width: 100px;
    height: 100px;
    background: skyblue;
    margin: 0 auto;
}

  • 方法二:将子元素变为table,再利用margin实现

html代码部分

<div class="box2">
    <div class="boy2">块元素水平居中</div>
</div>

css代码部分

.box3{
    width: 200px;
    height: 200px;
    background: tomato;
    
}
.boy3{
    width: 100px;
    height: 100px;
    background: skyblue;
    display: table;
    margin: 0 auto;
}

方法三:将子元素设置成inline-block,父元素设置为text-align: center
html代码部分

<div class="box4">
    <div class="boy4">块元素水平居中</div>
</div>

css代码部分

.box4{
    width: 200px;
    height: 200px;
    background: tomato;
    text-align: center;
}
.boy4{
    width: 100px;
    height: 100px;
    background: skyblue;
    display: inline-block;
}

方法四:利用弹性盒实现水平居中
html代码部分

<div class="box8">
    <div class="boy8">弹性盒水平居中</div>
</div>

css代码部分

.box8{
    width: 200px;
    height: 200px;
    background: tomato;
    display: flex;
    justify-content:center;
}
.boy8{
    width: 100px;
    height: 100px;
    background: skyblue;
}
  • 垂直居中

  • 行内元素垂直居中

方法:设置与高度相同的行高
html代码部分

<div class="box5">
     行内垂直居中
</div>

css代码部分

.box5{
    width: 200px;
    height: 200px;
    background: tomato;
    line-height: 200px;
}
  • 块元素垂直居中

方法一:父元素设置成table-cell,再用vertical-align:middle
html代码部分

<div class="box6">
    <div class="boy6">块元素垂直居中</div>
</div>

css代码部分

.box6{
    width: 200px;
    height: 200px;
    background: tomato;
    display: table-cell;
    vertical-align: middle;
}
.boy6{
    width: 100px;
    height: 100px;
    background: skyblue;
}

方法二:弹性盒实现垂直居中
html代码部分

<div class="box7">
    <div class="boy7">弹性盒块元素垂直居中</div>
</div>

css代码部分

.box7{
    width: 200px;
    height: 200px;
    background: tomato;
    display: flex;
    align-items: center;
}
.boy7{
    width: 100px;
    height: 100px;
    background: skyblue;
}

  • 水平垂直居中

水平居中记一下利用position的实现方法,剩余的有时间再补上吧,嘻嘻嘻。

方法:position设置0加margin的方法
html代码部分

 <div class="box9">
    <div class="boy9">margin,绝对定位和0实现居中</div>
 </div>

css代码部分

.box9{
    width: 200px;
    height: 200px;
    background: tomato;
    position: relative;
}
.boy9{
     width: 100px;
    height: 100px;
    background: skyblue;
    margin: auto; 
    position: absolute;
    left: 0; top: 0; right: 0; bottom: 0;   
}

后记

CSS居中的方法肯定不止这几种,只是把最常用的写了一下,后面学到其他的会继续补充。

相关文章

  • CSS 居中的各种方案

    CSS 居中的各种方案 实现居中的样式分为容器 (container) 的样式和需要居中的元素 (item) 的样...

  • CSS解决盒模型居中的问题

    CSS实现盒子模型水平居中、垂直居中、水平垂直居中的多种方法 CSS实现盒子模型水平居中的方法 全局样式 第一种:...

  • flex实现水平中下居中

    html结构 CSS 实现中间块上下居中只需在.inner样式中填写 或者在box父元素中添加样式

  • 4种常用方式实现前端垂直居中

    垂直居中 4种常用方式实现前端垂直居中Demo地址基本html结构 基本css样式 1. 定位布局 利用relat...

  • CSS实现居中样式

    水平居中 行内元素水平居中 方法:用text-align:center实现 html代码部分 css代码部分 块元...

  • day2(乱)

    css样式 1 css样式属性 #1.1 样式重置(初始化样式) 1.2 常用属性 1.3 元素水平居中 2 常用...

  • 关于react-native text 水平垂直居中问题

    为了实现text文本居中,原本在android 上开发以下的css样式是ok的 但在ios上只有水平居中,没有垂直...

  • HTML

    1.常用的html标签 2.常用的css样式 3.元素的水平居中 text-align:center实现居中 ma...

  • css居中十八式

    前言 在使用css设置页面样式时会经常遇到需要居中的情况,下面我总结了一些css在不同条件下实现居中的方法。大家看...

  • div设置垂直水平居中的几种方法

    html样式 --------children相对于box水平垂直居中 css样式 方法一: .box ...

网友评论

      本文标题:CSS实现居中样式

      本文链接:https://www.haomeiwen.com/subject/zsawmqtx.html