Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is position initial in CSS

Tags:

css

I was searching for the difference between position:static; and position:initial; when I found the developer mozilla docs page where position:static;'s description is given but position: initial/inherit/unset; are global values . What are those global values meant for?

like image 889
Arun Avatar asked May 27 '16 10:05

Arun


People also ask

What does initial mean in CSS?

The initial CSS keyword applies the initial (or default) value of a property to an element. It can be applied to any CSS property. This includes the CSS shorthand all, with which initial can be used to restore all CSS properties to their initial state. On inherited properties, the initial value may be unexpected.

What is position in CSS?

Position In CSS. Position is one of the core properties… | by Abhimanyu Chauhan | The Startup | Medium Position is one of the core properties of CSS like display or color. A better understanding of how you can position the elements on your html document, can help you better visualise the end result.

How do I change the position of an element in CSS?

On a Window's machine, right click on the element you want to select. A menu will then appear and from there select Inspect. The Chrome Developer Tools will open. Select the Computed tab and from there either scroll down to the position element or in the filter search box, type in position. What is the default position of HTML elements in CSS?

What is an absolutely positioned element in HTML?

An absolutely positioned element is an element whose computed position value is absolute or fixed. The top, right, bottom, and left properties specify offsets from the edges of the element's containing block.


4 Answers

initial is not only used for position, it is a generic value which you can set it for any property in CSS.

From MDN:

The initial CSS keyword applies the initial value of a property to an element. It is allowed on every CSS property and causes the element for which it is specified to use the initial value of the property.

When using initial value, it will pick the value as defined in the spec as properties default.

Worth noting that this is not supported in IE at all as of now, not even in IE 11, so don't use it unless you dumping support for IE users.

How this works?

Think of it that you have an element p with nested span with color set to orange, and later you use initial for span it will fall to a default value as mentioned in w3c spec.

Demo

span {
  color: red; 
  /* won't make any difference */
}

p {
  color: orange;
}

p span {
  color: initial; 
  /* sets to initial value defined in spec/user-agent */
}

Do not confuse it with inherit

inherit is something different all-together compared to initial. Where initial will pick the value from :root, inherit will take the value from it's parent. :root here am talking about initial values defined in the spec and is adapted by browsers.

Demo

p {
  margin-left: 20px;
}

p span {
  margin-left: inherit;
}

inherit is useful when the parent property value is not inherited by default, for example margin. Here, it will pick the value from it's parent declaration and not the :root

like image 143
Mr. Alien Avatar answered Oct 21 '22 01:10

Mr. Alien


The value initial can be used on most properties - it's not unique to position... It simply means to default back to the browser's initial value for that element. For example:

h2 {
    display: inline-block;
}

The above overrides the browser default setting for a h2 element, which is display: block.

Further down the line, if you want to override one particular h2 to back to its default block value, you can do this:

.parent h2 {
    display: initial;
}

Which would put the display back to block for that h2 in this case.

like image 45
David Wilkinson Avatar answered Oct 20 '22 23:10

David Wilkinson


position: initial means that it will catch the default(initial) value so it will catch position: static.

You have to use position: initial when you have changed the default value of the position of an element and you want to back to the default CSS position property for this element.

like image 31
Francisco Romero Avatar answered Oct 21 '22 00:10

Francisco Romero


Initial puts the elements position back to the default. So if we want all <p> elements to have margin except one which we want to be default then we can set that.

div p {
  margin-left: 20px;
}

.special {
   margin-left: initial;
}

See in more detail in this example: https://jsfiddle.net/s9zcsgfs/1/

You can also use it on things like color

like image 44
Ben Rhys-Lewis Avatar answered Oct 20 '22 23:10

Ben Rhys-Lewis