Using Margin VS Padding in CSS


For a novice CSS developer, the terms margin and padding can be somewhat confusing.

Both terms are referring to the space between certain elements.

However, there is a big difference between margin and padding and knowing the differences and using them accordingly will help a website developer in making better decisions in their design.

So, what is a Margin in CSS?

A margin is referring to the vertical and horizontal space between two elements.

Without having set margins, your elements would bump into each other.

You can define and set margins for the top, bottom, left and right side of your element.

The margin basically clears an area around your element, which is transparent without a background color.

And what is Padding in CSS?

Padding is the vertical and horizontal space that is set on the inside of your box.

It refers to the outer space of your content in your element.

You can set the padding for the top, bottom and right and left sides of your property independently.

Therefore, you can center your text, or have it set to the right or left side or your element, or set it to the top or bottom of your element.

Margin and padding may seem very similar due to the fact they both create space around your element, but there are specific reasons to use one or the other.

You want to use margins when you want your spacing to appear outside of your text box, especially when you are using borders.

Also if you are using background colors or images and you do not want them to invade the space of your element.

Padding behaves very differently in these circumstances.

Padding is contained inside your borders; therefore if you want all of your space inside the border, you would want to use padding.

And if you are using a background color or image and you want that color and/or image to continue behind your element you would want to use padding.

How to use Margin VS Padding

To summarize, margins separates your elements from each other and away from the edges which adds space to the outside of your elements.

And padding adds space and separates your text and content in the inside of your element.

It is important to use margins and padding even if you do not have a border to ensure the correct spacing between your elements.

要查看或添加评论,请登录

JL Faverio的更多文章

  • ?? NitroPack Users: Critical Update Required

    ?? NitroPack Users: Critical Update Required

    If you’re using NitroPack (free or paid), a manual update is required to keep your site secure and running smoothly…

  • 25 WordPress Plugins I Actually Use

    25 WordPress Plugins I Actually Use

    This is a very simple list of WordPress plugins that I actually use with agency clients as well as my own. Why should…

  • UPDATE: EasyApache 2018-01-09 Maintenance Release

    UPDATE: EasyApache 2018-01-09 Maintenance Release

    cPanel just sent out multiple updates people. Let's make sure your website's and personal information stays secure.

    1 条评论
  • [2018 Update] How to Delete a Company Page on LinkedIn

    [2018 Update] How to Delete a Company Page on LinkedIn

    WHAT LINKEDIN SAYS ON DELETING COMPANY PAGES: LinkedIn recommends you do not remove a company or business page as the…

    8 条评论
  • Entrepreneurs Love Mondays

    Entrepreneurs Love Mondays

    Since starting my own business less than 5 years ago, I've grown to love Mondays more and more. I really don't like…

  • If You’re a Business Owner, This is a MUST READ!

    If You’re a Business Owner, This is a MUST READ!

    My face is still warm to the touch, cheeks still rosey red, because I am so angry…no…disappointed with how I have been…

    4 条评论
  • It's Amazingly Difficult Giving Away Free Stuff

    It's Amazingly Difficult Giving Away Free Stuff

    I will keep this short and sweet, as I’ve already been promoting both offers for years now. As a Web Developer, and a…

    2 条评论
  • 18 Out of the Box Things You Can Donate to Homeless

    18 Out of the Box Things You Can Donate to Homeless

    Below is an updated version of a popular article I published titled Cans Are Good, Socks Even Better, Hear Mooch’s…

    21 条评论

社区洞察

其他会员也浏览了