HTML Basic
HTML Reference
- HTML Tags
- HTML Deprecated Tags
- HTML Events
- HTML Attributes
- accept
- accept-charset
- accesskey
- action
- align
- alt
- as
- async
- autocomplete
- autofocus
- autoplay
- bgcolor
- border
- charset
- checked
- cite
- class
- color
- cols
- colspan
- content
- contenteditable
- controls
- coords
- data
- data-*
- datetime
- default
- defer
- dir
- dirname
- disabled
- download
- draggable
- enctype
- enterkeyhint
- for
- form
- formaction
- headers
- height
- hidden
- high
- href
- hreflang
- 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 Global Attributes
- HTML Status Code
- HTML Language Code
- HTML Country Code
- HTML Charset
- MIME Types
HTML dirname Attribute
Photo Credit to CodeToFun
🙋 Introduction
The dirname
attribute in HTML is used to specify the text directionality of the content within an element.
This attribute is particularly useful when dealing with text that is not in the default direction of the document's language, such as right-to-left (RTL) text.
🎯 Purpose of dirname
The primary purpose of the dirname
attribute is to define the base directionality for the text contained within an element.
This helps browsers and user agents correctly render and display text that does not conform to the default text direction of the document.
💎 Values
The dirname
attribute accepts two main values:
- ltr: This is the default value and stands for left-to-right. It indicates that the text within the element should be displayed from left to right.
- rtl: This value stands for right-to-left. It is used when the text within the element should be displayed from right to left.
📄 Example
Let's look at a simple example of how to use the dirname
attribute in an HTML element:
<p dirname="rtl">This text will be displayed from right to left.</p>
🧠 How it Works
In this example, the dirname
attribute is set to "rtl" for the paragraph element, indicating that the text inside the paragraph should be rendered from right to left.
🔄 Dynamic Values with JavaScript
Similar to other attributes, the dirname
attribute can be dynamically set using JavaScript.
This is particularly useful when you need to adjust text direction based on user interactions or specific conditions. Here's a brief example:
<script>
// Dynamically set the dirname attribute for an element
document.getElementById("dynamicText").dirname = "ltr";
</script>
🧠 How it Works
In this script, the dirname
attribute is set to ltr for an element with the id dynamicText. This dynamic adjustment can be beneficial when dealing with interactive content or changing language preferences.
🏆 Best Practices
- Use the
dirname
attribute when dealing with text that deviates from the default text directionality of the document. - Choose the appropriate value (ltr or rtl) based on the language and writing direction of the text.
- Test your pages across different browsers to ensure consistent rendering, especially when dealing with RTL languages.
🎉 Conclusion
The dirname
attribute is a valuable tool for specifying the text directionality within HTML elements.
By using this attribute appropriately, you can ensure that your text is displayed correctly, enhancing the readability and user experience 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 dirname Attribute), please comment here. I will help you immediately.