定位案例

相对定位

效果图

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0
        }

        ul,
        ol,
        li {
            list-style: none;
        }

        ul {
            width: 600px;
            height: 40px;
            background-color: orange;
            margin: 0 auto;
        }

        ul>li {
            float: left;
            width: 120px;
            height: 40px;
            text-align: center;
            line-height: 40px;
        }

        span {
            width: 0px;
            height: 0px;
            display: inline-block;
            border: 5px solid transparent;
            border-top: 5px solid gray;
            /* 只想要单独移动span;移动一点点 */
            position: relative;
            top: 2px
        }
    </style>
</head>

<body>
    <!-- 制作三角形 -->
    <ul>
        <li>上路1
            <span></span>
        </li>
        <li>中路
            <span></span>
        </li>
        <li>下路
            <span></span>
        </li>
        <li>打野
            <span></span>
        </li>
        <li>辅助
            <span></span>
        </li>
    </ul>
</body>

</html>
绝对定位 - 1

效果图

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0
        }

        ul,
        ol,
        li {
            list-style: none;
        }

        /* 大的盒子 */
        .bigbox {
            width: 600px;
            height: 400px;
            border: 10px solid gray;
            margin: 0 auto;
            /* 父元素添加定位 */
            position: relative;
        }

        /* 强制设置图片的宽度高度 */
        .bigbox>img {
            width: 100%;
            height: 100%;
        }

        p {
            width: 60px;
            height: 100px;
            background-color: rgba(0, 0, 0, .5);
            /* 设置文本样式 */
            text-align: center;
            line-height: 100px;
            font-size: 30px;
            font-weight: bold;
            color: white;
            /* 定位 */
            position: absolute;
        }

        .lef {
            top: 150px;
            left: 0px;
        }

        .rig {
            top: 150px;
            right: 0px;
        }

        .bigbox li {
            width: 15px;
            height: 15px;
            background-color: red;
            margin:3px;
            float: left;
        }
        .bigbox ul{
            position: absolute;
            left:20px;
            bottom:20px
        }
    </style>
</head>

<body>
    <!-- 轮播图案例 
            轮播图里面布局:一个大的盒子,里面有一个图,还有左右两个耳朵, 以及焦点图
            左右耳朵;方向键
            焦点图:点击的时候,跳转到对应的图片上面
    -->
    <div class="bigbox">
        <img src="../img/q.jpg" alt="">
        <!-- 左右两个耳朵 -->
        <p class="lef">&lt;</p>
        <p class="rig">&gt;</p>
        <!-- 焦点图 -->
        <ul>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
    </div>

</body>

</html>
绝对定位 - 2

效果图

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin:0;padding:0
        }
        img{
            vertical-align: middle;
        }
        /* 设置这个大的盒子 */
        .bigbox{
            width: 230px;
            height:290px;
            border:1px solid red;
            /* 居中 */
            margin: 100px auto;
            /* 定位 */
            position: relative;
        }
        /* 控制图片大小 */
        .bigbox>img{
            width: 100%;
            height: 100%;
        }
        .bigbox>div{
            width: 100%;
            height: 88px;
            background-color: rgba(0,0,0,.5);
            position: absolute;
            bottom:0px;
            color:white;
            text-align: center;
        }
        .p1{
            line-height: 42px;
        }
        .p2{
            width: 124px;
            height: 30px;
            border:1px solid white;
            margin: 0 auto;
            line-height: 30px;
        }
    </style>
</head>
<body>
    <div class="bigbox">
        <img src="../img/q.jpg" alt="">
        <div>
            <p class="p1">XXXXXXXXXXXXX</p>
            <p class="p2">查看详情</p>
        </div>
    </div>
</body>
</html>
绝对定位 - 3

效果图

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin:0;padding:0
        }
        .bigbox{
            width: 270px;
            height:270px;
            border:10px solid gray;
            margin:100px auto;
            /* 父元素使用相对定位 */
            position: relative;
        }
        .bigbox>.dog{
            width: 100%;
            height:100%;
        }
        .tu1,.tu2{
            position: absolute;
            left:20px;
            bottom:20px;
        }
        /* 一块划过效果 */
        .tu2{
            display: none;
        }
        .bigbox:hover>.tu2{
            display: block;
        }
        /*第二块划过 */
        .bigbox:hover>.dog{
            opacity: 0.6;
        }
        p{
            position: absolute;
            top:10px;
            /* 隐藏 */
            display: none;
        }
        /* 第三块划过 */
        .bigbox:hover>p{
            display: block;
        }
    </style>
</head>
<body>
    <div class="bigbox">
        <img src="../img/img001.jpg" alt="" class="dog">
        <!-- + -->
        <img src="../img/tu.gif" alt="" class="tu1">
        <img src="../img/tu2.gif" alt="" class="tu2">
        <!-- 文本区域 -->
        <p>
            <img src="../img/logo.gif" alt="">
            马上快要下课了我好开心呀,终于可以抽华子了呀.
        </p>
    </div>
</body>
</html>
绝对定位 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin:0;padding:0
        }
        .bigbox{
            width: 1000px;
            height:800px;
            border: 10px solid gray;
            margin:20px auto;
            position: relative;
        }
        .red{
            width: 200px;
            height:200px;
            background-color: red;
            position: absolute;
            top:50%;
            margin-top:-100px;
            left: 50%;
            margin-left: -100px;

            
        }
    </style>
</head>
<body>
    <!-- 面试题: 要求让一个子元素,位于父元素里面是水平垂直正居中 
        使用定位: 父相子绝;
        1)给父元素添加相对定位
        2)给子元素添加绝对定位
            top:50%;
            left:50%;
            margin-top:-高度一半;
            margin-left:-宽度一半;
    
    -->
    <div class="bigbox">
        <div class="red"></div>
    </div>
</body>
</html>
固定定位

效果图

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin:0;padding:0
        }
        .bigbox{
            width: 1000px;
            height: 1500px;
            background-color: aqua;
            margin:0 auto;
        }
        .chat{
            width: 300px;
            height: 300px;
            background-color: orange;
            text-align: center;
            position: fixed;
            /* 偏移属性 */
            bottom:20px;
            right: 20px;
        }
    </style>
</head>
<body>
    <div class="bigbox"></div>
    <!-- 切记,所有的固定定位的效果,都要再布局结束后再去添加 -->
    <div class="chat">
        <h1>*****聊天窗口</h1>
    </div>
</body>
</html>
案例

效果图

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style> 
        *{
            margin:0;padding:0
        }
        ul,ol,li{
            list-style: none;
        }
        .bigbox{
            width: 1000px;
            height:400px;
            border:10px solid gray;
            margin:0 auto;

        }
        .bigbox>ul{
            width: 200px;
            height:400px;
            background-color: red;
            /* 浮动 */
            float: left;
            position: relative;
        }
        .bigbox>div{
            width: 800px;
            height: 400px;
            background-color: orange;
            float: left;
        }
        /* 设置左侧列表样式 */
        .bigbox>ul>li{
            line-height: 80px;
            height:80px;
            text-align: center;
            
        }
        /* 设置二级菜单 */
        .bigbox>ul>li div{
            width: 800px;
            height:400px;
            font-size: 100px;
            font-weight: bold;
            background-color: green;
            position: absolute;
            left: 200px;
            top:0px;
            line-height: 400px;
            /* 都需要隐藏 */
            display: none;

        }
        /* 鼠标划过li的时候让二级显示 */
        .bigbox>ul>li:hover>div{
            display: block;
        }
    </style>
</head>
<body>
    <div class="bigbox">
        <!-- 左侧列表 -->
        <ul>
            <li>电脑
                <div>我是电脑区域</div>
            </li>
            <li>笔记本
                <div>笔记本电脑区域</div>
            </li>
            <li>千锋教育
                <div>千锋教育区域</div>
            </li>
            <li>键盘</li>
            <li>鼠标</li>
        </ul>
        <!-- 右侧轮播图 -->
        <div></div>
    </div>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值