- CSS Integration Methods
CSS can be applied to HTML documents in three primary ways: inline styles, internal style sheeets, and external style sheets.
- Inline styles are applied directly to elements using the
styleattribute. - Internal style sheets are defined within the
<style>tag in the document's<head>. - External style sheets are linked via the
<link>tag in the<head>.
Priority Order: Inline styles take precedence over internal styles, which in turn override external styles.
Example of External CSS Integration
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="css/swiper.min.css">
<link rel="stylesheet" href="css/comm.css">
<title>Sample Page</title>
</head>
Example of Inline Style Usage
<a href="#" style="color: black; text-decoration: none;">Styled Link</a>
- CSS Selectors
Selectors determine which HTML elements a set of styles will apply to.
- Element Selector:
div {} - Class Selector:
.my-class {} - ID Selector:
#unique-id {} - Attribute Selector:
[type='text'] {}
Selector Priority: Attribute selectors have higher priority than ID selectors, followed by class selectors, and finally element selectors.
- Dimension Properties
- Width:
width: 200px;orwidth: 50%; - Height:
height: 100px;orheight: 30%;
- Background Color
Use the background-color property to apply a background color to an element.
div {
background-color: #f0f0f0;
}
- Text Alignment and Styling
- Center Text:
text-align: center; - Underline Text:
text-decoration: underline; - Vertical Centering: Use
line-heightequal to the element's height for single-line vertical centering.
- Font Styling
- Font Family:
font-family: "Microsoft Yahei", SimSun, SimHei; - Font Size:
font-size: 16px; - Font Wieght:
font-weight: bold;(700),font-weight: normal;(400) - Font Style:
font-style: italic;orfont-style: oblique;
- Box Model (Core Concept)
The CSS box model consists of four components: content, padding, border, and margin.
- Margin: Controls spacing outside the element. Use
margin: 0 auto;for horizontal centering. - Padding: Controls spacing between content and border.
- Border: Defines the element'ss border with properties like
border-width,border-style(solid, dashed, dotted, double), andborder-color.
Shorthand Margin Syntax: margin: 15px 25px 30px 45px; corresponds to top, right, bottom, and left margins respectively.
Margin Collapse: Vertical margins between elements can collapse into a single margin. To prevent this, use padding or a border.