html - CSS 按钮对齐除最后一个以外的所有 div
问题描述
我想显示对齐以填充完整 div 的所有按钮width
,除了最后一个。我的代码在下面,但最后一个 div 很短并且正在扩展全宽,我不希望最后一个 div 使用这个。
注意:按钮是动态生成的。
.block {
width: 400px;
display: flex;
flex-wrap: wrap;
}
.button {
background-color: #cec;
border: none;
color: white;
margin: 15px;
padding: 15px;
display: inline-block;
font-size: 16px;
flex: 1 0 auto;
}
<div class="block">
<div class="button"><a href="#">#1 - A LONG TEXT GOES HERE</a>
</div>
<div class="button"><a href="#">#2 - ANOTHER LONG TEXT HERE</a>
</div>
<div class="button"><a href="#">#3 - SOME TEXT HERE</a>
</div>
<div class="button"><a href="#">#4 - SHORT TEXT</a>
</div>
<div class="button"><a href="#">#5 - SHORT</a>
</div>
<div class="button"><a href="#">#6 - SHORT</a>
</div>
</div>
解决方案
You can use :last-child selector, it matches every element that is the last child of its parent.
.button:last-child
Or you can also use nth-last-child(1) because nth-last-child(1) equal to last-child selector
.button:nth-last-child(1)
推荐阅读
- spring - 是否可以有一个由星号定义的 @Value 属性?
- node.js - Skill Bot-Microsoft Bot Framework v4-NodeJS 中的对话框
- python-3.5 - 及时定位图像并执行指令
- http-headers - 为主页设置缓存标头后强制浏览器重新加载
- java - 有没有办法在Java中初始化多个变量?
- firebase-storage - Flutter Web:firebase_storage:MissingPluginException找不到方法StorageReference#putData的实现
- xamarin.forms - Xamarin.Forms 使用 Flyout/Menu Item 作为标题并自定义 BG 颜色和文本颜色
- ios - Xamarin 本机 iOS 布局
- r - 如何将模式转换为二分投影中的属性
- java - 如何查看 gmail api 的每个用户配额限制?