sass 垂直居中

Last updated on September 15, 2024 pm

🧙 Questions

对象如何垂直居中

☄️ Ideas

import React from "react";
import './Demo.scss'

export default function Demo() {

  return <>

    <div className={"contain-div"}>
      <div className={"son-div"}>
      </div>
    </div>

    <div className={"contain-div-1"}>
      <div className={"son-div"}>
      </div>
    </div>

    <div className={"contain-div-2"}>
      <div className={"son-div"}>
      </div>
    </div>

    <div className={"contain-div-3"}>
      <div className={"son-div"}>
      </div>
    </div>

    <div className={"contain-div-4"}>
      <div className={"son-div"}>
      </div>
    </div>

    <div className={"contain-div-5"}>
      <div className={"son-div"}>
      </div>
    </div>

  </>;

};
// 使用 justify-content 和  align-items 垂直居中
.contain-div {
    border: black solid 1px;
    margin-bottom: 10px;
    height: 300px;
    width: 100%;
    background: blanchedalmond;
    display: flex;
    justify-content: center;
    align-items: center;

    .son-div {
        height: 200px;
        width: 200px;
        background: #b07ff1;;
    }
}

// 使用 align-self 和 justify-self 垂直居中
.contain-div-1 {
    display: grid;
    border: black solid 1px;
    margin-bottom: 10px;
    height: 300px;
    width: 100%;
    background: blanchedalmond;

    .son-div {
        height: 200px;
        width: 200px;
        background: #b07ff1;
        align-self: center;
        justify-self: center;
    }
}

// 使用转换的方式translateX 垂直居中
.contain-div-2 {
    border: black solid 1px;
    margin-bottom: 10px;
    height: 300px;
    width: 100%;
    background: blanchedalmond;
    position: relative;

    .son-div {
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translateX(-50%) translateY(-50%);
        height: 200px;
        width: 200px;
        background: saddlebrown;
    }
}

// 使用绝对定位absolute 垂直居中
.contain-div-3 {
    border: black solid 1px;
    margin-bottom: 10px;
    height: 300px;
    width: 100%;
    background: blanchedalmond;
    position: relative;

    .son-div {
        position: absolute;
        top: 0;
        bottom: 0;
        right: 0;
        left: 0;
        margin: auto;
        height: 200px;
        width: 200px;
        background: saddlebrown;
    }
}

// 使用 line-height  垂直居中
.contain-div-4 {
    border: black solid 1px;
    margin-bottom: 10px;
    height: 300px;
    width: 100%;
    background: blanchedalmond;
    text-align: center;
    line-height: 300px;

    .son-div {
        display: inline-block;
        vertical-align: middle;
        height: 200px;
        line-height: 200px;
        width: 200px;
        background: saddlebrown;
    }
}

// 使用table-cell 垂直居中
.contain-div-5 {
    border: black solid 1px;
    margin-bottom: 10px;
    height: 300px;
    width: 100%;
    background: blanchedalmond;
    display: table-cell;
    text-align: center;
    vertical-align: middle;

    .son-div {
        height: 200px;
        width: 200px;
        background: saddlebrown;
        display: inline-block;
    }
}

sass 垂直居中
https://ispong.isxcode.com/vue/sass/sass 垂直居中/
Author
ispong
Posted on
October 29, 2021
Licensed under