最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

css中不確定盒子寬高上下左右居中的八種方法小結(jié)

  發(fā)布時(shí)間:2023-08-23 16:39:17   作者:Forestご   我要評論
本文主要介紹了css中不確定盒子寬高上下左右居中的八種方法小結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

第一種:利用絕對定位和margin:auto實(shí)現(xiàn)

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屬性實(shí)現(xiàn)

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布局實(shí)現(xiàn)

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;
        /* 左右居中  但是圖片高度會(huì)撐滿 */
        justify-content: center;
      }
</style>

第四種:利用grid布局實(shí)現(xiàn)

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實(shí)現(xiàn)

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實(shí)現(xiàn)

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實(shí)現(xiàn)

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實(shí)現(xiàn)

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;
        /* 是同時(shí)設(shè)置 align-items 和 justify-items 的快速方法。通過將其設(shè)置為 center , align-items 和 justify-items 都將設(shè)置為 center。 */
        place-items: center;
      }
      /* .img {
        沒有固定的寬高
      } */
</style>

到此這篇關(guān)于css中不確定盒子寬高上下左右居中的八種方法小結(jié)的文章就介紹到這了,更多相關(guān)css盒子上下左右居中內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持腳本之家!

相關(guān)文章

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

    本文主要介紹了CSS子盒子水平和垂直居中的五種方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)
    2022-07-19
  • css 利用 position + margin 實(shí)現(xiàn)固定盒子橫向縱向居中的方法

    這篇文章主要介紹了css 利用 position + margin 實(shí)現(xiàn)固定盒子橫向縱向居中的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要
    2020-12-23
  • 使用CSS實(shí)現(xiàn)盒子水平垂直居中的方法(8種)

    這篇文章主要介紹了使用CSS實(shí)現(xiàn)盒子水平垂直居中的方法(8種),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來
    2020-11-11
  • CSS盒子居中的常用的幾種方法(小結(jié))

    這篇文章主要介紹了CSS盒子居中的常用的幾種方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)
    2020-03-31

最新評論

涞源县| 屏东县| 鸡东县| 阿巴嘎旗| 砚山县| 建昌县| 绩溪县| 屏东市| 梁河县| 富平县| 元阳县| 永寿县| 克拉玛依市| 邵阳县| 阿勒泰市| 巴塘县| 鹿泉市| 长岭县| 肥东县| 新郑市| 赣榆县| 松阳县| 都江堰市| 湘潭县| 龙泉市| 武乡县| 高雄市| 吴桥县| 吉水县| 海安县| 射洪县| 石台县| 博湖县| 济南市| 隆回县| 通海县| 云霄县| 唐山市| 栖霞市| 双牌县| 高邑县|