EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Negative Margin CSS

By Priya PedamkarPriya Pedamkar

Home » Software Development » Software Development Tutorials » CSS Tutorial » Negative Margin CSS

Negative Margin CSS

Introduction to Negative Margin CSS

Positive margin values move the content towards the inner side of its position or page. But if we take negative margin values, it moves the content towards outside of its position or page. The margin property in html gives space around the outermost element’s content of the box-like structure. The padding property in html gives space around the innermost element’s content of the box-like structure. The space around padding and margin is called a border.

The difference between the padding, margin, and border you can see below:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

negative margin in css 1

The above image gets with positive margin values.

Negative margin in css 2

How does Negative Margin work in CSS?

  • The negative margin value gives an overlapping feature. Which is really helpful while developing web pages.
  • Real-time scenario: If we want some text on top of the image or some other then we simply use a negative margin.

Syntax 1:

div
{
margin: -10px,-10px,-10px,-10px; //margin positions
}

Syntax 1 Explanation:

  • If we apply margin with 4 negative values then the first value is for top, the second value is for right, the third value is for the bottom and the fourth value is for left applied respectively.

Syntax 2:

div
{
margin: -10px,-10px,-10px; //margin positions
}

Syntax 2 Explanation:

  • If we apply margin with 3 negative values, then the first value is for top, the second value is for left and right, the third value is for bottom applied respectively.

Syntax 3:

div
{
margin: -10px,-10px; //margin positions
}

Syntax 3 Explanation:

  • If we apply margin with 2 negative values, then the first value is for the top and bottom and the second value is for left and right applied respectively.

Syntax 4:

div
{
margin: -10px; //margin positions
}

Syntax 4 Explanation:

  • If we apply margin with only a single negative value, then applied it for all four sides equally.
  • If we want to apply only a negative margin to a single side, then CSS provides predefined properties.
  • margin-left: -10px: apply margin -10px to left side.

Syntax:

div
{
margin-left: -10px;
}

  • margin -right: -10px: apply margin -10px to right side.

Syntax:

div
{
margin-right: 10px;
}

  • margin -top: -10px: apply margin -10px to top side.

Syntax:

div
{
margin-top: -10px;
}

  • margin -bottom: -10px: apply margin -10px bottom side.

Syntax:

div
{
margin-bottom: -10px;
}

Examples of Negative Margin CSS

Given below are the examples of Negative Margin in CSS:

Example #1

Negative margin to the bottom side.

HTML Code:

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<link rel="stylesheet" href="NegativeMarginBottom.css">
<title>Negative Margin</title>
</head>
<body>
<font color="green"><h2 align="center">Negative Margin</h2></font>
<img src="p2.jpg" class="image">
<p class="style1">I am working with Negative margin</p>
</body>
</html>

CSS Code:

.style1 {
font-size: 30px;
margin-bottom: -50px;
background: olive;
}
.image {
width: 500px;
height: 500px;
float:left; /* Keeps the image at left side */
}

Output before applying negative value:

Negative margin in css 3

 

Output after applying negative value:

Neagtive margin in css 4

Explanation:

  • As you can see in the output negative bottom margin, paragraph overlaps by image.
  • The negative bottom margin moves the content bottom side.

Example #2 – Negative margin to the left side.

HTML Code:

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<link rel="stylesheet" href="NegativeMarginLeft.css">
<title>Negative Margin</title>
</head>
<body>
<font color="green"><h2 align="center">Negative Margin</h2></font>
<img src="p1.jpg" class="image">
<p class="style1">I am working with Negative margin</p>
</body>
</html>

CSS Code:

.style1 {
font-size: 30px;
margin-left: -50px;
background: olive;
}
.image {
width: 500px;
height: 500px;
float:left; /* Keeps the image at left side */
}

Output before applying negative value:

left side

Output after applying negative value:

Negative margin in css 6

Explanation:

  • As you can see in the output negative left margin, paragraph overlaps by image. Negative left margin moves the content left side.
  • As you can observe the very left end, the paragraph comes out from the image.

Example #3 – Negative margin to the top side.

HTML Code:

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<link rel="stylesheet" href="NegativeMarginTop.css">
<title>Negative Margin</title>
</head>
<body>
<font color="green"><h2 align="center">Negative Margin</h2></font>
<p>
<img src="p3.jpg" class="image">
</p>
<p class="style1" align="center">I am working with Negative margin</p>
</body>
</html>

CSS Code:

.style1 {
font-size: 30px;
margin-top:-50px;
background: olive;
}
.image {
width: 400px;
height: 400px;
display: block;
margin-left: auto;
margin-right: auto;
}

Popular Course in this category
CSS Training (9 Courses, 9+ Projects)9 Online Courses | 9 Hands-on Projects | 61+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,444 ratings)
Course Price

View Course

Related Courses
Bootstrap Training (2 Courses, 6+ Projects)jQuery Training (8 Courses, 5 Projects)

Output before applying negative value:

top side

Output after applying negative value:

Negative margin in css 8

Explanation:

  • As you can see in the output negative top margin, paragraph overlaps by image. Negative top margin moves the content top side.

Example #4 – Negative margin to the right side.

HTML Code:

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<link rel="stylesheet" href="NegativeMarginRight.css">
<title>Negative Margin</title>
</head>
<body>
<font color="green"><h2 align="center">Negative Margin</h2></font>
<img src="p2.jpg" class="image">
<p class="style1" align="center">I am working with Negative margin</p>
</body>
</html>

CSS Code:

font-size: 30px;
margin-right: -50px;
background: olive;
}
.image {
width: 500px;
height: 500px;
float:right; /* Keeps the image at right side */
}

Output before applying negative value:

Right margin output

Output after applying negative value:

Negative margin in css 10

Explanation:

  • As you can see in the output negative right margin, paragraph overlaps by image. The negative right margin moves the content right side.
  • As you can observe very right end, the paragraph comes out from the image.

Example #5 – Negative margin with text.

HTML Code:

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<link rel="stylesheet" href="NegativeMarginText.css">
<title>Negative Margin</title>
</head>
<body>
<font color="green"><h2 align="center">Negative Margin</h2></font>
<p class="style1" align="center">First Paragraph: I am working with Negative margin.It always tries to move the text away from it's position</p>
<p class="style2" align="center">Second Paragraph: I am working with Negative margin.It always tries to move the text away from it's position</p>
</body>
</html>

CSS Code:

.style1 {
font-size: 30px;
background: brown;
width: 600px;
}
.style2 {
font-size: 30px;
width: 600px;
background: orange;
margin: -50px -20px -50px -80px;
}

Output before applying negative value:

with text

Output after applying negative value:

Move the text away

Explanation:

  • As you can see in the output negative margin, the paragraph overlaps by another paragraph.
  • The negative margin moves the content away from its position.
  • We can conclude margin negative values can also be applied with text as well.

Conclusion

The negative margin moves the content away from its position towards outside. We can apply a negative margin to top, right, bottom and left sides and negative margin applied with multiple values like positive margin values.

Recommended Articles

This is a guide to the Negative Margin CSS. Here we discuss the basic concept, examples and how does Negative Margin works in CSS? You may also have a look at the following articles to learn more –

  1. CSS Shape Generator
  2. CSS Position
  3. CSS for Text-Shadow
  4. CSS Attribute Selector

CSS Training (9 Courses, 9+ Projects)

9 Online Courses

9 Hands-on Projects

61+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
CSS Tutorial
  • CSS
    • Introduction To CSS
    • What is CSS?
    • Uses Of CSS
    • Advantages of CSS
    • Career In CSS
    • CSS Commands
    • Is Css Case Sensitive
    • CSS object-fit
    • Types of CSS Selectors
    • CSS Radio Button
    • CSS Attribute Selector
    • CSS first child of class
    • CSS Selector nth Child
    • CSS Parent Selector
    • CSS Child Selector
    • CSS Not Selector
    • CSS Descendant Selector
    • CSS Inline Style
    • Checkbox CSS
    • CSS Appearance
    • CSS Font Properties
    • CSS font-variant
    • CSS Pagination
    • CSS Table Styling
    • CSS Table Cell Padding
    • CSS Padding Color
    • CSS Text Formatting
    • CSS for Text-Shadow
    • CSS text-stroke
    • CSS text-indent
    • CSS Rotate Text
    • CSS Text Color
    • CSS Center Div
    • CSS Arrow
    • CSS Arrow Down
    • CSS offset
    • CSS Cursor
    • CSS Layout
    • CSS Grid Layout
    • Button in CSS
    • CSS Button Border
    • text-align in CSS
    • CSS Horizontal Align
    • CSS Position
    • CSS Box Sizing
    • CSS box-shadow
    • CSS Text Underline
    • CSS Text Outline
    • CSS Blinking Text
    • Text Decoration CSS
    • CSS Vertical Align
    • CSS Word Wrap
    • CSS Padding
    • CSS Font Color
    • CSS Color Generator
    • CSS Margin Right
    • CSS Margin Color
    • CSS Color Codes
    • CSS Color Transparent
    • CSS Color Chart
    • CSS Link Color
    • CSS z-index
    • CSS Curved Border
    • CSS Border Left
    • CSS left
    • CSS Gradient Generator
    • Radial Gradient in CSS
    • CSS Shape Generator
    • CSS Triangle Generator
    • CSS background-color
    • CSS Background Image
    • CSS background-clip
    • CSS background-blend-mode
    • CSS Drop Shadow
    • CSS line height
    • CSS line break
    • Sticky Footer CSS
    • CSS Header Design
    • CSS Border Style
    • CSS Border Generator
    • Sticky Sidebar CSS
    • CSS Transparent Border
    • CSS Border Radius
    • CSS translate
    • CSS transform
    • CSS 3D Transforms
    • CSS Text Transform
    • CSS Transition Effects
    • CSS Transition Property
    • CSS Animation Transition
    • Negative Margin CSS
    • CSS Navigation Bar
    • CSS Overflow
    • CSS overflow-wrap
    • CSS Lists
    • CSS list-style
    • CSS Order
    • CSS Box Model
    • CSS Inner Border
    • CSS Icon
    • Menu Icon CSS
    • CSS Multiple Borders
    • Opacity in CSS
    • CSS Float Right
    • CSS Clear Float
    • CSS clip
    • CSS disabled
    • CSS Border Padding
    • Border Images in CSS
    • CSS Visibility
    • CSS Validator
    • CSS Clearfix
    • CSS Counter
    • CSS Letter Spacing
    • CSS root
    • CSS zoom
    • CSS calc()
    • CSS.supports()
    • CSS Loader
    • Media Query CSS
    • CSS @keyframes
    • CSS @bottom
    • CSS page-break-after Property
    • CSS page-break
    • CSS Position Fixed
    • CSS skew()
    • CSS Row
    • CSS Masking
    • CSS Scrollbar
    • CSS Overlay
    • CSS Important
    • CSS Cursor Hand
    • CSS Inherit
    • CSS Position Relative
    • CSS Compressor
    • CSS tricks
    • CSS Outline Property
    • CSS Flexbox Properties
    • CSS flex-direction
    • CSS content property
    • CSS Typography
    • CSS Formatter
    • CSS nowrap
    • CSS Column
    • GridView CSS
    • CSS Viewport
    • CSS Minify
    • CSS Combinators
    • CSS in React
    • CSS Matrix 
    • CSS Pseudo Elements
    • CSS Pseudo Classes
    • CSS Pointer Events
    • CSS Resize
    • CSS Inheritance
    • CSS Interview Questions
    • Cheat Sheet CSS
  • CSS3
    • What is CSS3?
    • CSS3 Interview Questions
    • Cheat sheet CSS3
  • sass
    • How to Install SASS
    • SASS Interview Questions
    • What is Sass
    • SASS Comments
    • Sass Variables
    • SASS Import
    • SASS if else
    • SASS Nesting
    • SASS @each
    • SASS @at-root
    • SASS @extend
    • SASS @media
    • SASS @for
    • SASS Map
    • SASS Selectors
    • SASS Color Functions
    • SASS Mixins

Related Courses

CSS Training Course

Bootstrap Training Course

JQuery Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - CSS Training (9 Courses, 9+ Projects) Learn More