search
HomeWeb Front-endCSS TutorialHTML CSS - learning margin and padding
HTML CSS - learning margin and paddingJun 28, 2017 pm 02:26 PM
htmlmarginpadding

Are you confused when you were learning margin and padding? - What the hell inner margin , what the hell outer margin . Hahaha, I was a little confused at first, but later I finally figured it out by looking up information. Now let me talk about my understanding of margin and padding:

1. What is margin

The margin in CSS refers to the distance (or space) between the border of the current element and the borders of other surrounding elements.

2. What is padding and what is margin

Code 2-1:


  1. span class="tag"> >

  2. html xmlns="http://www. w3.org/1999/xhtml">

  3. #​

    head> ;

  4. ##       
  5. meta http-equiv="Content-type" content="text/html; charset=UTF-8" />
  6. ​​​​
  7. style type="text/css">
  8.               body{                                                                                                                                                                                                                                            
  9. # .test1{
  10. width:150px;
  11. height:150px;

  12.                                                                                                                                                 ,,,,,,,,,,,,,,,,,

              .test2{                                                                                                                                                                                             #:40px;
  13. ##                        width:150px; 6px solid gray;

  14. completely: 80px;

  15. Height: 50px;

  16. ## Border: 12px solid blue;
  17. ##                                                                                     
  18.          style> /

    head
  19. >

    body
  20. > ;

    ##         p

  21. class
  22. =

    "test1">test1p>#        p

    class
  23. =
  24. "test2"

    >#                p class

    =
  25. "test2_son"
  26. >test2_sonp>##          p>

  27.     body>

  28. ## html>

  29. ##Picture 01 Note: Figure 01 The gray area is the border of p with class value test2, which has width;

  30. ①. Margin: Margin refers to the margin element (here It is p with the class value test

2
) The border extension is from the border extension of another element (if the other element is at the same level as the margin element (here is the class value test

1

's p)) or intension (if another element is the parent of the margin element), as shown above; ②, padding: padding It refers to the distance between the inner extension of the border of the margin element (here is p with class value test2) and the outer extension of the border of its child elements, as shown in the figure above;

③, inner margin and outer margin are for Regarding the elements it acts on and other elements, the inner margin of one element may appear to be the outer margin of another element. For example: the class value is test2 The inner margin of p appears as the outer margin when the class value is test2_test p element, so the above code can also be written like this: change the class value to test

2


Remove "padding-top:40px;" from the p style list, and add "margin-top:40px;" to the p element whose class value is test2_test - this effect is the same as code 2-1;

3. AttributeValue of margin and padding ①. Their default values ​​​​are all 0; their attribute values ​​​​can all be auto - the browser calculates the outer margins of the elements affected by margin, and the inner margins are calculated by the browser for elements affected by padding; both You can inherit the margins of the parent element by setting the attribute value to inherit - margin inherits the outer margin of the parent element, and padding inherits the inner margin of the parent element. However, since inherit can be used in any version This version of Internet Explorer (including IE8) does not support it, so there is no need to learn. ②. Margin allows you to specify negative margin values, but be careful when using them; padding does not allow you to specify negative margin values; ③. The attribute values ​​​​of margin and padding are both There can be 1, 2, 3 and 4:

a. Margin has 4 attribute values ​​(for example, margin:10px 5px 15px 20px;), which means: top margin 10px , right margin 5px, bottom margin 15px, left margin 20px;

                                                                                                                                                                                                                          padding has 4 attribute values ​​(for example, padding: 10px 5px 15px 20px;), which means: top padding 10px, right padding 5px, bottom padding 15px, left padding 20px;

Summary: Whether it is Margin or padding, if there are 4 attribute values, then their role direction is upper, right, lower, left;

                                                                                                                                                                                                                                                                                               

#                                                                                                                                                                                                                                 padding has 3 attribute values ​​(for example, padding: 10px 5px 15px;), which means: top padding 10px, right padding and left padding 5px, bottom padding 15px;

Summary: Whether it is Margin or padding, if there are 3 attribute values, then their role direction clockwise is upper, right left, lower; attribute value (for example, margin:10px 5px;), its meaning is: top and bottom margins 10px, right margin and left margin 5px; 10px 5px;), its meaning is: top padding and bottom padding 10px, right padding and left padding 5px;

Summary: Whether it is margin or padding, if there are 2 attributes value, then their direction of action

clockwise

is up and down, right and left; The margins are all 10px; padding, if there is 1 attribute value, then their margin values ​​are equal;

The above is the detailed content of HTML CSS - learning margin and padding. 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
HTML超文本标记语言--超在那里?(文档分析)HTML超文本标记语言--超在那里?(文档分析)Aug 02, 2022 pm 06:04 PM

本篇文章带大家了解一下HTML(超文本标记语言),介绍一下HTML的本质,HTML文档的结构、HTML文档的基本标签和图像标签、列表、表格标签、媒体元素、表单,希望对大家有所帮助!

web前端笔试题库之HTML篇web前端笔试题库之HTML篇Apr 21, 2022 am 11:56 AM

总结了一些web前端面试(笔试)题分享给大家,本篇文章就先给大家分享HTML部分的笔试题(附答案),大家可以自己做做,看看能答对几个!

CSS 边界属性详解:padding,margin 和 borderCSS 边界属性详解:padding,margin 和 borderOct 21, 2023 am 11:07 AM

CSS边界属性详解:padding,margin和borderCSS是一种用于控制和布局网页元素的样式表语言。在网页设计中,边界属性是其中一项非常重要的部分。本文将详细介绍CSS中边界属性的使用方法,并提供具体的代码示例。padding(内边距)padding属性用于设置元素的内边距,即元素内容和元素边界之间的空间。我们可以用正数或百分比值来设置内边距

HTML5中画布标签是什么HTML5中画布标签是什么May 18, 2022 pm 04:55 PM

HTML5中画布标签是“<canvas>”。canvas标签用于图形的绘制,它只是一个矩形的图形容器,绘制图形必须通过脚本(通常是JavaScript)来完成;开发者可利用多种js方法来在canvas中绘制路径、盒、圆、字符以及添加图像等。

总结HTML中a标签的使用方法及跳转方式总结HTML中a标签的使用方法及跳转方式Aug 05, 2022 am 09:18 AM

本文给大家总结介绍a标签使用方法和跳转方式,希望对大家有所帮助!

margin在css中什么意思margin在css中什么意思Dec 18, 2023 am 10:30 AM

在CSS中,margin是一个用于设置元素外边距的属性。外边距是元素边框与元素内容之间的空间。margin可以接受以下几种值:1、单独的值:例如,margin: 10px; 将所有四个外边距(上、右、下、左)都设置为10像素;2、两个值:例如,margin: 10px 20px; 将上下外边距设置为10像素,左右外边距设置为20像素;3、四个值等等。

html5废弃了哪个列表标签html5废弃了哪个列表标签Jun 01, 2022 pm 06:32 PM

html5废弃了dir列表标签。dir标签被用来定义目录列表,一般和li标签配合使用,在dir标签对中通过li标签来设置列表项,语法“<dir><li>列表项值</li>...</dir>”。HTML5已经不支持dir,可使用ul标签取代。

html中document是什么html中document是什么Jun 17, 2022 pm 04:18 PM

在html中,document是文档对象的意思,代表浏览器窗口的文档;document对象是window对象的子对象,所以可通过“window.document”属性对其进行访问,每个载入浏览器的HTML文档都会成为Document对象。

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

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.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

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),

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment