In this recipe we will create a list group pattern with badges that indicate a count.

A list of items with a badge indicating a count displayed to the right of the text.

要求

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.

Recipe

Download this example

Choices made

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.

justify-content

更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
Supported in Flex Layout Chrome 完整支持 52
完整支持 52
部分支持 29 注意事项
注意事项 Older versions of the specification treat absolute positioned children as though they are a 0 by 0 flex item. Later specification versions take the children out of the flow and set their positions based on align and justify properties. Chrome implements the new behavior beginning with Chrome 52.
完整支持 Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
Edge 完整支持 12 Firefox 完整支持 20 注意事项
完整支持 20 注意事项
注意事项 Before Firefox 27, Firefox supported only single-line flexbox.
不支持 18 — 20 Disabled
Disabled From version 18 until version 20 (exclusive): this feature is behind the layout.css.flexbox.enabled preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
preference (needs to be set to ). To change preferences in Firefox, visit about:config.
IE 完整支持 11 Opera 完整支持 12.1 Safari 完整支持 9 WebView Android 完整支持 52
完整支持 52
部分支持 4.4 注意事项
注意事项 Older versions of the specification treat absolute positioned children as though they are a 0 by 0 flex item. Later specification versions take the children out of the flow and set their positions based on align and justify properties. Chrome implements the new behavior beginning with Chrome 52.
完整支持 Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
Chrome Android 完整支持 52
完整支持 52
部分支持 29 注意事项
注意事项 Older versions of the specification treat absolute positioned children as though they are a 0 by 0 flex item. Later specification versions take the children out of the flow and set their positions based on align and justify properties. Chrome implements the new behavior beginning with Chrome 52.
完整支持 Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
Firefox Android 完整支持 20 注意事项
完整支持 20 注意事项
注意事项 Before Firefox 27, Firefox supported only single-line flexbox.
不支持 18 — 20 Disabled
Disabled From version 18 until version 20 (exclusive): this feature is behind the layout.css.flexbox.enabled preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
preference (needs to be set to ). To change preferences in Firefox, visit about:config.
Opera Android 完整支持 12.1 Safari iOS 完整支持 9 Samsung Internet Android 完整支持 6.0
完整支持 6.0
部分支持 2.0 注意事项
注意事项 Older versions of the specification treat absolute positioned children as though they are a 0 by 0 flex item. Later specification versions take the children out of the flow and set their positions based on align and justify properties. Chrome implements the new behavior beginning with Chrome 52.
完整支持 Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
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

图例

完整支持

完整支持

不支持

不支持

见实现注意事项。

见实现注意事项。

用户必须明确启用此特征。

用户必须明确启用此特征。

要求使用供应商前缀或不同名称。

要求使用供应商前缀或不同名称。

align-items

更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
Supported in Flex Layout Chrome 完整支持 52
完整支持 52
部分支持 29 注意事项
注意事项 Older versions of the specification treat absolute positioned children as though they are a 0 by 0 flex item. Later specification versions take the children out of the flow and set their positions based on align and justify properties. Chrome implements the new behavior beginning with Chrome 52.
完整支持 Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
Edge 完整支持 12 Firefox 完整支持 20 注意事项
完整支持 20 注意事项
注意事项 Multi-line flexbox has been supported since Firefox 28.
完整支持 Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
不支持 18 — 20 Disabled
Disabled From version 18 until version 20 (exclusive): this feature is behind the layout.css.flexbox.enabled preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
preference (needs to be set to ). To change preferences in Firefox, visit about:config.
IE 完整支持 11 注意事项
完整支持 11 注意事项
注意事项 In Internet Explorer 10 and 11, if column flex items have align-items: center; set on them and their content is too large, then they will overflow the bounds of their container. See Flexbug #2 .
Opera 完整支持 12.1 Safari 完整支持 9 WebView Android 完整支持 52
完整支持 52
部分支持 4.4 注意事项
注意事项 Older versions of the specification treat absolute positioned children as though they are a 0 by 0 flex item. Later specification versions take the children out of the flow and set their positions based on align and justify properties. Chrome implements the new behavior beginning with Chrome 52.
完整支持 Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
Chrome Android 完整支持 52
完整支持 52
部分支持 29 注意事项
注意事项 Older versions of the specification treat absolute positioned children as though they are a 0 by 0 flex item. Later specification versions take the children out of the flow and set their positions based on align and justify properties. Chrome implements the new behavior beginning with Chrome 52.
完整支持 Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
Firefox Android 完整支持 20 注意事项
完整支持 20 注意事项
注意事项 Multi-line flexbox has been supported since Firefox 28.
完整支持 Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
不支持 18 — 20 Disabled
Disabled From version 18 until version 20 (exclusive): this feature is behind the layout.css.flexbox.enabled preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
preference (needs to be set to ). To change preferences in Firefox, visit about:config.
Opera Android 完整支持 12.1 Safari iOS 完整支持 9 Samsung Internet Android 完整支持 6.0
完整支持 6.0
部分支持 2.0 注意事项
注意事项 Older versions of the specification treat absolute positioned children as though they are a 0 by 0 flex item. Later specification versions take the children out of the flow and set their positions based on align and justify properties. Chrome implements the new behavior beginning with Samsung Internet 6.0.
完整支持 Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
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

图例

完整支持

完整支持

不支持

不支持

见实现注意事项。

见实现注意事项。

用户必须明确启用此特征。

用户必须明确启用此特征。

要求使用供应商前缀或不同名称。

要求使用供应商前缀或不同名称。

另请参阅

元数据

  • 最后修改:
  1. CSS
  2. CSS 参考
  3. Layout cookbook
  4. 指南
    1. Breadcrumb Navigation
    2. Card
    3. Center an element
    4. Column layouts
    5. Contribute a recipe
    6. Grid wrapper
    7. List group with badges
    8. Pagination
    9. Recipe: Media objects
    10. Split Navigation
    11. Sticky footers

Copyright  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1