The basic syntax structure of CSS
Basic Syntax Structure of CSS
CSS (Cascading Style Sheets) is a language used to describe the styling of HTML or XML documents. It controls the appearance and layout of page elements through selectors and declaration blocks. Understanding the basic syntax structure of CSS is key to writing effective stylesheets.
Selectors
Selectors are used to specify which HTML elements to apply styles to. Common types of selectors include:
- Element Selector: Uses the HTML tag name directly
p {
color: blue;
}
- Class Selector: Begins with a dot (.)
.highlight {
background-color: yellow;
}
- ID Selector: Begins with a hash (#)
#header {
font-size: 24px;
}
- Attribute Selector: Selects based on element attributes
input[type="text"] {
border: 1px solid #ccc;
}
- Pseudo-class Selector: Used for specific states of elements
a:hover {
color: red;
}
Declaration Blocks
Declaration blocks are enclosed in curly braces {} and consist of one or more declarations. Each declaration is composed of a property and value, separated by a colon (:), and ends with a semicolon (;).
selector {
property1: value1;
property2: value2;
}
Properties and Values
CSS has hundreds of property-value combinations. Some common properties include:
- Color-related:
color: #FF0000;
background-color: rgba(255, 0, 0, 0.5);
border-color: hsl(120, 100%, 50%);
- Dimension-related:
width: 100px;
height: 50%;
max-width: 1200px;
min-height: 200px;
- Text-related:
font-family: "Arial", sans-serif;
font-size: 16px;
line-height: 1.5;
text-align: center;
Comments
CSS uses /* */
syntax for comments:
/* This is a CSS comment */
body {
margin: 0; /* Reset default margin */
}
At-rules
At-rules are special directives in CSS that begin with @:
- @import: Imports other CSS files
@import url("styles/print.css") print;
- @media: Media queries
@media (max-width: 600px) {
.sidebar {
display: none;
}
}
- @keyframes: Animation keyframes
@keyframes slidein {
from {
transform: translateX(0%);
}
to {
transform: translateX(100%);
}
}
Values and Units
CSS supports various values and units:
- Length units:
.box {
width: 300px; /* Pixels */
padding: 2em; /* Relative to current font size */
margin: 5%; /* Relative to containing block's width */
}
- Color values:
.text {
color: #FF5733; /* Hexadecimal */
background: rgb(255, 87, 51); /* RGB */
border-color: hsl(12, 100%, 60%); /* HSL */
}
- Function values:
.container {
width: calc(100% - 40px);
background: linear-gradient(to right, red, blue);
}
Selector Combinations
Selectors can be combined in various ways:
- Grouping Selectors: Separated by commas
h1, h2, h3 {
font-family: "Georgia", serif;
}
- Descendant Selector: Separated by spaces
article p {
line-height: 1.6;
}
- Child Selector: Separated by a greater-than sign (>)
ul > li {
list-style-type: square;
}
- Adjacent Sibling Selector: Separated by a plus sign (+)
h2 + p {
margin-top: 0;
}
Specificity and Inheritance
CSS rules follow specific priority:
- Inheritance: Some properties inherit from parent elements
body {
font-family: Arial;
} /* All child elements inherit this font */
- Specificity Calculation:
- Inline styles: 1000
- ID selectors: 100
- Class/attribute/pseudo-class selectors: 10
- Element/pseudo-element selectors: 1
#content .highlight { /* 100 + 10 = 110 */
color: red;
}
div.highlight { /* 1 + 10 = 11 */
color: blue;
}
Box Model
The CSS box model is fundamental to layout:
.box {
width: 300px;
height: 200px;
padding: 20px;
border: 5px solid black;
margin: 10px;
box-sizing: border-box; /* Optional, changes width calculation */
}
Layout-related Properties
- display:
.inline {
display: inline;
}
.block {
display: block;
}
.flex-container {
display: flex;
}
- position:
.relative {
position: relative;
top: 10px;
}
.absolute {
position: absolute;
right: 0;
}
.fixed {
position: fixed;
bottom: 0;
}
- float:
.image {
float: left;
margin-right: 15px;
}
Responsive Design Basics
Use media queries to create responsive layouts:
.container {
width: 100%;
padding: 15px;
}
@media (min-width: 768px) {
.container {
width: 750px;
margin: 0 auto;
}
}
@media (min-width: 992px) {
.container {
width: 970px;
}
}
CSS Variables
Custom properties (CSS variables) can store and reuse values:
:root {
--primary-color: #4285f4;
--spacing-unit: 8px;
}
.button {
background-color: var(--primary-color);
padding: calc(var(--spacing-unit) * 2);
}
Pseudo-elements
Pseudo-elements style specific parts of elements:
p::first-line {
font-weight: bold;
}
blockquote::before {
content: '"';
font-size: 2em;
color: gray;
}
Transitions and Animations
- Transitions:
.button {
transition: background-color 0.3s ease;
}
.button:hover {
background-color: #3367d6;
}
- Animations:
@keyframes bounce {
0%, 100% { transform: translateY(0); }
50% { transform: translateY(-20px); }
}
.ball {
animation: bounce 1s infinite;
}
Modern CSS Features
- Grid Layout:
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 20px;
}
- Flexbox Layout:
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
}
- Custom Filters:
.image {
filter: blur(2px) brightness(1.2);
}
本站部分内容来自互联网,一切版权均归源网站或源作者所有。
如果侵犯了你的权益请来信告知我们删除。邮箱:cc@cccx.cn
上一篇:CSS的发展历史
下一篇:CSS的三种引入方式