温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

实现CSS垂直居中的技巧有哪些

发布时间:2020-07-11 14:52:13 来源:亿速云 阅读:165 作者:Leah 栏目:web开发

本篇文章为大家展示了实现CSS垂直居中的技巧有哪些,代码简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

网页CSS的垂直居中需求始终没有停过,而其困难度也始终没有让人轻松过,经过了每位开发先烈的研究后,据说CSS的垂直居中技巧已达到近十种之多,但始终鲜为人知,部分公司甚至将CSS的垂直居中技巧当成面试题,其重要性可见一斑,今天就带着大家了解一下CSS的垂直居中的多种方式吧。

1、Line-height

适用情景:单行文字垂直居中技巧

这个方式应该是最多人知道的了,常见于单行文字的应用,像是按钮这一类对象,或者是下拉框、导航此类元素最常见到的方式了。此方式的原理是在于将单行文字的行高设定后,文字会位于行高的垂直中间位置,利用此原理就能轻松达成垂直居中的需求了。

<div class="content">Lorem ipsam.</div>
.content{
  width: 400px;
  background: #ccc;
  line-height:100px;
  margin: auto;
}

2、Line-height + inline-block

适用情景:多对象的垂直居中技巧

既然可以使用第一种方式对行元素达成垂直居中的话,当然没有理由不能做到多行啊~但是你需要将多个元素或多行元素当成一个行元素来看待,所以我们必须要将这些数据多包一层,并将其设定为inline-block,并在该inline-block对象的外层对象使用inline-block来代替height的设置,如此便可以达到垂直居中的目的了,从使你的数据是包含了标题跟内容在内也可以正常的垂直居中了。

<div class="box box2">
  <div class="content">
    立马来看Amos实际完成的    <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/">
      CSS3精美相册效果    </a>
    效果吧!別忘了拖拉一下窗口看看 RWD 效果喔!  </div>
</div>
h3{
  text-align: center;
}
.box{
  width: 500px;
  border: 1px solid #f00;
  margin: auto;
  line-height: 200px;
  text-align: center;
}
.box2 .content{
  display: inline-block;
  height: auto;
  line-height:1;
  width: 400px;
  background: #ccc;
}

3、:before + inline-block

适用情景:多对象的CSS垂直居中技巧

:before 伪类元素搭配 inline-block 属性的写法应该是很传统的垂直居中的技巧了,此方式的好处在于子元素居中可以不需要特别设定高度,我们将利用:before伪类元素设定为100%高的inline-block,再搭配上将需要居中的子元素同样设置成inline-block性质后,就能使用vertical-align:middle来达到垂直居中的目的了,此方式在以往其实是个非常棒的垂直居中解决方案,唯独需要特别处理掉inline-block元素之间的4-5px空间这个小缺陷,但也很实用了。

<h3>3.:before + inline-block</h3>
<div class="box box3">
  <div class="content">
    立马来看Amos实际完成的    <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/">
      CSS3精美相册效果    </a>
    效果吧!別忘了拖拉一下窗口看看 RWD 效果喔!  </div>
</div>
h3{
  text-align: center;
}
.box{
  width: 500px;
  height: 250px;
  border: 1px solid #f00;
  margin: auto;
  text-align: center;
}
.box::before{
  content:'';
  display: inline-block;
  height: 100%;
  width: 0;
  vertical-align: middle;
}
.box .content{
  width: 400px;
  background: #ccc;
  display: inline-block;
  vertical-align: middle;
}

4、absolute + margin 负值

适用情景:多行文字的垂直居中技巧

谁说绝对定位要少用?Amos认为没有少用多用的问题,重点在于你是否有妥善运用才是重点,绝对定位在这个例子中会设置top:50%来抓取空间高度的50%,接着在将居中元素的margin-top设定为负一半的高度,这样就能让元素居中了,此方法可是自古以来流传多年的居中方式呢?

<h3>4.absolute + margin 負值</h3>
<div class="box box4">
  <div class="content">
    立马来看Amos实际完成的    <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/">
      CSS3精美相册效果    </a>
    效果吧!別忘了拖拉一下窗口看看 RWD 效果喔!  </div>
</div>
h3{
  text-align: center;
}
.box{
  width: 500px;
  height: 250px;
  border: 1px solid #f00;
  margin: auto;
  position: relative;
}
.box4 .content{
  width: 400px;
  background: #ccc;
  height: 70px;
  position: absolute;
  top:50%;
  left: 50%;
  margin-left: -200px;
  margin-top: -35px;
}

5、absolute + margin auto

适用情景:多行文字的垂直居中技巧

又一个绝对定位的垂直居中的方案,这个方式比较特别一点,当元素设置为绝对定位后,假设它是抓不到整体可运用的空间范围,所以margin:auto会失效,但当你设置了top:0;bottom:0;时,绝对定位元素就抓到了可运用的空间了,这时你的margin:auto就生效了(神奇吧),如果你的绝对定位元素需要水平居中于父层,那你同样可以设定left:0;right:0;来让绝对定位元素取得空间可运用范围,再让marign-left与margin-right设定为auto即可居中。但此方式的缺点是你的定位元素必须有固定的宽高(百分比也算)才能正常居中。

<h3>5.absolute + translate(-50%, -50%)</h3>
<div class="box box5">
  <div class="content">
    立马来看Amos实际完成的    <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/">
      CSS3精美相册效果    </a>
    效果吧!別忘了拖拉一下窗口看看 RWD 效果喔!  </div>
</div>
h3{
  text-align: center;
}
.box{
  width: 500px;
  height: 250px;
  border: 1px solid #f00;
  margin: auto;
  position: relative;
}
.content{
  width: 400px;
  background: #ccc;
  height: 70px;
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  margin: auto;
}

6、Display:table-cell

适用情景:多行文字的垂直居中技巧

这一招我想有点年纪的开发者应该都有看过,当然像我这么嫩的开发者当然是第一次看到啦,这一招的原理在于使用 CSS display属性将div设置成表格的单元格,这样就能利用支持存储单元格对齐的vertical-align属性来将信息垂直居中

<h3>19.display: table-cell</h3>
<div class="box box19">
  <div class="content">
    立马来看Amos实际完成的    <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/">
      CSS3精美相册效果    </a>
    效果吧!別忘了拖拉一下窗口看看 RWD 效果喔!  </div>
</div>
h3{
  text-align: center;
}
.box{
  width: 500px;
  height: 250px;
  border: 1px solid #f00;
  margin: auto;
    text-align: center;
    display: table-cell;
  vertical-align: middle;
}
.content{
  width: 400px;
  background: #ccc;
  margin: auto;
}

7、padding

适用情景:多行文字的垂直居中技巧

什么!这也算垂直居中技巧,连我奶奶都知道这方式吧

对的,这的确也算是一种垂直居中的方式,不可讳言的这方式真的是简单过头了,以至于有些开发者认为这种方式都不能算是一种垂直居中的技巧,但同样的你无法反驳的是,我的数据的确垂直居中啦,好啦,就当我硬凹吧,你说的对,好吧

<h3>22.padding</h3>
<div class="box box22">
  <div class="content">
    立马来看Amos实际完成的    <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/">
      CSS3精美相册效果    </a>
    效果吧!別忘了拖拉一下窗口看看 RWD 效果喔!  </div>
</div>
h3{
  text-align: center;
}
.box{
  width: 500px;
  border: 1px solid #f00;
  margin: auto;
  height: auto;
  padding: 50px 0;
}
.content{
  width: 400px;
  background: #ccc;
  margin: auto;
}

上述内容就是实现CSS垂直居中的技巧有哪些,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI