Definition
Composes an html pageTop-level tag
Features
1. The width and height cannot be set;
2. Required At the highest level in the document flow;
3. Contains all block-level elements and inline elements and cannot be included;
Common elements
html
body
frameset
2.2. Block-level element (Block-level element)Definition
Displayed in blocks Element, often used for typesetting. By default, each block-level element will be line wrapped displayed.
Features
1. Each block-level element always starts on a new line;
2. Height, line height and margins and The padding can be controlled;
3. The width defaults to 100% of its container, unless a width is set;
4. It can accommodate inline elements and other block elements ;
Common elements
address - address
blockquote - block quote
center - center alignment block (cancelled in html5 This tag)
div - commonly used block level, also the main tag of css layout
dl - definition list
fieldset - form control group
form - Interactive form
h1 - Heading
h2 - Subtitle
h3 - Level 3 heading
h4 - Level 4 heading
h5 - Level 5 heading
h6 - Level 6 heading
hr - Horizontal separator line
isindex - input prompt
menu - Menu list
noframes - frames optional content, (this block content is displayed for browsers that do not support frames)
noscript - optional script content (this content is displayed for browsers that do not support scripts)
ol - sorted form
p - paragraph
pre - formatted text
table - table
ul - unsorted list (none) Sequence list)
address - address
2.3. Inline element (Inline element)Definition
Inline element, inline element, by default One or more inline elements will be displayed in a row .
Features
1. Inline elements will be displayed on the same line;
2. Setting the width width is invalid, the width depends on the element Chinese The width of words or pictures;
3. Setting the height height is invalid, the height can only be set through line-height;
4. Setting the outer margin only the left and right margins are valid, and the top and bottom are invalid;
5. Setting margin padding is only valid for left and right padding, not top and bottom;
6. It can only accommodate text or other inline elements;
Common elements
a - anchor
abbr - abbreviation
acronym - first word
b - bold (not recommended)
bdo - bidi override
big - big font
br - line break
cite - quote
code - computer code (required when citing source code)
dfn - Define fields
em - Emphasis
font - Font settings (not recommended) (html5 cancels this tag)
i - Italics
img - image
input - input box
kbd - define keyboard text
label - table label
q - short quote
s - underscore (not recommended)
samp - definition sample computer code
select - project selection
small - small font text
span - commonly used inline container, defining blocks within text
strike - underline
strong - bold emphasis
sub - subscript
sup - superscript
textarea - multi-line text input box
tt - telex text
u - underline
var - define variables
Default value, no positioning, that is, normal position , element Appears in the normal HTML stream. When a div is unpositioned, the top, bottom, left, right or z-index declarations are invalid. (Relative positioning demo1)
2.1.2. Relative positioning-relativeDefinition
Generates a relatively positioned element, relative to its normal position, combined with TRBLAttributes (TOP, RIGHT, BOTTOM, LEFT) for positioning .
Features
1. Relative maintains the position of the object in the document flow, that is to say, it has and static The same presentation method , it also occupies a fixed position in the document flow, and subsequent objects will not occupy or cover ;
2. Positioning refers to the parent The original point is the original point. If there is no parent, the original point of the BODY is the original point. When there are CSS attributes such as padding in the parent, the original point of the current level is positioned with reference to the original point of the parent content area;
2.1.3. Absolute positioning-absolutedefinition
Absolute positioning, positioning the element out of the document flow, Combined with TRBL attributes (TOP, RIGHT, BOTTOM, LEFT) for positioning.
Features
1. Absolute does not maintain the position of the object in the document flow. It does not occupy a fixed position in the document flow. The subsequent object will Occupy or cover ;
2. If the parent (infinite) does not set the position attribute , then the current absolute is Combined with the TRBL attribute, the upper left corner of the browser is used as the original point for positioning. If the parent (infinite) sets the position attribute , then the current absolute is combined with the TRBL attribute to the upper left corner of the parent (nearest) Position the original point;
2.1.4, fixed positioning-fixedDefinition
Fixed positioning, so that the element is separated from the document flow position, CombinedTRBL attributes (TOP, RIGHT, BOTTOM, LEFT) based on browser location Locate.
Features
When a layer is defined as fixed, this layer will always fix elements based on the browser window combined with the TRBL attribute Positioning, automatically adjusts the position of this layer relative to the browser.
2.1.5, div layer-z-indexDefinition
The z-index attribute sets the stacking order of elements. Elements with a higher stacking order will always appear in front of elements with a lower stacking order.
Features
1. Z-index can only be effective on elements whose position attribute value is relative, absolute, or fixed;
2. The same Elements whose level and position attribute values are both relative cannot compare z-index;
3. The z-index value only determines the stacking order of child elements of the same level in the same parent element. The z-index value of the parent element (If any) The stacking order is defined for the child elements (the CSS version of stacking "Pin Daddy");
4. If the parent element containing the z-index value cannot be found by tracing upward, it can be regarded as A free z-index element, which can compare the z-index value with the parent element's sibling positioned elements or other free positioned elements to determine its stacking order;
5. Z of sibling elements -If the index values are the same, the stacking order is determined by the position of the elements in the document, and the element that appears later will be on top;
2.2. Float (float) 2.2.1. Define thefloat attribute to define the direction of the element. float. In CSS, any element can be floated. A floated element creates a block-level box, regardless of what type of element it is.
If there is very little space for a floating element on a row, the element will jump to the next row. This process will continue until a row has enough space.
2.2.2. FeaturesLook at the picture below. When box 1 is floated to the right, it breaks away from the document flow and moves to the right until its right edge touches the right edge of the containing box.
Look at the image below again. When box 1 floats to the left, it breaks away from the document flow and moves to the left until its left edge touches the left edge of the containing box. Because it is no longer in the document flow, it takes up no space and actually covers box 2, causing box 2 to disappear from view.
If you move all three boxes to the left, then box 1 floats to the left until it hits the containing box, and the other two boxes float to the left until it hits the previous floated box.
As shown in the figure below, if the containing box is too narrow to accommodate three floating elements arranged horizontally, then the other floating blocks move downward until there is enough space. If floated elements are of different heights, they may get "stuck" by other floated elements as they move downward.
2.2.3. Float clear-clearDefinition
Theclear attribute specifies which side of the element does not allow other floating elements.
Attribute value
| Floating elements are not allowed on the left . | ||||||||||
right | No floating elements allowed on the right side. | ||||||||||
both | Floating elements are not allowed on the left and right sides. | ||||||||||
none | Default value. Allows floated elements to appear on both sides. | ||||||||||
inherit | Specifies that the value of the clear attribute should be inherited from the parent element. |
特点
假设希望让一个图片浮动到文本块的左边,并且希望这幅图片和文本包含在另一个具有背景颜色和边框的元素中。您可能编写下面的代码:
.news {
background-color: gray;
border: solid 1px black;
}
.news img {
float: left;
}
.news p {
float: right;
}
<div class="news">
<img src="news-pic.jpg" />
<p>some text</p>
</div>
In this case, a problem arises. Because floated elements are out of the document flow, the divs surrounding images and text take up no space.
How to make the surrounding element visually surround the floating element? Clear needs to be applied somewhere within this element.
With this implementation, since there is no existing element to which cleanup can be applied, you can only add an empty element and clean it. The code is as follows
.news {
background-color: gray;
border: solid 1px black;
}
.news img {
float: left;
}
.news p {
float: right;
}
.clear {
clear: both;
}
<div class="news">
<img src="news-pic.jpg" />
<p>some text</p>
<div class="clear"></div>
</div>
This can achieve the effect we want, but it requires adding extra code. There are often elements that can be cleared, but sometimes you have to add meaningless markup for layout purposes.
Therefore, to solve this problem, the following solution is recommended, which is to float the container div.
.news {
background-color: gray;
border: solid 1px black;
float: left;
}
.news img {
float: left;
}