Home > Article > Web Front-end > How to use margin in css
The margin attribute in CSS is used to set the blank area around the element. Different values can be set for the four sides of the element, which are called the top margin, right margin, bottom margin and left margin.
Usage of margin in CSS
The margin property is used in CSS to set the blank area around the element. It In pixels (px), percentage (%), or other length units. margin can set different values for the four sides of the element, called top margin, right margin, bottom margin and left margin.
Usage
The syntax of the margin attribute is:
<code>margin: top-margin right-margin bottom-margin left-margin;</code>
Among them, top-margin, right-margin, bottom-margin and left-margin respectively Represents the values of the top margin, right margin, bottom margin, and left margin. If you specify only one value, that value will be applied to all four margins. For example:
<code>margin: 10px;</code>
will set 10 pixels of white space for all margins of the element.
Effect
The margin attribute affects the position of the element on the page. By setting margin appropriately, you can control the spacing, alignment, and layout of elements. For example, the following CSS code will set a top margin of 20 pixels, a right margin of 10 pixels, and a bottom margin of 30 pixels for a paragraph element:
<code>p { margin: 20px 10px 30px; }</code>
This will cause a gap between the paragraph element and the previous element. 20 pixels of white space from the right edge, 10 pixels of white space from the next element, and 30 pixels of white space from the next element.
Notes
When using the margin attribute, you need to pay attention to the following:
The above is the detailed content of How to use margin in css. For more information, please follow other related articles on the PHP Chinese website!