Properties order in Margin
Codemia Knowledge Hub ·
Understanding the Order of Properties in CSS Margin
In CSS, the `margin` property plays a crucial role in controlling the layout of elements. The shorthand property allows developers to set margins for all four sides of an element in a compact way. Understanding how to order these properties correctly is paramount for obtaining the desired layout and aesthetic effects in web design.
The Basics of the Margin Property
The `margin` property is used to create space around elements, outside of any defined borders. This property can take one to four values, with each value corresponding to the margin on a specific side of the element: top, right, bottom, and left.
Order of Properties in Margin
- Single Value:
- If a single value is provided, it applies to all four sides.
- Example:
- This code applies a margin of 20px uniformly to the top, right, bottom, and left.
- If two values are provided, the first value applies to the top and bottom, while the second applies to the right and left.
- Example:
- In this scenario, the margin is 20px for the top and bottom, and 10px for the right and left.
- Three values apply to the top, horizontal (both right and left), and bottom, respectively.
- Example:
- The top margin is 20px, the right and left margins are 10px, and the bottom margin is 30px.
- Four values set the margins for each side in the following order: top, right, bottom, and left.
- Example:
- This sets a top margin of 20px, right margin of 10px, bottom margin of 30px, and left margin of 5px.
- Single-value shorthand essentially fills all slots (top, right, bottom, left) with the same value.
- Two-value shorthand implies a vertical and horizontal symmetry. It interprets values as:
- The first value for `margin-top` and `margin-bottom`.
- The second value for `margin-right` and `margin-left`.
- Three-value shorthand assumes:
- The first value for `margin-top`.
- The second shared by `margin-right` and `margin-left`.
- The third for `margin-bottom`.
- Four-value shorthand directly maps each value to a direction:
- `top`, `right`, `bottom`, and `left`.
Related reading
- Pros and cons of async/await
- Protractor- Generic wait for URL to change
- Protractor async/await UnhandledPromiseRejectionWarning Unhandled promise rejection
- Putting HTML inside Html.ActionLink, plus No Link Text?
- Python code to remove HTML tags from a string
- Queue.js with progress event
- QUnit Async Tests with setup And teardown
- Rabbit-MQ with React
System design interview problems on this topic
- Design Google Doc: solution and walkthrough
- Design Facebook Messenger: solution and walkthrough
- Design Typeahead Suggestion: solution and walkthrough
- Design an Online Presence Indicator Service: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.