css - 如何仅使用 CSS 和一个元素来编写此菜单图标?

标签 css linear-gradients

CSS menu icon

到目前为止我已经尝试过。

body {
  margin:0;
  padding:20px;
  background-color: #000;
}

.mobil-menu__icon {
  height: 50px;
  width: 50px;
  background: 
    linear-gradient(to bottom, #fff 0%, #fff 20%, transparent 20%, transparent 100%),
    linear-gradient(to bottom, transparent 0%, transparent 40%, #fff 40%, #fff 60%, transparent 60%, transparent 100%),
    linear-gradient(to bottom, transparent 0%, transparent 80%, #fff 80%, #fff 100%);
}
<div class="mobil-menu__icon"></div>

最佳答案

仅在渐变中使用纯色并依赖background-size:

.mobil-menu__icon {
  height: 50px;
  width: 50px;
  background:
    /*                         position    / width height */
    linear-gradient(#fff,#fff) top    left / 100%  20%,
    linear-gradient(#fff,#fff) center left / 80%   20%,
    linear-gradient(#fff,#fff) bottom left / 60%   20%,
    red;
  border:10px solid red;
  background-repeat:no-repeat; /* Never forget this! */
}
<div class="mobil-menu__icon"></div>

带有悬停动画:

.mobil-menu__icon {
  height: 50px;
  width: 50px;
  background:
    linear-gradient(#fff,#fff) top    left,
    linear-gradient(#fff,#fff) center left,
    linear-gradient(#fff,#fff) bottom left,
    red;
  background-size:
    100% 20%,
    80%  20%,
    60%  20%;
  border:10px solid red;
  background-repeat:no-repeat;
  transition:0.3s all;
}
.mobil-menu__icon:hover {
  background-size:100% 20%;
}
<div class="mobil-menu__icon"></div>

如果你想要透明度:

.mobil-menu__icon {
  height: 50px;
  width: 50px;
  background:
    linear-gradient(red,red) 0    calc(1*100%/4) / 100%  20%,
    linear-gradient(red,red) 0    calc(3*100%/4) / 100%  20%,
    
    linear-gradient(red,red) 100% calc(2*100%/4) / 20%  20%,
    linear-gradient(red,red) 100% calc(4*100%/4) / 40%  20%;
  border:10px solid red;
  background-repeat:no-repeat; 
}

body {
  background:repeating-linear-gradient(to right,white 0 5px,grey 10px);
}
<div class="mobil-menu__icon"></div>

您的代码也几乎很好,但您缺少大小和重复:

body {
  margin: 0;
  padding: 20px;
  background-color: #000;
}

.mobil-menu__icon {
  height: 50px;
  width: 50px;
  background: 
    linear-gradient(to bottom, #fff 0%, #fff 20%, transparent 20%, transparent 100%), 
    linear-gradient(to bottom, transparent 0%, transparent 40%, #fff 40%, #fff 60%, transparent 60%, transparent 100%), 
    linear-gradient(to bottom, transparent 0%, transparent 80%, #fff 80%, #fff 100%);
   background-size:100% 100%,80% 100%, 60% 100%;
   background-repeat:no-repeat;
}
<div class="mobil-menu__icon"></div>

<小时/>

有关不同值的更多详细信息的相关问题: Using percentage values with background-position on a linear gradient

关于css - 如何仅使用 CSS 和一个元素来编写此菜单图标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56872934/

相关文章:

css - 尝试从另一侧开始重新创建线性渐变效果

css - React - 如何在 Prop 值上应用带有浏览器前缀的渐变?

css - 在 bootstrap col 中定位绝对 div

javascript - 溢出属性在 Amazon Kindle Fire WebView 中不起作用

CSS 居中固定大小的 div 内左对齐的任何文本

css - Bug 或 Intention - 固定 CSS 渐变被裁剪为 50%

html - 为容器设置 CSS 渐变

css - 如何在某些文本下方使用线性渐变?

html - 滚动条没有完全向下滚动页面

javascript - 在 JavaScript 中-选择具有相同类的所有元素-除了单击的元素