To achieve separation of content and style; adjust the layout of the page, the color and size of elements, add background images and borders, create animation effects, etc., using CSS.
selector {
property: value;
}
/* Tag selector: This CSS rule will apply to all h1 elements in the HTML document, making the text color of these h1 elements blue and the font size 24 pixels */
h1 {
color: blue;
font-size: 24px;
}
/* Class selector */
/* <div class="block1"> turns red </div> */
.block1 {
color: red;
}
/* ID selector */
/* <p id="id1"> turns red </p> */
#id1 {
color: red;
}
/* Universal selector: Clears the margin and padding of all element tags */
* {
margin: 0;
padding: 0;
}
/* Attribute selector */
/* Starting from css2 */
[attribute] { property: value; } /* Matches all elements with the attribute */
[attribute="value"] { property: value; } /**/
[attribute^="value"] { property: value; } /* Matches all elements whose attribute value starts with "value" */
[attribute|="value"] { property: value; } /* Matches elements whose attribute value is "value" or starts with "value-" */
/* css3 */
[attribute$="value"] { property: value; } /* Matches all elements whose attribute value ends with "value" */
[attribute*="value"] { property: value; } /* Matches all elements whose attribute value contains the string "value" */
[attribute~="value"] { property: value; } /* Matches elements whose attribute value contains the word "value" */
- The selector specifies which elements the CSS rules apply to; selectors include: tag selectors, class selectors, ID selectors, and universal selectors.
- property: value;: A set of CSS declarations.
Compound Selector#
Union Selector#
Separate multiple basic selectors with commas.
p, b, i, span { /* Select all <p>, <b>, <i>, <span> elements and set their color to red; */
color: red;
}
Intersection Selector#
div.class { /* Both a Div tag and a class */
color: red;
}
Descendant Selector#
The descendant selector selects all descendant elements within a certain element, regardless of the depth of these descendant elements.
p b { /* Selects all <b> elements within <p> elements, no matter how deeply nested <b> is within <p> */
color: red;
}
Child Selector#
Selects direct child elements of a certain element.
ul > li {
border: 1px solid red;
}
Adjacent Sibling Selector#
Selects the sibling element immediately following another element.
p + b { /* Selects the first <b> element immediately following a <p> element */
color: red;
}
General Sibling Selector#
Selects all sibling elements that are located after a certain element.
p ~ b { /* Selects all <b> elements located after a <p> element, not necessarily immediately following, as long as they are at the same level in the DOM tree */
color: red;
}
Pseudo-class Selector#
Pseudo-class Selector | Description |
---|---|
Matches the first child element of the parent | |
Matches the last child element of the parent | |
(n) | Matches the nth child element in the parent; n is an incrementing natural number, can also be a constant or odd, even |
(n) | Matches the nth child element of the same type in the parent |
Matches the first child element of the same type in the parent | |
Matches the last child element of the same type in the parent | |
Matches all unvisited links | |
Matches all visited links | |
Matches elements when the mouse hovers over them | |
Matches links that are clicked (e.g., the state when the mouse is held down) | |
Matches elements that are checked | |
Matches each disabled element | |
Matches any element that has no child elements | |
Matches each enabled element | |
Matches elements that have focus | |
Matches elements with specified value ranges | |
Matches all elements with invalid values | |
(language) | Matches each element whose lang attribute value starts with "it" |
(selector) | Matches each element that is not a element |
(n) | Matches the second to last child element of the parent |
(n) | Matches the second to last child element of the parent |
Matches the only element in the parent | |
Matches the only child element in the parent | |
Matches elements that do not have the "required" attribute | |
Matches elements with values outside the specified range | |
Matches elements that have the "readonly" attribute | |
Matches elements that do not have the "readonly" attribute | |
Matches elements that have the "required" attribute | |
Matches the root element of the document, in HTML, the root element is always HTML | |
Matches the currently active #news element (clicking the URL containing that anchor name) | |
Matches all elements with valid values |
Pseudo-element Selector#
Matches parts of an element (such as the first letter, first line of text, etc.)
Pseudo-element | Example Description | CSS |
---|---|---|
::after | Inserts content after the element | 2 |
::before | Inserts content before the element | 2 |
::first-letter | Matches the first character of block-level element content | 1 |
::first-line | Matches the first line of block-level element content | 1 |
::selection | Matches the element selected by the user | |
::placeholder | Matches placeholder text of form controls (like ); used to customize text styles |
<p>strick</p>
<input type="text" placeholder="Please enter"/>
p::first-letter {
font-size: 200%;
}
p::first-line {
font-weight: bold;
}
input::placeholder {
color: red;
}
p::selection {
color: red;
}
p::before {
content: "I "<!-- can be an image url('./smiley.gif'); -->
}
p::after {
content: " Strick"
}
CSS Inclusion#
When multiple stylesheets affect the same element, the browser determines which styles to apply based on the cascading priority rules of conflicting styles: !important
> inline styles > embedded styles > external linked styles > browser default styles;
Selector priority: the smaller the selected range, the higher the priority.
Inline Styles#
Set CSS styles in the style attribute within the element tag. Suitable for modifying simple styles; each declaration key-value pair is separated by ;
.
<p style="color:red;font-size:50px;">hello world</p>
Embedded Styles#
Create a
<style type="text/css">
p {
color: blue;
font-size: 40px;
}
</style>
<p>This is a piece of text</p>
External Linked Styles#
<link rel="stylesheet" type="text/css" href="style.css">
style.css:
@charset "utf-8";
p {
color: green;
font-size: 30px;
}
Force Set Highest Priority#
color: green !important;
Style Inheritance#
- If an element does not set styles related to its parent element, it inherits the styles of the parent element (only applicable to text-, font-, line- prefixed styles and color properties can be inherited, not applicable to layout styles).
- Force inheritance of layout styles.
<p style="color:red;">This is <b>HTML5</b></p> <!--<b> element inherits the style of <p> element-->
<p>This is <b>HTML5</b></p>
<style type="text/css">
p {
border: 1px solid red;<!-- border is a non-inheritable style -->
}
b {
border: inherit;
}
</style>
Box Model#
Web page layout process:
- Set the styles of each web element box; place them in appropriate positions.
- Fill the box with content.
The distance between content and border is the padding; the distance between boxes is the margin.
Element Box Types#
Block-level Elements#
- Block-level elements occupy a whole line;
- Height, width, margin, and padding can all be controlled.
- The default width is 100% of the container (parent width).
- Text elements cannot contain block-level elements; for example,
~#
.
Common blocks:
<h1>~<h6>、<p>、<div>、<ul>、<ol>、<li>
Inline Elements#
- Multiple inline elements can be displayed in one line;
- Directly setting height and width is ineffective. They can only adapt to the width of the content.
- Inline elements can only contain text or other inline elements.
- Links cannot contain links; in special cases, links can contain block-level elements, but converting to block-level mode is the safest.
Common inline elements:
<a>、<strong>、<b>、<em>、<i>、<del>、<s>、<ins>、<u>、<span>
Inline-block Elements#
- Multiple inline-block elements can be displayed in one line, but there will be a gap between them;
- The default width is adaptive to the content width; height, line height, margin, and padding can all be controlled.
Common inline-block elements:
<img />、<input />、<td>
Type Conversion#
span { /* Convert to block element */
display: block;
}
div { /* Convert to inline element */
display: inline;
}
div { /* Convert to inline-block element */
display: inline-block;
}
div { display: none; /* Hide the element and do not occupy space */
}
Set Element Box Size#
Property | Value | Description | CSS Version |
---|---|---|---|
width | auto, length value or percentage | Set the width of the element | 1 |
height | auto, length value or percentage | Set the height of the element | 1 |
min-width | auto, length value or percentage | Set the minimum width of the element | 2 |
min-height | auto, length value or percentage | Set the minimum height of the element | 2 |
max-width | auto, length value or percentage | Set the maximum width of the element | 2 |
max-height | auto, length value or percentage | Set the maximum height of the element | 2 |
box-sizing | content-box border-box | Default value, the width and height of the element only calculate the content size, excluding padding and border ; the width and height of the element include padding and border | 3, initially an experimental feature in various kernels, required browser prefixes, later no longer needed. |
resize | none both horizontal vertical | Default value for ordinary elements, does not allow client users to adjust the size of the element; default value for form textarea elements, client users can adjust the width and height of the element; client users can adjust the width of the element; client users can adjust the height of the element. |
div {
background-color: purple;
width: 200px; height: 200px;
min-width: 100px;
min-height: 100px;
max-width: 300px;
max-height: 300px;
resize: both;
overflow: auto; /* With this, a draggable size graphic appears */
/* -webkit-box-sizing: border-box;*/ /* Safari and Chrome */
/* -moz-box-sizing: border-box; */ /* Firefox */
/* -ms-box-sizing: border-box; */ /* IE */
box-sizing: border-box; /* Standard */
padding: 10px; border: 5px solid red;
}
Set Padding#
Set the size of the internal edge padding of the element.
Property | Value | Description | CSS Version |
---|---|---|---|
padding-top | length value or percentage | Set the top padding | 1 |
padding-right | length value or percentage | Set the right padding | 1 |
padding-bottom | length value or percentage | Set the bottom padding | 1 |
padding-left | length value or percentage | Set the left padding | 1 |
padding | 1 ~ 4 length values or percentages | Shorthand property | 1 |
div {
padding-top: 10px;
padding-right: 10px;
padding-bottom: 10px;
padding-left: 10px;
}
div {
padding: 10px 10px 10px 10px;
}
div {
padding: 10px 20px; /* Top and bottom, left and right */
}
div {
padding: 10px;
}
Set Margin#
Set the size of the external edge padding of the element.
Property | Value | Description | CSS Version |
---|---|---|---|
margin-top | |||
margin-right | |||
margin-bottom | |||
margin-left | |||
margin | length value percentage auto: margin automatically occupies the browser width | Set the top padding | |
Set the right padding | |||
Set the bottom padding | |||
Set the left padding | |||
Shorthand property | 1 |
div {
margin-top: 10px;
margin-right: 10px;
margin-bottom: 10px;
margin-left: 10px;
}
div {
margin: 10px 10px 10px 10px;
}
div {
margin: 10px 20px;
}
div {
margin: 10px;
}
/* Center the box */
div {
margin: 0 auto;
}
/* Center the box horizontally */
div {
margin-right: auto;
margin-left: auto;
}
Margin Collapse When Boxes Are Vertically Aligned#
Adjacent boxes arranged vertically; the actual distance is the maximum value of the two boxes' top and bottom margins, rather than the sum of the margins in other situations.
Margin Collapse of Child Elements When Vertically Aligned#
When a child element adds margin-top, it causes both the parent and child elements to move down together.
Solutions: The motivation for adding margin-top to the child element is generally to adjust the vertical alignment of the child element within the parent element. Better methods to adjust this vertical alignment:
- Remove the child's margin, set the parent's padding-top.
- Set overflow: hidden on the parent.
- Set border-top on the parent.
Handle Overflow#
Property | Value | Description | CSS Version |
---|---|---|---|
overflow-x | overflow value | Set horizontal overflow | 3 |
overflow-y | overflow value | Set vertical overflow | 3 |
overflow | Same values as above: | ||
auto: the browser handles overflow content. If there is overflow content, a scrollbar will be displayed; otherwise, no scrollbar will be displayed. | |||
hidden: if there is overflow content, it will be cut off directly. | |||
scroll: regardless of whether there is overflow, a scrollbar will always appear. However, different platforms and browsers display differently. | |||
visible: default value, regardless of whether there is overflow, the content will be displayed. | Shorthand property | 2 |
div {
overflow: auto;
}
Set Element Visibility#
Property | Value |
---|---|
display | |
visibility | visible: default value, the element is visible on the page. hidden: the element is not visible, but occupies space collapse: the element is not visible, hiding table rows and columns; if not a table, it is the same as hidden; may not be supported by Chrome and Opera. |
div { visibility: hidden;
}
And the visibility of the box during debugging is achieved by setting borders and background colors.
Set Element Float#
Property | Value | Description |
---|---|---|
float | left right none | Floating element to the left Floating element to the right Disable floating |
clear | none left right both | Floating elements can be tightly attached to the left and right edges Floating elements cannot be tightly attached to the left edge Floating elements cannot be tightly attached to the right edge Floating elements cannot be tightly attached to both edges |
#a { /* Floating element to the left */
background: gray;
float: left;
clear: none;
}
#c { /* Floating element to the right */
background: navy;
float: right;
clear: both;
}
#c { /* Disable floating */
background: navy;
float: none;
}
Set Element Positioning Properties#
Can be understood as setting the positioning properties, the element rises to a position layer for layout.
Property | value | Description |
---|---|---|
position | static absolute relative fixed | Default value, no positioning. Absolute positioning, removed from the document flow, with the top left corner of the window document as the starting point; using top, right, bottom, left to describe relative distances. Relative positioning, using top, right, bottom, left to describe relative distances. Relative to its original position. Fixed positioning, using top, right, bottom, left to describe relative distances. |
header { /* Absolute positioning: starting from the top left corner of the window document */
position: absolute; top: 100px; left: 100px;
}
header { /* Fixed positioning: will scroll with the scrollbar, achieving a relative stationary effect */
position: fixed; top: 100px; left: 100px;
}
/* Positioning relative to a certain element: does not leave the document flow layer */
body { /* Set the parent element as relative reference, and do not set coordinates */
position: relative;
}
header { /* If the parent element has set a reference point, the absolute positioning of the child element will be based on it */
position: absolute; top: 0px; left: 0px;
}
Set Stacking Order#
Property | value | Description |
---|---|---|
z-index | auto number | Default layer Set layer, the larger the number, the higher the layer |
header {
z-index: 100;
}
Layout#
Standard Document Layout#
Only use the layout characteristics of the box type itself + type conversion; generally, the overall structure of the webpage is standard flow layout, and multiple child blocks in one line are special layouts.
If the parent box has no height, the child box will stretch the parent box.
Table Layout#
In the early days, tables were used to describe the structure of web pages by setting the block style width table { width: 100% }
to always be 100% of the browser window. Then set the width of the child blocks (cells) as a percentage of the parent block to make the page adapt to the window size.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Table Layout</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<table border="0">
<tr>
<td colspan="2" class="header">Header</td>
</tr>
<tr>
<td class="aside">Aside</td>
<td class="section">Section</td>
</tr>
<tr>
<td colspan="2" class="footer">Footer</td>
</tr>
</table>
</body>
</html>
styles.css:
/* Basic page styles */
body {
margin: 0;
}
/* Fluid layout table styles */
table {
margin: 0 auto;
width: 100%; /* Set table width to 100% to adapt to window width */
border-spacing: 0;
}
/* Header styles */
.header {
height: 120px;
background-color: #2714d1;
text-align: center;
font-size: 24px;
}
/* Sidebar styles */
.aside {
width: 20%; /* Set width to 20% */
height: 500px;
background-color: #2e53d0;
padding: 10px;
box-sizing: border-box;
}
/* Main content area styles */
.section {
width: 80%; /* Set width to 80% */
height: 500px;
background-color: #4eb776;
padding: 10px;
box-sizing: border-box;
}
/* Footer styles */
.footer {
height: 120px;
background-color: #f2f2f2;
text-align: center;
font-size: 18px;
}
Float~~~~ Layout#
- After adding the float property to an element, the element floats above the standard layout blocks, which can be understood as layout performed on the float layer; it also means that if the parent box has no height, the child box will no longer stretch the parent box.
- The element has inline-block characteristics.
- The elements are top-aligned.
Floating is easy to achieve text wrapping around images:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Float Layout - Fluid Layout</title>
<link rel="stylesheet" href="styles.css">
</head>
<body class="clearfix">
<header>header</header>
<aside>aside</aside>
<section>section</section>
<footer>footer</footer>
</body>
</html>
styles.css:
/* Set body width to auto, so that the page width adjusts automatically according to the viewport */
body {
width: 100%;
margin: 0;
color: white;
background-color: #333;
}
header {
height: 120px;
background-color: #444;
text-align: center;
padding-top: 40px;
font-size: 24px;
}
aside {
width: 20%;
height: 500px;
float: left;
background-color: #555;
padding: 10px;
box-sizing: border-box;
}
section {
width: 80%;
height: 500px;
float: left;
background-color: #666;
padding: 10px;
box-sizing: border-box;
}
footer {
height: 120px;
clear: both;
background-color: #444;
text-align: center;
padding-top: 40px;
font-size: 20px;
}
.clearfix::before, /* Add clearfix class to all parent boxes that need to adapt to height */
.clearfix::after {
content: "";
display: table;
}
.clearfix::after { /* Add a child box inside the parent box to clear floats, the purpose is to stretch the parent box that has no specified height */
clear: both;
}
Positioning Layout#
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Positioning Layout Example</title>
<style>
body {
margin: 0;
padding: 0;
height: 100vh;
}
.container {
position: relative;
height: 100%;
background-color: #f0f0f0;
}
.box {
position: absolute;
width: 200px;
height: 100px;
background-color: #3498db;
color: white;
text-align: center;
line-height: 100px;
}
/* Position the first box: 50px from the top left corner of the container */
.box1 {
top: 50px;
left: 50px;
}
/* Position the second box: 50px from the bottom right corner of the container */
.box2 {
bottom: 50px;
right: 50px;
}
/* Position the third box: center of the container */
.box3 {
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
</style>
</head>
<body>
<div class="container">
<div class="box box1">Box 1</div>
<div class="box box2">Box 2</div>
<div class="box box3">Box 3</div>
</div>
</body>
</html>
Multi-column Layout#
Multi-column layout is useful for scenarios like horizontally scrolling pages (novels).
Property | val | Description |
---|---|---|
column-width | auto: default value, adaptive length value: set column width | Defines the width of each column |
column-count | auto: default value; indicates just 1 column number: sets the number of columns | Defines the number of columns |
columns | Integrates column-width and column-count properties | |
column-gap | Defines the gap between columns | |
column-rule column-rule-width column-rule-style column-rule-color | Defines column borders column-rule-width: separately set border width column-rule-style: separately set border style column-rule-color: separately set border color column-rule: shorthand | |
column-span | none: default value; indicates no spanning all: indicates spanning across columns | Sets a large title that spans columns |
column-fill |
div {
column-width: 200px;
column-count: 4;
column-gap: 100px;
column-rule: 2px dashed gray;
column-span: all;
}
div {
columns: auto 4;
column-gap: 100px;
column-rule: 2px dashed gray;
column-span: all;
}
Flex Layout#
Property | Value | Description |
---|---|---|
display | flex: displays the container box model as a block-level flexible box inline-flex: displays the container box model as an inline-level flexible box | |
flex-direction | row: arranges from left to right row-reverse: arranges from right to left column: arranges from top to bottom column-reverse: arranges from bottom to top | The arrangement direction of child elements, which is the main axis direction setting |
flex-wrap | nowrap: default value; all displayed in one row or column, will automatically compress wrap: automatically wraps when flexible items cannot fit, without compression wrap-reverse: automatically wraps when flexible items cannot fit, the direction is opposite to wrap | |
flex-flow | Shorthand for the above two | |
justify-content | flex-start: child elements left-aligned, no gaps between elements flex-end: child elements right-aligned, no gaps between elements center: child elements aligned to the center, no gaps between elements space-between: evenly aligned, remaining blank width serves as spacing between child elements, no gaps on both sides space-around: evenly aligned, remaining blank width serves as spacing between child elements, gaps on both sides space-evenly: evenly aligned, remaining blank width serves as spacing between child elements, gaps on both sides, and margins are the same size | Main axis alignment method (default x-axis) |
align-items | flex-start: top-aligned flex-end: bottom-aligned center: center-aligned baseline: flexible items are based on the baseline to clear extra space stretch: default, stretches child elements to fill the entire vertical space of the parent box, of course, provided that the child elements do not have a height set | Cross-axis alignment method, effective only when there is one row and the parent box has height |
align-content | flex-start: child elements top-aligned, no gaps between elements flex-end: child elements bottom-aligned, no gaps between elements center: child elements aligned to the center, no gaps between elements space-between: evenly aligned, remaining blank width serves as spacing between child elements, no gaps on both sides space-around: evenly aligned, remaining blank width serves as spacing between child elements, gaps on both sides space-evenly: evenly aligned, remaining blank width serves as spacing between child elements, gaps on both sides, and margins are the same size | Cross-axis alignment method for multiple rows |
align-self | flex-start: top-aligned flex-end: bottom-aligned center: center-aligned baseline: flexible items are based on the baseline to clear extra space stretch: default, stretches child elements to fill the entire horizontal space of the parent box, of course, provided that the child elements do not have a height set | Control layout of individual child boxes |
flex | integer | The width ratio of child elements on the main axis; achieves adaptive width control on the main axis |
order | Controls the order in which flexible items appear |
<div class="flex-container">
<p>First paragraph</p>
<p>Second paragraph</p>
<p>Third paragraph</p>
</div>
.flex-container {
display: flex;
flex-direction: column;
flex-wrap: wrap;
justify-content: space-around;
/*align-content: ;*/
align-items: center;
}
/*.flex-container {
display: flex;
flex-flow: row wrap;
justify-content: space-around;
align-items: center;
/*align-content: ;*/
}*/
/* Occupying 5 parts, appearing in the order 231 */
p:nth-child(1) { /* Occupies 1 part of the container space */
flex: 1; /* Main axis width ratio 1:3:1 */
order: 2;
align-self: center; /* Center the first child box */
}
p:nth-child(2) {
flex: 3;
order: 3;
}
p:nth-child(3) {
flex: 1;
order: 1;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Personal Homepage</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<header class="header">
<h1>Welcome to My Personal Homepage</h1>
</header>
<nav class="navigation">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About Me</a></li>
<li><a href="#">My Projects</a></li>
<li><a href="#">Contact Me</a></li>
</ul>
</nav>
<section class="main-content">
<article class="bio">
<h2>Personal Introduction</h2>
<p>I am a web developer who loves programming and technological innovation.</p>
</article>
<article class="projects">
<h2>My Projects</h2>
<ul>
<li>Project 1</li>
<li>Project 2</li>
<li>Project 3</li>
</ul>
</article>
<article class="contact">
<h2>Contact Information</h2>
<p>Email: [email protected]</p>
</article>
</section>
<footer class="footer">
<p>© 2024 My Personal Homepage</p>
</footer>
</div>
</body>
</html>
styles.css:
/* Set basic page styles */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
/* Core container, using flexbox layout */
.container {
display: flex;
flex-direction: column;
min-height: 100vh;
}
/* Header */
.header {
background-color: #4CAF50;
color: white;
padding: 20px;
text-align: center;
}
/* Navigation bar, arranged horizontally, allows wrapping */
.navigation {
display: flex;
flex-flow: row wrap; /* Allow wrapping */
justify-content: center; /* Center alignment */
background-color: #333;
}
.navigation ul {
list-style: none;
display: flex;
padding: 10px;
}
.navigation li {
margin: 0 15px;
}
.navigation a {
color: white;
text-decoration: none;
}
/* Main content area, using flexbox layout */
.main-content {
display: flex;
flex-wrap: wrap;
justify-content: space-between; /* Evenly distribute child elements */
padding: 20px;
gap: 20px;
}
/* Personal introduction area */
.bio {
flex: 1 1 300px; /* Set a flexible box */
background-color: #f4f4f4;
padding: 15px;
border-radius: 8px;
}
.projects {
flex: 2 1 400px; /* Set a flexible ratio */
background-color: #f9f9f9;
padding: 15px;
border-radius: 8px;
}
.contact {
flex: 1 1 300px; /* Set a flexible box */
background-color: #f4f4f4;
padding: 15px;
border-radius: 8px;
}
/* Footer */
.footer {
background-color: #333;
color: white;
padding: 10px;
text-align: center;
}
/* Adjust layout order */
.projects {
order: -1; /* Make "My Projects" appear before "Personal Introduction" */
}
/* Responsive adjustments */
@media (max-width: 768px) {
.navigation {
flex-direction: column;
}
.main-content {
flex-direction: column;
}
}
Grid Layout#
Inherited Style Properties#
You can specify properties in the parent element box (div); child elements will inherit, so there is no need to write a child element selector. Most elements have default styles, which may need to be cleared first.
Property | value | Description | css |
---|---|---|---|
color | color value | Set the foreground color of the element | 1 |
opacity | 0 ~ 1 | Set the transparency of the element | 3 |
text-align | Set the horizontal alignment of the element in the box | start and end are new features added in CSS3, but IE and Opera may not support them yet left: left-aligned, default right: right-aligned, center: center-aligned, justify: content justified | 1,3 |
// Clear default styles
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
Box Styles#
Property | value | Description | css |
---|---|---|---|
vertical-align | distance length value/percentage relative to the baseline | Vertical alignment of the box | |
box-shadow | hoffset: X-axis offset of the shadow voffset: Y-axis offset of the shadow blur: (optional) blur radius; a length value, the larger the value, the more blurred the box's boundary. Default value 0, clear boundary spread: (optional) spread radius; a length value, positive value represents the shadow extending outward in all directions from the box, negative value represents the shadow shrinking in the opposite direction color: (optional) sets the shadow color, if omitted, the browser will choose a color by itself inset: (optional): default outer shadow, to make it an inner shadow this value is inset multi-value, separated by spaces | Box shadow, generally used for interactive response styles, this is difficult to adjust, it is recommended to directly copy Xiaomi, JD | 3 |
outline-color | Outline color | 3 | |
outline-offset | Offset of the outline from the element's border edge | 3 | |
outline-style | Outline style, consistent with border-style | 3 | |
outline-width | Outline width | 3 | |
outline | Shorthand | 3 | |
cursor | auto, default, none, context-menu, help, pointer, progress, wait, cell, crosshair, text, vertical-text, alias, copy, move, no-drop, not-allowed, e-resize, n-resize, ne-resize, nw-resize, s-resize, se-resize, sw-resize, w-resize, ew-resize, ns-resize, nesw-resize, nwse-resize, col-resize, row-resize, all-scroll | Cursor style on the box, try each one on the left | 1 |
div span {
vertical-align: -200px;
box-shadow: 5px 4px 10px 2px gray; /* Shadow */
/* box-shadow: 5px 4px 10px 2px gray inset; */ /* Inner shadow */
outline: 10px double red; /* Add an outline to the box */
cursor: move;
}
Font Styles#
body { font-size: 50px; } /* Set the font size of the parent element */
@font-face { font-family: abc; src: url('BrushScriptStd.otf'); } /* Server provides abc font file */
body {
font-style: ;
font-weight: ;
font-variant: ;
font-size: smaller; /* Set relative font size to the parent element */
font-family: 楷体, 微软雅黑, 宋体; /* Sometimes for compatibility with many browser systems, several backup fonts can be set */
/* font: italic bold normal smaller 楷体; /* Assign values in the above order */
color: red;
opacity: 0.5;
}
Text Styles#
Property Name | Description | Value | CSS Version |
---|---|---|---|
text-decoration | Text strikethrough | none: no strikethrough underline: text underline overline: text overline line-through: strikethrough blink: makes text blink; basically not supported anymore | 1 |
text-transform | English text case | none: restores the default state of already transformed case capitalize: capitalizes the first letter of English uppercase: converts English to uppercase lowercase: converts English to lowercase | 1 |
text-shadow | Adds shadow to text | Described by four values, e.g.: 5px 5px 3px black The first value: horizontal offset; The second value: vertical offset; The third value: shadow blur (optional); The fourth value: shadow color (optional) | 3 |
text-align | Sets the horizontal alignment of text | start and end are new features added in CSS3, but IE and Opera may not support them yet left: left-aligned, default right: right-aligned, center: center-aligned, justify: content justified | 1,3 |
white-space | White space handling method | normal: default value, white space is compressed, text wraps automatically nowrap: white space is compressed, text does not wrap pre: white space is preserved; wraps when encountering a newline pre-line: white space is compressed; text wraps when full or encounters a newline pre-wrap: white space is preserved; text wraps when full or encounters a newline | 1 |
letter-spacing | Letter spacing | normal: sets default spacing Length value: e.g.: "number" + "px" | 1 |
word-spacing | Word spacing | normal: sets default spacing Length value: e.g.: "number" + "px" | 1 |
line-height | Sets line height (line spacing) | normal: sets default spacing Length value: e.g.: "number" + "px" Number: e.g.: 1, 2.3 %: e.g.: 200% | 1 |
word-wrap | Allows long English words to break | normal: words do not break break-word: break words | 3 |
text-indent | Text first line indentation | normal: sets default spacing Length value: e.g.: "number" + "px" | 1 |
vertical-align | sub: vertically aligns text as a subscript super: vertically aligns text as a superscript | Vertical alignment of text |
p {
text-decoration: underline;
text-transform: uppercase;
text-shadow: 5px 5px 3px black;
text-align: center;
white-space: nowrap;
letter-spacing: 4px;
word-spacing: 14px;
line-height: 200%;
word-wrap: break-word;
text-indent: 2rem;
vertical-align: super;
}
Vertical Centering with Line Height#
Line height = box height.
<div>Chinese</div>
div {
height: 40px;
line-height: 40px;
}
Border Styles#
The border style property border-style must be declared; the other two properties are optional, as they have default values.
Property | Value | Description | CSS |
---|---|---|---|
border-style border-top-style border-middle-style border-left-style border-right-style | none dashed dotted double groove inset outset ridge solid | No border Dashed border Dotted border Double border Groove border Border that gives the element content an embedded effect Border that gives the element content a protruding effect Ridge border Solid border | |
border-width border-top-width border-middle-width border-left-width border-right-width | Length value/percentage thin medium thick | Sets the border width, optional, as it has default values | |
border-color border-top-color border-middle-color border-left-color border-right-color | Color value | Sets the border color, optional, as it has default values | |
border-radius border-top-left-radius border-top-right-radius border-middle-left-radius border-middle-right-radius | Length value or percentage | Sets the border radius of the four corners, used to create rounded corners; border-radius=50% creates a pure circular control Top left corner Top right corner Bottom left corner Bottom right corner | 3 |
border-image-source | Introduces background image address | 3 | |
border-image-slice | Cuts the introduced background image | 3 | |
border-image-width | Border image width, can set four values for top, right, bottom, left, two values for top and bottom/right and left | 3 | |
border-image-outset | Border background expands outward | 3 | |
border-image-repeat | stretch: stretches to fill. Default value repeat: tiles the fill; when the image touches the edge, it is truncated if it exceeds round: tiles the fill; dynamically adjusts the image size according to the border size until it fills the border space space: tiles the fill; dynamically adjusts the image spacing according to the border size until it fills the border space | Arrangement method of border background image | 3 |
border-image | Shorthand for the above five properties | 3 |
div { /* Just set the specific side with a prefix */
border-width: 2px;
border-style: solid;
border-color: red;
border-radius: 10px 20px 30px 40px;
border-image-source: url(border.png);
border-image-slice: 27; /* Just right for 4 images */
/* border-image-slice: 0 fill; */
border-image-width: 81px;
border-image-outset: 20px;
border-image-repeat: round;
}
div {
border: 2px solid red;
border-radius: 10px;
/* border-top: */
/* border-middle: */
/* border-left: */
/* border-right: */
border-image: url(border.png) 27/27px round;
}
Background Styles#
Property | Value | Description | CSS Version |
---|---|---|---|
background-color | Color transparent | Sets the background color to the specified color Default value, sets the background color to transparent | 1 |
background-image | none url | Batch sets block backgrounds, and if one does not need a background, can separately set none to cancel the background Set background image through URL | 1/3 |
background-repeat | repeat-x repeat-y repeat no-repeat | Tiles the image horizontally Tiles the image vertically Tiles the image both horizontally and vertically Displays only one background image, does not tile | 1/3 |
background-attachment | scroll fixed | Default value, the background is fixed on the element, will not scroll with the content The background is fixed in the viewport, the content scrolls while the background does not move, can be used for watermark effects | 1/3 |
background-position | Length value/percentage top left right bottom center | Offsets the position of the image using length values Positions the background image at the top of the element | 3 |
background-size | Length value/percentage auto cover contain | CSS length value, such as px, em Default value, the image displays at its original size Scales the image proportionally so that it at least covers the container, but may exceed the container Scales the image proportionally so that the larger of the width or height aligns with the container horizontally or vertically | 3 |
background-origin | border-box padding-box content-box | Draws the background inside the element box Draws the background inside the padding box Draws the background inside the content box | 3 |
background-clip | border-box padding-box content-box | Clips the background inside the element box Clips the background inside the padding box Clips the background inside the content box | 3 |
background | Shorthand for background image | 1 |
div {
background-color: silver;
background-image: url(loading.gif);
background-repeat: no-repeat;
background-attachment: fixed;
background-position: top left; /* 20px 20px; */
background-size: auto;
background-origin: content-box;
background-clip: padding-box;
}
div {
width: 400px;
height: 300px;
border: 10px dashed red;
padding: 50px;
background: silver url(img.png) no-repeat scroll left top/100% border-box content-box;
}
Table Styles#
Property | Value | Description | Version | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
border-collapse | separate: default value, cell borders are independent collapse: adjacent cell borders are merged | 2 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
border-spacing | length value | The spacing between adjacent cell borders; effective when border-collapse: separate . Independent borders are the premise for setting spacing | 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
caption-side | top: default value, the title is above bottom: the title is below | Position of the table title | 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
empty-cells | show: default value, shows borders hide: does not show borders | Whether to show borders for empty cells | 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
table-layout | auto: default value, stretches the entire cell when content is too long fixed: does not stretch the entire cell when content is too long | Specifies the layout style of the table | 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
vertical-align | baseline: content object aligns with the baseline top: middle: bottom: | In ,
List Styles#
Transformation Effects#Can be used to create interactive animations for elements, such as a transformation feedback when the mouse hovers over. 2D Transformations#
3D Transformations#
Transition Effects#
Animation Effects#
CSS Distance Measurement#
Experimental Property Prefixes#When new properties are introduced, these properties are still in an unstable phase and may be removed at any time. At this time, browser vendors use prefixes to implement these properties.
Emmet Rules#Emmet syntax abbreviations improve the speed of writing HTML/CSS, and this syntax is already integrated into Vscode. Element: Directly input the element name, for example, Child Elements: Use Sibling Elements: Use Parent Elements: Use Repetition: Use Grouping: Use Class Name: Use ID: Use Attributes: Use Text Content: Use Numbering: Use Implicit Tags: Some tags can be omitted, for example, CSS basically adopts the first letter abbreviation: base.css#
|