To help determine if you're writing valid CSS rules with concern for attributes, you can find a list of HTML attributes here: https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes
Technically, you never set any HTML attribute with CSS. HTML attributes are used to configure aspects of an element's functionality, not style (though there is some stylistic crossover discussed later). Attributes are set inline or through JavaScript and can be used as selectors in CSS to style elements, but their values are never changed or affected by CSS.
The difference is that, in CSS, you are declaring the value of style properties as opposed to element functionality. In fact, style
is a global attribute of HTML elements that accepts a value of inline CSS rules and functions by applying those styles. Even with pseudo-classes like input:checked { color: red; }
, the CSS does not cause the input element to be checked. Rather, it adds a red value of the color
property to any input element that is checked (for instance, explicitly with an attribute on the html: <input checked />
.
Likewise, in your example [draggable=true]{ property: value; }
you are not changing the functionality of any element to be draggable
. Instead, you are selecting any element that has a defined draggable
attribute and adding style properties to it via the CSS declaration.
Whereas in your example div { draggable: true; }
you are attempting to set div
s to have a true style of draggable which doesn't work because draggable
is not a style property.
There are some legacy attributes whose functionality affects style and appear to be the same as CSS properties (i.e. width
, height
, color
). These are the ones that likely cause the confusion between style properties and element attributes as they do blur the line.