

You could also decide to use 1em as the vertical spacing between flow content, which would achieve consistent spacing based on the element's font-size. A good way to achieve this is use consistent measures for your spacing.įor example, you could commit to using 20px as a consistent measure for all gaps between elements-known as gutters-so all layouts look and feel consistent. It is a really good idea to choose a strategy and stick with it to help you create a consistent user interface that has good flow and rhythm. With both flexbox and grid, you can also create space using their distribution and alignment capabilities, which we cover in the grid module and flexbox module. If you define only one value, the same gap will be applied to both the rows and columns, but if you define both values, the first value is row-gap and the second value is column-gap. You can also use keywords such as unset, initial and inherit. The gap property is shorthand for row-gap and column-gap, it accepts one or two values, which can be lengths or percentages. Lastly, in both grid and flexbox you can use the gap property to create space between child elements. In the logical properties module, you learn about the inset-block and inset-inline properties, which allow you to set directional values that honor writing mode.īoth properties are shorthands combining the start and end values and as such accept either one value to be set for start and end or two individual values.

See the Pen Pen NWdNGZB by web-dot-dev on Codepen

An element with position: absolute will base the directional values from the relative parent's position.They will be relative to your element's position too. An element with position: relative will maintain its place in the document flow, even when you set these values.There are some differences with how these directional values behave: Positioning #Īlso covered in the layout module, if you set a value for position that is anything other than static, you can space elements with the top, right, bottom and left properties. Just like margin, padding has logical properties, too: padding-block-start, padding-inline-end, padding-block-end and padding-inline-start. The padding property is shorthand for padding-top, padding-right, padding-bottom and padding-left. See the Pen Pen eYgRrzz by web-dot-dev on Codepen Three values: the first value is top, the second value is left and right, and the third value is bottom.Two values: the first value will be applied to the top and bottom sides, and the second value will be applied to the left and right sides.One value will be applied to all sides.Adding a fourth value lets you set each individual side. The margin shorthand can also be used with one, two, or three values. You can remember these with trouble: TRouBLe. The margin shorthand applies properties in a particular order: top, right, bottom and left.

The margin property is shorthand for margin-top, margin-right, margin-bottom and margin-left. Margin is like adding a cushion around your element. If you want to add space to the outside of an element, you use the margin property. If you just want a line with space around it, adding a border with CSS might be more appropriate. For example, an doesn't just add space, it creates a logical separation of two chunks of content. Use HTML elements to add space only when the element helps with the understanding of the document.
