CSS
css grouping element
-
<div>
The HTML Content Division element (<div>
) is the generic container for flow content. It has no effect on the content or layout until styled using CSS. As a "pure" container, the <div>
element does not inherently represent anything. Instead, it's used to group content so it can be easily styled using the class
or id
attributes, marking a section of a document as being written in a different language (using the lang
attribute), and so on.
HTML
<!-- Learn about this code on MDN: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/div --> <div class="shadowbox"> <p>Here's a very interesting note displayed in a lovely shadowed box.</p> </div>
CSS
.shadowbox { width: 15em; border: 1px solid #333; box-shadow: 8px 8px 5px #444; padding: 8px 12px; background-image: linear-gradient(180deg, #fff, #ddd 40%, #ccc); }
-
<span>
The HTML <span>
element is a generic inline container for phrasing content, which does not inherently represent anything. It can be used to group elements for styling purposes (using the class
or id
attributes), or because they share attribute values, such as lang
.It should be used only when no other semantic element is appropriate. <span>
is very much like a <div>
element, but <div>
is a block-level element whereas a <span>
is aninline element.
HTML
<li><span> <a href="portfolio.html" target="_blank">See my portfolio</a> </span></li>
CSS
li span { background: gold; }
-
overflow
The overflow
CSS property specifies what to do when an element's content is too large to fit in its block formatting context. It is a shorthand for the overflow-x
and overflow-y
properties.
syntax:
/* Keyword values */ overflow: visible; overflow: hidden; overflow: scroll; overflow: auto; /* Global values */ overflow: inherit; overflow: initial; overflow: unset;
Values
visible
- Content is not clipped and may be rendered outside the padding box.
hidden
- Content is clipped if necessary to fit the padding box. No scrollbars are provided.
scroll
- Content is clipped if necessary to fit the padding box. Browsers display scrollbars whether or not any content is actually clipped. (This prevents scrollbars from appearing or disappearing when the content changes.) Printers may still print overflowing content.
auto
- Depends on the user agent. If content fits inside the padding box, it looks the same as
visible
, but still establishes a new block-formatting context. Desktop browsers provide scrollbars if content overflows.
The box model
-
margin
The margin
CSS property sets the margin area on all four sides of an element. It is a shorthand for setting all individual margins at once: margin-top
, margin-right
, margin-bottom
, and margin-left
.
The top and bottom margins have no effect on non-replaced inline elements, such as <span>
or <code>
.
Margins create extra space around an element. In contrast, padding
creates extra space within an element.
/* Apply to all four sides */ margin: 1em; margin: -3px; /* vertical | horizontal */ margin: 5% auto; /* top | horizontal | bottom */ margin: 1em auto 2em; /* top | right | bottom | left */ margin: 2px 1em 0 auto; /* Global values */ margin: inherit; margin: initial; margin: unset;
The margin
property may be specified using one, two, three, or four values. Each value is a <length>
, a <percentage>
, or the keyword auto
. Negative values draw the element closer to its neighbors than it would be by default.
- When one value is specified, it applies the same margin to all four sides.
- When two values are specified, the first margin applies to the top and bottom, the second to the left and right.
- When three values are specified, the first margin applies to the top, the second to the left and right, the third to the bottom.
- When four values are specified, the margins apply to the top, right, bottom, and leftin that order (clockwise).
-
padding
The padding
CSS property sets the padding area on all four sides of an element. It is a shorthand for setting all individual paddings at once: padding-top
, padding-right
, padding-bottom
, and padding-left
.
/* Apply to all four sides */ padding: 1em; /* vertical | horizontal */ padding: 5% 10%; /* top | horizontal | bottom */ padding: 1em 2em 2em; /* top | right | bottom | left */ padding: 5px 1em 0 2em; /* Global values */ padding: inherit; padding: initial; padding: unset;
The padding
property may be specified using one, two, three, or four values. Each value is a <length>
or a <percentage>
. Negative values are invalid.
- When one value is specified, it applies the same padding to all four sides.
- When two values are specified, the first padding applies to the top and bottom, the second to the left and right.
- When three values are specified, the first padding applies to the top, the second to the left and right, the third to the bottom.
- When four values are specified, the paddings apply to the top, right, bottom, and left in that order (clockwise).
-
border
The border
CSS property is a shorthand for setting all individual border property values in a single declaration: border-width
, border-style
, and border-color
.
syntax:
/* style */ border: solid; /* width | style */ border: 2px dotted; /* style | color */ border: outset #f33; /* width | style | color */ border: medium dashed green; /* Global values */ border: inherit; border: initial; border: unset;
-
CSS position
CSS position
属性用于指定一个元素在文档中的定位方式。top
,right
,bottom
和 left
属性则决定了该元素的最终位置。
定位类型
- 定位元素(positioned element)是其计算后位置属性为
relative
,absolute
,fixed
或sticky
的一个元素。 - 相对定位元素(relatively positioned element)是计算后位置属性为
relative
的元素。 - 绝对定位元素(absolutely positioned element)是计算后位置属性为
absolute
或fixed
的元素。 - 粘性定位元素(stickily positioned element)是计算后位置属性为
sticky
的元素。
大多数情况下,height
和width
被设定为auto的绝对定位元素,按其内容大小调整尺寸。但是,被绝对定位的元素可以通过指定top
和bottom
,保留height
未指定(即auto
),来填充可用的垂直空间。它们同样可以通过指定left
和 right
并将width
指定为auto
来填充可用的水平空间。
除了刚刚描述的情况(绝对定位元素填充可用空间):
- 如果
top
和bottom
都被指定(技术上,而不是auto
),top
胜出。 - 如果指定了
left
和right
两侧,则在direction
为ltr(英语,水平日语等)时left
赢,并且在direction
为rtl时right
赢(阿拉伯文,希伯来文等)。
常见语法
static | relative | absolute | sticky | fixed
语法
position
属性被指定为从下面的值列表中选择的单个关键字。
取值
static
- 该关键字指定元素使用正常的布局行为,即元素在文档常规流中当前的布局位置。此时
top
,right
,bottom
,left
和z-index
属性无效。 relative
- 该关键字下,元素先放置在未添加定位时的位置,再在不改变页面布局的前提下调整元素位置(因此会在此元素未添加定位时所在位置留下空白)。position:relative 对 table-*-group, table-row, table-column, table-cell, table-caption 元素无效。
absolute
- 不为元素预留空间,通过指定元素相对于最近的非 static 定位祖先元素的偏移,来确定元素位置。绝对定位的元素可以设置外边距(margins),且不会与其他边距合并。
- fixed
- 不为元素预留空间,而是通过指定元素相对于屏幕视口(viewport)的位置来指定元素位置。元素的位置在屏幕滚动时不会改变。打印时,元素会出现在的每页的固定位置。
fixed
属性会创建新的层叠上下文。当元素祖先的transform
属性非none
时,容器由视口改为该祖先。
sticky
- 盒位置根据正常流计算(这称为正常流动中的位置),然后相对于该元素在流中的 flow root(BFC)和 containing block(最近的块级祖先元素)定位。在所有情况下(即便被定位元素为
table 时
),该元素定位均不对后续元素造成影响。当元素 B 被粘性定位时,后续元素的位置仍按照 B 未定位时的位置来确定。position: sticky
对table
元素的效果与position: relative
相同。 - open in MDN
-
z-index
z-index
属性指定了一个具有定位属性的元素及其子代元素的 z-order。 当元素之间重叠的时候,z-order 决定哪一个元素覆盖在其余元素的上方显示。 通常来说 z-index 较大的元素会覆盖较小的一个。
对于一个已经定位的元素(即position属性值是非static的元素),z-index
属性指定:
- 元素在当前堆叠上下文中的堆叠层级。
- 元素是否创建一个新的本地堆叠上下文。
语法
/* Keyword value */ z-index: auto; /* <integer> values */ z-index: 0; z-index: 3; z-index: 289; z-index: -1;/* 使用负值降低优先级 */ /* Global values */ z-index: inherit; z-index: initial; z-index: unset;