关于flexbox属性justify-content失效原因终于找到了【坑点】

记录一下,坑我好一会儿了
正常我们期望

justify-content: space-between;

得到的结果是

在这里插入图片描述

但是有时候设置了,会变成这样
在这里插入图片描述
原因一:子元素设置了 margin:auto,注意:margin-left/right auto 这些是没问题的
在这里插入图片描述

原因二:盒子本身伪类选择器 被设置成了如下样式
在这里插入图片描述
解决方案: content: normal; 即可解决

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
justify-content是CSS中用于设置弹性盒子(flexbox)元素在主轴上的对齐方式的属性。它有以下几个常用的属性值: 1. flex-start:弹性盒子元素向主轴起始位置对齐。 2. flex-end:弹性盒子元素向主轴结束位置对齐。 3. center:弹性盒子元素在主轴上居中对齐。 4. space-between:弹性盒子元素在主轴上平均分布,首尾两端不留空白。 5. space-around:弹性盒子元素在主轴上平均分布,首尾两端留有空白。 以下是两个示例,分别展示了justify-content属性值为inherit和center的效果: 1. justify-content属性值为inherit的示例: ```html <div class="container"> <div class="box box1">Box 1</div> <div class="box box2">Box 2</div> <div class="box box3">Box 3</div> </div> <style> .container { display: flex; justify-content: inherit; } .box { width: 100px; height: 100px; background-color: #ccc; margin-right: 10px; } .box1 { background-color: red; } </style> ``` 在这个示例中,我们将justify-content属性值设置为inherit,这意味着它将继承其父元素的justify-content属性值。在这种情况下,容器元素的justify-content属性值为默认值flex-start,因此弹性盒子元素将向主轴起始位置对齐。 2. justify-content属性值为center的示例: ```html <div class="container"> <div class="box box1">Box 1</div> <div class="box box2">Box 2</div> <div class="box box3">Box 3</div> </div> <style> .container { display: flex; justify-content: center; } .box { width: 100px; height: 100px; background-color: #ccc; margin-right: 10px; } .box1 { background-color: red; } </style> ``` 在这个示例中,我们将justify-content属性值设置为center,这意味着弹性盒子元素将在主轴上居中对齐。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值