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 draggable Attribute
Photo Credit to CodeToFun
🙋 Introduction
The draggable
attribute in HTML is a powerful feature that allows developers to control the drag-and-drop behavior of elements on a web page.
By specifying the draggable
attribute, you can make elements draggable by users, providing a more interactive and dynamic user interface.
🎯 Purpose of draggable
The primary purpose of the draggable
attribute is to define whether an element can be dragged by the user.
This attribute can be applied to a variety of HTML elements, such as images, text, or entire containers, enabling the creation of intuitive and user-friendly interfaces.
💎 Values
The draggable
attribute has two main values:
- true: When set to true, the element is marked as draggable, allowing users to initiate drag-and-drop operations on the specified element.
- false: This is the default value. When set to false or not specified, the element is not draggable, and users cannot initiate drag-and-drop operations.
📄 Example
Let's explore a simple example of using the draggable
attribute in HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Draggable Element Example</title>
<style>
.draggable-element {
width: 100px;
height: 100px;
background-color: #3498db;
color: #fff;
text-align: center;
line-height: 100px;
cursor: move;
}
</style>
</head>
<body>
<div class="draggable-element" draggable="true">Drag me!</div>
</body>
</html>
🧠 How it Works
In this example, a div element with the class draggable-element is made draggable by setting the draggable
attribute to "true."
The element is styled to be visually identifiable and has a "move" cursor to indicate draggability.
🔄 Dynamic Values with JavaScript
The draggable
attribute can be dynamically updated using JavaScript.
This allows you to change the draggability of an element based on user interactions or specific conditions. Here's a brief example:
<script>
// Dynamically set draggable attribute to true
document.getElementById("dynamicDraggable").draggable = true;
</script>
🧠 How it Works
In this script, the draggable
attribute is set to true for an element with the id dynamicDraggable. This dynamic approach can be useful for creating more interactive and responsive web applications.
🏆 Best Practices
- Use the
draggable
attribute to enhance user experience by making relevant elements draggable. - Ensure that elements with drag-and-drop functionality have a clear indication of their draggability, such as a distinct appearance or cursor change.
- Test the behavior of draggable elements across different browsers to ensure consistent functionality.
🎉 Conclusion
The draggable
attribute is a valuable tool for adding drag-and-drop capabilities to elements on your web page.
By understanding and implementing this attribute, you can create more engaging and interactive user interfaces.
👨💻 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 draggable Attribute), please comment here. I will help you immediately.