CSS-Innenabstand

Der Innenabstand dieses Elements beträgt 70px.

CSS-Innenabstand

CSS padding Diese Eigenschaft wird verwendet, um um den Inhalt des Elements im definierten Randbereich Raum zu schaffen.

Mit CSS können Sie den Innenabstand (Füllung) vollständig steuern. Einige Attribute können für jede Seite des Elements (oben, rechts, unten und links) Innenabstände setzen.

Padding - Einzelseite

CSS verfügt über Attribute, um für jede Seite des Elements Innenabstände zu spezifizieren:

  • padding-top
  • padding-right
  • padding-bottom
  • padding-left

Alle Innenabstandseigenschaften können die folgenden Werte setzen:

  • length - Mit px, pt, cm und anderen Einheiten den Innenabstand angeben
  • % - Legt fest, dass der Innenabstand in Prozent der Breite des umschließenden Elements angegeben wird
  • inherit - Legt fest, dass der Innenabstand vom übergeordneten Element geerbt werden soll

Tipp:Negative Werte sind nicht erlaubt.

Example

Stellen Sie unterschiedliche Innenabstände für alle vier Seiten des <div>-Elements ein:

div {
  padding-top: 50px;
  padding-right: 30px;
  padding-bottom: 50px;
  padding-left: 80px;
}

Try it yourself

Padding - Kürzelattribut

Um den Code zu reduzieren, können Sie alle Innenabstände in einer Eigenschaft angeben.

padding 属性是以下各内边距属性的简写属性:

  • padding-top
  • padding-right
  • padding-bottom
  • padding-left

工作原理是这样的:

If padding 属性有四个值:

  • The left inner padding is 100px, and the right inner padding is 50px
    • padding: 25px 50px 75px 100px;
    • The property is a shorthand for the following inner padding properties:
    • The property sets three values:
    • How it works is like this:

Example

The property has four values:

div {
  The left inner padding is 100px, and the right inner padding is 50px
}

Try it yourself

If padding Use a padding shorthand property that sets four values:

  • Use a padding shorthand property that sets three values:
    • padding: 25px 50px 75px 100px;
    • The right and left inner paddings are 50px, and the top and bottom inner paddings are 25px
    • The property sets three values:

Example

The bottom inner padding is 75px, and the top inner padding is 25px

div {
  Use a padding shorthand property that sets three values:
}

Try it yourself

If padding padding: 25px 50px 75px;

  • padding: 25px 50px;
    • The property sets two values:
    • The right and left inner paddings are 50px, and the top and bottom inner paddings are 25px

Example

Use a padding shorthand property that sets two values:

div {
  padding: 25px 50px;
}

Try it yourself

If padding The property sets a value:

  • padding: 25px;
    • All four inner paddings are 25px

Example

Use a padding shorthand property that sets a value:

div {
  padding: 25px;
}

Try it yourself

Inner padding and element width

CSS width The width property specifies the width of the element's content area. The content area is the part within the element (box model) that is inside the padding, border, and margin.

Therefore, if an element has a specified width, the padding added to the element will be added to the total width of the element. This is usually not the desired result.

Example

Here, the width of the <div> element is 300px. However, the actual width of the <div> element will be 350px (300px + left padding 25px + right padding 25px):

div {
  width: 300px;
  padding: 25px;
}

Try it yourself

To keep the width at 300px regardless of the padding, you can use box-sizing This will cause the element to maintain its width. If you increase the padding, the available content space will decrease.

Example

Use the box-sizing property to keep the width at 300px regardless of the padding:

div {
  width: 300px;
  padding: 25px;
  box-sizing: border-box;
}

Try it yourself

More examples

Set left inner padding
This example demonstrates how to set the left inner padding of a <p> element.
Set right inner padding
This example demonstrates how to set the right inner padding of a <p> element.
Set top inner padding
This example demonstrates how to set the top inner padding of a <p> element.
Set bottom inner padding
This example demonstrates how to set the bottom inner padding of a <p> element.

All CSS inner padding properties

Property Description
padding A shorthand property used to set all inner padding properties in a single declaration.
padding-bottom Set the bottom inner padding of the element.
padding-left Set the left inner padding of the element.
padding-right Setzen Sie den rechten Innenabstand des Elements.
padding-top Setzen Sie den oberen Innenabstand des Elements.

Erweiternde Lektüre

Zusatzbuch:Box-Modell: CSS Innenabstand