0

I would like to add a class to elements, it works but would like to know if it's legal.

unor
  • 92,415
  • 26
  • 211
  • 360
chandan
  • 1,574
  • 4
  • 23
  • 52
  • 1
    Please, refer to this answer: http://stackoverflow.com/a/3919471/4443053 Basically, yes you can do that (it is legal). – Leonel Atencio May 13 '16 at 21:10
  • 1
    Can you explain why you thought it might not be legal? Is there some website that hints it's not or something? – Mr Lister May 15 '16 at 12:21
  • @MrLister: I though they were used exclusively declare different parts of a document and using ids and classes was not valid code and would show error in w3c validator. – chandan May 15 '16 at 19:01

1 Answers1

1

yes it is fine to add classes or id's to any valid markup such as <header>, but i would do it only if it adds further value to the selectivity, since you can always target it as

//css
header {//CSS style rules}

but if you want to add it it will be valid and correct

   //css
 .someClass{//CSS style rules}

//html
<header class='someClass'>Title</header >
gavgrif
  • 15,194
  • 2
  • 25
  • 27
  • I was concerned about the newly added elements. – chandan May 13 '16 at 21:14
  • Newly added elements like
    etc
    – chandan May 13 '16 at 21:16
  • yes - sorry - just got that from re-reading your question title. Answer amended to reflect that :)) – gavgrif May 13 '16 at 21:16
  • 2
    It is not only acceptable but it is also encouraged if you are going to be assigning styles to that element. The trend now is to use class selectors instead of element selectors in your css. This is because class selectors resolve quicker then do element selectors. Plus it keeps you from relying on descendent selectors which can cause specificity issues. And, yes, every single HTML element accepts class and id attributes. – haltersweb May 13 '16 at 21:17
  • For reference you can check what's acceptable on any HTML element in the W3 spec: https://www.w3.org/TR/html-markup/header.html#header "Permitted Attributes" are all global attributes: https://www.w3.org/TR/html-markup/global-attributes.html – skyline3000 May 13 '16 at 21:27