html - 具有不同按钮样式的CSS导航栏

标签 html angular css flexbox bootstrap-sass

大家好

我尝试用 sass 构建一个导航栏,如下所示:

一排导航栏

enter image description here

到目前为止,我毫无问题地获得了第一行:

第二行的棘手之处在于,当导航栏扩展到另一个元素时,border-radius 应该与第二行对齐。看这个例子:

enter image description here

第六项应该只在右下角有一个 border-radius。

希望我的解释清楚。有没有人建议我如何使用 sass 来管理它?

这里是一些片段:

<div class="tabs">
<button class="tab"><button class="btn">1. Topic</button>
<button class="tab"><button class="btn">2. Topic</button>
<button class="tab"><button class="btn">3. Topic</button>
</div>

CSS:

.tabs{
width: 100%;
display: flex;
justify-content: start;
align-content: center;
flex-wrap: wrap;
}

.tab{
&:first-child:not(:last-child) {
.btn {
border-bottom-right-radius: 0;
border-top-right-radius: 0;
}
}
&:last-child:not(:first-child) {
.btn {
border-bottom-left-radius: 0;
border-top-left-radius: 0;
}
}
&:not(:first-child):not(:last-child) {
.btn {
border-radius: 0px;
}
}
}

最佳答案

这有点毛茸茸,但我会尽力在这里分解它。您基本上可以使用五种不同的选择器来选择您需要的每种情况,它们是:

  1. 第一项。
  2. 第三项。
  3. 最后一项。
  4. 最后一行的第一项。
  5. 最后一整行的最后一项。

无论如何,第一项总是需要在左上角有一个边框半径,所以让我们添加它。

.tab {
  &:first-child .btn {
    border-top-left-radius: 1em;
  }
}

第三项总是位于整个列表的右上角(但列表的右上角并不总是第三项,我会回到那个),所以另一个简单的选择器添加。

.tab {
  &:first-child .btn {
    border-top-left-radius: 1em;
  }
  &:nth-child(3) .btn {
    border-top-right-radius: 1em;
  }
}

列表的最后一项始终需要右下边框半径。

.tab {
  &:first-child .btn {
    border-top-left-radius: 1em;
  }
  &:nth-child(3) .btn {
    border-top-right-radius: 1em;
  }
  &:last-child .btn {
    border-bottom-right-radius: 1em;
  }
}

现在是不那么不言自明的部分。我们需要选择最后一行的第一项。知道我们在每行中有三个元素,选择每行的第一个元素是使用 :nth-child(3n - 2) 完成的。这会选择第一个、第四个、第七个等等。

我们需要将它与 :nth-last-child 伪类结合起来,以排除不在最后一行的元素。 :nth-last-child(-n + 3) 将选择最后三个元素,因此如果我们将两者结合起来,我们就有了自己的解决方案。

.tab {
  &:first-child .btn {
    border-top-left-radius: 1em;
  }
  &:nth-child(3) .btn {
    border-top-right-radius: 1em;
  }
  &:last-child .btn {
    border-bottom-right-radius: 1em;
  }
  &:nth-child(3n - 2):nth-last-child(-n + 3) .btn {
    border-bottom-left-radius: 1em;
  }
}

到目前为止一切顺利,但还有一步要走。我们现在已将 border-radius 应用于所有四个 Angular ,但仍需要确保每一整行的最后一项都具有 border-bottom-right-radius

这意味着您需要使用 :nth-child(3n) 选择每三个元素,并使用 :nth- 过滤 最后三个元素的结果last-child(-n + 3) 来 self 们之前的例子。

.tab {
  &:first-child .btn {
    border-top-left-radius: 1em;
  }
  &:nth-child(3) .btn {
    border-top-right-radius: 1em;
  }
  &:last-child .btn {
    border-bottom-right-radius: 1em;
  }
  &:nth-child(3n - 2):nth-last-child(-n + 3) .btn {
    border-bottom-left-radius: 1em;
  }
  &:nth-child(3n):nth-last-child(-n + 3) .btn {
    border-bottom-right-radius: 1em;
  }
}

为了确保我们的列表在少于三项时看起来不错,让我们将 ˙border-top-right-radius` 添加到最后一项如果它在第一项行,所以要么是第一项,要么是第二项。

.tab {
  &:first-child .btn {
    border-top-left-radius: 1em;
  }
  &:nth-child(3) .btn {
    border-top-right-radius: 1em;
  }
  &:last-child .btn {
    border-bottom-right-radius: 1em;
  }
  &:nth-child(3n - 2):nth-last-child(-n + 3) .btn {
    border-bottom-left-radius: 1em;
  }
  &:nth-child(3n):nth-last-child(-n + 3) .btn {
    border-bottom-right-radius: 1em;
  }
  &:nth-child(-n + 2):last-child .btn {
    border-top-right-radius: 1em;
  }
}

我创建了一个 CodePen demo以帮助了解您需要选择的元素以及您需要应用的规则。随意调整列表中的元素数量。

关于html - 具有不同按钮样式的CSS导航栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47656285/

相关文章:

HTML 下拉菜单在 OSX 上显示不同

html - 获取无效的 HTTP 方法/URL 对尝试将 Firebase 存储图像 URL 加载到元标记时出错

image - IONIC 2 图像显示在浏览器上,但不显示在 Android 设备上

javascript - angular2 使用 npm 中的 twitter 模块

css - Material 框架的导航栏样式问题

html - 添加可见性 :Hidden effect and keep <a href> download ability

html - 有谁见过这个 - HTML doctype ="HTML1.2/TRANSITIONAL//"

playframework - Play 中的 TypeScript 模块解析是如何工作的?

css - 为什么我的图像重叠?

css - 使用 css 在文本后创建行