Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

HTML Basic

HTML Reference

HTML compact Attribute

Posted in HTML Tutorial
Updated on Sep 22, 2024
By Mari Selvan
👁️ 38 - Views
⏳ 4 mins
💬 1 Comment
HTML compact Attribute

Photo Credit to CodeToFun

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

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

💎 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, or its value is not explicitly set to "compact," the browser will render the list with default spacing between items.

📄 Implementation Example:

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

index.html
Copied
Copy To Clipboard
<ul compact>
  <li>Item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>
</ul>

🧠 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:

index.html
Copied
Copy To Clipboard
<script>
  // Dynamically set compact attribute for a list
  document.getElementById("dynamicList").compact = true;
</script>

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

🏆 Best Practices

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

👨‍💻 Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy