美文网首页
css中几种常见的居中方式

css中几种常见的居中方式

作者: Simon_s | 来源:发表于2017-02-21 10:24 被阅读77次

直接上代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>居中</title>
<style>
{
margin: 0;
padding: 0;
}
/
水平居中 margin : 0 auto; */
.box{
width: 300px;
height: 300px;
border: 2px solid red;
}
.img1{
display: block;
width: 100px;
height: 100px;
margin: 0 auto;
}

1.0.jpg
  /*水平垂直居中*/
  .box2{
    width: 300px;
    height: 300px;
    position: relative;
    border: 1px solid blue;
    background: yellow;
  }
  .img2{
      width: 100px;
      height: 100px;
      position: absolute;
      top:50%;
      left:50%;
      margin-left: -50px;
      margin-top: -50px;
   }
2.0.jpg
       /*水平垂直居中  2   */
   .box3{
     width: 300px;
     height: 300px;
     position: relative;
     border: 1px solid blue;
   }
   .img3{
       width: 100px;
       height: 100px;
       position: absolute;
       top: 0;
       left: 0;
       bottom: 0;
       right: 0;
       margin: auto;
    }
3.0.jpg
       /*水平垂直居中  3   css3新属性 */
    .box4{
      width: 300px;
      height: 300px;
      position: relative;
      border: 1px solid blue;
    }
    .img4{
        width: 100px;
        height: 100px;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%,-50%);
     }
4.0.jpg
     /* table-cell  将其变为表格 */
     .box5{
       width: 300px;
       height: 300px;
       display: table-cell;
       vertical-align: middle;
       text-align: center;
       border: 1px solid blue;
     }
     .img5{
         width: 100px;
         height: 100px;
      }
5.0.jpg
      /* flex布局完成 */
      .box6{
        width: 300px;
        height: 300px;
        display: flex;
        justify-content: center;
        align-items: center;
        border: 1px solid blue;
      }
      .img6{
          width: 100px;
          height: 100px;
       }
6.0.jpg

</style>
</head>
<body>
<div class="box">



</div>
<div class="box2">



</div>
<div class="box3">

</div>
<div class="box4">

</div>
<div class="box5">



</div>
<div class="box6">

</div>
</body>
</html>

相关文章

  • CSS常见布局技巧

    1.HTML中css水平居中的几种方式

  • CSS居中的几种方式

    本文主要总结几种常见的CSS居中方式,下面我准备分为三个方向来写,分别是水平居中,垂直居中,水平垂直居中。水平居中...

  • css中几种常见的居中方式

    直接上代码: 居中 {margin: 0;padding: 0;}/水平...

  • CSS 的几种典型居中

    CSS 中,有几种经典的居中,水平居中,垂直居中。其中水平居中比较常见,也相对比较简单;垂直居中相对少见,但是也很...

  • 面试之道 - 元素实现水平垂直居中的CSS解决方案

    声明:文章转载自《面试之道 - 元素实现水平垂直居中的CSS解决方案》 写出几种常见的水平垂直居中方式。 这是一个...

  • 水平居中和垂直居中

    本章介绍几种常见的水平居中和垂直居中的实现方式

  • CSS的布局与居中

    今天就总结一下CSS中几种常用的布局方式和居中方式。 1. 左右布局 网页布局常见左右两列布局,左侧栏是固定宽度。...

  • CSS居中小结

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

  • Css

    1. 介绍一下 CSS 的盒子模型? 2. css 选择器优先级? 3. 垂直居中几种方式? 4. 水平居中几种方...

  • CSS中几种居中方式

    1.水平居中的margin:0 auto; (用于子元素前提是不受float影响) *{ paddin...

网友评论

      本文标题:css中几种常见的居中方式

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