Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relative padding is relative to what?

Tags:

css

padding

If I style an <h1> element with padding: 1.2em or padding: 10%, is that relative to the:

  1. font-size of the <h1> element?
  2. height of the <h1> element?
  3. padding of the parent element?
  4. something else?

And is it different for em and %?

like image 345
koen Avatar asked Nov 21 '11 11:11

koen


People also ask

What is a relative unit of measurement?

What is a relative unit? A relative unit gets sizing from something else. In the specification the relative length units are defined as em , ex , ch and rem . These are font-relative lengths. The specification also defines a % value, which is always relative to another value.

What are the relative units in CSS?

Relative length unitsFont size of the parent, in the case of typographical properties like font-size , and font size of the element itself, in the case of other properties like width . x-height of the element's font. The advance measure (width) of the glyph "0" of the element's font. Font size of the root element.

What is the padding in CSS?

CSS Demo: padding An element's padding area is the space between its content and its border. Note: Padding creates extra space within an element. In contrast, margin creates extra space around an element.

What are the different padding properties in CSS?

All CSS Padding Properties Property Description padding-bottom Sets the bottom padding of an element padding-left Sets the left padding of an element padding-right Sets the right padding of an element padding-top Sets the top padding of an element 1 more rows ...

What are the values of the padding properties?

All the padding properties can have the following values: 1 length - specifies a padding in px, pt, cm, etc. 2 % - specifies a padding in % of the width of the containing element 3 inherit - specifies that the padding should be inherited from the parent element

What happens when padding is added to the width of an element?

So, if an element has a specified width, the padding added to that element will be added to the total width of the element. This is often an undesirable result.

What is padding in web design?

Simply, Padding means Push Inside. When we say push inside we mean in a rectangle type view the content pushed itself according to the dimension specified in the padding attribute. Diagrammatically, the concept of Padding is shown as: The Above syntax will specify an extra space inside the view in all direction equally i.e.,


1 Answers

It is indeed different for em and %:

Ems

The padding size is relative to the calculated font size of that element.

So, if your <h1>’s calculated font size is 16px, then 1.2 ems of padding = 1.2 × 16 pixels = 19.2 pixels.

Percentages

The padding size is relative to the width of that element’s content area (i.e. the width inside, and not including, the padding, border and margin of the element).

So, if your <h1> is 500px wide, 10% padding = 0.1 × 500 pixels = 50 pixels.

(Note that top and bottom padding will also be 10% of the width of the element, not 10% of the height of the element.)

like image 166
Paul D. Waite Avatar answered Nov 15 '22 19:11

Paul D. Waite