| Start | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Margin Collapsing When two margins are touching each other vertically, they are collapsed. When two margins touch horizontally, they do not collapse. Example of adjacent vertical margins: Consider the following styles and markup: They will be 10px apart since vertical margins collapse over one and other. (The spacing will not be the sum of two margins.) Example of adjacent horizontal margins: Consider the following styles and markup: They will be 20px apart since horizontal margins don't collapse over one and other. (The spacing will be the sum of two margins.) Overlapping with different sizes These elements will be spaced 15px apart vertically. The margins overlap as much as they can, but the larger margin will determine the spacing between the elements. Overlapping margin gotcha What will be the spacing between the two texts? (hover to see answer)
Now, what about if we add some borders to the markup above. What will be the spacing between the two texts? (hover to see answer)
Collapsing Margins Between Parent and Child Elements: HTML: CSS In the example above, only the largest margin applies. You may have expected that the paragraph would be located 60px from the h1 (since the div element has a margin-top of 40px and the p has a 20px margin-top). This does not happen because the margins collapse together to form one margin. |
||||||||||||
Apply Margin on a Given Side Direction-Specific PropertiesCSS allows you to specify a given side to apply margin to. The four properties provided for this purpose are:
The following code would apply a margin of 30 pixels to the left side of the selected div. View Result (opens new window) HTML CSS
Specifying Direction Using Shorthand PropertyThe standard The following example applies a zero-width margin to the top of the div, a 10px margin to the right side, a 50px margin to the left side, and a 100px margin to the left side. View Result (opens new window) HTML CSS |
||||||||||||
Margin property simplification Another exapmle: |
||||||||||||
| Horizontally center elements on a page using margin As long as the element is a block, and it has an explicitly set width value, margins can be used to center block elements on a page horizontally. We add a width value that is lower than the width of the window and the auto property of margin then distributes the remaining space to the left and the right: In the example above we use the shorthand In the example above, the #myDiv element is set to 80% width which leaves use 20% leftover. The browser distributes this value to the remaining sides so: (100% - 80%) / 2 = 10% |
||||||||||||
| Negative margins Margin is one of a few CSS properties that can be set to negative values. This property can be used to overlap elements without absolute positioning. |
||||||||||||
| Example 1: It is obvious to assume that the percentage value of margin to But that is not the case, when comes to So, Syntax
Parameters
RemarksMore on "Collapsing Margins": here (opens new window). |
Read Article
https://develop.horje.com/learn/1434/reference