HTML compact Attribute

Beginner
⏱️ 4 min read
📚 Updated: Jun 2026
🎯 2 Examples
Lists & Layout

Introduction

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.

What You’ll Learn

01

Boolean Attr

Present or absent.

02

ul and ol

Primary elements.

03

Tight Lists

Less item spacing.

04

CSS Alternative

margin, line-height.

05

JavaScript

.compact = true

06

Deprecated

Use CSS instead.

Purpose of compact Attribute

The 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.

⚠️
Deprecated in HTML5

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.

📝 Syntax

Add the boolean compact attribute to a list element:

compact.html
<ul compact>
  <li>Item 1</li>
  <li>Item 2</li>
</ul>

<ol compact>...</ol>

Syntax Rules

  • Boolean attribute — presence means compact; no value required.
  • Valid on <ul>, <ol>, and legacy <menu>, <dir>, <dl>.
  • Do not use compact="false"; remove the attribute to disable.
  • For modern layouts, style li elements with CSS instead.

💎 Values

The compact attribute accepts a Boolean value:

  • compact — When set, it indicates that the browser should render the list with reduced spacing between items.
  • not set — If the compact attribute is not present, the browser renders the list with default spacing between items.
compact-values.html
<!-- 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>

⚡ Quick Reference

StateMarkupCSS 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
JavaScriptlist.compact = trueToggle a CSS class
HTML5 statusDeprecatedUse CSS for new code
Modern browsersOften ignoredCSS is reliable

Applicable Elements

ElementSupported?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>NoSet compact on the list, not items
<table>NoDifferent attribute family

Examples Gallery

Implementation example with compact on ul and dynamic JavaScript adjustment.

👀 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

Implementation Example

Here’s an example of how to use the compact attribute in an HTML list:

index.html
<ul compact>
  <li>Item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>
</ul>
Try It Yourself

How It Works

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.

Dynamic Values with JavaScript

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:

dynamic-compact.html
<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>
Try It Yourself

How It Works

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.

♿ Accessibility

  • Readability first — Overly tight list spacing can hurt readability for low-vision users.
  • Do not sacrifice touch targets — Compact lists in interactive menus need adequate click area.
  • Use semantic lists — Keep <ul> and <ol> for real lists, not layout hacks.
  • Test with screen readers — List structure matters more than visual density.
  • Prefer CSS with care — Maintain sufficient line-height when tightening spacing.

🧠 How compact Works

1

Author adds compact

Include the boolean attribute on a list element.

Markup
2

Browser reads the hint

Legacy user agents may reduce vertical space between items.

Parse
3

List is rendered

Modern browsers often ignore compact and use default spacing.

Render
=

Condensed list (legacy)

Use CSS list spacing in modern projects for predictable results.

Browser Support

The compact attribute is deprecated in HTML5. Most modern browsers no longer apply visible compact styling to lists.

⚠️ HTML4 legacy · Deprecated

Largely ignored today

Use CSS margin and line-height on list items for reliable spacing control.

Legacy Visual effect
Google Chrome Mostly ignored
Deprecated
Mozilla Firefox Mostly ignored
Deprecated
Apple Safari Mostly ignored
Deprecated
Microsoft Edge Mostly ignored
Deprecated
compact attribute Deprecated

Bottom line: Do not rely on compact for layout. CSS list styling works consistently across browsers.

💡 Best Practices

✅ Do

  • Use CSS to control list item spacing in new projects
  • Test readability when tightening list layout
  • Keep semantic ul/ol structure for accessibility
  • Use utility classes for consistent tight lists
  • Document legacy compact usage when maintaining old HTML

❌ Don’t

  • Depend on compact for visible spacing in modern browsers
  • Squeeze lists so much that text becomes hard to read
  • Put compact on individual li elements
  • Use lists purely for layout without list semantics
  • Ignore mobile touch target size when compacting menus
  • Use the compact attribute sparingly and considerately, as reducing spacing between list items may affect readability and accessibility.
  • Test the visual presentation of lists with and without the compact attribute to ensure it aligns with your design goals and user experience objectives.

Conclusion

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.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about compact

Bookmark these before tightening your HTML lists.

5
Core concepts
📝 02

ul and ol

List elements.

Scope
💻 03

CSS Wins

Reliable spacing.

Modern
⚙️ 04

.compact

JS property.

Script
05

Readability

Don’t over-tighten.

A11y

❓ Frequently Asked Questions

It requests reduced spacing between list items on elements like <ul> and <ol>.
It is deprecated. Modern browsers often ignore it. Use CSS for list spacing instead.
<ul>, <ol>, and legacy <dl>, <menu>, <dir> elements.
Reduce li spacing with margin-block and line-height, or apply a utility class to the list.
Yes. Use listElement.compact = true or setAttribute("compact", ""). CSS classes are more reliable for visible changes.
Generally no visible effect in modern browsers. Do not rely on compact for layout.

Style HTML lists the modern way

Practice the legacy compact attribute and compare it with CSS list spacing in the Try It editor.

Try compact list example →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

2 people found this page helpful