👀 Live Preview
Compare default spacing vs a CSS tight list (modern equivalent of compact):
Default
- Item 1
- Item 2
- Item 3
Tight (CSS)
- Item 1
- Item 2
- Item 3

The compact attribute is used in HTML to control the spacing between elements, particularly within lists. It is commonly applied to <ul> and <ol> elements to specify whether the browser should render them with reduced spacing between list items.
Present or absent.
Primary elements.
Less item spacing.
margin, line-height.
.compact = true
Use CSS instead.
compact AttributeThe primary purpose of the compact attribute is to adjust the visual presentation of lists by reducing the space between items. This can be useful for creating more condensed lists, especially when dealing with large amounts of content or when space is limited.
The compact attribute is not recommended for new projects. Most modern browsers ignore it or render lists with default spacing. Use CSS to control list item spacing instead.
Add the boolean compact attribute to a list element:
<ul compact>
<li>Item 1</li>
<li>Item 2</li>
</ul>
<ol compact>...</ol><ul>, <ol>, and legacy <menu>, <dir>, <dl>.compact="false"; remove the attribute to disable.li elements with CSS instead.The compact attribute accepts a Boolean value:
compact attribute is not present, the browser renders the list with default spacing between items.<!-- Compact (attribute present) -->
<ul compact>...</ul>
<!-- Default spacing (attribute absent) -->
<ul>...</ul>
<!-- CSS alternative -->
<style>
.tight-list li { margin-block: 0.15rem; line-height: 1.35; }
</style>
<ul class="tight-list">...</ul>| State | Markup | CSS Alternative |
|---|---|---|
| Compact list | <ul compact> | li { margin-block: 0.15rem; } |
| Default list | <ul> | Browser default spacing |
| Ordered list | <ol compact> | Same CSS on ol li |
| JavaScript | list.compact = true | Toggle a CSS class |
| HTML5 status | Deprecated | Use CSS for new code |
| Modern browsers | Often ignored | CSS is reliable |
| Element | Supported? | Notes |
|---|---|---|
<ul> | Yes (legacy) | Unordered lists |
<ol> | Yes (legacy) | Ordered lists |
<dl> | Yes (legacy) | Description lists in HTML4 |
<menu>, <dir> | Yes (legacy) | Obsolete list types |
<li> | No | Set compact on the list, not items |
<table> | No | Different attribute family |
Implementation example with compact on ul and dynamic JavaScript adjustment.
Compare default spacing vs a CSS tight list (modern equivalent of compact):
Default
Tight (CSS)
Here’s an example of how to use the compact attribute in an HTML list:
<ul compact>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>In this example, the compact attribute is applied to the <ul> element, indicating that the browser should render the list with reduced spacing between its items.
You can dynamically set the compact attribute using JavaScript, allowing you to adjust the spacing of lists based on certain conditions or user interactions. Here’s a basic example:
<ul id="dynamicList">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
<script>
// Dynamically set compact attribute for a list
document.getElementById("dynamicList").compact = true;
</script>In this script, the compact attribute is set to true for a list with the id dynamicList. This can be particularly useful when you need to dynamically adjust the presentation of lists based on changes in the content or layout of your web page. For new code, toggling a CSS class is more reliable.
<ul> and <ol> for real lists, not layout hacks.Include the boolean attribute on a list element.
Legacy user agents may reduce vertical space between items.
Modern browsers often ignore compact and use default spacing.
Use CSS list spacing in modern projects for predictable results.
The compact attribute is deprecated in HTML5. Most modern browsers no longer apply visible compact styling to lists.
Use CSS margin and line-height on list items for reliable spacing control.
Bottom line: Do not rely on compact for layout. CSS list styling works consistently across browsers.
compact attribute sparingly and considerately, as reducing spacing between list items may affect readability and accessibility.compact attribute to ensure it aligns with your design goals and user experience objectives.The compact attribute provides a simple yet effective way to control the spacing of lists in HTML, allowing developers to create more condensed layouts when necessary. By understanding how to use this attribute correctly, you can enhance the presentation and usability of your web pages.
For modern development, reach for CSS instead. Style li elements with controlled margin and line-height to achieve compact lists that render consistently in every browser.
compactBookmark these before tightening your HTML lists.
Present = compact.
ValuesList elements.
ScopeReliable spacing.
ModernJS property.
ScriptDon’t over-tighten.
A11y<ul> and <ol>.<ul>, <ol>, and legacy <dl>, <menu>, <dir> elements.li spacing with margin-block and line-height, or apply a utility class to the list.listElement.compact = true or setAttribute("compact", ""). CSS classes are more reliable for visible changes.Practice the legacy compact attribute and compare it with CSS list spacing in the Try It editor.
2 people found this page helpful