MyBlogHelp– A Blog for Digital Marketing, Affiliate Marketing, Organic Lead Generation.
  • Home
  • About
  • Contact
  • Privacy
  • Sitemap
  • BLOG
  • AFFILIATE MARKETING
    • LeadsArk
  • MORE
    • MAKE MONEY
      • ONLINE BUSINESS
      • ADSENSE
      • AFFILIATE MARKETING
    • BLOGGING
      • SEO
      • BlogSpot
      • WORPRESS
      • GOOGLE
      • COMPUTER TIPS
    • WEB DESIGN
      • HTML
      • CSS
      • BOOTSTRAP
      • JAVASCRIPT
      • JQUERY
    • WEB DESIGN
      • HTML
      • CSS
      • BOOTSTRAP
      • JAVASCRIPT
      • JQUERY
    • WEB DEVELOPMENT
      • PHP
      • WORDPRESS
  • DOWNLOADS
    • Blogger Template
    • Wordpress Theme
    • PDF

Saturday, March 30, 2019

How to use css padding and margin properties? CSS Tutorial 09

 MyBlogHelp     March 30, 2019     css     No comments   

How-to-use-css-padding-&-margin-properties


 

CSS Padding and Margin Properties

  1. Introduction to CSS Padding & Margin Properties.
  2. CSS padding properties.
  3. CSS padding shorthand property.
  4. CSS margin properties.
  5. CSS margin shorthand property.


Introduction to Padding and Margin Properties

Padding and margin are very important for the designing of any webpage or website. CSS margin and padding are defined in the context of the border of any HTML element (<div>, <h1> and <p> etc.). Padding is a space that is defined inside the border and margin is defined outside of the border. These are explained by the below diagram.


css-padding-and-margin-diagram


In the diagram which is given above, the Suva Grey color area between the text and border of DIV is called padding. The Black color area is a border which comes between margin and padding. As you know, the space inside the border and outside the content/text area is called padding. The Scarlet color area outside the border is the margin of DIV. The margin is used to define the space between elements from one element to another.

You should always keep in mind that every HTML element has the border which is hidden by default. To show the border of an element, you can set its border property.

CSS Padding Properties

To define the padding of elements, CSS provides you four properties. You can define the padding of each side independently by using these properties. If you want to keep the padding of all sides same, CSS provides you another property.

From this property, you define the padding of all sides in a single statement. Padding is defined in pixels and the value of padding can’t be defined negatively. Now we will try to know about CSS padding properties.

padding-left Property

By this property, you can define the left padding of an element. Whatever value you will give to this property, the same space creates inside the left side of the border. An example of this has been given below.

padding-left: 20px;

padding-right Property

By this property, you can define the right padding of an element. Whatever value you will give to this property, the same space creates inside the right side of the border. An example of this has been given below.

padding-right: 30px;

padding-top Property

This property is used to define padding inside the border on the top of the element. An example of this has been given below.

padding-top: 10px;

padding-bottom Property

By this property, you can define padding on the bottom side of the element. Whatever value you will give to this property, the same space creates inside the bottom side of the border. An example of this has been given below.

padding-bottom: 10px;

Let's try to understand the use of these properties through an example.


<!DOCTYPE html>
<html>
<head>
    <title> CSS Padding Property Example </title>
<style type="text/css">
#MbhDiv
{
   width: 25%;
   border: 5px solid blue;
   padding-left: 30px;
   padding-right: 50px;
   padding-top: 20px;
   padding-bottom: 40px;
   text-align: justify;
}
</style>
</head>
<body>
<div id = "MbhDiv">
Padding creates space between content and border. It is very important to make your content(text,image and video etc.) appear beautiful on the webpage.
</div>
</body>
</html>


In the above example, a div is created which id is MbhDiv. The padding of all sides has been set through the CSS in the <head> tag. This example will generate the below output.


CSS-padding-properties-example

padding Shorthand Property

If you want, you can use a padding shorthand property instead of using these four different properties. In this shorthand property, you can define padding for all sides. Its syntax is being given below.

padding: top right bottom left;

In this property, first of all, you will have to define the padding of the top side of the element. After this, the padding of the left side will be defined again right side, and at last bottom side. An example of this has been given below.

padding: 20px 10px 20px 10px;

If you define only one value with this property then the same padding applies to all sides. You can see in the below example.

padding: 10px;

The above statement sets the padding 10px for all sides.

Let's try to understand the use of these properties through an example.


<!DOCTYPE html>
<html>
<head>
    <title> CSS Padding Shorthand Property Example </title>
<style type="text/css">
#MbhDiv
{
   width: 25%;
   border: 5px solid green;
   padding: 20px 60px 40px 20px;   
    text-align: justify;
}
</style>
</head>
<body>
<div id = "MbhDiv">
Padding creates space between content and border. It is very important to make your content(text,image and video etc.) appear beautiful on the webpage.
</div>
</body>
</html>


The above script will generate the below output.



CSS-padding-shorthand-property-example

CSS Margin Properties

CSS margin properties are used to create space around the elements. Margin properties determine how much space will be located outside of the elements. CSS provides you four margin properties. From these properties, you can define the margin of the four sides.

Also, CSS gives you a margin shorthand property as well. From which you can define values for all these properties in a single statement. You can define the value of all these properties by four types.

  • auto - When you give auto value, the browser automatically sets the appropriate margin.
  • length - In this type of value, you define the margin length in pixels.
  • percent(%) - If you want to set the margin of the element, you can set the width in percent of the element that contains the margin.
  • inherit - If you want to inherit margin from the parent element, you can pass this inheritance value.

margin-top Property

By this property, you set the margin on the top of the element. This property defines how much space will be on the outside side of the border above the element. An example of this has been given below.

margin-top: 20px;

margin-right Property

By this property, you set the margin on the right side of the element. This creates a specified space on the right side of the element and outside of the border. An example of this has been given below.

margin-right: 10px;

margin-bottom Property

By this property, you set the margin below of the element. Whatever value you will give to this property, the same space will be created at the bottom of the element on the outside of the border. An example of this has been given below.

margin-bottom: 20px;

margin-left Property

This property is used to set the margin on the left of the element. When you set this property with value then space creates outside of the border on the left side of the element.

margin-left: 20px;

Now we will try to understand these margin properties through an example.


<!DOCTYPE html>
<html>
<head>
    <title> CSS Margin Properties Example </title>
<style type="text/css">
#MbhDiv
{
   width: 25%;
   border: 5px solid red;
   margin-top: 50px;
   margin-right: 20px;
   margin-bottom: 30px;
   margin-left: 40px;
   text-align: justify;
}
</style>
</head> 
<body>
<div id="MbhDiv">
It is important to know the CSS margin properties for the best website/webpage design. Margin creates space between elements.
</div>
</body>
</html>


As you can see, in the above example, margins of all sides have been set through four properties. This example produces the below output.


CSS-margin-properties-example

margin Shorthand Property

Instead of using four properties, you can use a single margin shorthand property. This property is called margin property. Its syntax has been given below.

margin: top right bottom left;

Now, look at the example of this property.

margin 20px 10px 20px 10px;

Let's try to understand the use of these properties through an example.


<!DOCTYPE html>
<html>
<head>
    <title> CSS Margin Shorthand Property Example </title>
<style type="text/css">
#MbhDiv
{
   width: 25%;
   border: 5px solid black;
   margin: 50px 10px 20px 30px;
   text-align: justify;
}
</style>
</head> 
<body>
<div id="MbhDiv">
It is important to know the CSS margin shorthand properties for the best website/webpage design. Margin creates space between elements. 
</div>
</body>
</html>



As you can see, in the above example, margins of all sides have been set through single properties. This example will produce the below output.



CSS-margin-shorthand-property-example


If you assign a single value to this property then the margin of all sides is set by the same value.

margin: 20px;

It will set the 20px margin each and every side.


      <<-- PREVIOUS                                                                     NEXT -->>







  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Thank you for reading this post. Please do not enter any spam link in the comment box.

Followers

Blog Archive

  • ►  2020 (4)
    • ►  October (1)
    • ►  September (1)
    • ►  August (2)
  • ▼  2019 (52)
    • ►  December (1)
    • ►  November (2)
    • ►  October (1)
    • ►  August (1)
    • ►  July (1)
    • ►  May (11)
    • ►  April (10)
    • ▼  March (6)
      • How to use css padding and margin properties? CSS ...
      • How to use css border property? CSS Tutorial 08
      • How to use css width and height properties? CSS Tu...
      • How to create a css box model? CSS Tutorial 06
      • How to use link properties? CSS Tutorial 05
      • How to set css styling of images, table and lists?...
    • ►  February (3)
    • ►  January (16)
  • ►  2018 (54)
    • ►  December (27)
    • ►  November (5)
    • ►  May (5)
    • ►  April (4)
    • ►  February (3)
    • ►  January (10)
  • ►  2017 (17)
    • ►  December (9)
    • ►  November (8)

Popular Posts

  • How to use the main tag in html5?
  • How to use keygen tag in html5?
  • How to use html5 mark tag to highlight text?
  • How to start Online Business without money from home?
  • What is a search engine?

Contact Us

Name

Email *

Message *

About MyBlogHelp

Most of the posts, we share on this blog related to Affiliate Marketing, Organic Lead Generation, Digital Marketing, Social Media Marketing, Make Money Online, SEO, and web design.

Featured Post

LeadArk - Affiliate Marketing | Qualified Organic Lead Generation | LeadsArk Review

Copyright © MyBlogHelp– A Blog for Digital Marketing, Affiliate Marketing, Organic Lead Generation.
Design by Md. Abdussamad | MyBlogHelp.com