CSS 不同线宽的汉堡菜单图标
CSS hamburger menu icon with different line widths
我创建了一个简单的汉堡包菜单图标,我希望其中的中间线比其他两条线稍短。 不 创建多个 divs
是否可以做到这一点?我目前的解决方案是由多个 box-shadows
, see my working example.
这是我所拥有的与我想要实现的目标:
我的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;
}
谢谢!
这是一种使用尽可能少的标记制作此形状的方法:
- 创建具有特定
width
/ height
的元素,具有顶部/底部边框。
- 使用
linear-gradient()
绘制中心条并使用 background-size
控制其大小,使用 .background-position
css 属性控制其位置。
需要HTML:
只有一个元素(可能有一个class):
<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>
是的,这可以在没有额外标记的情况下完成。这是您可以做到的一种方法:
.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>
我的建议(使用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>
我创建了一个简单的汉堡包菜单图标,我希望其中的中间线比其他两条线稍短。 不 创建多个 divs
是否可以做到这一点?我目前的解决方案是由多个 box-shadows
, see my working example.
这是我所拥有的与我想要实现的目标:
我的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;
}
谢谢!
这是一种使用尽可能少的标记制作此形状的方法:
- 创建具有特定
width
/height
的元素,具有顶部/底部边框。 - 使用
linear-gradient()
绘制中心条并使用background-size
控制其大小,使用.background-position
css 属性控制其位置。
需要HTML:
只有一个元素(可能有一个class):
<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>
是的,这可以在没有额外标记的情况下完成。这是您可以做到的一种方法:
.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>
我的建议(使用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>