定位之淘宝轮播图——HTML+CSS学习笔记20

本文详细介绍了一种淘宝广告轮播图的实现方法,包括使用相对定位和绝对定位来布局大盒子、图片、按钮和底部小圆点。通过具体的HTML和CSS代码示例,展示了如何使元素垂直居中和水平居中,以及如何利用ul元素创建底部导航圆点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果

在这里插入图片描述

分析

在这里插入图片描述

  1. 大盒子我们类名为: tb-promo 淘宝广告
  2. 里面先放一张图片。
  3. 左右两个按钮 用链接就好了。 左箭头 prev 右箭头 next
  • 那么大盒子采用相对定位,按钮采用绝对定位。
  • 垂直居中如何做到?
    以左边按钮为例子:left:0 top:50% margin-top:15px
  1. 底侧小圆点ul 继续做。 类名为 promo-nav
  • 底部的这个盒子依旧绝对定位,水平居中方式类似于按钮的居中方式。
  • 圆点利用ul做,通过浮动使得圆点排在一行。

代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    * {
            margin: 0;
            padding: 0;
        }
    li {
        list-style: none;       
    }
    .tb-promo {
        position: relative;
        width: 520px;
        height: 280px;
        background-color: pink;
        /* 这里是相对定位,没有脱离标准流 可以用这种方法水平居中*/
        margin: 100px auto;
    }
    .tb-promo img {
        width: 520px;
        height: 280px;
    }
    .next,
    .prev {
        /* 子绝父相 */
        position: absolute;
        /* 绝对定位设置垂直居中 */
        top: 50%;
        margin-top: -15px;
        /* 是定位,可以设置宽度高度,与浮动类似 */
        width: 20px;
        height: 30px;
        background-color: rgba(0, 0, 0, .3);
        color:white;
        text-align: center;
        line-height: 30px;
        text-decoration: none;
        
    }
    .prev {
        left: 0px;
        border-top-right-radius: 15px;
        border-bottom-right-radius: 15px;
    }
    .next {
        right: 0px;
        border-top-left-radius: 15px;
        border-bottom-left-radius: 15px;
    }
    .promo-nav {
        position: absolute;
        /* 绝对定位居中方式 */
        bottom: 15px;
        left: 50%;
        margin-left: -35px;
        width: 70px;
        height: 13px;
        background-color: rgba(255, 255, 255, .3);
        border-radius: 7px;
    }
    .promo-nav li {
        float:left;
        width: 8px;
        height: 8px;
        background-color: white;
        border-radius: 50%;
        margin: 3px;
    }
</style>
<body>
    <div class="tb-promo">
        <img src="pic.jpg" alt="图片">
        <a href="#" class="prev">&lt;</a>
        <a href="#" class="next">&gt;</a>
        <ul class="promo-nav">
            <li class="selected"></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
    </div>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值