用于填充容器宽度的按钮

时间:2015-01-28 19:57:42

标签: html css button

我有一个CSS按钮组的代码:

http://jsfiddle.net/Lxunuqf5/

基本上,我的目标是让.button-group成为.container的100%,按钮均匀分布。

我尝试添加:

.button-group .button {
    width: 33.3%; /* There is three buttons, so 100/3% */
}

但它在最后留下了一个间隙(不是100%宽度)?

2 个答案:

答案 0 :(得分:1)

  1. position: relative应用于.button-group
  2. margin-left: -1px和以下内容中删除.button

    .button + .button,
    .button + .button-group,
    .button-group + .button,
    .button-group + .button-group {
        margin-left: 15px;
    }
    
  3. display: inline-block
  4. 中使用float: left代替.button
  5. 删除标记中a标记之间的空格
  6. <强> Updated Fiddle

    .container {
      width: 600px;
      background: yellow;
    }
    .button {
      display: inline-block;
      height: 45px;
      padding: 0 30px;
      color: #555;
      text-align: center;
      font-weight: 700;
      line-height: 45px;
      letter-spacing: .01rem;
      text-decoration: none;
      white-space: nowrap;
      background-color: transparent;
      border-radius: 4px;
      border: 1px solid darken($GRAY, 10%);
      font-size: em(13);
      cursor: pointer;
      box-sizing: border-box;
      background: teal;
      width: 33.33333333%;
    }
    .button-group {
      position: relative;
      width: 100%;
      display: inline-block;
      list-style: none;
      padding: 0;
      margin: 0;
      /* IE hacks */
      zoom: 1;
      *display: inline;
    }
    .button-group li {
      float: left;
      padding: 0;
      margin: 0;
    }
    .button-group .button {
      display: inline-block;
    }
    .button-group > .button:not(:first-child):not(:last-child),
    .button-group li:not(:first-child):not(:last-child) .button {
      border-radius: 0;
    }
    .button-group > .button:first-child,
    .button-group li:first-child .button {
      margin-left: 0;
      border-top-right-radius: 0;
      border-bottom-right-radius: 0;
    }
    .button-group > .button:last-child,
    .button-group li:last-child > .button {
      border-top-left-radius: 0;
      border-bottom-left-radius: 0;
    }
    <div class='container'>
      <div class='button-group'>
        <a class='button'>Test Uno</a
        ><a class='button'>Test Dos</a
        ><a class='button'>Test Tres</a>
      </div>
    </div>

答案 1 :(得分:0)

如果您使用float,则无法均匀分发按钮。

相反,您可以使用flexbox:

.button-group {
    display: flex;
    justify-content: space-between;
}

&#13;
&#13;
.container {
  width: 600px;
  background: yellow;
}
.button {
  display: inline-block;
  height: 45px;
  padding: 0 30px;
  color: #555;
  text-align: center;
  font-weight: 700;
  line-height: 45px;
  letter-spacing: .01rem;
  text-decoration: none;
  white-space: nowrap;
  background-color: transparent;
  border-radius: 4px;
  border: 1px solid darken($GRAY, 10%);
  font-size: em(13);
  cursor: pointer;
  box-sizing: border-box;
  background: teal;
}
.button-group {
  display: inline-block;
  list-style: none;
  padding: 0;
  margin: 0;
  /* IE hacks */
  zoom: 1;
  *display: inline;
}
.button + .button,
.button + .button-group,
.button-group + .button,
.button-group + .button-group {
  margin-left: 15px;
}
.button-group li {
  float: left;
  padding: 0;
  margin: 0;
}
.button-group .button {
  float: left;
  margin-left: -1px;
}
.button-group > .button:not(:first-child):not(:last-child),
.button-group li:not(:first-child):not(:last-child) .button {
  border-radius: 0;
}
.button-group > .button:first-child,
.button-group li:first-child .button {
  margin-left: 0;
  border-top-right-radius: 0;
  border-bottom-right-radius: 0;
}
.button-group > .button:last-child,
.button-group li:last-child > .button {
  border-top-left-radius: 0;
  border-bottom-left-radius: 0;
}
.button-group {
  display: flex;
  justify-content: space-between;
}
&#13;
<div class='container'>
  <div class='button-group'>
    <a class='button'>Test Uno</a>
    <a class='button'>Test Dos</a>
    <a class='button'>Test Tres</a>
  </div>
</div>
&#13;
&#13;
&#13;