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