search
HomeWeb Front-endCSS TutorialWhat does CSS margin do?

First of all, we need to know what margin means. The margin attribute is the abbreviation attribute of CSS for setting all margin attributes in one statement. It is an attribute used to control the distance between each element. They are transparent and invisible.

The margin attribute includes margin left: the distance from the left element block (set the distance from the left inner margin ); margin top: the distance from the top (top) element block (set the distance from the top element block) distance); margin right: distance from the right element block (set the block distance from the right element); margin bottom: distance from the bottom element block (set the distance from the lower (lower) element block). Its two-dimensional construction diagram can be seen in the CSS attribute two-dimensional diagram.

margin left usage: margin-left:10px; This means 10 pixels from the left element block, which can be followed by a percentage such as (margin-left:10%; 10% from the left element block) distance) css margin-left tutorial;

margin right usage: margin-right:10px; This means that the block distance from the right element is 10 pixels, which can be followed by a percentage such as (margin-right: 10%; 10% of the distance from the right element block);

margin top usage: margin-top:10px; This means 10 pixels from the top element block, which can be followed by the percentage, such as (margin-top:10%; 10% distance from the top element block) css margin-top;

margin bottom usage: margin-bottom:10px; This means distance from the lower edge The element block distance is 10 pixels, which can be followed by a percentage such as (margin-bottom:10%; 10% distance from the bottom element block) css margin-bottom;

Pay attention to the link "-" in the middle of the margin, set Use " : " to assign a distance value, end with " ; ", and use all lowercase half-width letters.

If you need to set the margin value for both the left and right sides, you can do it in abbreviation to optimize css.

For example, the abbreviation is:

margin:10px; It means that the distance between the top, bottom, left and right element blocks is 10px (10 pixels), which is equal to margin-top:10px; margin-bottom:10px; margin-left :10px; margin-right:10px; abbreviation for the same effect;

margin:5px 10px; meaning that the distance between the upper and lower element blocks is 5px, and the distance between the left and right element blocks is 10px, which is equal to margin-top:5px; margin-bottom :5px; margin-left:10px; margin-right:10px; Abbreviation for the same effect;

margin:5px 6px 7px; meaning the distance between the upper element block is 5px, the distance between the lower element block is 7PX, and the distance between the left and right element blocks is 6px, equal to margin-top:5px; margin-bottom:7px; margin-left:6px; margin-right:6px; abbreviation for the same effect;

margin:5px 6px 7px 8px; meaning the element block is 5px , the distance between the right element block is 6px, the distance between the lower element block is 7px, and the distance between the left element block is 8px, which is equal to margin-top:5px; margin-right:6px; margin-bottom:7px; margin-right:8px; abbreviation for the same effect ;

There are so many ways to use the margin attribute of CSS. Friends who need it can save it. Please also continue to pay attention to other updates on this site.

Related reading:

CSS3 tips for drawing various basic graphics


html and css Tutorial on making QQ Penguin


Do you understand these css principles?

The above is the detailed content of What does CSS margin do?. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Lost CSS Tricks of Cohost.orgThe Lost CSS Tricks of Cohost.orgApr 25, 2025 am 09:51 AM

In this post, Blackle Mori shows you a few of the hacks found while trying to push the limits of Cohost’s HTML support. Use these if you dare, lest you too get labelled a CSS criminal.

Next Level CSS Styling for CursorsNext Level CSS Styling for CursorsApr 23, 2025 am 11:04 AM

Custom cursors with CSS are great, but we can take things to the next level with JavaScript. Using JavaScript, we can transition between cursor states, place dynamic text within the cursor, apply complex animations, and apply filters.

Worlds Collide: Keyframe Collision Detection Using Style QueriesWorlds Collide: Keyframe Collision Detection Using Style QueriesApr 23, 2025 am 10:42 AM

Interactive CSS animations with elements ricocheting off each other seem more plausible in 2025. While it’s unnecessary to implement Pong in CSS, the increasing flexibility and power of CSS reinforce Lee's suspicion that one day it will be a

Using CSS backdrop-filter for UI EffectsUsing CSS backdrop-filter for UI EffectsApr 23, 2025 am 10:20 AM

Tips and tricks on utilizing the CSS backdrop-filter property to style user interfaces. You’ll learn how to layer backdrop filters among multiple elements, and integrate them with other CSS graphical effects to create elaborate designs.

SMIL on?SMIL on?Apr 23, 2025 am 09:57 AM

Well, it turns out that SVG's built-in animation features were never deprecated as planned. Sure, CSS and JavaScript are more than capable of carrying the load, but it's good to know that SMIL is not dead in the water as previously

'Pretty' is in the eye of the beholder'Pretty' is in the eye of the beholderApr 23, 2025 am 09:40 AM

Yay, let's jump for text-wrap: pretty landing in Safari Technology Preview! But beware that it's different from how it works in Chromium browsers.

CSS-Tricks Chronicles XLIIICSS-Tricks Chronicles XLIIIApr 23, 2025 am 09:35 AM

This CSS-Tricks update highlights significant progress in the Almanac, recent podcast appearances, a new CSS counters guide, and the addition of several new authors contributing valuable content.

Tailwind's @apply Feature is Better Than it SoundsTailwind's @apply Feature is Better Than it SoundsApr 23, 2025 am 09:23 AM

Most of the time, people showcase Tailwind's @apply feature with one of Tailwind's single-property utilities (which changes a single CSS declaration). When showcased this way, @apply doesn't sound promising at all. So obvio

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools