Preloader

Margin Padding and Border

CSS
CodeVigyaan is a free education platform offering programming tutorials, study materials, notes, and real project ideas for college student.

As you learn to design a website, one of the most useful things to learn about is how margin, padding, and border interact. In CSS, every item, from a button to a picture to a paragraph of text, is a box. Learning to control the margins and padding of boxes is how you learn to turn a difficult layout into a elegant, clean design.

Why the CSS Box Model Matters

The “Box Model” is the basis of every web page you will ever see. Not understanding margin, padding, and border can cause your page‘s elements to stack on top of each other, or just appear really cluttered, when viewed on mobile. Here‘s what I mean: your website is like a house, your content is the people occupying the house, the padding is the space between the people and the walls, the border is the wall itself, and the margin is the ground, keeping your neighbor‘s house far away

Margin: The space outside the frame, between the image and other images. Margin: The space outside the frame (the gap between your picture and neighbouring pictures).

Border: The frame itself.

Padding: The “matting” (empty space) within the frame (distance from frame to photograph).

Content: The real photograph.

1. Padding (The Inner Space)

Padding is the space inside an element, between the content and the border. The easiest way to make text on a button or box readable is to add some padding.

Visual Tip: If you have a background color on your box the padding will match it. It “blows up” the element from the inside.

Property: padding: 20px;

When to use it: When you want to enlarge a button or add some “space to breathe” around some text inside a colored box.

Example:
Padding: 20px; (Space on all 4 sides)

Padding-top: 10px;( Only top space)

Simple Rule. Adding padding makes the element itself bigger, e. G. if you have a green box and add padding, the area covered by the green increases.

2. Border (The Edge)

Border is just between your padding and your margin. Think of it as the “frame” around your content. To make a border visible, you need to specify…

Design Tip: Another way to soften the edges of your design is to apply a border-radius to your ‘boxes’.

Style=“text-align: center; border: 2px solid #000;”> Property: border: 2px solid #000;

Where to use it: if you want to mark out the border of an element or to divide sections.

3 Requirements: To view a border you need to give CSS three pieces of information: Width, Style & Color.

Example:
Has a 2px. broad border with a solid black color; indicates there will be a single, blackborder.

Border-radius: 10px; (This makes the corners rounded!)

Margin (The Outer Space)

Margin is the transparent area outside the border. It is important for pushing adjacent elements away or for creating space between different parts of your website.

Pro Tips: Margins, in contrast to padding, do not have a background color of the element, so they are always transparent, they are just there to create space.

Property: margin: 30px;

Where to use: when you want to space out two different buttons or move a paragraph down from the top.

Example:
Margin: 50px; (Pushes everything 50px away from this box)

Margin: 0 auto; (A popular trick to position a box exactly in the middle of the screen)

Simple Rule: The boundaries should have a background color—they should be opaque. Also, there should be no background color on the margins. The margins should always be transparent.

Check out our resources!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *