In this recipe we will create a list group pattern with badges that indicate a count.
Our list items should be displayed with the badges lined up on the right, no matter how much content the item has. The badge should always be centered vertically whether there is a single line of content, or more than one.
Flexbox makes this particular pattern straightforward and also makes it easy to make changes to the layout.
To ensure the text and badge line up correctly I use the
justify-content
property with a value of
space-between
. This places any extra space between the items. In the live example, if you remove this property and you will see the badge move to the end of the text on items with text shorter than the one line.
To align the content horizontally, I use the
align-items
property to align the text and badge on the cross axis. If instead, you want the badge to align to the top of the content, change this to
align-items: flex-start
.
The various layout methods have different browser support. See the charts below for details on basic support for the properties used.
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Supported in Flex Layout |
Chrome
完整支持
52
|
Edge 完整支持 12 |
Firefox
完整支持
20
注意事项
|
IE 完整支持 11 | Opera 完整支持 12.1 | Safari 完整支持 9 |
WebView Android
完整支持
52
|
Chrome Android
完整支持
52
|
Firefox Android
完整支持
20
注意事项
|
Opera Android 完整支持 12.1 | Safari iOS 完整支持 9 |
Samsung Internet Android
完整支持
6.0
|
| Supported in Grid Layout | Chrome 完整支持 57 | Edge 完整支持 16 | Firefox 完整支持 52 | IE 不支持 No | Opera 完整支持 44 | Safari 完整支持 10.1 | WebView Android 完整支持 57 | Chrome Android 完整支持 52 | Firefox Android 完整支持 52 | Opera Android 完整支持 43 | Safari iOS 完整支持 10.3 | Samsung Internet Android 完整支持 6.2 |
完整支持
不支持
见实现注意事项。
用户必须明确启用此特征。
要求使用供应商前缀或不同名称。
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Supported in Flex Layout |
Chrome
完整支持
52
|
Edge 完整支持 12 |
Firefox
完整支持
20
注意事项
|
IE
完整支持
11
注意事项
|
Opera 完整支持 12.1 | Safari 完整支持 9 |
WebView Android
完整支持
52
|
Chrome Android
完整支持
52
|
Firefox Android
完整支持
20
注意事项
|
Opera Android 完整支持 12.1 | Safari iOS 完整支持 9 |
Samsung Internet Android
完整支持
6.0
|
| Supported in Grid Layout | Chrome 完整支持 57 | Edge 完整支持 16 | Firefox 完整支持 52 | IE 不支持 No | Opera 完整支持 44 | Safari 完整支持 10.1 | WebView Android 完整支持 57 | Chrome Android 完整支持 52 | Firefox Android 完整支持 52 | Opera Android 完整支持 43 | Safari iOS 完整支持 10.3 | Samsung Internet Android 完整支持 6.2 |
完整支持
不支持
见实现注意事项。
用户必须明确启用此特征。
要求使用供应商前缀或不同名称。