What is Margin in CSS | 4 Essential Properties of Margin for Effective Layouts

What is Margin in CSS

Many students are confused about the concept of Margin and Padding. But after reading this post all your confusion will be cleared. Many students have this question in their mind what is margin in CSS and what is the use of margin in CSS?

Join to Become PHP Web Developer - Future Scope Career Guidance

WhatsApp Group Join Now
Telegram Group Join Now
Follow On Instagram Follow Us

Definition of Margin in CSS

Margin is a space around elements, outside of any defined borders. This means space outside the border is called a Margin.

Margin in CSS with Diagram
Margin in CSS with Diagram

In the above diagram, there is an HTML Element, like <p>,<h1> etc. There is a border for the element. Outside/around the space of that border is a Margin. Each element has 4 borders. We will discuss now about that.

CSS Margin Properties

CSS has properties to specify the margin for each side of an element.

PropertyDescriptionExample
margin-topSets the top margin of an elementmargin-top: 100px;
margin-bottomSets the bottom margin of an elementmargin-bottom: 100px;
margin-leftSets the left margin of an elementmargin-left: 100px;
margin-rightSets the right margin of an elementmargin-right: 100px;
CSS Margin Properties with Example

If you want to set a margin on all four sides of an element, you need to set all four margins like margin-top, margin-bottom, margin-left, and margin-right. Like as below.

We have declared 4 properties only for a single p element. So this is not a good practice. For this, there is one solution, the solution below.

Difference Between Margin and Padding in CSS

Margin shorthand CSS

To reduce the code, specify all properties in one property. All properties are specified in a single property, which is a shorthand property of margin CSS.

We have given 4 side margins in a single property instead of 4 separate properties. In the above example, top margin is 30px, right margin is 60px, bottom margin is 90px and left margin is 120px we have given in a single property. It is a use of shorthand property.

Note: Negative values are allowed.

margin-top: -20px; // It is allowed.

Credit: Learn Coding With Rahul Upmanyu

Follow Us on Telegram

Follow Quiz Lancer Telegram Channel to solve daily interview questions. We share important interview questions which are asked in the interview. Most of the students get good values from this channel and got selected as a developer.

Interview Questions on Margin in CSS

If you want to become a web designer, then CSS Margin is a very important concept. In an interview, many questions are asked about Margin. Below are the questions which are asked in the interview. You should solve the quiz as many times as possible and practice paddling.

0%
2
Created on By quizlancer

CSS Margin

1 / 10

1.

Margins are used to create space around elements, inside of any defined borders.

2 / 10

2. If the Margin property has four values. margin: 25px 50px 75px 100px; What is the value of bottom margin?

3 / 10

3. margin-top: -25px; Is negative value allowed in margin?

4 / 10

4. Negative values are allowed in Margin.

5 / 10

5. Is it possible two margins collapse into a single margin?

6 / 10

6. Which property is used for setting all the margin properties in one declaration?

7 / 10

7. who to sets the right margin of an element?

8 / 10

8. If the margin property has four values. margin: 25px 50px 75px 100px; What is the value of right margin?

9 / 10

9. What is use of margin left property?

10 / 10

10. Which property is used to set the top margin of an element?

Your score is

The average score is 65%

0%

  1. What is the difference between “float” and “margin” in CSS?

    Float specifies whether an element should move to the right, left, or not at all. And positioning elements ignore the float property. Elements next to the float will flow around it.
    Margin specifies whether an element should move top, right, bottom, and left or position in the center of its parent element. And it can also take different property values.

Trending Topics for You

85 / 100
Scroll to Top