CSS-----绝对定位盒子水平居中

在CSS中绝对定位的盒子无法通过margin:0 auto;实现水平居中。

正确做法:子盒子通过left属性向右移动父盒子宽度的50%,再通过margin-left负值向左移动自身宽度的50%;

垂直居中同理可得:通过top属性向下移动父盒子高度的一半,再通过margin-top负值向上移动自身个高度的一半。

代码如下:

 .father {
            /* 给父盒子添加相对定位 */
            position: relative;
            height: 400px;
            width: 600px;
            background-color: aquamarine;
            margin: 0 auto;
        }
        
        .son {
            /* 给子盒子添加绝对定位 */
            height: 200px;
            position: absolute;
            width: 300px;
            background-color: bisque;
            /* 使子盒子水平居中 */
            left: 50%;
            margin-left: -150px;
            /* 使得子盒子垂直居中 */
            top: 50%;
            margin-top: -100px;
        }

效果图:

应用:焦点图制作

如图所示:

要实现此图效果,需要熟练使用定位相关知识:

CSS代码如下:

* {
            margin: 0;
            padding: 0;
        }
        
        li {
            list-style: none;
        }
        
        .box {
            position: relative;
            width: 520px;
            height: 280px;
            background-color: pink;
            margin: 100px auto;
        }
        
        .box img {
            width: 520px;
            height: 280px;
        }
        
        .left,
        .right {
            position: absolute;
            top: 50%;
            margin-top: -15px;
            width: 20px;
            height: 30px;
            background: rgba(0, 0, 0, .3);
            text-align: center;
            line-height: 30px;
            color: #fff;
            text-decoration: none;
        }
        
        .left {
            left: 0;
            border-top-right-radius: 15px;
            border-bottom-right-radius: 15px;
        }
        
        .right {
            right: 0;
            border-top-left-radius: 15px;
            border-bottom-left-radius: 15px;
        }
        
        .nav {
            position: absolute;
            bottom: 15px;
            left: 50%;
            margin-left: -90px;
            width: 180px;
            height: 13px;
        }
        
        .nav li {
            background: #000;
            width: 25px;
            height: 5px;
            display: inline-block;
            overflow: hidden;
            margin-left: 5px;
            cursor: pointer;
        }
        
        .nav .selected {
            background-color: brown;
        }

html代码如下:

<div class="box">
        <img src="./images/1.jpg" alt="">
        <a href="#" class="left"> &lt; </a>
        <a href="#" class="right"> &gt; </a>
        <ul class="nav">
            <li class="selected"></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
    </div>

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值