Open zhangxinxu opened 5 years ago
<div class="btn-group">
<button class="btn btn-danger">按钮</button>
</div>
<div class="btn-group">
<button class="btn btn-danger">按钮</button>
<button class="btn btn-danger">按钮</button>
</div>
<div class="btn-group">
<button class="btn btn-danger">按钮</button>
<button class="btn btn-danger">按钮</button>
<button class="btn btn-danger">按钮</button>
</div>
/* 解法1:grid 方案 */
.btn-group {
display: grid;
grid-gap: 1rem;
grid-template-columns: repeat(auto-fit, minmax(0, 1fr));
}
.btn-group + .btn-group {margin-top: 15px;}
.btn {
padding: 6px 0;
text-align: center;
color: #666;
background: transparent;
border: 1px solid;
border-radius: 10em;
}
.btn-danger {color: #ef7077;}
/* 解法2:flex 方案 */
.btn-group {
display: flex;
margin-left: -.5rem;
margin-right: -.5rem;
}
.btn-group + .btn-group {margin-top: 15px;}
.btn {
flex: 1;
padding: 6px 0;
margin: 0 .5rem;
text-align: center;
color: #666;
background: transparent;
border: 1px solid;
border-radius: 10em;
}
.btn-danger {color: #ef7077;}
* {
padding: 0;
margin: 0;
}
.wrapper {
height: 100px;
padding: 25px 1rem;
display: flex;
}
.wrapper > div {
flex: 1;
border-radius: 25px;
height: 50px;
line-height: 50px;
text-align: center;
border: 1px solid red;
box-sizing: border-box;
margin-right: 1rem;
}
.wrapper > div:last-child {
margin-right: 0;
}
<div class="wrapper">
<div>按钮</div>
<div>按钮</div>
<div>按钮</div>
<div>按钮</div>
<div>按钮</div>
<div>按钮</div>
<div>按钮</div>
<div>按钮</div>
</div>
<!-- zxx: 视觉还原和HTML代码这块略有不足 -->
.btn-group{
display: flex;
padding: 1rem .5rem;
justify-content: space-between; /* zxx: 这个可以没有 */
}
.btn{
flex: 1;
height: 30px;
border: 1px solid #ef7b82;
color: #ef7b82;
font-size: 14px;
border-radius: 30px;
background-color: transparent;
box-shadow: 0 0 1px #ef7b82;
padding: 0;
margin: 0 .5rem;
}
<div class="btn-group">
<button type="button" class="btn">按钮</button>
</div>
<div class="btn-group">
<button type="button" class="btn">按钮</button>
<button type="button" class="btn">按钮</button>
</div>
<div class="btn-group">
<button type="button" class="btn">按钮</button>
<button type="button" class="btn">按钮</button>
<button type="button" class="btn">按钮</button>
</div>
<div class="btn-group">
<button type="button" class="btn">按钮</button>
<button type="button" class="btn">按钮</button>
<button type="button" class="btn">按钮</button>
<button type="button" class="btn">按钮</button>
</div>
.wrapper{
display:flex;
flex-wrap: nowrap;
padding: 0 1rem;
margin-bottom: 4rem;
.btn{
flex:1;
margin-right:1rem;
box-sizing:border-box;
border-radius: 1em;
border: 1px solid currentColor;
text-align:center;
color: pink;
line-height: 2em;
&:last-child{
margin-right:0
}
}
}
<div class="wrapper">
<div class="btn">按钮</div>
</div>
<div class="wrapper">
<div class="btn">按钮</div>
<div class="btn">按钮</div>
</div>
<div class="wrapper">
<div class="btn">按钮</div>
<div class="btn">按钮</div>
<div class="btn">按钮</div>
</div>
demo:https://codepen.io/anon/pen/YbOvQP?editors=1111
.wrapper{
display:grid;
grid-gap: 1rem;
grid-template-columns: repeat(auto-fit, minmax(0, 1fr));
padding: 0 1rem;
margin-bottom: 4rem;
.btn{
box-sizing:border-box;
border-radius: 1em;
border: 1px solid currentColor;
text-align:center;
color: pink;
line-height: 2em;
}
}
<div class="wrapper">
<div class="btn">按钮</div>
</div>
<div class="wrapper">
<div class="btn">按钮</div>
<div class="btn">按钮</div>
</div>
<div class="wrapper">
<div class="btn">按钮</div>
<div class="btn">按钮</div>
<div class="btn">按钮</div>
</div>
.container {
background: #f6f7f9;
}
.btn-group {
display: flex;
padding: 1rem;
}
[class|='ui-btn'] {
flex: 1;
margin-right: 1rem;
background: transparent;
border: 1px solid;
padding: 1em;
border-radius: 10em;
}
.btn-group > [class|='ui-btn']:last-child {
margin-right: 0;
}
[class*='-red'] {
color: #ef7077;
}
[class*='-blue'] {
color: skyblue;
}
<div class="container">
<div class="btn-group">
<button class="ui-btn-red">按钮</button>
<button class="ui-btn-red">按钮</button>
<button class="ui-btn-blue">按钮</button>
<button class="ui-btn-blue">按钮</button>
</div>
</div>
<div class="button-group">
<a tabindex="0" role="button" class="button">按钮</a>
</div>
<div class="button-group">
<a tabindex="0" role="button" class="button">按钮</a>
<a tabindex="0" role="button" class="button">按钮</a>
</div>
<div class="button-group">
<a tabindex="0" role="button" class="button">按钮</a>
<a tabindex="0" role="button" class="button">按钮</a>
<a tabindex="0" role="button" class="button">按钮</a>
</div>
.button-group{
font-size:0;
display:flex;
padding:0 0.5rem;
}
.button{
flex:auto;
font-size:1rem;
color:#ef7077;
position: relative;
height: 2em;
line-height:2em;
min-width:4em;
text-align:center;
margin:0.5rem;
}
.button::after{
content: '';
position: absolute;
top: 0;
left: 0;
border: 2px solid #ef7077;
box-sizing: border-box;
border-radius:2em;
width: 200%;
height: 200%;
transform: scale(0.5);
transform-origin: left top;
}
.average-button-group {
display: table;
table-layout: fixed;
text-align: center;
width: 100%;
}
.average-button-wrap {
display: table-cell;
}
.average-button {
margin-left: .5rem;
margin-right: .5rem;
}
.average-button-wrap:first-child .average-button {
margin-left: 0;
}
.average-button-wrap:last-child .average-button {
margin-right: 0;
}
.average-button {
display: block;
border: 1px solid currentColor;
background: transparent;
border-radius: 1rem;
white-space: nowrap;
color: #ef7077;
}
<div class="average-button-group">
<div class="average-button-wrap">
<a class="average-button" role="button">按钮</a>
</div>
</div>
<div class="average-button-group">
<div class="average-button-wrap">
<a class="average-button" role="button">按钮</a>
</div>
<div class="average-button-wrap">
<a class="average-button" role="button">按钮</a>
</div>
</div>
<div class="average-button-group">
<div class="average-button-wrap">
<a class="average-button" role="button">按钮</a>
</div>
<div class="average-button-wrap">
<a class="average-button" role="button">按钮</a>
</div>
<div class="average-button-wrap">
<a class="average-button" role="button">按钮</a>
</div>
</div>
<div class="average-button-group">
<div class="average-button-wrap">
<a class="average-button" role="button">按钮</a>
</div>
<div class="average-button-wrap">
<a class="average-button" role="button">按钮</a>
</div>
<div class="average-button-wrap">
<a class="average-button" role="button">按钮</a>
</div>
<div class="average-button-wrap">
<a class="average-button" role="button">按钮</a>
</div>
</div>
<div class="average-button-group">
<div class="average-button-wrap">
<a class="average-button" role="button">按钮</a>
</div>
<div class="average-button-wrap">
<a class="average-button" role="button">按钮</a>
</div>
<div class="average-button-wrap">
<a class="average-button" role="button">按钮</a>
</div>
<div class="average-button-wrap">
<a class="average-button" role="button">按钮</a>
</div>
<div class="average-button-wrap">
<a class="average-button" role="button">按钮</a>
</div>
</div>
<div class="average-button-group">
<div class="average-button-wrap">
<a class="average-button" role="button">按钮</a>
</div>
<div class="average-button-wrap">
<a class="average-button" role="button">按钮</a>
</div>
<div class="average-button-wrap">
<a class="average-button" role="button">按钮</a>
</div>
<div class="average-button-wrap">
<a class="average-button" role="button">按钮</a>
</div>
<div class="average-button-wrap">
<a class="average-button" role="button">按钮</a>
</div>
<div class="average-button-wrap">
<a class="average-button" role="button">按钮</a>
</div>
</div>
<div class="average-button-group">
<div class="average-button-wrap">
<a class="average-button" role="button">按</a>
</div>
<div class="average-button-wrap">
<a class="average-button" role="button">按钮</a>
</div>
<div class="average-button-wrap">
<a class="average-button" role="button">按钮按</a>
</div>
<div class="average-button-wrap">
<a class="average-button" role="button">按钮按钮</a>
</div>
</div>
.container {
width: 50%;
margin: 1rem auto;
background: #ddd;
display: flex;
padding: 1rem 0.5rem;
}
.item {
margin: 0 0.5rem;
width: 100%;
height: 2rem;
outline: none;
border: 1px solid red;
border-radius: 1rem;
color: red;
}
<div class="container">
<button class="item">按钮</button>
</div>
<div class="container">
<button class="item">按钮</button>
<button class="item">按钮</button>
</div>
<div class="container">
<button class="item">按钮</button>
<button class="item">按钮</button>
<button class="item">按钮</button>
</div>
<div class="btn">
<button class="btn__item">按钮</button>
<button class="btn__item">按钮</button>
<button class="btn__item">按钮</button>
<button class="btn__item">按钮</button>
<button class="btn__item">按钮</button>
</div>
body{
margin: 0;
}
.btn{
display:flex;
padding:10px;
background: #f7f7f7;
}
.btn__item{
display:flex;
align-items: center;
justify-content: center;
/* zxx: 以上3个是不是可以不需要,可以看到比一开始参与小测的实现要好不少 */
flex:1;
padding: 6px 0;
text-align: center;
color: #ef7077;
margin-right:1rem;
background: transparent;
border: 1px solid;
border-radius: 10em;
}
.btn__item:last-child{
margin-right: 0;
}
.btn {
padding: .5em 1em;
text-align: center;
background: none;
border: 1px solid;
font-size:1rem;
border-radius: 5em;
color: #ef7077;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.btn-group{
display: flex;
padding: .5rem;
}
.btn-group .btn{
flex: 1;
margin: 0 .5rem;
}
<div class="btn-group">
<button class="btn">很多文字的按钮按钮按钮按钮按钮按钮按钮按钮按钮按钮按钮</button>
<button class="btn">按钮</button>
<button class="btn">按钮</button>
<button class="btn">按钮</button>
<button class="btn">按钮</button>
</div>
<div class="button-list">
<button>按钮</button>
</div>
<div class="button-list">
<button>按钮</button>
<button>按钮</button>
</div>
<div class="button-list">
<button>按钮</button>
<button>按钮</button>
<button>按钮</button>
</div>
.button-list {
margin-top: 10px;
display: flex;
}
.button-list button:first-child {
margin-left: 1rem;
}
.button-list button {
position: relative; /* zxx: 这里relative作用是? */
/* 本来想用before伪类模拟按钮聚焦的,忘记删了 */
flex: 1;
font-size: 14px;
color: #ef7077;
background: inherit;
border-radius: 9999px;
border: 1px solid #ef7077;
margin-right: 1rem;
}
.button-list button:focus {
outline: none;
box-shadow: 0px 0px 00px 1px skyblue;
}
<div class="container">
<button class="btn">按钮</button>
</div>
<div class="container">
<button class="btn">按钮</button>
<button class="btn">按钮</button>
</div>
<div class="container">
<button class="btn">按钮</button>
<button class="btn">按钮</button>
<button class="btn">按钮</button>
</div>
<div class="container">
<button class="btn">按钮</button>
<button class="btn">按钮</button>
<button class="btn">按钮</button>
<button class="btn">按钮</button>
<button class="btn">按钮</button>
</div>
body {
margin: 0;
}
.container {
display: flex;
padding-left: 0.5rem;
padding-right: 0.5rem;
margin-top: 10px;
}
.btn {
flex: 1;
border: 1px solid red;
outline: none;
background: none;
height: 26px;
line-height: 26px;
border-radius: 13px;
margin: 0 0.5rem ;
overflow: hidden;
}
<div class="btn-group">
<button>点击</button>
</div>
<div class="btn-group">
<button>点击</button>
<button>点击</button>
<button>点击</button>
</div>
<div class="btn-group">
<button>点击</button>
<button>点击</button>
<button>点击</button>
<button>点击</button>
</div>
<div class="btn-group">
<button>点击</button>
<button>点击</button>
<button>点击</button>
<button>点击</button>
<button>点击</button>
</div>
body{
margin: 0;
}
.btn-group{
display: flex;
justify-content: space-between;
margin: 10rem 0;
}
button{
text-align: center;
width: 100%;
cursor: pointer;
border-radius: 10rem;
background-color: transparent;
border: 1px solid #ccc;
outline: none;
margin: 0 0.5rem;
}
//zxx: 按钮的ui还原差别大了点
demo https://codepen.io/crazyboy/pen/MMEraE
<div class="container1">
<button>按钮</button>
<button>按钮</button>
<button>按钮</button>
<button>按钮</button>
<button>按钮</button>
<button>按钮</button>
</div>
<div class="container2">
<div><button>按钮</button></div>
<div><button>按钮</button></div>
<div><button>按钮</button></div>
<div><button>按钮</button></div>
<div><button>按钮</button></div>
<div><button>按钮</button></div>
</div>
body {
margin: 0;
}
button {
text-align: center;
border: 1px solid red;
border-radius: 1rem;
background: transparent;
}
/* 方法1 flex */
.container1 {
display: flex;
margin-left: 1rem;
}
.container1>button {
flex-grow: 1;
margin-right: 1rem;
}
/* 方法2 table */
.container2 {
display: table;
white-space: nowrap;
width: calc(100% - 1rem);
margin-left: 1rem; /* zxx: 使用auto更佳 */
}
.container2>div {
display: table-cell;
padding-right: 1rem;
}
.container2>div>button {
width: 100%;
}
//zxx: 视觉还原有待加强,选择器的选择随意了点
body,html{ /* zxx: html标签无需参与,padding多余 */
margin: 0;
padding: 0;
}
button{
background: none;
border: 1px solid #ef7077;
color:#ef7077;
height: 30px;
flex: 1;
margin: 0.5rem;
border-radius: 20px;
}
.button_box{
display: flex;
margin: 0 0.5rem;
}
<div class="button_box">
<button>按钮</button>
</div>
<div class="button_box">
<button>按钮</button>
<button>按钮</button>
</div>
<div class="button_box">
<button>按钮</button>
<button>按钮</button>
<button>按钮</button>
<button>按钮</button>
<button>按钮</button>
<button>按钮</button>
<button>按钮</button>
<button>按钮</button>
</div>
<div class="btn-group">
<button class="btn">按钮</button>
</div>
<div class="btn-group">
<button class="btn">按钮</button>
<button class="btn">按钮</button>
</div>
<div class="btn-group">
<button class="btn">按钮</button>
<button class="btn">按钮</button>
<button class="btn">按钮</button>
</div>
<div class="btn-group">
<button class="btn">按钮</button>
<button class="btn">按钮</button>
<button class="btn">按钮</button>
<button class="btn">按钮</button>
</div>
.btn-group {
display: -webkit-flex;
display: flex;
padding: 1rem;
margin-bottom: 1rem;
background: #f6f7f9;
}
.btn {
flex: 1;
height: 30px;
margin-right: 1rem;
border: 2px solid #ef7077; /* zxx: 截图发糊,实际宽度是1px */
border-radius: 15px;
color: #ef7077;
background: transparent;
box-sizing: border-box;
outline: none;
}
.btn:focus {
background: #fff;
}
.btn:last-child {
margin-right: 0;
}
<div class="btn_container">
<button class="btn_item">按钮</button>
<button class="btn_item">按钮</button>
<button class="btn_item">按钮</button>
</div>
/* zxx: 下次请粘贴编译好的CSS */
.btn_container {
display: flex;
flex-wrap: nowrap; /* zxx: 可以去掉 */
.btn_item {
+ .btn_item {
margin-left: 1rem;
}
&:focus {
outline: none;
}
flex: 1;
color: #ef7077;
border: 1px solid;
background: none;
border-radius: 10rem;
}
}
<div class="button-group">
<button class="button-item">按钮</button>
</div>
<div class="button-group">
<button class="button-item">按钮</button>
<button class="button-item">按钮</button>
</div>
<div class="button-group">
<button class="button-item">按钮</button>
<button class="button-item">按钮</button>
<button class="button-item">按钮</button>
</div>
/* zxx: 下次请粘贴编译好的CSS */
.button-group{
display: flex;
margin: 0 1rem 1rem 1rem;
.button-item{
flex: 1;
background-color: #fff;
border: 1px solid #f56c6c;
border-radius: 2em;
color: #f56c6c;
outline: none;
padding: .5rem;
&:not(:last-child) {
margin-right: 1rem;
}
}
}
<div class="buttons-container">
<button class="button">按钮</button>
</div>
<div class="buttons-container">
<button class="button">按钮</button>
<button class="button">按钮</button>
</div>
<div class="buttons-container">
<button class="button">按钮</button>
<button class="button">按钮</button>
<button class="button">按钮</button>
</div>
<div class="buttons-container">
<button class="button">按钮</button>
<button class="button">按钮</button>
<button class="button">按钮</button>
<button class="button">按钮</button>
<button class="button">按钮</button>
<button class="button">按钮</button>
<button class="button">按钮</button>
<button class="button">按钮</button>
<button class="button">按钮</button>
<button class="button">按钮</button>
<button class="button">按钮</button>
<button class="button">按钮</button>
<button class="button">按钮</button>
<button class="button">按钮</button>
<button class="button">按钮</button>
<button class="button">按钮</button>
<button class="button">按钮</button>
</div>
html {
font-size: 16px;
margin: 0;
}
body {
margin: 0;
}
.buttons-container {
display: flex;
padding: 1rem;
}
.buttons-container .button:first-child {
margin-left: 0
}
.buttons-container .button:last-child {
margin-right: 0;
}
.button {
flex: auto;
height: 20px;
line-height: 20px;
box-sizing: border-box;
border: 1px solid #f1949a;
color: #f1949a;
font-size: 12px;
border-radius: 20px;
text-align: center;
margin: 0 0.5rem;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
//zxx: 按钮的视觉还原实在偏差太大
<div class="btn-group">
<button class="cus-btn">按钮</button>
</div>
<div class="btn-group">
<button class="cus-btn">按钮</button>
<button class="cus-btn">按钮</button>
</div>
<div class="btn-group">
<button class="cus-btn">按钮</button>
<button class="cus-btn">按钮</button>
<button class="cus-btn">按钮</button>
</div>
.btn-group {
display: flex;
flex-wrap: nowrap;
padding: 0 0 1rem 1rem ;
}
.cus-btn {
flex: 1;
margin-right: 1rem;
color: red;
border: 1px solid red;
border-radius: 1em;
background: transparent;
white-space: nowrap;
}
> 点击这里 \<
.main {
background-color: #f5f5f5;
padding: 16px;
}
.row {
border: 1px solid black; // 显示父子之间的宽度关系
display: flex;
flex-basis: row nowrap;
justify-content: space-around;
}
.col {
flex: 1 1;
}
.col + .col {
margin-left: 1rem; // 设置间隔
}
.btn {
color: #dd0000;
width: 100%;
font-size: 16px;
background: none;
padding: 8px 3px;
border-radius: 20px;
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
outline: none;
border: 1px solid currentcolor;
transition: all 0.2s;
}
.btn:active {
color: #dd0025;
box-shadow: 0 0 8px -4px rgba(221, 0, 37, 0.7);
background-color: rgba(221, 0, 37, 0.1);
}
<section class="container">
<div class="container-row">
<button class="btn">按钮</button>
</div>
<div class="container-row">
<button class="btn">按钮</button>
<button class="btn">按钮</button>
</div>
<div class="container-row">
<button class="btn">按钮</button>
<button class="btn">按钮</button>
<button class="btn">按钮</button>
</div>
<div class="container-row">
<button class="btn">按钮</button>
<button class="btn">按钮</button>
<button class="btn">按钮</button>
<button class="btn">按钮</button>
</div>
</section>
.container {
overflow: hidden;
background-color: #f6f6f6;
}
.container-row {
display: flex;
flex-flow: row nowrap; /* zxx: 多余 */
justify-content: space-between; /* zxx: 多余 */
margin: 20px 1rem;
}
.btn {
flex: 1;
margin-right: 1rem;
height: 28px;
line-height: 28px;
outline: 0;
border-radius: 14px;
border: 1px solid #f36;
color: #f36; /* zxx: 取色有误差 */
background-color: transparent;
overflow: hidden;
}
.btn:last-child {
margin-right: 0;
}
后面想到也许grid布局更合适。
body{
margin: 0
}
.btn-wrapper{
display: flex;
flex-wrap: nowrap;
background: #eee;
padding: 10px 0;
}
.btn {
outline: none;
border: 1px solid #ef7b82;
color: #ef7b82;
padding: 6px 0;
border-radius: 1em;
background-color: #fff;
flex-basis: 10em;
flex: 1;
margin: 0 0.5rem;
}
.btn:first-of-type{
margin-left: 1rem;
}
.btn:last-of-type {
margin-right: 1rem;
}
.btn {
color: red;
background: none;
border: red 1px solid;
border-radius: 50px;
width: 100%;
margin: 0 0.5rem;
padding: 0.5rem;
overflow: hidden;
}
.btn-group {
padding: 0.5rem;
display: flex;
}
<div class="btn-group">
<button class="btn">按钮</button>
</div>
<div class="btn-group">
<button `class="btn">按钮</button>`
<button class="btn">按钮</button>
</div>
<div class="btn-group">
<button class="btn">按钮</button>
<button class="btn">按钮</button>
<button class="btn">按钮</button>
<button class="btn">按钮</button>
</div>
<div class="btn-group">
<button class="btn">按钮</button>
<button class="btn">按钮</button>
<button class="btn">按钮</button>
<button class="btn">按钮</button>
<button class="btn">按钮</button>
</div>
<div class="ui-container">
<a href="javascript:" class="ui-button" role="button">按钮</a>
</div>
<div class="ui-container">
<a href="javascript:" class="ui-button" role="button">按钮</a>
<a href="javascript:" class="ui-button" role="button">按钮</a>
</div>
<div class="ui-container">
<a href="javascript:" class="ui-button" role="button">按钮</a>
<a href="javascript:" class="ui-button" role="button">按钮</a>
<a href="javascript:" class="ui-button" role="button">按钮</a>
</div>
.ui-button {
display: inline-block;
margin: 0 0.5rem;
padding: 9px 15px;
line-height: 20px;
font-size: 14px;
text-align: center;
color: #f4615c;
border: 1px solid;
border-radius: 20px;
min-width: 50px;
text-decoration: none;
cursor: pointer;
}
.ui-button:hover {
background-color: #aa4642;
color: #fff;
}
.ui-container {
display: flex;
margin: 0.5rem;
}
.ui-container .ui-button {
flex: 1;
}
<div class="btn_wrapper">
<button>按钮</button>
</div>
.btn_wrapper {
margin: 10px;
display: flex;
border: 1px solid black;
padding-left: 1rem;
}
button {
border: 2px solid pink;
padding: 5px 10px;
margin-right: 1rem;
border-radius: 20px;
background: none;
white-space: nowrap;
flex: 1;
color: pink;
font-size: medium;
text-overflow: ellipsis;
overflow: hidden;
}
//zxx: 按钮取色误差较大
.box {
padding: 0 1em;
display: grid;
grid-auto-flow: column;
grid-gap: 1em;
}
.btn {
border: 1px solid #ef777e;
border-radius: 1em;
text-align: center;
line-height: 2em;
color: #ee715c;
}
//zxx: 言简意赅666,可惜HTML代码没放出来,9分变8分
本期题目如下:
请附上对应的CSS代码,注意缩进和代码高亮,可以使用下面语法:
请提供在线的可访问的demo地址(精力有限,没有demo减1分),如jsbin.com、jsfiddle.net或codepen.io,使用国内的类似工具也可以。
本次小测为上学期最后一期小测,直播答疑本周六6月29日上午10:00 。我还在思考该给前几名奖励什么礼品。
直播地址:https://live.bilibili.com/21193211
首位答题者会额外2积分,同时会被翻牌。
感谢您的参与!