|
| 1 | +--- |
| 2 | +id: margin |
| 3 | +title: CSS Margin |
| 4 | +sidebar_label: Margin |
| 5 | +sidebar_position: 3 |
| 6 | +keywords: |
| 7 | + [ |
| 8 | + css margin, |
| 9 | + margin property, |
| 10 | + css margin property, |
| 11 | + css spacing, |
| 12 | + margin shorthand, |
| 13 | + margin values, |
| 14 | + ] |
| 15 | +description: Learn how to use the CSS margin property to create space around elements in your web page layout. |
| 16 | +tags: [css, margin, css margin, margin property, css margin property] |
| 17 | +--- |
| 18 | + |
| 19 | +In CSS, the `margin` property is used to create space around elements in your web page layout. Margins are the transparent spaces outside the border of an element that separate it from other elements on the page. By adjusting the margin values, you can control the spacing between elements and create visually appealing layouts. |
| 20 | + |
| 21 | +<AdsComponent /> |
| 22 | + |
| 23 | +## Syntax |
| 24 | + |
| 25 | +The syntax for the `margin` property is as follows: |
| 26 | + |
| 27 | +```css title="index.css" |
| 28 | +selector { |
| 29 | + margin: value; |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +- `selector`: The element to which the margin is applied. |
| 34 | +- `margin`: The CSS property used to set the margin around an element. |
| 35 | +- `value`: Specifies the margin values for the top, right, bottom, and left sides of the element. It can take one of the following forms: |
| 36 | + - `<length>`: Specifies a fixed margin value in pixels (e.g., `10px`). |
| 37 | + - `<percentage>`: Specifies a margin value as a percentage of the width of the containing element. |
| 38 | + - `auto`: Adjusts the margin to automatically distribute the space between elements. |
| 39 | + - `initial`: Sets the margin to its default value. |
| 40 | + - `inherit`: Inherits the margin value from the parent element. |
| 41 | + - `unset`: Resets the margin to its inherited value if it inherits from its parent, or to its initial value if not. |
| 42 | + |
| 43 | +The `margin` property can be set using one of the following shorthand values: |
| 44 | + |
| 45 | +- `margin: value;`: Sets the same margin value for all four sides. |
| 46 | +- `margin: vertical horizontal;`: Sets the vertical margin (top and bottom) and horizontal margin (left and right) to different values. |
| 47 | +- `margin: top right bottom left;`: Sets individual margin values for the top, right, bottom, and left sides. |
| 48 | +- `margin: initial;`: Sets the margin to its default value. |
| 49 | +- `margin: inherit;`: Inherits the margin value from the parent element. |
| 50 | +- `margin: unset;`: Resets the margin to its inherited value if it inherits from its parent, or to its initial value if not. |
| 51 | +- `margin-top: value;`: Sets the margin value for the top side. |
| 52 | +- `margin-right: value;`: Sets the margin value for the right side. |
| 53 | +- `margin-bottom: value;`: Sets the margin value for the bottom side. |
| 54 | +- `margin-left: value;`: Sets the margin value for the left side. |
| 55 | +- `margin-block-start: value;`: Sets the margin value for the block-start side (top margin in horizontal writing mode). |
| 56 | +- `margin-block-end: value;`: Sets the margin value for the block-end side (bottom margin in horizontal writing mode). |
| 57 | + |
| 58 | +The default value of the `margin` property is `0`, which means no margin is applied to the element. |
| 59 | + |
| 60 | +## Example |
| 61 | + |
| 62 | +In the following example, the `margin` property is used to set the margin around a `<div>` element to `20px` on all sides: |
| 63 | + |
| 64 | +```css title="index.css" |
| 65 | +div { |
| 66 | + margin: 20px; |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +In the HTML code below, the CSS rule will apply the `20px` margin to the `<div>` element: |
| 71 | + |
| 72 | +```html title="index.html" |
| 73 | +<div>This is a div element with a 20px margin around it.</div> |
| 74 | +``` |
| 75 | + |
| 76 | +By adjusting the margin values, you can control the spacing between elements and create visually appealing layouts in your web page design. |
| 77 | + |
| 78 | +<AdsComponent /> |
| 79 | + |
| 80 | +:::info Additional Information |
| 81 | + |
| 82 | +**Margin Values:** |
| 83 | + |
| 84 | +- Margins are the transparent spaces around elements that separate them from other elements on the page. |
| 85 | +- The `margin` property is used to set the margin around an element. |
| 86 | +- Margin values can be specified in pixels, percentages, or using the `auto`, `initial`, `inherit`, or `unset` keywords. |
| 87 | +- The default value of the `margin` property is `0`, which means no margin is applied to the element. |
| 88 | +- The `margin` property can be set using shorthand values or individual properties for each side. |
| 89 | +- By adjusting the margin values, you can control the spacing between elements and create visually appealing layouts in your web page design. |
| 90 | +- Margins are part of the CSS box model and are used to create space around elements in the layout. |
| 91 | + |
| 92 | +::: |
| 93 | + |
| 94 | +## Example for Margin |
| 95 | + |
| 96 | +Now, let's look at an example that demonstrates how the `margin` property can be used to create space around an element in a web page layout. |
| 97 | + |
| 98 | +<Tabs> |
| 99 | + <TabItem value="HTML" label="index.html"> |
| 100 | + |
| 101 | +```html title="index.html" |
| 102 | +<!DOCTYPE html> |
| 103 | +<html lang="en"> |
| 104 | +<head> |
| 105 | + <meta charset="UTF-8"> |
| 106 | + <meta http-equiv="X-UA-Compatible" content="IE=edge"> |
| 107 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 108 | + <title>Margin Example</title> |
| 109 | + <link rel="stylesheet" href="styles.css"> |
| 110 | +</head> |
| 111 | +<body> |
| 112 | + <div> |
| 113 | + <p> |
| 114 | + This is a div element with a 20px margin around it. |
| 115 | + </p> |
| 116 | + </div> |
| 117 | +</body> |
| 118 | +</html> |
| 119 | +``` |
| 120 | + |
| 121 | +</TabItem> |
| 122 | + |
| 123 | +<TabItem value="CSS" label="styles.css"> |
| 124 | + |
| 125 | +```css title="styles.css" |
| 126 | +div { |
| 127 | + margin: 20px; |
| 128 | + padding: 10px; |
| 129 | + border: 1px solid black; |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 133 | +</TabItem> |
| 134 | +</Tabs> |
| 135 | + |
| 136 | +Now, you can see the output of the above code in the Browser Window like this: |
| 137 | + |
| 138 | +<BrowserWindow url="http://127.0.0.1:5500/index.html" bodyStyle={{ backgroundColor: "#fff", color: "#000" }}> |
| 139 | + <div style={{margin: "20px", padding: "10px", border: "1px solid black"}}> |
| 140 | + <p> |
| 141 | + This is a div element with a 20px margin around it. |
| 142 | + </p> |
| 143 | + </div> |
| 144 | +</BrowserWindow> |
| 145 | + |
| 146 | +By following this example, you can use the `margin` property to create space around an element in your web page layout. Adjusting the margin values allows you to control the spacing between elements and create visually appealing designs. |
| 147 | + |
| 148 | +## Conclusion |
| 149 | + |
| 150 | +The `margin` property in CSS is a powerful tool for creating space around elements in your web page layout. By setting the margin values for an element, you can control the spacing between elements and create visually appealing designs. Understanding how to use the `margin` property effectively will help you create well-structured and visually appealing web pages. |
0 commit comments