博客列表 >定位以及三行三列布局方式

定位以及三行三列布局方式

guyuqing
guyuqing原创
2021年07月04日 18:24:371106浏览

1.固定定位

特点:

  • position: fixed;
  • 固定定位总是相对于html,这样就省去像绝对定位需要一个定位元素当父级的麻烦

实例效果

代码

代码结构

qq.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>在线QQ客服</title>
  8. <link rel="stylesheet" href="./qq.css">
  9. <link rel="stylesheet" href="modal.css" />
  10. </head>
  11. <body>
  12. <header>
  13. <h1>首页</h1>
  14. </header>
  15. <div class="modal">
  16. <div class="modal-backdrop"></div>
  17. <div class="modal-body">
  18. <h2>点击下方在线咨询</h2>
  19. <span class="iconfont icon-QQzaixiankefu"></span>
  20. </div>
  21. </div>
  22. </body>
  23. </html>

qq.css

  1. @import url("./icons/iconfont.css");
  2. .icon-QQzaixiankefu{
  3. font-size: 3em;
  4. background-color: orange;
  5. }

modal.css

  1. * {
  2. margin: 0;
  3. padding: 0;
  4. box-sizing: border-box;
  5. }
  6. header {
  7. background-color: #1fa28a;
  8. /* padding: 上下 左右 */
  9. padding: 1em 2em;
  10. overflow: hidden;
  11. }
  12. header h1 {
  13. text-align: center;
  14. }
  15. .modal {
  16. position: relative;
  17. }
  18. .modal .modal-backdrop{
  19. position: fixed;
  20. top: 0;
  21. left: 0;
  22. right: 0;
  23. bottom: 0;
  24. background-color: rgb(0, 0, 0, 0.5);
  25. }
  26. .modal .modal-body {
  27. border: 3px solid;
  28. padding: 2em;
  29. margin: 1em;
  30. background-color: rgb(246, 255, 224);
  31. position: fixed;
  32. left: 15em;
  33. right: 15em;
  34. top: 10em;
  35. text-align: center;
  36. }

2.绝对定位

PS:实例内容为传统三行三列定位布局

特点:

  • position: absolute;
  • 绝对定位必须要一个定位父级元素(定位元素)
  • 通过 position: relative;元素转为定位元素

页面显示

代码

html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>传统定位布局</title>
  8. <link rel="stylesheet" href="./modal1.css">
  9. </head>
  10. <body>
  11. <header>页眉</header>
  12. <div class=container>
  13. <aside>左侧</aside>
  14. <main>中间</main>
  15. <aside>右侧</aside>
  16. </div>
  17. <footer>页脚</footer>
  18. </body>
  19. </html>

css

  1. * {
  2. margin: 0;
  3. padding: 0;
  4. box-sizing: border-box;
  5. }
  6. :root {
  7. font-size: 10px;
  8. }
  9. body {
  10. font-size: 1.6rem;
  11. }
  12. header,footer {
  13. min-height: 10rem;
  14. background-color: rgba(173, 216, 230, 0.884);
  15. }
  16. .container {
  17. margin: 1rem 0rem;
  18. min-height: calc(100vh - 22rem);
  19. position: relative;
  20. }
  21. .container aside {
  22. background-color: rgba(255, 255, 0, 0.385);
  23. width: 15rem;
  24. position: absolute;
  25. min-height: inherit;
  26. }
  27. .container aside:last-of-type{
  28. right: 0rem;
  29. top: 0rem;
  30. }
  31. .container main{
  32. position: absolute;
  33. left: 16rem;
  34. right: 16rem;
  35. background-color: rgb(10, 104, 10, 0.45);
  36. min-height: inherit;
  37. }
上一条:0630作业下一条:前端学习第一课
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议