html - 向像这样的元素添加向外 flex 的边框 : ◝◟___◞◜

标签 html css css-shapes

问题

我的导航栏看起来像这样:

enter image description here

HTMLCSS

nav {
  width: 1040px;
}
nav ul {
  display: block;
  list-style-type: none;
  height: 40px;
  border-top: 3px solid #222;
}
nav ul li {
  width: 130px;
  display: inline-block;
  line-height: 40px;
  text-align: center;
  font-size: 16px;
  font-family: Vollkorn;
  font-weight: 400;
}
nav ul li a {
  display: block;
  text-decoration: none;
  color: #333;
}
nav ul li a:hover,
nav ul li a:focus {
  color: #000;
  box-shadow: 0px 0px 2px black;
  border-radius: 10px;
  border: 2px solid #222;
  border-top: 0px;
  transition: 0.2s ease all;
}
<nav>
  <ul>
    <li><a href="#">Home</a>
    </li>
    <li>
      <a href="#">Random</a>
    </li>
    <li>
      <a href="#">Blog</a>
    </li>
    <li>
      <a href="#">About us</a>
    </li>
    <li>
      <a href="#">Contact Us</a>
    </li>
  </ul>
</nav>

期望的输出

在上图中,列表项blog 处于焦点或悬停状态。在悬停样式中,它的边框半径为 5px。问题是我希望左上角和右上角在悬停时像这样向外 flex :

enter image description here

我可以使用背景图像以及位置样式和 z-index 来实现此结果。我想要一个 CSS border-only 解决方案。

附言

所需的输出可能看起来并不理想(至少对我而言),但我只是想知道这是否可以完成。

最佳答案

从我的头顶开始:在元素上绘制下半边框,在定位的伪元素上绘制左上边框和右上边框。

/* using 4px border, 1em border radius, 1em padding */
nav ul {
  font: medium sans-serif;
  text-align: center;
  padding: 0;
  list-style-type: none;
  border-top: 4px solid;
}
nav li {
  display: inline-block;
  border-left: 4px solid transparent;
  border-right: 4px solid transparent;
  border-bottom: 4px solid transparent;
  border-radius: 0 0 1em 1em;
  padding: 0 1em 1em;
  position: relative;
  top: 1em;
}
nav li:before,
nav li:after {
  display: none;
  content: "";
  position: absolute;
  top: -1em;
  width: 1em;
  height: 1em;
  margin-top: -4px;
}
nav li:before {
  right: 100%;
  border-top: 4px solid;
  border-right: 4px solid;
  border-top-right-radius: 1em;
}
nav li:after {
  left: 100%;
  border-top: 4px solid;
  border-left: 4px solid;
  border-top-left-radius: 1em;
}
nav li:hover {
  border-color: initial;
}
nav li:hover:before,
nav li:hover:after {
  display: block;
}
<nav>
  <ul>
    <li>Home</li>
    <li>Random</li>
    <li>Blog</li>
    <li>About us</li>
    <li>Contact Us</li>
  </ul>
</nav>

关于html - 向像这样的元素添加向外 flex 的边框 : ◝◟___◞◜,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27676867/

相关文章:

php - 将鼠标悬停在 <nav> 链接上时,网站会随机更改悬停背景大小

html - 将 css 应用于带有 div 的表单

html - 当子元素具有背景颜色并且浏览器缩小小于 100% 时,CSS 边框消失

html - 移除 CSS 中的侧边框

html - 交通灯的 CSS 形状

html - Z-index 不适用于 css 形状内的图像

html - 是否可以引用 HTML 文档中其他位置定义的 SVG?

html - 如何缩小 div 以适应自动换行的文本

html - 在正常流程中缩小我的 div 之间的差距

html - 使照片覆盖六边形的整个区域