html5布局位于中间,使用flex布局,有没有简单的方式实现中间有空隙

09d1c06d7a23339a25571a0d90c25137.png

这是我现在的代码,两个div宽度50%,然后再嵌入div感觉很冗余

.btn-login-container {

display: flex;

.btn-retry-container {

width: 50%;

box-sizing: border-box;

padding-right: 10px;

.btn-retry {

width: 100%;

background: #409EFF;

white-space: nowrap;

cursor: pointer;

font-weight: 500;

color: #FFF;

border-radius: 4px;

display: flex;

align-items: center;

justify-content: center;

}

}

.btn-tourist-container {

width: 50%;

box-sizing: border-box;

padding-left: 10px;

/*background: #409EFF;*/

height: 100%;

.btn-tourist {

width: 100%;

background: #409EFF;

white-space: nowrap;

cursor: pointer;

font-weight: 500;

color: #FFF;

border-radius: 4px;

display: flex;

align-items: center;

justify-content: center;

}

}

}

回答

justify-content: space-between??

可是你width不是50%?

如果两个按钮的容器可以固定,那么可以给容器设置justify-content: space-between;,如果不能固定宽度,可以在元素中间加一个固定宽度的

.button-container:not(:last-child) {

margin-right: 10px;

}

justify-content: space-between两边对齐

两个按钮宽度小于50%中间就会留出间隙了

.btn-login-container {

display: flex;

justify-content: space-between;

.btn-retry{

width: 40%

}

}

个人认为使用grid更好一点

gird的使用可以看阮一峰的gird布局

.btn-login-container {

display: grid;

grid-template-columns: 1fr 1fr;

grid-column-gap:20px;

.btn-retry-container {

box-sizing: border-box;

.btn-retry {

width: 100%;

background: #409EFF;

white-space: nowrap;

cursor: pointer;

font-weight: 500;

color: #FFF;

border-radius: 4px;

display: flex;

align-items: center;

justify-content: center;

}

}

.btn-tourist-container {

box-sizing: border-box;

/*background: #409EFF;*/

height: 100%;

.btn-tourist {

width: 100%;

background: #409EFF;

white-space: nowrap;

cursor: pointer;

font-weight: 500;

color: #FFF;

border-radius: 4px;

display: flex;

align-items: center;

justify-content: center;

}

}

}

我理解的是你不想额外多一层容器 btn-retry-container 和 btn-tourist-container,那可以这样:

.btn-login-container {

display: flex;

.btn {

flex: 1;

/* 原先的按钮样式 */

}

.btn + .btn {

margin-left: 20px;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值