Html css -- learning of margin and padding, marginpadding

Source: Internet
Author: User

Html css -- learning of margin and padding, marginpadding
Http://www.cnblogs.com/fengyv/p/3789333.html when you are learning margin and padding is not done, -- what his mother's padding, what his mother's margin. Haha, I didn't understand it at first, but I finally understood it through data query. Now I will talk about my understanding of margin and padding:

I. What are margins?

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

2. What is the padding and what is the margin?

Code 2-1:

  1. <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Strict // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  2. <Html xmlns = "http://www.w3.org/1999/xhtml">
  3. <Head>
  4. <Meta http-equiv = "Content-type" content = "text/html; charset = UTF-8"/>
  5. <Style type = "text/css">
  6. Body {
  7. Margin: 0px;
  8. }
  9. . Test1 {
  10. Width: 150px;
  11. Height: 150px;
  12. Border: 6px solid red;
  13. }
  14. . Test2 {
  15. Margin-top: 40px;
  16. Padding-top: 40px;
  17. Width: 150px;
  18. Height: 150px;
  19. Border: 6px solid gray;
  20. }
  21. . Test2_son {
  22. Width: 80px;
  23. Height: 50px;
  24. Border: 12px solid blue;
  25. }
  26. </Style>
  27. </Head>
  28. <Body>
  29. <Div class = "test1"> test1 </div>
  30. <Div class = "test2">
  31. <Div class = "test2_son"> test2_son </div>
  32. </Div>
  33. </Body>
  34. </Html>

Figure 01

Note: In Figure 01, the gray area is the border of the div whose class value is test2. It has a width;

①. Margin: the margin refers to the elements used by margin (here, the class value is test.2(If the other element is the same as the element used by margin, the class value is test .)1(Div) or inner (if another element is the parent level of the element to which margin is applied), such;

②. Padding: the padding refers to the extension distance between the border of the margin element (the div with the class value test2) and the border of its child element, for example;

③ The internal and external margins are used for the elements and other elements. The internal margins of an element may appear in the eyes of another element, for example: the class value is test.2The inner margin of the div seems to be the outer margin when the class value is test2_test div, so the code above can also write as follows: Set the class value to test2In the div style list of, remove "padding-top: 40px;" and add "margin-top: 40px" to the class value test2_test div element; "-- the effect is the same as that of Code 2-1;

3. Attributes of margin and padding

① Their default values are all 0; their attribute values can be used to calculate the outer margin for the auto -- margin element, and the padding for the padding element is calculated by the browser; you can inherit the margin of the parent element by setting the property value to inherit -- margin inherits the margin of the parent element, and padding inherits the padding of the parent element, however, because inherit is not supported in any version of Internet Explorer (including IE8), it is unnecessary to learn.

②. Margin allows you to specify a negative margin value, but be careful when using it. padding does not allow you to specify a negative margin value;

③ Attributes of margin and padding can have one, two, three, and four attributes:

A. margin has four attribute values (for example, margin: 10px 5px 15px 20px;). The values are: Top margin: 10px, right margin: 5px, bottom margin: 15px, and left margin: 20px;

Padding has four attribute values (for example, padding: 10px 5px 15px 20px;). It indicates that the top margin is 10px, the right margin is 5px, the bottom margin is 15px, and the left margin is 20px;

Conclusion: whether it is margin or padding, if there are four attribute values, their direction is clockwise, top, right, bottom, left;

B. margin has three attribute values (for example, margin: 10px 5px 15px;). The values include: Top margin 10px, right margin and left margin 5px, and bottom margin 15px;

Padding has three attribute values (for example, padding: 10px 5px 15px;), which indicate: Top margin: 10px, right margin, left margin: 5px, and bottom margin: 15px;

Conclusion: whether it is margin or padding, if there are three attribute values, their direction is clockwise, top right left, bottom;

C. margin has two attribute values (for example, margin: 10px 5px;), which indicate: Top margin and bottom margin 10px, right margin and left margin 5px;

Padding has two attribute values (for example, padding: 10px 5px;). It indicates that the top and bottom margins are 10px, the right and left margins are 5px;

Conclusion: whether it is margin or padding, if there are two attribute values, their direction is clockwise up and down, right left;

D. margin has one attribute value (for example, margin: 10px;), which means that the four margins are 10px;

Padding has one attribute value (for example, padding: 10px;), which means that the four padding values are 10px;

Conclusion: whether it is margin or padding, if one attribute value exists, their margins are equal.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.