Which attribute is applied if both id and class are specified for an element and why?

Ankur Agarwal

I have this:

<!DOCTYPE html>
<html>
    <head>
        <link type="text/css" rel="stylesheet" href="stylesheet.css"/>
        <title>My Social Network</title>
    </head>
    <body>
        <div id="best_friend" class="friend" ></div>
        <div class="family"></div>
        <div class="enemy" id="archnemesis"></div>

    </body>
</html>


div {
    display: inline-block;
    margin-left: 5px;
    height: 100px;
    width: 100px;
    border-radius:100%;
    border: 2px solid black;

}

#best_friend {
 border: 4px solid #00C957;   
}

.friend {
 border: 2px dashed #008000;   
}

.family {

 border: 2px dashed #0000FF;   

}

.enemy {
  border: 2px dashed #FF0000;   

}

#archnemesis {
 border: 4px solid #CC0000;   
}

My question is: Notice how I define border for both class and id. The border that is applied is the one coming from the id. Why so? Why is the border specification in id overriding the one in class.

EternalHour

The browser determines which styles to apply in what order based on Specificity, the higher number determines which will be applied.

Universal selectors have a specificity of 0,0,0,0.
* = 0

HTML selectors have a specificity of 1.
p, div, etc.. = 1 each

So each HTML selector adds to the specificity.
div p = 2, div p span = 3

Class selectors have a specificity of 10.
.class = 10

Class selectors combined with HTML selectors.
div p.class = 12

ID selectors have a specificity value of 100.
#id = 100

ID selectors combined with HTML selectors.
#id div = 101

!important overrides all other styles unless combined with another selector. table td {height: 50px !important;} Would override any height style applied to only a td within a table.

Inline styles have the highest specificity of 1000.
style= = 1000

Useful resources

CSS Specificity: Things You Should Know
Specificity | HTML Dog

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Which class will be applied to div and why?

From Dev

Why is the "hidden" class applied to an element BEFORE it is clicked?

From Dev

Can an element have both id and attribute at the same time?

From Dev

The class is added to the HTML element, but the CSS rules are not applied at all. Why?

From Dev

Check if Div which has both "unique class and Id" exist

From Dev

Get the class on which the annotation is applied

From Dev

A directive which can be element, attribute, css class and comment?

From Dev

A directive which can be element, attribute, css class and comment?

From Dev

How to select element which doesn't has Id attribute?

From Dev

Assign id to Input element which has only name attribute

From Dev

How to same element with unique XPath/CSS selector by both ID and Class?

From Dev

jQuery: selecting an element having a certain id and a certain class, both as variables

From Dev

XSD enumerations on both element and attribute

From Dev

Getting class or ID name of an element based on the value of a particular attribute

From Dev

why css class not applied in checkbox?

From Dev

Why does WordPress remove my code element's class attribute?

From Dev

Undoing an applied style attribute with another class

From Dev

nokogiri each attribute in id - @id in both nodes

From Dev

Styles applied to custom polymer element only inside an element with layout attribute

From Dev

At which devices is the 'screen' attribute (CSS media query) applied to?

From Dev

At which devices is the 'screen' attribute (CSS media query) applied to?

From Dev

hide an element base on the specified data attribute

From Dev

Why is code not applied to each element with in page

From Dev

Can we target a element which has (data-toggle="collapse") attribute and no class using jquery?

From Dev

Why does the javac error "(x) cannot be applied to (y)", happen when both parameters and arguments match up? (inner-class calling outer-class method)

From Dev

Declare an attribute that both extends a class and implements an interface

From Dev

Change style of an element which contains a specified word

From Dev

Why does calling of test() function of Foo class (which extends Bar class) returns result mixed from both classes?

From Dev

Why is my css Class not being applied consistently?

Related Related

  1. 1

    Which class will be applied to div and why?

  2. 2

    Why is the "hidden" class applied to an element BEFORE it is clicked?

  3. 3

    Can an element have both id and attribute at the same time?

  4. 4

    The class is added to the HTML element, but the CSS rules are not applied at all. Why?

  5. 5

    Check if Div which has both "unique class and Id" exist

  6. 6

    Get the class on which the annotation is applied

  7. 7

    A directive which can be element, attribute, css class and comment?

  8. 8

    A directive which can be element, attribute, css class and comment?

  9. 9

    How to select element which doesn't has Id attribute?

  10. 10

    Assign id to Input element which has only name attribute

  11. 11

    How to same element with unique XPath/CSS selector by both ID and Class?

  12. 12

    jQuery: selecting an element having a certain id and a certain class, both as variables

  13. 13

    XSD enumerations on both element and attribute

  14. 14

    Getting class or ID name of an element based on the value of a particular attribute

  15. 15

    why css class not applied in checkbox?

  16. 16

    Why does WordPress remove my code element's class attribute?

  17. 17

    Undoing an applied style attribute with another class

  18. 18

    nokogiri each attribute in id - @id in both nodes

  19. 19

    Styles applied to custom polymer element only inside an element with layout attribute

  20. 20

    At which devices is the 'screen' attribute (CSS media query) applied to?

  21. 21

    At which devices is the 'screen' attribute (CSS media query) applied to?

  22. 22

    hide an element base on the specified data attribute

  23. 23

    Why is code not applied to each element with in page

  24. 24

    Can we target a element which has (data-toggle="collapse") attribute and no class using jquery?

  25. 25

    Why does the javac error "(x) cannot be applied to (y)", happen when both parameters and arguments match up? (inner-class calling outer-class method)

  26. 26

    Declare an attribute that both extends a class and implements an interface

  27. 27

    Change style of an element which contains a specified word

  28. 28

    Why does calling of test() function of Foo class (which extends Bar class) returns result mixed from both classes?

  29. 29

    Why is my css Class not being applied consistently?

HotTag

Archive