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

  发布时间:2023-08-23 16:39:17   作者:Forestご   我要评论
本文主要介绍了css中不确定盒子宽高上下左右居中的八种方法小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

第一种:利用绝对定位和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盒子上下左右居中内容请搜索脚本之家以前的文章或继续浏览下面的相关文章,希望大家以后多多支持脚本之家!

相关文章

  • CSS子盒子水平和垂直居中的五种方法

    本文主要介绍了CSS子盒子水平和垂直居中的五种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习
    2022-07-19
  • css 利用 position + margin 实现固定盒子横向纵向居中的方法

    这篇文章主要介绍了css 利用 position + margin 实现固定盒子横向纵向居中的方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要
    2020-12-23
  • 使用CSS实现盒子水平垂直居中的方法(8种)

    这篇文章主要介绍了使用CSS实现盒子水平垂直居中的方法(8种),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来
    2020-11-11
  • CSS盒子居中的常用的几种方法(小结)

    这篇文章主要介绍了CSS盒子居中的常用的几种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学
    2020-03-31

最新评论