😄
前端学习
  • 👋Welcome
  • 📖前端基础
    • HTML
      • 基础知识
      • 进阶知识
      • HTML5
    • CSS
      • 基础知识
      • 进阶知识
      • CSS 专题
        • CSS 选择器
        • CSS 布局
        • CSS 动画
        • CSS 画图
        • 响应式方案
        • CSS BEM 规范
        • CSS 案例
    • JavaScript
      • 基础知识
      • 进阶知识
      • 常用内置对象
        • Array 对象
        • String 对象
        • Number 对象
        • Boolean 对象
        • Math 对象
        • Date 对象
        • RegExp 对象
        • Object 对象
      • JS 专题
        • 数据类型
        • 原型链/继承
        • 对象赋值与拷贝
        • this 的指向
        • 异步操作
        • 模块化
        • 设计模式
    • 浏览器
      • 浏览器模型
      • 事件
      • 位置属性
      • Web 缓存
      • 本地存储
    • 综合内容
      • 前端跨域
      • 登录鉴权
      • 文件上传与下载
  • 🏗️前端框架
    • Vue.js
      • 基础知识
      • Vue 组件通信
      • Vuex 使用指南
      • Vue 动画
      • 静态网站框架 VuePress
    • React.js
      • 基础知识
      • 组件通信
  • 📦计算机基础/后端
    • 图解计算机网络
    • HTTP/HTTPS
    • TCP/UDP
    • Node.js
    • MongoDB
  • 🛠️开发工具
    • 版本控制工具-Git
      • git submodule
    • 构建工具-Webpack
    • 错误监控工具-Sentry
    • 单元测试工具-Jest
    • 包管理工具-NPM
    • 代码编辑器-VSCode
  • 🤔专题内容
    • 前端工程化
    • 代码规范
      • JavaScript 代码规范
      • CSS 代码规范
      • Vue 代码规范
      • Git Commit 规范
      • 代码规范配置
    • 网络安全与防御
    • 性能优化
    • 算法编程
    • 数据可视化
  • 🧑‍💻 面试相关
    • 面试知识总结
    • 面试问题总结
    • 面试常见编程
    • 面试资源汇总
  • 🍭其他
    • 项目经验❗️
    • 踩坑指南❗️
      • JavaScript 踩坑指南
      • CSS 踩坑指南
      • Vue 踩坑指南
    • 学习资源
    • 综合收藏夹
由 GitBook 提供支持
在本页
  • 1. 基于 border 属性
  • 2. 基于 skew 属性
  • 3. 基于 border-radius 属性
  • 4. 经典案例
  • 4.1 阴阳图
  • 4.2 对话框
  • 4.3 爱心

这有帮助吗?

  1. 前端基础
  2. CSS
  3. CSS 专题

CSS 画图

上一页CSS 动画下一页响应式方案

最后更新于4年前

这有帮助吗?

参考资料:

1. 基于 border 属性

通过对border设置大小,对内容设置宽高/颜色,可以得到梯形、三角形等。

<style>
  div {
    display: inline-block;
    height: 100px;
    width: 100px;
    border-style: solid;
    border-color: red green yellow blue;
  }
  div:nth-child(1) {
    border-width: 90px;
  }
  div:nth-child(2) {
    border-width: 90px;
    border-left-width: 0;
  }
  div:nth-child(3) {
    width: 0;
    border-width: 90px;
  }
  div:nth-child(4) {
    width: 0;
    height: 0;
    border-width: 140px;
  }
  div:nth-child(5) {
    width: 0;
    height: 0;
    border-width: 140px;
    border-right-color: transparent; /*透明*/
  }
</style>

<div></div>
<div></div>
<div></div>
<div></div>
<div></div>

2. 基于 skew 属性

3. 基于 border-radius 属性

<style>
  div {
    display: inline-block;
    border: 1px solid red;
  }
  div:nth-child(1) {
    height: 100px;
    width: 100px;
    border-radius: 100px;
  }
  div:nth-child(2) {
    height: 100px;
    width: 100px;
    border-radius: 100px 100px 0 0;
  }
  div:nth-child(3) {
    height: 50px;
    width: 100px;
    border-radius: 100px 100px 0 0;
  }
  div:nth-child(4) {
    height: 100px;
    width: 100px;
    border-radius: 50px 0 0 0;
  }
  div:nth-child(5) {
    height: 100px;
    width: 100px;
    border-radius: 100px 0 0 0;
  }
</style>

<div></div>
<div></div>
<div></div>
<div></div>
<div></div>

border-radius 可以用来实现圆形,这个众所周知,但是还有一个重要内容是,border-radius 接受水平和垂直方向不同值,使用斜杠(/)来分隔它们,这可以让我们在圆角处取整来创建椭圆。

<style>
  div {
    display: inline-block;
    border: 1px solid red;
  }
  div:nth-child(1) {
    height: 50px;
    width: 100px;
    border-radius: 100px / 50px;
  }
  div:nth-child(2) {
    height: 100px;
    width: 50px;
    border-radius: 50px / 100px;
  }
  div:nth-child(3) {
    height: 25px;
    width: 100px;
    border-radius: 50px 50px 0 0 / 25px 25px 0 0;
  }
  div:nth-child(4) {
    height: 100px;
    width: 25px;
    border-radius: 25px 0 0 25px / 50px 0 0 50px;
  }
  div:nth-child(5) {
    height: 50px;
    width: 100px;
    border-radius: 100px 0 0 0 / 50px 0 0 0;
  }
</style>

<div></div>
<div></div>
<div></div>
<div></div>
<div></div>

扇形画法是利用 border-radius 并设置透明度来实现的,类似于 CSS 画三角形

<style>
  div {
    display: inline-block;
    height: 0;
    width: 0;
  }
  div:nth-child(1) {
    border: 100px solid red;
    border-radius: 100px;
  }
  div:nth-child(2) {
    border: 100px solid red;
    border-radius: 200px 0 0 0;
  }
  div:nth-child(3) {
    border: 100px solid transparent;
    border-top-color: red;
    border-radius: 100px;
  }
  div:nth-child(4) {
    border: 100px solid transparent;
    border-left-color: red;
    border-top-color: red;
    border-bottom-color: red;
    border-radius: 100px;
  }
</style>

<div></div>
<div></div>
<div></div>
<div></div>

4. 经典案例

4.1 阴阳图

<style>
  div{
    position: relative;
    width: 180px;
    border: 0 solid;
    border-top-width: 90px;
    border-bottom-width: 90px;
    border-style: solid;
    border-top-color: red;
    border-bottom-color: green;
    border-right-color: red;
    border-radius: 90px;
  }
  div::after, div::before{
    content: "";
    position: absolute;
    width: 20px;
    height: 20px;
    border-width: 35px;
    border-style: solid;
    border-radius: 45px;
    top: 50%;
    transform: translateY(-50%);
  }
  div::after {
    background-color: red;
    border-color: green;
    left: 0;
  }
  div::before{
    background-color: green;
    border-color: red;
    right: 0;
  }
</style>

4.2 对话框

<style>
  div {
    display: inline-block;
  }

  div:nth-child(1) {
    width: 160px;
    height: 80px;
    background-color: red;
    border-radius: 6px;
    position: relative;
  }

  div:nth-child(1)::after {
    content: "";
    width: 0;
    height: 0;
    border-width: 10px;
    border-style: solid;
    border-color: transparent;
    border-right-color: red;
    position: absolute;
    top: 16px;
    left: -20px;
  }

  div:nth-child(2) {
    width: 160px;
    height: 80px;
    background-color: red;
    border-radius: 6px;
    position: relative;
  }

  div:nth-child(2)::after {
    content: '';
    position: absolute;
    top: 0;
    right: -20px;
    width: 30px;
    height: 30px;
    border-width: 0 0 30px 30px;
    border-style: solid;
    border-bottom-color: red;
    border-left-color: transparent;
    border-radius: 0 0 60px 0;
  }


</style>

<div></div>
<div></div>

4.3 爱心

<style>
  .heart {
    position: relative;
  }

  .heart::before, .heart::after {
    content: "";
    background-color: red;
    width: 200px;
    height: 300px;
    position: absolute;
    left: 0;
    top: 0;
    border-radius: 100px 100px 0 0;
  }
  .heart::before {
    transform: rotate(-45deg);
  }
  .heart::after {
    left: 71px;
    transform: rotate(45deg);
  }
</style>

<div class="heart"></div>

如果你对内容有任何疑问,欢迎提交 或

📖
❕issues
✉️ email
如何用 CSS 画三角形
倾斜 skew() 方法
纯 CSS 画的基本图形
利用 CSS 样式画各种图形