Once you know when to use padding vs. margins, you can begin learning how to use padding and margins with your website's CSS files.
What is CSS?
CSS, or Cascading Style Sheets, is one of the most popular computer programming languages explicitly designed with the stylization of markup in mind. CSS is used with HTML to bring color schemes and other visual elements to life. Without CSS, a website would likely appear plain or outdated and devoid of any color.
CSS allows developers the ability to define the formatting and style of a website's layout, logo, fonts, padding, and more. The use of CSS provides more capabilities when setting the padding and margins of specific elements or universal elements of a website, depending on how the website has been developed and formatted.
Adding margins (CSS)
If you want to add a margin to any web page, you can use your own CSS file to help streamline the process and apply margins across the board. Using a CSS file to set margins in place can help save time without requiring manual editing of each individual page that you would like to use a margin on.
To start adding margins to your website's CSS file, it's important to remember that each HTML element you implement into your website has 4 margins that can be edited and formatted. The 4 margins of HTML elements include: left, top, right, and bottom. When you want to set the margin the same amount on all sides, you can use the simplified margin property rather than specifying each side individually.
For example, if you want to make a section of your website have a margin of 10px, your CSS file may appear with a snippet that looks like this:
#maincontent { margin: 10px; }
However, if you want to edit just the amount of space that an element has from the bottom of a page, your CSS snippet may appear as:
#maincontent { margin-bottom: 10px; }
When using any formatting variation (margin-left, margin-top, margin-right, or margin-bottom), remember that the number you set will be the number of pixels the element appears from said designation. For example, if you set your margin-right to "50px", your element will appear 50px from the far right of the page or the element it's contained within.
Note that it's also possible to apply more than 1 margin value to an element. You can apply 2, 3, or even 4 individual margin definitions to an element based on the current layout and setup of your website. If you choose to incorporate more than one margin value into an element, keep in mind that they'll load and be displayed using the following system:
- 2 values. Using 2 margin values will apply to both the top and bottom as well as the right and left of an element simultaneously.
- 3 values. 3 margin values will apply to the top, left, and right (together) and the bottom of an element.
- 4 values. 4 margin values will apply individually, starting at the top and working their way to the right, bottom, and left.
With the ability to add more than one margin value to an element, you can maintain complete control over how your website's layout loads for your visitors, whether they're browsing on the web or a smartphone.