当前位置: 首页 > news >正文

【CSS揭秘】笔记

目录

    • 背景与边框
      • 半透明边框
      • 多重边框
      • 灵活的背景定位
      • 条纹背景
      • 复杂的背景图像
    • 形状
      • 自适应椭圆
      • 平行四边形
    • 视觉效果
      • 不规则投影
    • 字体排印
      • 自定义下划线
    • 用户体验
      • 选用合适的鼠标光标

背景与边框

半透明边框

假设我们想给一个容器设置一层白色背景和一道半透明的白色边框,body的背景会从它的半透明边框透上来。我们最开始的尝试可能是这样:

background: #fff; <br/>// border: 10px solid hsla(0, 0%, 100%, 0.5);<br/>
border: 10px solid rgba(255, 255, 255,0.5);<br/>

但是效果却不是我们所想要的效果,我们的边框去哪里了
在这里插入图片描述


我可以通过background-clip属性来调整上述默认行为所带来的不变,这个属性的初始值是border-box,这个值意味着背景会被元素的外边距裁剪掉,但是可以通过设置background-clip属性为padding-box,这样就是用元素的内边距把背景裁切掉:

  background: #fff;// border: 10px solid hsla(0, 0%, 100%, 0.5);border: 10px solid rgba(255, 255, 255,0.5);background-clip: padding-box;

在这里插入图片描述

多重边框

【box-shadow方案:使用它的第四个参数,可以让投影面积加大或减少,得到的“投影”就像一道边框】

background: #8fe603;
box-shadow: 0 0 10px #655,0 0 0 10px deeppink,0 2px 5px 15px rgba(0,0,0,.6)

在这里插入图片描述

【outline方案:你可以只需要两层边框,可以先设置一层常规边框,再加上outline(描边)属性来产生外层的边框】

  background: #8fe603;border: 10px solid red;outline: 5px dashed #000;outline-offset: 10px; // 控制它跟元素边缘之间的间距

在这里插入图片描述

灵活的背景定位

举例说明,如果想要让背景图片跟右边缘保持20px的偏移量,同时跟底部保持10px的偏移量。

【background-position方案】

background:url('../assets/logo.png') no-repeat #0a0a0a;
background-position: right 20px bottom 10px;

思考:right,bottom是那个的右下角?
每个元素身上都存在三个矩形框:border box(边框的外沿框),padding box(内边距的外沿框),content box(内容外沿框)。
默认情况下,background-position是以padding box为参照的,这样边框才不会遮住背景图片。因此right,bottom默认指的是padding box的右下角。不过在背景与边框(第三版)中,我们得到了一个新的属性:background-origin,它可以设置背景图片的参照物。background-origin属性有三个值:padding-box(默认值),border-box,content-box。

【calc()方案】

background:url('../assets/logo.png') no-repeat #0a0a0a;
background-position: calc(100% - 20px) calc(100% - 10px);

条纹背景

【水平条纹】

  background: linear-gradient(#fb3 50%, #58a 50%); // 或者 background: linear-gradient(to bottom,#fb3 50%, #58a 50%); background-size: 100% 30px;

在这里插入图片描述

【垂直条纹】

  background: linear-gradient(to right,#fb3 50%, #58a 50%);background-size: 30px 100%;

在这里插入图片描述

【斜向条纹】

  background: linear-gradient(45deg, #fb3 50%, #58a 50%);background-size: 100% 50%;

在这里插入图片描述

background: linear-gradient(45deg,#fb3 25%,#58a 0,#58a 50%,#fb3 0,#fb3 75%,#58a 0);
background-size: 30px 30px;

在这里插入图片描述

复杂的背景图像

【网格】

  background:rgb(2, 2, 2);background-image: linear-gradient(white 1px,transparent 0),linear-gradient(90deg,rgb(235, 13, 13) 1px,transparent 0);background-size: 30px 30px;

在这里插入图片描述

【波点】

  background-image: radial-gradient(tan 30%,transparent 0),radial-gradient(tan 30%,transparent 0);background-size: 30px 30px;background-position: 0 0,15px 15px;

在这里插入图片描述

【棋盘】

    background: #eee;background-image: linear-gradient(45deg, #bbb 25%, transparent 0),linear-gradient(45deg, transparent 75%, #bbb 0),linear-gradient(45deg, #bbb 25%, transparent 0),linear-gradient(45deg, transparent 75%, #bbb 0);background-position: 0 0, 25px 25px, 25px 25px, 50px 50px;background-size: 50px 50px;

在这里插入图片描述

形状

自适应椭圆

如果我们有一个尺寸为 200px X 150px的元素,就可以把它圆角的两个半经值分别指定为元素宽高的一半,从而得到一个精准的椭圆:

  width: 200px;height: 150px;border-radius: 100px / 75px;

但是,这段代码存在很大的缺陷,只要元素的尺寸发生变化,border-radius属性的值就得跟着改变。因此有更好的解决方法,使用百分比:border-radius: 50%;

【半椭圆】

  width: 200px;height: 150px;border: 1px solid;border-radius: 50%/100% 100% 0 0;

在这里插入图片描述

【四分之一椭圆】

  width: 200px;height: 150px;border: 1px solid;border-radius: 100% 0  0 0;

在这里插入图片描述

平行四边形

  width: 200px;height: 150px;border: 1px solid;transform: skewX(-45deg);

在这里插入图片描述

但是,这导致它的内容也发生了斜向变形,不好看,有没有办法只让容器的形状倾斜,而保持其内容不变呢?
1,内容再应用一次skew()变形;

2,使用伪元素

.test {position: relative;width: 200px;height: 150px;
}
.test::before { content: "";top: 0;right: 0;bottom: 0;left: 0;border: 1px solid;position: absolute;transform: skewX(-45deg);
}

视觉效果

不规则投影

// drop-shadow(offset-x offset-y blur-radius color);
filter: drop-shadow(2px 2px 5px rgba(231, 7, 7, 0.5));

在这里插入图片描述

字体排印

自定义下划线

【方法一】

  width: 50px;border-bottom:1px solid red 

;

在这里插入图片描述

【方法二】

  width: 50px;box-shadow: 0 -1px gray inset;

在这里插入图片描述

box-shadow: x-offset y-offset blur-radius spread-radius color [inset];
x-offset:水平方向偏移量,正值向右,负值向左。
y-offset:垂直方向偏移量,正值向下,负值向上。
blur-radius:模糊半径,值越大越模糊,默认为 0 表示无模糊。
spread-radius:阴影扩大/缩小,正值阴影变大,负值变小(可选)。
color:阴影颜色(可选,默认为黑色)。
inset:可选值,表示阴影是向内的(内部阴影),如果不加则默认是外阴影。

【方法三】

  width: 50px;background: linear-gradient(90deg, red 66%, transparent 0) repeat-x;background-size: 100% 1px;background-position: 0 1.5em;

在这里插入图片描述

【方法一】和【方法二】会阻止正常的文本换行行为,最佳方法是使用【方法三】。

用户体验

选用合适的鼠标光标

cursor: pointer;

在这里插入图片描述
在这里插入图片描述

http://www.lryc.cn/news/579536.html

相关文章:

  • Ubuntu20.4编译AOSP源码实践
  • 开源 C# .net mvc 开发(六)发送邮件、定时以及CMD编程
  • XILINX Ultrascale+ Kintex系列FPGA的架构
  • 支持向量机(SVM)分类
  • ReactNative【实战系列教程】我的小红书 3 -- 自定义底栏Tab导航(含图片选择 expo-image-picker 的使用)
  • GPT-2论文阅读:Language Models are Unsupervised Multitask Learners
  • Mac电脑 触摸板增强工具 BetterTouchTool
  • 探秘展销编辑器:相较于传统展销的卓越优势与甄选指南​
  • Redis实现哨兵模式
  • MCP协议打破数据孤岛
  • 在Ubuntu24上安装ollama
  • VsCode 配置 C/C++ 开发环境
  • 【第三章:神经网络原理详解与Pytorch入门】01.神经网络算法理论详解与实践-(3)神经网络中的前向传播、反向传播的原理与实现
  • JavaScript的初步学习
  • 2021/7 N2 jlpt 词汇
  • npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree
  • Apache POI 详解 - Java 操作 Excel/Word/PPT
  • docker-compose一键部署全栈项目。springboot后端,react前端
  • 如何将信息从 iPhone 同步到Mac(完整步骤和示意图)
  • mac 电脑安装Homebrew来安装npm与node成功后,安装nvm的流程
  • MySQL 8.0 OCP 1Z0-908 题目解析(19)
  • 标准测试测试数据STDF学习笔记
  • MediaCrawler:强大的自媒体平台爬虫工具
  • Spring Boot 多 ActiveMQ 通道配置与多连接消息发送实战(含完整示例与踩坑记录)
  • Ubuntu 24.04 LTS 服务器配置:安装 JDK、Nginx、Redis。
  • 一体机电脑为何热度持续上升?消费者更看重哪些功能?
  • 关于系统无法找到 arm-linux-gcc 命令,这表明你的环境中尚未安装 ARM 交叉编译工具链。以下是详细的解决方案:(DIY机器人工房)
  • 牛客:HJ16 购物单【01背包】【华为机考】
  • 封装 获取paramsByKey 方法
  • 毕业设计(启智模块化机器人的组装与K5的使用