HTML Topics
- HTML Intro
- HTML Basic
- HTML Editors
- HTML CSS
- HTML Tags
- HTML Deprecated Tags
- HTML Events
- HTML Event Attributes
- HTML Global Attributes
- HTML Attributes
- abbr
- accept
- accept-charset
- accesskey
- action
- align
- alt
- archive
- as
- async
- autocapitalize
- autocomplete
- autofocus
- autoplay
- axis
- bgcolor
- border
- cellpadding
- cellspacing
- char
- charoff
- charset
- checked
- cite
- class
- classid
- codebase
- codetype
- color
- cols
- colspan
- compact
- content
- contenteditable
- controls
- coords
- crossorigin
- data
- data-*
- datetime
- declare
- default
- defer
- dir
- dirname
- disabled
- download
- draggable
- enctype
- enterkeyhint
- for
- form
- formaction
- formenctype
- formmethod
- formnovalidate
- formtarget
- frame
- frameborder
- frameset
- headers
- height
- hidden
- high
- href
- hreflang
- hspace
- http-equiv
- id
- inert
- inputmode
- ismap
- kind
- label
- lang
- list
- loop
- low
- max
- maxlength
- media
- method
- min
- multiple
- muted
- name
- novalidate
- onabort
- onafterprint
- onbeforeprint
- onbeforeunload
- onblur
- oncanplay
- oncanplaythrough
- onchange
- onclick
- oncontextmenu
- oncopy
- oncuechange
- oncut
- ondblclick
- ondrag
- ondragend
- ondragenter
- ondragleave
- ondragover
- ondragstart
- ondrop
- ondurationchange
- onemptied
- onended
- onerror
- onfocus
- onhashchange
- oninput
- oninvalid
- onkeydown
- onkeypress
- onkeyup
- onload
- onloadeddata
- onloadedmetadata
- onloadstart
- onmousedown
- onmousemove
- onmouseout
- onmouseover
- onmouseup
- onmousewheel
- onoffline
- ononline
- onpagehide
- onpageshow
- onpaste
- onpause
- onplay
- onplaying
- onpopstate
- onprogress
- onratechange
- onreset
- onresize
- onscroll
- onsearch
- onseeked
- onseeking
- onselect
- onstalled
- onstorage
- onsubmit
- onsuspend
- ontimeupdate
- ontoggle
- onunload
- onvolumechange
- onwaiting
- onwheel
- open
- optimum
- pattern
- placeholder
- popover
- popovertarget
- popovertargetaction
- poster
- preload
- readonly
- rel
- required
- reversed
- rows
- rowspan
- sandbox
- scope
- selected
- shape
- size
- sizes
- span
- spellcheck
- src
- srcdoc
- srclang
- srcset
- start
- step
- style
- tabindex
- target
- title
- translate
- type
- usemap
- value
- width
- wrap
- HTML Comments
- HTML Entity
- HTML Head
- HTML Form
- HTML IndexedDB
- HTML Drag & Drop
- HTML Geolocation
- HTML Canvas
- HTML Status Code
- HTML Language Code
- HTML Country Code
- HTML Charset
- MIME Types
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:
<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:
<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:
Author
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
If you have any doubts regarding this article (HTML compact Attribute), please comment here. I will help you immediately.