Introduction to the four properties in the CSS box model (with code)
不言
Release: 2018-08-07 14:20:09
Original
7332 people have browsed it
<p>This article brings you an introduction to the four attributes in the CSS box model (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
<p>1. Replacement elements and non-replacement elements
<p>Depending on whether the "outer box" is inline or block level We can divide elements into inline elements and block-level elements, and depending on whether has replaceable content, we can also divide elements into replaced elements and non-replaced elements
1.1 Replacement elements Definition
<p>Elements that can be replaced by modifying the content presented by a certain attribute value are called "replacement elements". Common replacement elements are: ,
1.2 Features of replacement elements
<p>1. The appearance of the content is not affected by the CSS on the page ; Such as the inner spacing, background color and other styles of the single check box
<p>2, have their own size; such as
<p>3, Many CSS properties have their own set of performance rules; in vertical-align, the replacement element defaults to baseline (the lower edge of the letter x), and the baseline of the replacement element is abruptly defined as the lower edge of the element
1.3 Size calculation rules for replaced elements
<p>The sizes of replaced elements are divided into 3 categories from the inside out: intrinsic size, HTML size and CSS size
<p>Inherent size refers to the original size of the replacement content; for example, when pictures, videos, and input exist as an independent file, they all have their own width and height;
<p>HTML size, "HTML size" can only be changed through HTMLnative properties. These HTMLnative properties include<img alt="Introduction to the four properties in the CSS box model (with code)" >## The width and height properties of #, the size property of , the
<p>CSS size specifically refers to the width and The size set by height or max-width/min-width and max-height/min-height corresponds to the content box# in the box size.
content<p> attribute can be used not only in ::before/::after, but also in elements, but there is some compatibility. sex. Under the Chrome browser, all elements support the content attribute, while other browsers only support the ::before/::after pseudo-element
<p>Case 1: Based on pseudo-elements Image content generation technology for elements
<p>HTML:
<p>
<p><p>Principle <p>: When the picture does not have src, ::before and ::after can take effect; when adding a src address to the picture, the picture changes from ordinary The element becomes a replacement element. ::before and ::after, which were originally supported, are all invalid at this time. Case 2: Content introduces images
img { content: url(1.jpg); }
Copy after login
<p>Case 3: Hover image replacement
<img src="laugh.png" alt="Introduction to the four properties in the CSS box model (with code)" >
img:hover {
content: url(laugh-tear.png);
}
#padding affects both the horizontal and vertical directions of inline elements<p>
padding width and height percentages are calculated based on the width of the parent element<p>
Many, many front-end colleagues have such a wrong understanding: inline The padding of elements only affects the horizontal direction, not the vertical direction. This perception is inaccurate, inline elements
Padding also affects layout in the vertical direction and affects visual performance. Just because inline elements have no visual width and visual height (clientHeight and
clientWidth is always 0), vertical behavior is completely affected by line-height and vertical-align
The impact is that the distance between the content of the previous line and the next line is not visually changed. Therefore, it gives us the feeling that vertical padding has no effect. <p>Case 1: Increase the click area
<p>Layout principle: <p>By default, the top and bottom distance of vertical block-level elements is 0, once margin-bottom:-9999px means all following elements and the above element
The spatial distance of the element becomes -9999px, which means that all subsequent elements have moved upward by 9999px. At this point, make a stroke of God padding-bottom:9999px
Increasing the height of the element, which is plus or minus, has no effect on the layout layer, but it brings what we need - the visual layer has an additional 9999px height available for the background color. <p>4.2 Margin mergingThe top margin (margin-top) and bottom margin (margin-bottom) of block-level elements are sometimes merged into a single margin. This phenomenon Called "margin merging"<p>. Two conditions: block-level elements and three scenarios where margin merging occurs only in the vertical direction <p>(1) Margin merging of adjacent sibling elements . This is the most common and basic
The above is the detailed content of Introduction to the four properties in the CSS box model (with code). For more information, please follow other related articles on the PHP Chinese website!
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