标签搜索

目 录CONTENT

文章目录

css中不确定盒子宽高上下左右居中的八种方法小结

沙漠渔溏 / 2024-01-29 03:01:31 / 共计5,118 字
温馨提示:
本文最后更新于 2024-01-29,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

第一种:利用绝对定位和margin:auto实现

html:

<div class="box">
    <img src="./img/77.jpeg" alt="" class="img">
</div>

css:

<style>
    body{
      margin: 0;
    }
    .box{
      height: 100vh;
      background-color: hotpink;
      position: relative;
    }
    .img{
      position: absolute;
      top: 0;
      left: 0;
      bottom: 0;
      right: 0;
      margin: auto;
    }
</style>

第二种:利用css3的transform属性实现

html:

<div class="box">
    <img src="./img/77.jpeg" alt="" class="img">
</div>

css:

<style>
      body {
        margin: 0;
      }
      .box {
        height: 100vh;
        background-color: hotpink;
        position: relative;
      }
      .img {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
      }
</style>

第三种:利用flex布局实现

html:

<div class="box">
    <img src="./img/77.jpeg" alt="" class="img">
</div>

css:

<style>
      body {
        margin: 0;
      }
      .box {
        height: 100vh;
        background-color: hotpink;
        display: flex;
        /* 上下居中 */
        align-items: center;
        /* 左右居中  但是图片高度会撑满 */
        justify-content: center;
      }
</style>

第四种:利用grid布局实现

html:

<div class="box">
    <img src="./img/77.jpeg" alt="" class="img">
</div>

css:

<style>
      body {
        margin: 0;
      }
      .box {
        height: 100vh;
        background-color: hotpink;
        display: grid;
      }
      .img {
        display: inline-block;
        /* 上下居中 */
        align-self: center;
        /* 左右中 */
        justify-self: center;
      }
</style>

第五种:利用display: -webkit-box实现

html:

<div class="box">
    <img src="./img/77.jpeg" alt="" class="img">
</div>

css:

<style>
      body {
        margin: 0;
      }
      .box {
        height: 100vh;
        background-color: hotpink;
        display: -webkit-box;
        /* 上下居中 */
        -webkit-box-align: center;
        /* 左右居中 */
        -webkit-box-pack: center;
      }
</style>

第六种:利用display: flex和margin: auto实现

html:

<div class="box">
    <img src="./img/77.jpeg" alt="" class="img">
</div>

css:

<style>
      body {
        margin: 0;
      }
      .box {
        width: 100vw;
        height: 100vh;
        background-color: hotpink;
        display: flex;
      }
      .img {
        margin: auto;
      }
    </style>

第七种:利用table-cell实现

html:

<div class="box">
    <img src="./img/77.jpeg" alt="" class="img">
</div>

css:

<style>
      body {
        margin: 0;
      }
      .box {
      	/* 要有宽高 */
        width: 100vw;
        height: 100vh;
        background-color: hotpink;
        display: table-cell;
        /* 左右居中 */
        text-align: center;
        /* 上下居中 */
        vertical-align: middle;
      }
      .img {
        /* 不加也可以 */
        display: inline-block;
      }
</style>

第八种:利用display: grid和place-items: center实现

html:

<div class="box">
    <img src="./img/77.jpeg" alt="" class="img">
</div>

css:

<style>
      body {
        margin: 0;
      }
      div {
        height: 100vh;
        background-color: hotpink;
        display: grid;
        /* 是同时设置 align-items 和 justify-items 的快速方法。通过将其设置为 center , align-items 和 justify-items 都将设置为 center。 */
        place-items: center;
      }
      /* .img {
        没有固定的宽高
      } */
</style>

到此这篇关于css中不确定盒子宽高上下左右居中的八种方法小结的文章就介绍到这了,更多相关css盒子上下左右居中内容请搜索 『沙漠渔溏』 以前的文章或继续浏览下面的相关文章,希望大家以后多多支持 『沙漠渔溏』 !