day 55 定位流(相对,绝对,固定)

    科技2024-11-27  20

    本周内容: day01 css 定位流:相对定位、绝对定位、固定定位 js ECMAScript=》语法 BOM=》控制浏览器 day02 DOM=》控制文档内容的动态效果 案列 https://www.cnblogs.com/linhaifeng/articles/9472477.html#_label2 day03 jquery bootstrap框架

    1.相对定位

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .box1 { width: 200px; height: 200px; background-color: red; /*display: inline-block;*/ } .box2 { width: 200px; height: 200px; background-color: green; /*position: relative;*/ /*top: 100px;*/ /*bottom: 10px;*/ /*left: 10px;*/ /*right: 10px;*/ /*margin-top: 10px;*/ /* display: inline-block;*/ } .box3 { /*width: 200px;*/ /*height: 200px;*/ background-color: blue; /*display: inline-block;*/ } img { height: 50px; position: relative; top: 22px; left: 10px; } input { height: 50px; } </style> </head> <body> <div class="box1"></div> <div class="box2"></div> <div class="box3"></div> <input type="text"> <img src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1600660576985&di=2d70af39c234727def903cbdcc0d6e92&imgtype=0&src=http%3A%2F%2Fwww.zhuangjiba.com%2Fd%2Ffile%2Fhelp%2F2018%2F08%2Fcfdefaddb3f47d78f8c66a7de28720aa.png" alt=""> </body> </html>

    2.绝对定位

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .box1 { width: 200px; height: 200px; background-color: red; } .box2 { width: 200px; height: 200px; background-color: green; position: absolute; top: 50px; left: 50px; } .box3 { width: 300px; height: 300px; background-color: blue; position: relative; } </style> </head> <body> <div class="box1"></div> <div class="box3"> <div class="box2"></div> </div> </body> </html>

    练习

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> ul { list-style: none; } ul>li { float: left; width: 100px; height: 50px; background-color: gray; text-align: center; line-height: 50px; } ul>li:hover { background-color: green; } .bg { background-image: url("代码/1.png"); background-repeat: no-repeat; background-size: 30px; background-position: right top; } ul>li:nth-child(4) { position: relative; } img { position: absolute; width: 30px; left: 30px; top: -3px; } </style> </head> <body> <ul> <li>服装城</li> <li>服装城</li> <li>服装城</li> <li> 服装城 <img src="1.png" alt=""> </li> <li>服装城</li> <li>服装城</li> <li>服装城</li> </ul> <div style="width: 200px;height: 200px;background-color: red;clear: both"> asdfsafadsf <p>哈哈哈</p> </div> </body> </html>

    2.绝对定位-以body首屏作为参考点

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .box1 { width: 200px; height: 200px; background-color: red; } .box2 { width: 2000px; height: 200px; background-color: green; } .box3 { width: 300px; height: 3000px; background-color: blue; position: relative; } span { display: inline-block; width: 100px; height: 100px; background-color: pink; text-align: center; line-height: 100px; border-radius: 50%; position: absolute; bottom: 0; right: 0; } </style> </head> <body> <div class="box1"></div> <div class="box2"></div> <div class="box3"></div> <span>回到顶部</span> </body> </html>

    3.绝对定位居中

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .box1 { width: 200px; height: 200px; background-color: red; } .box2 { width: 50px; height: 50px; background-color: green; position: absolute; left: 50%; margin-left: -25px; top: 50%; margin-top: -25px; } .box3 { width: 300px; height: 300px; background-color: blue; position: relative; } </style> </head> <body> <div class="box1"></div> <div class="box3"> <div class="box2"></div> </div> </body> </html>

    3.忽略父级的padding属性

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .box1 { width: 200px; height: 200px; background-color: red; } .box2 { width: 200px; height: 200px; background-color: green; position: absolute; top:10px; left:10px; } .box3 { width: 300px; height: 300px; background-color: blue; position: relative; padding: 50px; } </style> </head> <body> <div class="box1"></div> <div class="box3"> <div class="box2"></div> </div> </body> </html>

    3.固定定位

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .box1 { width: 200px; height: 200px; background-color: red; } .box2 { width: 2000px; height: 200px; background-color: green; } .box3 { width: 300px; height: 3000px; background-color: blue; } a { width: 100px; height: 100px; background-color: pink; text-align: center; line-height: 100px; border-radius: 50%; position: fixed; bottom: 0; right: 0; text-decoration: none; } </style> </head> <body> <div class="box1"></div> <div class="box2"></div> <div class="box3"></div> <a href="#">回到顶部</a> </body> </html>

    4. z-index属性(防止覆盖效果)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .fathter1 { width: 300px; height: 300px; background-color: red; position: relative; z-index: 10; } .son1 { width: 100px; height: 100px; background-color: yellow; position: absolute; bottom: -10px; right: -50px; z-index: 3; } .fathter2 { width: 300px; height: 300px; background-color: green; position: relative; z-index: 1; } .son2 { width: 100px; height: 100px; background-color: blue; position: absolute; top: -20px; right: -10px; z-index: 4; } </style> </head> <body> <div class="fathter1"> <div class="son1"></div> </div> <div class="fathter2"> <div class="son2"></div> </div> <form action=""> <input type="text" name="xx"> <input type="submit" value="1111"> </form> </body> </html>

    5.js的引入方式

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .box1 { width: 200px; height: 200px; background-color: red; } </style> </head> <body> <div class="box1"></div> <script src="1.js"></script> </body> </html>
    Processed: 0.011, SQL: 8