具有不同线宽的CSS汉堡菜单图标

5
我已经创建了一个简单的汉堡菜单图标,其中我希望中间的线比其他两条线稍微短一些。是否可以 创建多个 div 来完成这个操作?我的当前解决方案是通过多个 box-shadows 实现的,请参考我的工作示例
以下是我想要实现的效果和我目前拥有的效果对比: enter image description here 我的 CSS:
.menu-button:before {
    content: "";
    position: absolute;
    left: 20px;
    top: 20px;
    width: 24px;
    height: 4px;
    background: #0e3c89;
    box-shadow: 0 8px 0 0 #0e3c89, 0 16px 0 0 #0e3c89;
}

谢谢!

3个回答

5

是的,这可以在不添加额外标记的情况下完成。以下是一种可能的方法:

.menu-button {
    width: 20px;
    height: 4px;
    background: #0e3c89;
    margin: 20px 0 0 20px;
}

.menu-button:before {
    display: block;
    content: "";
    width: 28px;
    height: 4px;
    box-shadow: 0 -8px 0 0 #0e3c89, 0 8px 0 0 #0e3c89;
}
<div class="menu-button"></div>


4
以下是关于如何用最少的标记创造这个形状的方法:
  1. 创建一个具有特定宽度/高度的元素,带有上/下边框。
  2. 使用 linear-gradient() 绘制中央条,并使用 background-size 控制其大小,使用 .background-position css 属性控制其位置。
必需的HTML:

只需单个元素(可能带有类):

<div class="menu-button"></div>

必要的CSS:

.menu-button {
  // draws the central bar
  background: linear-gradient(to right, #0e3c89, #0e3c89) no-repeat;
  background-position: center left;
  background-size: 85% 4px;

  // draws the top / bottom bars
  border: solid #0e3c89;
  border-width: 4px 0;

  height: 24px;
  width: 28px;
}

截图:

打开器图片

* {box-sizing: border-box;}

.menu-button {
  background: linear-gradient(to right, #0e3c89, #0e3c89) no-repeat;
  background-position: center left;
  background-size: 85% 4px;
  border: solid #0e3c89;
  border-width: 4px 0;
  height: 24px;
  width: 28px;
}
<div class="menu-button"></div>


0
我的建议(使用flexbox):

.hamburger {
  display: flex;
  align-items: center;
  width: 40px;
  height: 23px;
  cursor: pointer;
  border-top: 6px solid #0e3c89;
  border-bottom: 6px solid #0e3c89;
}

.hamburger::before {
  content: "";
  width: 70%;
  border-bottom: 6px solid #0e3c89;
}
<span class="hamburger"></span>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接