Web/CSS/Layout cookbook/List group with badges

From Get docs

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

[[File:../../../../../../media.prod.mdn.mozit.cloud/attachments/2018/10/17/16255/52c4b7094120b5acb862744f876889dd/list-group-badges.png|A list of items with a badge indicating a count displayed to the right of the text.]]

Requirements

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

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.

Browser compatibility

The various layout methods have different browser support. See the charts below for details on basic support for the properties used.

justify-content

Update compatibility data on 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 Full support 52


Full support 52


Partial support 29

Notes'

Notes' 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. Full support 21

Prefixed'

Prefixed' Implemented with the vendor prefix: -webkit-

Edge Full support 12


Full support 12


Full support 12

Prefixed'

Prefixed' Implemented with the vendor prefix: -webkit-

Firefox Full support 20

Notes'

Full support 20

Notes'

Notes' Before Firefox 27, Firefox supported only single-line flexbox. No support 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. Full support 49

Prefixed'

Prefixed' Implemented with the vendor prefix: -webkit- Full support 48

Prefixed' Disabled'

Prefixed' Implemented with the vendor prefix: -webkit- Disabled' From version 48: this feature is behind the layout.css.prefixes.webkit preference (needs to be set to true). To change preferences in Firefox, visit about:config.

IE

Full support 11

Opera

Full support 12.1

Safari Full support 9


Full support 9


Full support 6.1

Prefixed'

Prefixed' Implemented with the vendor prefix: -webkit-

WebView Android Full support 52


Full support 52


Partial support 4.4

Notes'

Notes' 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. Full support ≤37

Prefixed'

Prefixed' Implemented with the vendor prefix: -webkit-

Chrome Android Full support 52


Full support 52


Partial support 29

Notes'

Notes' 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. Full support 25

Prefixed'

Prefixed' Implemented with the vendor prefix: -webkit-

Firefox Android Full support 20

Notes'

Full support 20

Notes'

Notes' Before Firefox 27, Firefox supported only single-line flexbox. No support 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. Full support 49

Prefixed'

Prefixed' Implemented with the vendor prefix: -webkit- Full support 48

Prefixed' Disabled'

Prefixed' Implemented with the vendor prefix: -webkit- Disabled' From version 48: this feature is behind the layout.css.prefixes.webkit preference (needs to be set to true). To change preferences in Firefox, visit about:config.

Opera Android

Full support 12.1

Safari iOS Full support 9


Full support 9


Full support 6.1

Prefixed'

Prefixed' Implemented with the vendor prefix: -webkit-

Samsung Internet Android Full support 6.0


Full support 6.0


Partial support 2.0

Notes'

Notes' 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. Full support 1.5

Prefixed'

Prefixed' Implemented with the vendor prefix: -webkit-

Supported in Grid Layout Chrome

Full support 57

Edge

Full support 16

Firefox

Full support 52

IE

No support No

Opera

Full support 44

Safari

Full support 10.1

WebView Android

Full support 57

Chrome Android

Full support 52

Firefox Android

Full support 52

Opera Android

Full support 43

Safari iOS

Full support 10.3

Samsung Internet Android

Full support 6.2

Legend

Full support  
Full support
No support  
No support
See implementation notes.'
See implementation notes.
User must explicitly enable this feature.'
User must explicitly enable this feature.
Requires a vendor prefix or different name for use.'
Requires a vendor prefix or different name for use.


align-items

Update compatibility data on 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 Full support 52


Full support 52


Partial support 29

Notes'

Notes' 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. Full support 21

Prefixed'

Prefixed' Implemented with the vendor prefix: -webkit-

Edge Full support 12


Full support 12


Full support 12

Prefixed'

Prefixed' Implemented with the vendor prefix: -webkit-

Firefox Full support 20

Notes'

Full support 20

Notes'

Notes' Multi-line flexbox has been supported since Firefox 28. Full support 49

Prefixed'

Prefixed' Implemented with the vendor prefix: -webkit- No support 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. Full support 48

Prefixed' Disabled'

Prefixed' Implemented with the vendor prefix: -webkit- Disabled' From version 48: this feature is behind the layout.css.prefixes.webkit preference (needs to be set to true). To change preferences in Firefox, visit about:config.

IE Full support 11

Notes'

Full support 11

Notes'

Notes' 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

Full support 12.1

Safari Full support 9


Full support 9


Full support 7

Prefixed'

Prefixed' Implemented with the vendor prefix: -webkit-

WebView Android Full support 52


Full support 52


Partial support 4.4

Notes'

Notes' 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. Full support ≤37

Prefixed'

Prefixed' Implemented with the vendor prefix: -webkit-

Chrome Android Full support 52


Full support 52


Partial support 29

Notes'

Notes' 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. Full support 25

Prefixed'

Prefixed' Implemented with the vendor prefix: -webkit-

Firefox Android Full support 20

Notes'

Full support 20

Notes'

Notes' Multi-line flexbox has been supported since Firefox 28. Full support 49

Prefixed'

Prefixed' Implemented with the vendor prefix: -webkit- No support 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. Full support 48

Prefixed' Disabled'

Prefixed' Implemented with the vendor prefix: -webkit- Disabled' From version 48: this feature is behind the layout.css.prefixes.webkit preference (needs to be set to true). To change preferences in Firefox, visit about:config.

Opera Android

Full support 12.1

Safari iOS Full support 9


Full support 9


Full support 7

Prefixed'

Prefixed' Implemented with the vendor prefix: -webkit-

Samsung Internet Android Full support 6.0


Full support 6.0


Partial support 2.0

Notes'

Notes' 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. Full support 1.5

Prefixed'

Prefixed' Implemented with the vendor prefix: -webkit-

Supported in Grid Layout Chrome

Full support 57

Edge

Full support 16

Firefox

Full support 52

IE

No support No

Opera

Full support 44

Safari

Full support 10.1

WebView Android

Full support 57

Chrome Android

Full support 52

Firefox Android

Full support 52

Opera Android

Full support 43

Safari iOS

Full support 10.3

Samsung Internet Android

Full support 6.2

Legend

Full support  
Full support
No support  
No support
See implementation notes.'
See implementation notes.
User must explicitly enable this feature.'
User must explicitly enable this feature.
Requires a vendor prefix or different name for use.'
Requires a vendor prefix or different name for use.


See also