• CSS高级的详细解析


    CSS高级

    目标:掌握定位的作用及特点;掌握 CSS 高级技巧

    01-定位

    作用:灵活的改变盒子在网页中的位置

    实现:

    1.定位模式:position

    2.边偏移:设置盒子的位置

    • left

    • right

    • top

    • bottom

    相对定位

    position: relative

    特点:

    • 不脱标,占用自己原来位置

    • 显示模式特点保持不变

    • 设置边偏移则相对自己原来位置移动

      1. div {
      2.  position: relative;
      3.  top: 100px;
      4.  left: 200px;
      5. }

     
    

    绝对定位

    position: absolute

    使用场景:子级绝对定位,父级相对定位(子绝父相

    特点:

    • 脱标,不占位

    • 显示模式具备行内块特点

    • 设置边偏移则相对最近的已经定位的祖先元素改变位置

    • 如果祖先元素都未定位,则相对浏览器可视区改变位置

    1. .father {
    2.  position: relative;
    3. }
    4. .father span {
    5.  position: absolute;
    6.  top: 0;
    7.  right: 0;
    8. }

    定位居中

    实现步骤:

    1. 绝对定位

    2. 水平、垂直边偏移为 50%

    3. 子级向左、上移动自身尺寸的一半

    • 左、上的外边距为 –尺寸的一半

    • transform: translate(-50%, -50%)

    1. img {
    2.  position: absolute;
    3.  left: 50%;
    4.  top: 50%;
    5.  /* margin-left: -265px;
    6. margin-top: -127px; */
    7.  /* 方便: 50% 就是自己宽高的一半 */
    8.  transform: translate(-50%, -50%);
    9. }

    固定定位

    position: fixed

    场景:元素的位置在网页滚动时不会改变

    特点:

    • 脱标,不占位

    • 显示模式具备行内块特点

    • 设置边偏移相对浏览器窗口改变位置

    1. div {
    2.  position: fixed;
    3.  top: 0;
    4.  right: 0;
    5.  width: 500px;
    6. }

    堆叠层级z-index

    默认效果:按照标签书写顺序,后来者居上

    作用:设置定位元素的层级顺序,改变定位元素的显示顺序

    属性名:z-index

    属性值:整数数字(默认值为0,取值越大,层级越高)

    1. .box1 {
    2.  background-color: pink;
    3.  /* 取值是整数,默认是0,取值越大显示顺序越靠上 */
    4.  z-index: 1;
    5. }
    6. .box2 {
    7.  background-color: skyblue;
    8.  left: 100px;
    9.  top: 100px;
    10.  z-index: 2;
    11. }

    02-高级技巧

    CSS精灵

    CSS 精灵,也叫 CSS Sprites,是一种网页图片应用处理方式。把网页中一些背景图片整合到一张图片文件中,再background-position 精确的定位出背景图片的位置。

    优点:减少服务器被请求次数,减轻服务器的压力,提高页面加载速度

    实现步骤:

    1. 创建盒子,盒子尺寸小图尺寸相同

    2. 设置盒子背景图为精灵图

    3. 添加 background-position 属性,改变背景图位置

    3.1 使用 PxCook 测量小图片左上角坐标

    3.2 取负数坐标为 background-position 属性值(向左上移动图片位置)

    案例-京东服务

    HTML结构
    1. <div class="service">
    2.  <ul>
    3.    <li>
    4.      <h5>h5>
    5.      <p>品类齐全,轻松购物p>
    6.    li>
    7.    <li>
    8.      <h5>h5>
    9.      <p>多仓直发,极速配送p>
    10.    li>
    11.    <li>
    12.      <h5>h5>
    13.      <p>正品行货,精致服务p>
    14.    li>
    15.    <li>
    16.      <h5>h5>
    17.      <p>天天低价,畅选无忧p>
    18.    li>
    19.  ul>
    20. div>
    CSS样式
    1. <style>
    2. * {
    3.    margin: 0;
    4.    padding: 0;
    5.    box-sizing: border-box;
    6. }
    7.  li {
    8.    list-style: none;
    9. }
    10.  .service {
    11.    margin: 100px auto;
    12.    width: 1190px;
    13.    height: 42px;
    14.    /* background-color: pink; */
    15. }
    16.  .service ul {
    17.    display: flex;
    18. }
    19.  .service li {
    20.    display: flex;
    21.    padding-left: 40px;
    22.    width: 297px;
    23.    height: 42px;
    24.    /* background-color: skyblue; */
    25. }
    26.  .service li h5 {
    27.    margin-right: 10px;
    28.    width: 36px;
    29.    height: 42px;
    30.    /* background-color: pink; */
    31.    background: url(./images/sprite.png) 0 -192px;
    32. }
    33.  .service li:nth-child(2) h5 {
    34.    background-position: -41px -192px;
    35. }
    36.  .service li:nth-child(3) h5 {
    37.    background-position: -82px -192px;
    38. }
    39.  .service li:nth-child(4) h5 {
    40.    background-position: -123px -192px;
    41. }
    42.  .service li p {
    43.    font-size: 18px;
    44.    color: #444;
    45.    font-weight: 700;
    46.    line-height: 42px;
    47. }
    48. style>

    字体图标

    字体图标:展示的是图标,本质是字体

    作用:在网页中添加简单的、颜色单一的小图标

    优点

    • 灵活性:灵活地修改样式,例如:尺寸、颜色等

    • 轻量级:体积小、渲染快、降低服务器请求次数

    • 兼容性:几乎兼容所有主流浏览器

    • 使用方便:先下载再使用

    下载字体

    iconfont 图标库:iconfont-阿里巴巴矢量图标库

    登录 → 素材库 → 官方图标库 → 进入图标库 → 选图标,加入购物车 → 购物车,添加至项目,确定 → 下载至本地

    使用字体
    1. 引入字体样式表(iconfont.css)

    1. 标签使用字体图标类名

      • iconfont:字体图标基本样式(字体名,字体大小等等)

      • icon-xxx:图标对应的类名

    上传矢量图

    作用:项目特有的图标上传到 iconfont 图标库,生成字体

    上传步骤:上传 → 上传图标 → 选择 svg 矢量图,打开 → 提交 → 系统审核

    03-CSS修饰属性

    垂直对齐方式

    属性名:vertical-align

    过渡

    作用:可以为一个元素在不同状态之间切换的时候添加过渡效果

    属性名:transition(复合属性)

    属性值:过渡的属性 花费时间 (s)

    提示:

    • 过渡的属性可以是具体的 CSS 属性

    • 也可以为 all(两个状态属性值不同的所有属性,都产生过渡效果)

    • transition 设置给元素本身

    1. img {
    2.   width: 200px;
    3.   height: 200px;
    4.   transition: all 1s;
    5. }
    6. img:hover {
    7.   width: 500px;
    8.   height: 500px;
    9. }

    透明度opacity

    作用:设置整个元素的透明度(包含背景和内容)

    属性名:opacity

    属性值:0 – 1

    • 0:完全透明(元素不可见)

    • 1:不透明

    • 0-1之间小数:半透明

    光标类型cursor

    作用:鼠标悬停在元素上时指针显示样式

    属性名:cursor

    04-综合案例-轮播图

    图片效果

    HTML结构
    1. <div class="banner">
    2.    
    3.    <ul>
    4.      <li><a href="#"><img src="./images/banner1.jpg" alt="">a>li>
    5.      <li><a href="#"><img src="./images/banner2.jpg" alt="">a>li>
    6.      <li><a href="#"><img src="./images/banner3.jpg" alt="">a>li>
    7.    ul>
    8. div>
    CSS样式
    1. * {
    2.  margin: 0;
    3.  padding: 0;
    4.  box-sizing: border-box;
    5. }
    6. li {
    7.  list-style: none;
    8. }
    9. .banner {
    10.  position: relative;
    11.  margin: 100px auto;
    12.  width: 564px;
    13.  height: 315px;
    14.  /* background-color: pink; */
    15.  overflow: hidden;
    16. }
    17. /* 图片 */
    18. .banner img {
    19.  width: 564px;
    20.  border-radius: 12px;
    21.  vertical-align: middle;
    22. }
    23. .banner ul {
    24.  display: flex;
    25. }

    箭头

    HTML结构
    1. <a href="#" class="prev">
    2.  <i class="iconfont icon-zuoce">i>
    3. a>
    4. <a href="#" class="next">
    5.  <i class="iconfont icon-youce">i>
    6. a>
    CSS样式
    1. /* 箭头 */
    2. .banner .prev,
    3. .banner .next {
    4.  /* 隐藏 */
    5.  display: none;
    6.  position: absolute;
    7.  top: 50%;
    8.  transform: translateY(-50%);
    9.  width: 20px;
    10.  height: 30px;
    11.  background-color: rgba(0,0,0, 0.3);
    12.  text-decoration: none;
    13.  color: #fff;
    14.  line-height: 30px;
    15. }
    16. /* 鼠标滑到banner区域,箭头要显示 display:block */
    17. .banner:hover .prev,
    18. .banner:hover .next {
    19.  display: block;
    20. }
    21. .banner .prev {
    22.  left: 0;
    23.  border-radius: 0 15px 15px 0;
    24. }
    25. .banner .next {
    26.  right: 0;
    27.  border-radius: 15px 0 0 15px;
    28.  text-align: center;
    29. }

    圆点

    HTML结构
    1. <ol>
    2.  <li>li>
    3.  <li class="active">li>
    4.  <li>li>
    5. ol>
    CSS样式
    1. /* 圆点 */
    2. .banner ol {
    3.  position: absolute;
    4.  bottom: 20px;
    5.  left: 50%;
    6.  transform: translateX(-50%);
    7.  height: 13px;
    8.  background-color: rgba(255,255,255,0.3);
    9.  display: flex;
    10.  border-radius: 10px;
    11. }
    12. .banner ol li {
    13.  margin: 3px;
    14.  width: 8px;
    15.  height: 8px;
    16.  background-color: #fff;
    17.  border-radius: 50%;
    18.  cursor: pointer;
    19. }
    20. /* 橙色的li */
    21. .banner ol .active {
    22.  background-color: #ff5000;
    23. }
  • 相关阅读:
    windows环境python2.7安装pyinstaller
    shell中的条件判断
    ActiveRecord::Migration.maintain_test_schema!
    Flask的一种启动方式和三种托管方式
    leetcode 503. Next Greater Element II 下一个更大元素 II(中等)
    Oracle 误删表后数据恢复操作
    React报错之Type ‘() => JSX.Element[]‘ is not assignable to type FunctionComponent
    德克萨斯大学奥斯汀分校自然语言处理硕士课程汉化版(第八周) - 现代大语言模型
    以太坊合并进展及合并后的 MEV 与矿工
    面试字节,过关斩将到 3 面,结果找了个架构师来吊打我?
  • 原文地址:https://blog.csdn.net/qq_69748833/article/details/134042366