六种居中

2018-09-12  本文已影响31人  StarLikeRain

用 table 自带的功能居中

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title></title>
</head>
<body>
  <table class="parent">
    <tr>
      <td class="child">
      jdoadhuwhefuheuwihfuihewuif
        wefjiowejoifhouiwheoufhouwehoufhoewj
        fiuwehfoiuhewoijfoijweoijfoijweiofjiew
        fnewiohfqoihewophfuoihdsuiafhuiohewuihfoufewfew
        fewfqwef
        
      </td>
    </tr>
  </table>
</body>
</html>
.parent{
  border: 1px solid red;
  height: 600px;
}

.child{
  border: 1px solid green;
}
image.png

100% 高度的 afrer before 加上 inline block

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title></title>
</head>
<body>
  <div class="parent">
    <span class=before></span><div class="child">
      
     jdoadhuwhefuheuwihfuihewuif
        wefjiowejoifhouiwheoufhouwehoufhoewj
        fiuwehfoiuhewoijfoijweoijfoijweiofjiew
        fnewiohfqoihewophfuoihdsuiafhuiohew
uihfoufewfew
        fewfqwef
      
    </div><span class=after></span>
  </div>
</body>
</html>
.parent{
  border: 3px solid red;
  height: 600px;
  text-align: center;
}

.child{
  border: 3px solid black;
  display: inline-block;
  width: 300px;
  vertical-align: middle;
}

.parent .before{
  outline: 3px solid red;
  display: inline-block;
  height: 100%;
  vertical-align: middle;
}
.parent .after{
  outline: 3px solid red;
  display: inline-block;
  height: 100%;
  vertical-align: middle;
}
image.png

依靠 table 的特性来居中(不太好)

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title></title>
</head>
<body>
  <div class="table">
      <div class="td">
        <div class="child">
          jdoadhuwhefuheuwihfuihewuif
        wefjiowejoifhouiwheoufhouwehoufhoewj
        fiuwehfoiuhewoijfoijweoijfoijweiofjiew
        fnewiohfqoihewophfuoihdsuiafhuiohewuihfoufewfew
        fewfqwef
        </div>
    </div>
  </div>
</body>
</html>
div.table{
  display: table;
  border: 1px solid red;
  height: 600px;
}
div.td{
  display: table-cell;
  border: 1px solid blue;
  vertical-align: middle;
}
.child{
  border: 10px solid black;
}
image.png

margin-top: -50%,transform:-50%来做到

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title></title>
</head>
<body>
  <div class="parent">
    <div class="child">
      文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文
    </div>
  </div>
</body>
</html>
.parent{
  height: 600px;
  border: 1px solid red;
  position: relative;
}
.child{
  border: 1px solid green;
  width: 300px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform:translate(-50%,-50%);
  /* margin-left: -150px; */
  height: 100px;
  /* margin-top: -50px; */
}
image.png

absolute margin auto

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title></title>
</head>
<body>
  <div class="parent">
    <div class="child">
      文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字
    </div>
  </div>
</body>
</html>
.parent{
  height: 600px;
  border: 1px solid red;
  position: relative;
}
.child{
  border: 1px solid green;
  position: absolute;
  width: 300px;
  height: 200px;
  margin: auto;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
}
image.png

最棒棒的flex

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title></title>
</head>
<body>
  <div class="parent">
    <div class="child">
      文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字
    </div>
    
  </div>
</body>
</html>
.parent{
  height: 600px;
  border: 3px solid red;
  
  display: flex;
  justify-content: center;
  align-items: center;
}
.child{
  border: 3px solid green;
  width: 300px;
  word-break: break-all;
}
image.png
上一篇 下一篇

猜你喜欢

热点阅读