CSS居中——水平居中、垂直居中方法

水平居中

1、行内或类行内元素(如文本、链接)

在块级父元素中用CSS样式实现行内元素水平居中,只需要设置:text-align: center;

这种方法可以让 inline / inline-block / inline-table / flex 等类型的元素实现居中。

效果图:

代码:

<style>
    body{
        background-color: coral;
    }
    #div1,#div2{
        width: 400px;
        height: 100px;
        text-align: center;
        margin: 20px 0;
        padding: 20px;
        background-color: white;
    }
    #div2 a{
        text-decoration: none;
        color: white;
        padding: 3px 8px;
        border-radius: 5px;
        background-color: cornflowerblue;
    }
</style>

<body>
    <div id="div1">
        这是一段简单的文字,水平居中
    </div>
    <div id="div2">
        <a href="#">hyperlink1</a>
        <a href="#">hyperlink2</a>
        <a href="#">hyperlink3</a>
        <a href="#">hyperlink4</a>
    </div>
</body>

2、子盒子已知宽度

效果图​​​​​​​

html代码

<body>
    <div class="div1">
        <div class="div2"></div>
    </div>
</body>

实现方法

margin: 0 auto;

<style>
body{
        background-color: lightgoldenrodyellow;
    }
    .div1{
        background-color: cyan;
    }
    .div2{
        width: 200px;
        height: 100px;
        margin: 0 auto;
        background-color: rgb(132, 247, 65);
        color: white;
    }
</style>

② position定位+margin-left

<style>
    body{
        background-color: lightgoldenrodyellow;
    }
    .div1{
        position: relative;
    }
    .div2{
        width: 200px;
        height: 100px;
        position: absolute;
        left: 50%;
        /* margin-left: 负的一半宽度width */
        margin-left: -100px;
        background-color: rgb(132, 247, 65);
        color: white;
    }
</style>

3、子盒子有无宽度均适用

效果图

html代码

<body>
    <div class="div1">
        <div class="div2">居中</div>
    </div>
</body>

实现方法

①  position定位 + transform

<style>
    body{
        background-color: rgb(238, 197, 203);
    }
    .div1{
        position: relative;
    }
    .div2{
        position: absolute;
        left: 50%;
        /* 使用transform在X轴方向移-50%,无需根据宽度计算 */
        transform: translateX(-50%);
        background-color: rgb(145, 212, 235);
        font-size: 40px;
    }
</style>

flex布局

<style>
    body{
        background-color: rgb(238, 197, 203);
    }
    .div1{
        display: flex;
        justify-content: center;
    }
    .div2{
        background-color: rgb(145, 212, 235);
        font-size: 40px;
    }
</style>

垂直居中

1、行内元素

设置line-height与父级元素的height相等

效果图:

代码:

<style>
    .div1{
        height: 400px;
        background-color: aquamarine;
    }
    .div2{
        width: 200px;
        height: 200px;
        line-height: 400px;     /*设置line-height与父级元素的height相等*/
        font-size: 30px;
    }
</style>

<body>
    <div class="div1">
        <div class="div2">垂直居中</div>
    </div>
</body>

2、子盒子已知高度

效果图

html代码

<body>
    <div class="div1">
        <div class="div2">垂直居中</div>
    </div>
</body>

实现方法

① position定位+ margin-top

<style>
    .div1{
        height: 400px;
        background-color: aquamarine;
        position: relative;
    }
    .div2{
        width: 250px;
        height: 200px;
        background-color: coral;
        position: absolute;
        top: 50%;
      /* margin-top: 负的一半高度height */
        margin-top: -100px;
        line-height: 200px;
    }
</style>

3、子盒子有无高度均适用

效果图

html代码​​​​​​​

<body>
    <div class="div1">
        <div class="div2">垂直居中</div>
    </div>
</body>

实现方法

父盒子CSS样式设置伪类元素

基本思路:使用display: inline-block, vertical-align: middle和一个伪元素让内容块处于容器中央。

<style>
    .div1 {
        height: 400px;
        background-color: rgb(177, 236, 247);
    }
    .div1::after{
        content:'';
        height:100%;
        display:inline-block;
        vertical-align:middle;
    }
    .div2{
        width: 250px;
        background-color: rgb(219, 166, 166);
        display:inline-block;
        vertical-align:middle;
        line-height: 200px;
    }
</style>

②  position + transform​​​​​​​

<style>
    .div1{
        height: 400px;
        background-color: rgb(177, 236, 247);
        position: relative;
    }
    .div2{
        width: 250px;
        background-color: rgb(219, 166, 166);
        position: absolute;
        top: 50%;
        transform: translateY(-50%);
        line-height: 200px;
    }
</style>

flex布局

<style>
    .div1{
        height: 400px;
        background-color: rgb(177, 236, 247);
        display: flex;
        align-items: center;
    }
    .div2{
        width: 250px;
        background-color: rgb(219, 166, 166);
        line-height: 200px;
    }
</style>

<body>
    <div class="div1">
        <div class="div2">垂直居中</div>
    </div>
</body>

水平垂直居中

根据需求综合以上方法即可实现。

  • 3
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用如下的CSS代码实现垂直水平居中: ``` .container { display: flex; justify-content: center; align-items: center; } ``` 其中,`.container`是包含需要居中的元素的容器。使用`display: flex`属性可以使容器变为弹性盒子,`justify-content: center`和`align-items: center`可以分别将元素水平和垂直居中。 ### 回答2: 在CSS中,有多种方法可以实现元素的垂直和水平居中。以下是几种常用的方法: 1. 使用Flexbox布局:在父元素上应用以下样式可以实现垂直和水平居中: ``` display: flex; justify-content: center; align-items: center; ``` 这样可以使子元素在垂直和水平方向上居中。 2. 使用绝对定位:如果元素的宽高是已知的,可以使用绝对定位来实现居中。父元素需要设置相对定位,并在子元素上应用以下样式: ``` position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); ``` 通过将子元素的左上角移动到父元素的中心来实现垂直和水平居中。 3. 使用表格布局:将父元素的display属性设置为table,子元素的display属性设置为table-cell,然后应用以下样式可以实现居中: ``` display: table; margin: 0 auto; ``` 将子元素的左右边距设置为auto可以使其在水平方向上居中。 以上是几种常用的CSS垂直和水平居中方法,具体使用哪种方法取决于具体的需求和布局。 ### 回答3: 在CSS中,可以使用以下方法实现元素的垂直水平居中: 1. 使用Flexbox布局:设置父容器的display属性为flex,并且使用align-items和justify-content属性分别设置为center,可以实现元素的垂直水平居中。 ```css .container { display: flex; align-items: center; justify-content: center; } ``` 2. 使用绝对定位和transform属性:将需要居中的元素的position属性设置为absolute,然后使用top、bottom、left和right属性设置为50%,并使用transform属性进行偏移。 ```css .box { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); } ``` 3. 使用表格布局:将需要居中的元素放置在一个display属性设置为table的容器中,然后使用margin属性设置为auto实现水平居中,使用vertical-align属性设置为middle实现垂直居中。 ```css .container { display: table; margin: 0 auto; } .box { display: table-cell; vertical-align: middle; } ``` 4. 使用Grid布局:设置父容器的display属性为grid,然后使用place-items属性设置为center,可以实现元素的垂直水平居中。 ```css .container { display: grid; place-items: center; } ``` 以上是几种常用的CSS垂直水平居中方法,可以根据具体的需求选择合适的方法来实现布局。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值