search
HomeWeb Front-endHTML Tutorialcss details review notes--padding, borders and margins_html/css_WEB-ITnose

The padding, border, and margin properties of an element affect how the entire document is laid out, and more importantly, they can heavily affect the appearance of a given element.

Height and Width

These two attributes cannot be applied to inline non-replaced elements, whose height and width are determined by the content of the link, not by the author. Elements in normal flow rarely have a set height.

Margins and padding

If the element has a background, it will affect the use of padding and margins, because the background will extend into the padding, But it won't extend to the margins. The padding and margins specified for an element affect when the element's background ends.

Add 10 pixels of margin to each side of the content area, margin: 10px, similar to HTML's hspace and vspace, set an extra space around the image. Margin follows the following pattern:

margin: top right bottom left. The values ​​rotate clockwise around the element from top to bottom. If you want to get the effect you want, you have to order the values ​​correctly. When one of the values ​​is defaulted, the following rules will be followed:

1) If the value of the left margin is missing, the value of the right margin will be used.

2) If the value of the bottom margin is missing, the value of the top margin is used.

3) If the value of the right margin is missing, the value of the top margin is used.

In other words, if you specify 3 values ​​for margin, the 4th value will be copied from the 2nd one. If two values ​​are given, the 4th value will be copied from the 2nd value, and the 3rd value will be copied from the 1st value.

Single-sided margin attribute , for margin:auto auto auto 3em; equivalent to margin-left:3em; you can use a single margin-top, margin-right, margin-bottom and margin-left single setting margin.

The rules of "negative margins and merged margins" were discussed in the previous section: the margins of vertically adjacent block-level elements will be merged in normal flow. But when margins are applied to inline non-replaced elements, they have no effect on line height. Since margins are actually transparent, this declaration has no visual effect, since margins on inline non-replaced elements do not change an element's line height.

But note that there is an extra space between the end of the word before the inline non-replaced element and the border of the inline element's background. If you like, you can add this extra space to both ends of the inline element:

<p style="font-size:12px;line-height:14px;"> This is text<em>some of which emphasized</em>,plus other text<br>which id <strong style ="margin:25px;background:silver">strongly emphasized</strong>and which is<br>larger than the surrounding text. </p>

The effect is as follows:

If the inline element is not Things get more interesting by replacing the element with negative margins. The top and bottom margins of the element are not affected, nor is the line height, but the left and right ends of the element may overlap other content.

Replaced elements are different: the margins set for the replaced element do affect the line height, possibly making it increase or decrease depending on the value of the top and bottom margins. The left and right margins of an inline replacement element have the same effect as the left and right margins of the replacing element.

Border

Every border has 3 aspects: its width or thickness, its style or appearance, and its color. The default style of the border is none, and the border may not be visible. The default border color is the foreground color of the element itself, if no color is declared for the border it will be the same as the element text color.

Border style

If a border has no style, it will have no width, but in some cases you may want to create an invisible one frame. This introduces the border color transparent, which is used to create invisible borders with width. This transparent border acts as padding, so the element's background extends into the border area (assuming there is a visible background).

Percent value and padding

You can set a percentage value for the padding of an element. As with outer margins, percentage values ​​are calculated relative to the width of their parent element, so if the width of the parent element changes, they will change.

Note that the top and bottom padding are consistent with the left and right padding; that is, the top and bottom padding will be calculated relative to the width of the parent element, not relative to the height.

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 Future of HTML: Evolution and TrendsThe Future of HTML: Evolution and TrendsMay 13, 2025 am 12:01 AM

The future of HTML will develop in a more semantic, functional and modular direction. 1) Semanticization will make the tag describe the content more clearly, improving SEO and barrier-free access. 2) Functionalization will introduce new elements and attributes to meet user needs. 3) Modularity will support component development and improve code reusability.

Why are HTML attributes important for web development?Why are HTML attributes important for web development?May 12, 2025 am 12:01 AM

HTMLattributesarecrucialinwebdevelopmentforcontrollingbehavior,appearance,andfunctionality.Theyenhanceinteractivity,accessibility,andSEO.Forexample,thesrcattributeintagsimpactsSEO,whileonclickintagsaddsinteractivity.Touseattributeseffectively:1)Usese

What is the purpose of the alt attribute? Why is it important?What is the purpose of the alt attribute? Why is it important?May 11, 2025 am 12:01 AM

The alt attribute is an important part of the tag in HTML and is used to provide alternative text for images. 1. When the image cannot be loaded, the text in the alt attribute will be displayed to improve the user experience. 2. Screen readers use the alt attribute to help visually impaired users understand the content of the picture. 3. Search engines index text in the alt attribute to improve the SEO ranking of web pages.

HTML, CSS, and JavaScript: Examples and Practical ApplicationsHTML, CSS, and JavaScript: Examples and Practical ApplicationsMay 09, 2025 am 12:01 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML is used to build web page structure; 2. CSS is used to beautify the appearance of web pages; 3. JavaScript is used to achieve dynamic interaction. Through tags, styles and scripts, these three together build the core functions of modern web pages.

How do you set the lang attribute on the  tag? Why is this important?How do you set the lang attribute on the tag? Why is this important?May 08, 2025 am 12:03 AM

Setting the lang attributes of a tag is a key step in optimizing web accessibility and SEO. 1) Set the lang attribute in the tag, such as. 2) In multilingual content, set lang attributes for different language parts, such as. 3) Use language codes that comply with ISO639-1 standards, such as "en", "fr", "zh", etc. Correctly setting the lang attribute can improve the accessibility of web pages and search engine rankings.

What is the purpose of HTML attributes?What is the purpose of HTML attributes?May 07, 2025 am 12:01 AM

HTMLattributesareessentialforenhancingwebelements'functionalityandappearance.Theyaddinformationtodefinebehavior,appearance,andinteraction,makingwebsitesinteractive,responsive,andvisuallyappealing.Attributeslikesrc,href,class,type,anddisabledtransform

How do you create a list in HTML?How do you create a list in HTML?May 06, 2025 am 12:01 AM

TocreatealistinHTML,useforunorderedlistsandfororderedlists:1)Forunorderedlists,wrapitemsinanduseforeachitem,renderingasabulletedlist.2)Fororderedlists,useandfornumberedlists,customizablewiththetypeattributefordifferentnumberingstyles.

HTML in Action: Examples of Website StructureHTML in Action: Examples of Website StructureMay 05, 2025 am 12:03 AM

HTML is used to build websites with clear structure. 1) Use tags such as, and define the website structure. 2) Examples show the structure of blogs and e-commerce websites. 3) Avoid common mistakes such as incorrect label nesting. 4) Optimize performance by reducing HTTP requests and using semantic tags.

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 Article

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

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.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.