Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reset margin-bottom to its default value

Tags:

html

css

I'd like to be able to set the margin-bottom of an element to its default value.

Consider the following example in which there are h1 elements which have their respective margin-bottom style properties set to 0:

h1 {
  border: 1px solid red;
  margin-bottom: 0;
}

p {
  margin: 0;
}
<h1>First Heading</h1>
<p>Paragraph</p>

<h1 id="normal-margin">Second Heading</h1>
<p>Paragraph</p>

How can I reset the margin-bottom value of #normal-margin to its initial, default value? Obviously using initial won't work, as the initial value of margin-bottom is 0.

I realise in this trivial example I can simply add :not(#normal-margin) to the style definition of h1 to fix the issue. I would however like a solution which would “undo” the margin and reset it to its initial value.

I’m thinking that I’m going to have to hard-code values into the CSS, which to me seems a bit cheap. Is that the only solution to this problem?

like image 337
Jack Greenhill Avatar asked Sep 23 '15 10:09

Jack Greenhill


People also ask

How do you reset margins in HTML?

You can remove this margin by setting the top and left margin to zero. Like the padding and border, the sizes of specific sides of the margin can be set using margin-left , margin-right , margin-top , and margin-bottom .

Is margin Auto the default?

All current browsers have margin: 0 as the default margin for div elements. margin: 0 auto is not the same thing -- if the div has a fixed width, margin: 0 auto creates a div that is horizontally centered in its container. Save this answer.


1 Answers

Currently it's not possible. In some future, use the revert keyword

As explained in 7.3. Explicit Defaulting, there are 4 diferent defaulting behaviors:

  • initial sets a property to its initial value.

    In the case of margin-*, that's 0.

  • inherit sets a property to the value of the parent element (or to the initial value for the root element).

  • unset behaves as inherit for inherited properties, and as initial otherwise.

    margin-* are not inherited, so it would produce 0.

  • The behavior of revert depends on the origin of the declaration

    • For user-agent origin, it behaves as unset
    • For user origin, it rolls back the cascade to the user-agent level, so that the specified value is calculated as if no author-level or user-level rules were specified for the property.
    • For author origin, it rolls back the cascade to the user level, so that the specified value is calculated as if no author-level rules were specified for the property.

So the behavior you want is revert's one, i.e.

  • The user-agent adds some margin to h1 elements
  • One of your (author) declarations removes that margin
  • revert rolls back that to the margin defined by the user-agent

Note revert is a recent addition to the CSS Cascading and Inheritance Level 4 spec, which is still only a draft. Therefore, browsers don't support it yet.

like image 142
Oriol Avatar answered Oct 17 '22 10:10

Oriol