
The margin of an element allows you to set the space between that element and other elements in the window by specifying one to four values that correspond to all four sides together, the top/ bottom and left/right sides as pairs, or all four sides independently. To define the margins of an element: 1. Start your declaration by typing the margin shortcut property name in the declaration block, followed by a colon (:) margin: 2. Type a value for the margin. 0 10px; Use one of the following values: A length value A percentage, which creates a margin proportional to the parent element’s width auto, which returns control of the margins to the browser’s discretion You can enter one to four values, separated by spaces, to define the margins as follows: One value sets the margin for all four sides equally. Two values set the top/bottom margins and left/right margins. Three values set the top margin, the left/right margins (the same), and the bottom margin. Four values set each margin clockwise, starting from top: top, right, bottom, and left. 3. You can also set the margin for just one side of the box independently without setting the other three margins. margin-right: 10px; This is useful when used with an inline style to override margins set elsewhere. To do this, specify the margin side you want to define (margin-top, marginbottom, margin-left, or margin-right) and enter a legitimate margin value from Setting Negative Margins Although you can use negative margins (for example, margin:-5em;) to create interesting effects for overlapping pieces of text, this method is frowned upon because various browsers present different results. Overlapping text is better achieved using CSS positioning. Be careful when setting negative margins around a hypertext link. If one element has margins that cause it to cover the link, the link will not work as expected.
Margin Classes | |
---|---|
Class | Defines |
w3-margin | Adds an 16px margin to an element |
w3-margin-top | Adds an 16px top margin to an element |
w3-margin-right | Adds an 16px right margin to an element |
w3-margin-bottom | Adds an 16px bottom margin to an element |
w3-margin-left | Adds an 16px left margin to an element |
w3-section | Adds an 16px top and bottom margin to an element |
w3-margin-0 | Deprecated (Use CSS margin:0 instead) |