Skip to main content

How To Use Margin In CSS?

The CSS margin properties set the size of the white space outside the border. The margins are completely transparent and cannot have a background color.

CSS has properties for specifying the margin for each side of an element:
§  margin-top
§  margin-right
§  margin-bottom
§  margin-left

All the margin properties can have the following values:
§  auto - the browser calculates the margin
§  length - specifies a margin in px, pt, cm, etc.
§  % - specifies a margin in % of the width of the containing element
§  inherit - specifies that the margin will be inherited from the parent element

It is also possible to use negative values for margins; to overlap content.

Example 1: The following example sets different margins for all four sides of a <p> element:
<!DOCTYPE html>
<html>
<head>
<style>

p { background-color: yellow; }

p.ex {
border:1px solid red;
margin-top: 100px;
margin-bottom: 100px;
margin-right: 150px;
margin-left: 80px;
}
</style>
</head>
<body>

<h2>Using individual margin properties:</h2>

<p>This is a paragraph with no specified margins.</p>
<p class="ex">This paragraph has a top and bottom margin of 100px, a left margin of 80px, and a right margin of 150px.</p>

</body>
</html>

Example 2: The following example lets the left margin be inherited from the parent element:

<!DOCTYPE html>
<html>
<head>
<style>

div.container {
border: 1px solid red;
margin-left: 100px;
}

p.one { margin-left: inherit; }
</style>
</head>
<body>

<h2>Use of the inherit value</h2>
<p>Let the left margin be inherited from the parent element:</p>

<div class="container">
<p class="one">This is a paragraph with an inherited left margin (from the div element). </p>
</div>

</body>
</html>

Example 3: To shorten the code, it is possible to specify all the margin properties in one property.

<!DOCTYPE html>
<html>
<head>
<style>

p { background-color: yellow; }

p.ex {
border:1px solid red;
margin: 100px 150px 100px 80px;
}

</style>
</head>
<body>

<h2>Using the margin shorthand property:</h2>

<p>This is a paragraph with no specified margins.</p>
<p class="ex">This paragraph has a top and bottom margin of 100px, a left margin of 80px, and a right margin of 150px.</p>

</body>
</html>

Explanation: So, here is how it works:
If the margin property has four values:
§  margin: 25px 50px 75px 100px;
o   top margin is 25px
o   right margin is 50px
o   bottom margin is 75px
o   left margin is 100px

If the margin property has three values:
§  margin: 25px 50px 75px;
o   top margin is 25px
o   right and left margins are 50px
o   bottom margin is 75px

If the margin property has two values:
§  margin: 25px 50px;
o   top and bottom margins are 25px
o   right and left margins are 50px

If the margin property has one value:
§  margin: 25px;
o   All four margins are 25px

Example 4: You can set the margin property to auto to horizontally center the element within its container. The element will then take up the specified width, and the remaining space will be split equally between the left and right margins:

<!DOCTYPE html>
<html>
<head>
<style>

div {
width:300px;
margin: auto;
border: 1px solid red;
}

</style>
</head>
<body>

<h2>Use of the auto Value</h2>
<p>You can set the margin property to auto to horizontally center the element within its container. The element will then take up the specified width, and the remaining space will be split equally between the left and right margins:</p>

<div> This div will be centered because it has margin: auto; </div>

</body>
</html> 

Popular posts from this blog

Screenshots from Windows 1.01

Windows 1.0 is a graphical personal computer operating environment developed by Microsoft, released on November 20, 1985, as the first version of the Microsoft Windows line. Version 1.01 , also released in 1985, was the first point-release after Windows 1.00.   Screenshots from Windows 1.01: ⇰ Desktop  First Run Empty Desktop Desktop With Applications ⇰  Office Applications Notepad Text Editor Calculator Calendar Clock Address Book ⇰  Multimedia Applications Media player, CD player, Volume level, and Sound: This GUI doesn’t have these features. ⇰  Networking Applications Terminal Phone Dialer: This GUI doesn’t have this feature. ⇰  Internet Applications Browser, and Mail: This GUI doesn’t have these features. ⇰  Accessibility Applications Keyboard Map:  This GUI doesn’t have this feature. ⇰  Settings Desktop themes,  Display,  S...

Introduction To Algorithms, 3rd Edition

Before there were computers, there were algorithms. But now that there are computers, there are even more algorithms, and algorithms lie at the heart of computing. This book provides a comprehensive introduction to the modern study of computer algorithms. It presents many algorithms and covers them in considerable depth, yet makes their design and analysis accessible to all levels of readers. In this book, the authors tried to keep explanations elementary without sacrificing depth of coverage or mathematical rigor. Each chapter presents an algorithm, a design technique, an application area, or a related topic. Algorithms are described in English and in a pseudocode designed to be readable by anyone who has done a little programming. The book contains 244 figures — many with multiple parts — illustrating how the algorithms work. It also includes careful analysis of the running times of all algorithms. In this third edition, the entire book once again updated including changes cove...

C++ Program To Implement Casino Number Guessing Game.

#include <iostream> #include <string> #include <cstdlib> #include <ctime> using namespace std; void drawLine(int n, char symbol); void rules(); int main() { string playerName; int amount; int bettingAmount; int guess; int dice; char choice; srand(time(0)); drawLine(70,'_'); cout << "\n\n\n\t\tCASINO GAME\n\n\n\n"; drawLine(70,'_'); cout << "\n\nEnter Your Name : "; getline(cin, playerName); cout << "\n\nEnter Deposit Amount To Play Game : $"; cin >> amount;

Java: The Complete Reference, 9th Edition

This is Herb's most popular book on Java, fully updated and expanded to cover Java SE 8 (JDK 8).    Whether you're an experienced pro or just starting out, this one-stop guide will help you master this important language.  Inside you'll find comprehensive coverage of the Java language, its keywords, syntax, and fundamental programming principles.  Of course, descriptions of Java's newest features, such as lambda expressions, default interface methods, and the stream API are included. This lasting resource also describes key elements of the Java API library, such as the Collections Framework, concurrency, applets, servlets, Beans, event handling,  AWT,  Swing, and more. Coverage of JavaFX, Java's newest GUI framework, is also included. *** TO REVIEW BOOK ***  (click below) *** TO REVIEW SOURCE CODE PROBLEM  SOLUTIONS, VISIT   THIS   LINK ***