Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I reset a CSS property rather than overriding it?

Tags:

html

css

The webpage I'm working on already has some base stylesheets, one of which contains this rule:

address:not(:last-child),  fieldset:not(:last-child),  li:not(:last-child),  ol:not(:last-child),  p:not(:last-child),  table:not(:last-child),  ul:not(:last-child) {margin-bottom: 12px} 

This is applying the 12px margin-bottom on my <p class="mainCopy"> tags, which I'm not interested in. I want to be able to override it with .mainCopy {margin-bottom: 0}, but obviously the base rule is more specific than my rule. This forces me to make a rule that's more specific than I want or need, such as p.mainCopy. Moreover, I sometimes need to have <li class="mainCopy">, and this would force me to add a second rule, to cater for the <li> as well.

Is there any way I can simply reset this property, or revert the problematic CSS declaration?

like image 807
Liran H Avatar asked Nov 16 '16 11:11

Liran H


People also ask

How do you reset your property in CSS?

Use the revert keyword to reset a property to the value established by the user-agent stylesheet (or by user styles, if any exist). Use the revert-layer keyword to reset a property to the value established in a previous cascade layer.

How do you override and remove CSS properties?

There are several ways to overwrite CSS properties from external libraries. Case 1: if you're using Bootstrap or Zurb Foundation via npm package, you need to change a variable value that is responsible for given property and place it after importing all library files to ovewrite correctyly eg.

What is reset rule in CSS?

A CSS Reset style sheet is a list of rules that 'reset' all of the default browser styles. We reset the browser styles for two primary reasons: Not all browsers apply the same default rules. They may be similar, but not exact.


2 Answers

Since address:not(:last-child) and similars has 11 points of specificity, you can duplicate the classname to make it stronger. For example, the following declaration is totally valid and it has 20 points of specificity:

.mainCopy.mainCopy {      margin-bottom: 0; } 

And you must add only one mainCopy in your html:

<ul class="mainCopy"> 

Edit

Take care on the "points" of specificity, because there aren't decimal points. Them are number positions by the specificity. For example:

 address:not(:last-child) /* is 0-0-1-1 specificity (1 tagname, 1 pseudoclass) */  .mainCopy.mainCopy /* is 0-0-2-0 specificity (2 classnames) */ 
like image 93
Marcos Pérez Gude Avatar answered Oct 03 '22 10:10

Marcos Pérez Gude


There exists the following property:

all: unset; 

Which I believe can be used like so:

.mainCopy {     all: unset;     margin-bottom: 0 } 

https://developer.mozilla.org/en/docs/Web/CSS/unset

EDIT: Actually I believe due to the specificity of your specific problem this may not work.

like image 38
ESR Avatar answered Oct 03 '22 09:10

ESR