CSS Performance between class and attribute selectors

前端 未结 4 1501
我寻月下人不归
我寻月下人不归 2021-01-04 07:47

I\'m wondering if there is a performance issue using attribute selectors instead of class selectors.

div.test {}
div[test] {}

P.S. : I\

4条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-04 08:39

    There is no performance issue. Both act same. But there is difference in specificity of the css with class versus Elements.

    Specificity - Specificity determines, which CSS rule is applied by browsers.

    If two selectors apply to the same element, the one with higher specificity wins.

    But specificity has hierarchy.

    1. Inline styles (Presence of style in document). An inline style lives within your XHTML document. It is attached directly to the element to be styled. E.g.
    2. IDs (# of ID selectors) ID is an identifier for your page elements, such as #div.
    3. Classes, attributes and pseudo-classes (# of class selectors). This group includes .classes, [attributes] and pseudo-classes such as :hover, :focus etc.
    4. Elements and pseudo-elements (# of Element (type) selectors). Including for instance :before and :after.

    Source: http://coding.smashingmagazine.com/2007/07/27/css-specificity-things-you-should-know/

    Hence div.test {} is more specific.

提交回复
热议问题