美文网首页
六种居中

六种居中

作者: StarLikeRain | 来源:发表于2018-09-12 22:09 被阅读31次

用 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

相关文章

  • 六种居中

    用 table 自带的功能居中 100% 高度的 afrer before 加上 inline block 依靠 ...

  • 11.21学习日志

    关于vim的使用 如何commit 下午的布局的n种方法,蕾蕾分享了六种水平居中样式,分别是设定行内块元素、flo...

  • 六种实现垂直居中的方法

    方式一、利用绝对定位的方式+margin:auto 方式二、利用绝对定位的方式+margin反向偏移 方式三、基于...

  • div水平垂直居中的方法

    在平时开发过程中我们经常会对元素设置水平垂直居中,以下整理了六种方法: 方法一: 绝对定位方法:不确定当前div的...

  • 六种实现网站水平居中方法

    转载:http://www.w3cplus.com/css/elements-horizontally-cente...

  • 多行文本固定高度垂直居中

    话不多说直接上代码! html: 居中居中居中居中居中居中 css: #Mian{ width:50...

  • css 居中

    居中有水平居中和垂直居中。 水平居中+垂直居中 flex法 position法 就是计算呗~ 参考 CSS各种居中...

  • css居中方式总结(亲测有效)

    水平居中(行内元素水平居中、块级元素水平居中) 垂直居中 水平垂直居中 行内元素水平居中 text-align: ...

  • 居中布局

    水平居中 垂直居中 垂直水平居中 已知元素的宽高的居中布局 定位居中布局 盒模型居中布局 图片的垂直水平居中(利用...

  • css 居中效果

    上下居中: 左右居中: 整页居中:

网友评论

      本文标题:六种居中

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