25

What are all the valid DIV attributes?

Based on this link, it appears only class, id, title. Is that correct? Meaning, the for attribute and others is not valid for DIV?

7
  • 2
    What would you expect <div for="..."> to do? Commented Feb 2, 2010 at 16:44
  • 18
    @Lazarus - I think the best comment would be to educate JasonK on the proper place to find this information like Sinan Ünür has done. Rather than calling his question lazy. He may be new to web development and not actually know where to find the answer. Commented Feb 2, 2010 at 19:19
  • @Bryan, if you are happy to do what is simple, basic investigation using Google or similar, just like JasonK could have done, for him then go ahead. I will continue to be disappointed that people here continue to pander to the lazy, and it is lazy, it's clear he didn't try for more than 30 seconds to find the information before deciding to get someone here to do it for them. Sinan didn't educate him in how to find the information, he gave it to him. "Search using Google" is education in how to find the information. Commented Feb 3, 2010 at 10:31
  • 4
    @Lazarus - Sinan Ünür did educate him on how to find this information. "See W3C specs:" He was also helpful in providing the information in his answer rather than blindly linking to the W3C spec. I'm sorry, but this site becomes useless when one answers with "Search using Google"...or refuses to answer at all when they know the information. Commented Feb 5, 2010 at 5:23
  • 8
    @Lazarus This is the first search result from google, "non-lazy" people in the future searching for this will come here. Commented Nov 22, 2013 at 2:22

4 Answers 4

38

See W3C specs:

  • id, class (document-wide identifiers)
  • lang (language information), dir (text direction)
  • title (element title)
  • style (inline style information)
  • align (alignment)
  • onclick, ondblclick, onmousedown, onmouseup, onmouseover, onmousemove, onmouseout, onkeypress, onkeydown, onkeyup

for is for label and specifies the id attribute of the input element to which the label applies.

See also the HTML5 specs:

The div element has no special meaning at all. It represents its children. It can be used with the class, lang, and title attributes to mark up semantics common to a group of consecutive elements.

Note: Authors are strongly encouraged to view the div element as an element of last resort, for when no other element is suitable. Use of more appropriate elements instead of the div element leads to better accessibility for readers and easier maintainability for authors.

Sign up to request clarification or add additional context in comments.

Comments

14

With HTML5, besides using the global attributes which can be used on any element, you can also define your own attributes using the data- attributes.

<div class="marvellous" data-for="xy" data-another-attribute="nothing"></div> 

John Resig on data- attributes

All you have to do is use the HTML5 doctype (<!DOCTYPE html>) and your page will be valid. This doctype works fine even in older browsers.

2 Comments

"You can use any attribute if you give them the data- prefix:": This is a misleading oversimplification, certainly not worth bumping this thread. data- attributes are not synonyms for other attributes, they are generic and mean nothing on their own.
@Madmartigan You are right about that. Deleted the misleading sentence. I added this information because the answers to this question are a bit outdated.
5

A list of attributes is available here: https://www.w3schools.com/tags/tag_div.asp

  • class
  • dir
  • id
  • lang
  • style
  • title
  • xml:lang

Plus all the event attributes:

  • onclick
  • ondblclick
  • onmousedown
  • onmousemove
  • onmouseout
  • onmouseover
  • onmouseup
  • onkeydown
  • onkeypress
  • onkeyup

Comments

3

contenteditable attribute also helpful and rarely used.

<div contenteditable="true">Using this on runtime content can changed.</div> 

This attribute is provide a way to use this like a Normal textbox. but text changed on browser side is not saved in coding.

FIDDLE

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.