html - 在菜单项悬停时创建菱形

标签 html css css-shapes

我有一个菜单项,当鼠标悬停在它上面时,它会掉落一个橙色框。

.menu > li > a:hover:after {
    position: absolute;
    left: 0px;
    border-style: solid;
    border-color: transparent;
}

但是我试图实现下拉框下方的菱形效果。

我在这里看到一篇链接此 [网站][4] 的帖子,我尝试使用 diamond:after 方法,但结果很糟糕。需要编辑的css部分我已经注释掉了,方便快速引用。如果有人可以提供帮助。

最佳答案

1) 在您的 CSS 中没有 :after 伪元素的声明 (.menu > li > a:hover:after {/* your css */>)。 例如:

.menu > li > a:hover:after {
    content: ""; /* empty content */
    position: absolute;
    bottom: -50px; /* position 50px below the bottom of the menu link */
    left: 0px;
    border-style: solid;
    border-color: transparent;
    border-width: 0px 60px; /* diamond has 60px to the left and to the right of the midpoint, so the menu item should be 120px width */
    border-top: 50px solid #FF6E0D;
}

此方法要求您指定菜单项的宽度,因为您希望菱形横跨整个链接底部。 (使用左/右 border-width。)

2) 要使用这个方法,你应该添加 position: relative;.menu > li > a 声明,因为 :after 伪元素需要这个定位自己。

关于html - 在菜单项悬停时创建菱形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28794487/

相关文章:

html - 如何在图像旁边 float 多个段落

html - 按钮中的垂直文本

javascript - chrome 和 css 问题

html - 如何使用 HTML、CSS 使背景 div 内部 flex ?

css - 如何在 css 中在矩形的顶部制作曲线?仅在上边缘

html - 简单的边框颜色不适用于 IE7

jquery - 如何在渲染之前使用 jQuery 隐藏元素?

javascript - 在方向更改时刷新 iframe 的内容

html - 可以使用 CSS Flexbox 在每一行上拉伸(stretch)元素同时保持一致的宽度吗?

html - 是否可以使用 border-radius 的 CSS Arch?