Which css style can override other? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Which css style can override other?

An element can have an id and many classes. So, i want to know which style (#id or .class1 or .class2 ) overrides which other styles. https://code.sololearn.com/WT2uHsriiMlk/?ref=app

17th Jul 2020, 4:36 AM
Sameer Crestha
Sameer Crestha - avatar
4 Answers
17th Jul 2020, 6:08 AM
Hanuma Ukkadapu
Hanuma Ukkadapu - avatar
+ 1
Sameer Crestha I think the order in which you place them is important. As the css file renders from top to bottom the latest style would be applied. For Ex. --------- #id{ border: 1px solid #222; } .class{ border: 2px solid red; } -------- In this case the style defined by .class will be applied and creates a 2px wide red coloured border.
17th Jul 2020, 4:43 AM
Hanuma Ukkadapu
Hanuma Ukkadapu - avatar
+ 1
If #child style applied to a child & #parent style applied to it's parent , which overrides?or does it happens per order too?
17th Jul 2020, 4:50 AM
Sameer Crestha
Sameer Crestha - avatar
+ 1
Sameer Crestha If you select them with their ids and apply those styles, they will applied seperately i.e. parent styles doesn't effect child. like: Ex. HTML ------- <div id="parent"> <div id="child"></div> </div> ------- CSS ------- #parent{ border: 1px solid #222; } #child{ border: 2px solid red; } ------- Result: Child will have a 2px wide red border. Interestingly enough, when you select the #child element as a child of the #parent element, it doesn't care about order: CSS ------- #parent div{ border: 1px solid #222; } #child{ border: 2px solid red; } -------- Result: The child element has a 1px wide black border. Even though the #child element is specifically selected and placed below, the styles defined by #parent div{} are applied to #child element. See this code: https://code.sololearn.com/W2UOK5pT6AWx/?ref=app
17th Jul 2020, 5:11 AM
Hanuma Ukkadapu
Hanuma Ukkadapu - avatar