我有一个CSS按钮组的代码:
基本上,我的目标是让.button-group
成为.container的100%,按钮均匀分布。
我尝试添加:
.button-group .button {
width: 33.3%; /* There is three buttons, so 100/3% */
}
但它在最后留下了一个间隙(不是100%宽度)?
答案 0 :(得分:1)
position: relative
应用于.button-group
。从margin-left: -1px
和以下内容中删除.button
:
.button + .button,
.button + .button-group,
.button-group + .button,
.button-group + .button-group {
margin-left: 15px;
}
display: inline-block
。float: left
代替.button
a
标记之间的空格。<强> 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;
}
.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;