相同宽度的标签基于最大内容并排

时间:2018-05-29 04:16:54

标签: css sass tabs flexbox

所以我想要实现的条件是

  • 每个标签的宽度基于标签
  • 所有标签与最长标签尺寸相同(标签最长)
    • 因此,标签组应固定或占用100%的父级。应该基于最长的标签。
  • 标签的布局是并排的。

这是我目前的进展(宽度与最大幅度不同):https://jsfiddle.net/cyhwx7z0/5/

我试过this solution,但布局不是并排的。 https://jsfiddle.net/alexkwa/L9oy6hbp/1/

HTML

<div class="wrapper">
  <ul class="tab-group">
    <li class="single-tab">Tab 1</li>
    <li class="single-tab">Tab 2</li>
    <li class="single-tab">Very Long Tab Name</li>
    <li class="single-tab active">Tab 4</li>
    <li class="single-tab">Tab 5</li>
  </ul>
</div>

SCSS

$active-color: #262933;
$inactive-color: #B3B4B8;
$inactive-font-color: #8B8C8F;
$border-thickness: 1px;
$border-radius: 3px;

body {
  background: #fff;
  padding: 20px;
  font-family: Helvetica;
}

.wrapper {
  display: inline-block;
}

.tab-group {
  display: flex;
  flex-direction: column;
  list-style: none;
}

.single-tab {
  background-color: #fff;
  border: $border-thickness solid $inactive-color;
  border-right: 0;
  border-sizing: border-box;
  color: $inactive-font-color;
  cursor: normal;
  font-size: 14px;
  padding: 9px 21px;
  margin: 0;  
  margin-left: -6px;
  user-select: none; 

  display: flex;
  justify-content: center;
  flex-direction: column;
  text-align: center;
}

.single-tab:first-child {
  border-radius: $border-radius 0 0 $border-radius;
}

.single-tab:last-child {
  border-radius: 0 $border-radius $border-radius 0;
  border-right: $border-thickness solid $inactive-color;
}

.single-tab.active {
  background-color: $active-color;
  border: $border-thickness solid $active-color;
  color: #fff;
}

.single-tab.active + .single-tab {
  border-left: 0;
}

.single-tab:last-child.active {
  border-left: 0;
}

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

试试这个

.tab-group {
  display: flex; // instead of inline flex
  ... all other styles
}

.single-tab {
  flex: 1; // makes your tabs of same width
  ... all other styles
}

这是工作小提琴 - https://jsfiddle.net/flexdinesh/xuc6amcu/2/

如果您要求父级不占用整个宽度,则必须手动定义,例如60%。示例小提琴 - https://jsfiddle.net/flexdinesh/xuc6amcu/3/