CSS实现元素居中的五种方法

作者: 只会前端的切图仔 | 来源:发表于2018-05-23 10:23 被阅读48次

1.使用text-align水平居中

<style>
.center{
  text-align: center;
  background: rgba(0,0,0,.5);
}
.center img{
  width: 33%;
  height: auto;
}
</style>
<body>
  <div class="center">
        <img src="img/img.jpg" alt="img">
    </div>
</body>

这个属性适合自适应居中,但必须注意的是:居中的元素必须是行内元素。


2.使用absolute定位居中

<style>
.center{
    position: relative;
    min-height: 500px;
    background: rgba(0,0,0,.5);
}
.center img{
    width: 200px;
    height: 200px;
    overflow: auto;
    position: absolute;
    left: 50%;
    top: 50%;
    margin: -100px 0 0 -100px;
}
</style>
<body>
<div class="center">
        <img src="img/img.jpg" alt="img">
    </div>
</body>

这种 方案 有非常好的跨浏览器支持。有一个缺点就是必须显式声明外部容器元素的height


3.使用translate居中

<style>
.center{
    position: relative;
    min-height: 500px;
    background: rgba(0,0,0,.5);
}
.center img{
  position: absolute;
  top:50%;
  left:50%;
  transform: translate(-50%,-50%);
  -webkit-transform: translate(-50%,-50%);
  width:30%;
  height: auto;
}
</style>
<body>
<div class="center">
        <img src="img/img.jpg" alt="img">
    </div>
</body>

但是有以下几种缺点:
1.CSS transform 在部分就浏览器上需要使用 前缀;
2.不支持 IE9 以下的浏览器;
3.外部容器需要设置height (或者用其他方式设置),因为不能获取 绝对定位 的内容的高度
4.如果内容包含文字,现在的浏览器合成技术会使文字模糊不清


4.使用table-cell居中

<style>
.center{
    display: table;
    background: rgba(0,0,0,0.5);
    width: 200%;
    height: 200%;
    text-align: center;
}
.center-core{
    display: table-cell;
    text-align: center;
    vertical-align: middle;
}
</style>
<body>
<div class="center">
     <div class="center-core">
           居中
     </div> 
</div>
</body>

利用的知识点:
display:table 此元素会作为块级表格来显示(类似 <table>)
display:table-cell 此元素会作为一个表格单元格显示(类似 <td> 和 <th>)


5.使用Flexbox居中

<style>
.center{
    display: flex;
    justify-content: center;
    background: rgba(0,0,0,.5);
}
.square{
    height: 200px;
    width: 200px;
    background: red;
}
.circle{
    width: 150px;
    height: 150px;
    background: blue;
}
</style>
<body>
<div class="center">
        <div class="square"></div>
        <div class="circle"></div>
        <div class="square"></div>
    </div>
</body>

利用的知识点:
flex-start:弹性盒子元素将向行起始位置对齐。该行的第一个子元素的主起始位置的边界将与该行的主起始位置的边界对齐,同时所有后续的伸缩盒项目与其前一个项目对齐。


相关文章

  • css3 元素居中的几个方法

    原文参考 纯CSS实现垂直居中的几种方法 html 当元素为 block时 position position +...

  • CSS垂直居中,你会多少种写法?

    CSS控制居中是前端开发中非常常用的布局技能,本文列出几种CSS控制元素居中的几种方法。  谈及HTML元素居中展...

  • CSS居中小结

    下面是CSS居中的几种方法: 水平居中元素: 通用方法,元素的宽高未知 方式一:CSS3 transform 方式...

  • Flexbox实现元素的水平居中和垂直居中

    网上有N种方法实现元素的水平居中和垂直居中,如CSS制作水平垂直居中对齐,这些方法各有各的优缺点。在这里,我们使用...

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

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

  • CSS实现元素居中的五种方法

    1.使用text-align水平居中 这个属性适合自适应居中,但必须注意的是:居中的元素必须是行内元素。 2.使用...

  • css 水平垂直居中实现方式

    css 水平垂直居中实现方式 水平垂直居中包括行内元素居中,以及块级元素居中 行内元素html结构 块级元素结构 ...

  • 垂直居中的方法

    行内元素居中 html css before元素居中 html css table-cell居中 css 垂直居中...

  • 垂直居中,水平居中

    CSS设置行内元素的水平居中 CSS设置行内元素的垂直居中 CSS设置块级元素的水平居中 CSS设置块级元素的垂直居中

  • 前端学习day4-总结

    关键词:CSS伪元素before after ,盒子里面元素居中对齐的两种方法,flex弹性布局 ...

网友评论

    本文标题:CSS实现元素居中的五种方法

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