Home >Web Front-end >Front-end Q&A >How to write good css
<!DOCTYPE html> <html> <head> <title>示例</title> <style type="text/css"> p { color: red; } </style> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <p style="color: blue;">这是一段文字</p> </body> </html><p>In this example, we use both internal style sheets and external style sheets, and also use inline style sheets. <p>CSS syntax is mainly composed of selectors and attributes. Selectors are used to select HTML elements, and attributes are used to control the style of elements. Common properties include background, color, font size, borders, margins, etc. <p>2. Use of selectors <p>Selectors are one of the most basic concepts in CSS. Different selectors can select different HTML elements. The following are some common selectors:
p { color: red; }<p>This selector will select all
<p>
elements.
.
and is applied to elements with the same class name. For example:
.red { color: red; }<p>This selector will select all HTML elements with
class="red"
.
# number and applies to unique HTML elements. For example:
#header { background-color: gray; }<p>This selector will select the HTML element with
id="header"
.
div p { color: red; }<p>This selector selects all
<p>
elements within the <div>
element.
a:hover { text-decoration: underline; }<p>This selector selects the state when the mouse is hovering over the
<a>
element.
<p>3. Master common attributes
<p>There are many attributes in CSS, but some are more commonly used and we need to master them proficiently. The following are some common attributes:
body { background-color: #f1f1f1; background-image: url('background.jpg'); background-repeat: no-repeat; }<p>This style places a background image named
background.jpg
in the lower right corner of the page.
h1 { font-family: Arial; font-size: 24px; color: #333333; }<p>This style sets the font of all
<h1>
title elements to Arial, the font size to 24px, and the color to #333333.
div { border: 1px solid #e5e5e5; border-radius: 10px; }<p>This style sets the border of the
<div>
element to 1px wide, the color to #e5e5e5, and the border rounded corners to 10px.
img { width: 100%; height: auto; margin-bottom: 10px; padding: 5px; }<p>This style sets the width of all images to 100%, the height to automatically adjust, the outer bottom margin to 10px, and the inner margin to 5px. <p>4. Use style inheritance <p>Style inheritance in CSS can reduce code duplication and improve code maintainability. For example:
body { color: #333333; font-size: 16px; } h1 { font-size: 24px; font-weight: bold; }<p>This style defines the color of all text to be #333333 and the font size to be 16px.
<h1>
The title element inherits the same color, but different font size and weight.
<p>5. Use composite styles
<p>In CSS, multiple attribute values can be combined into a composite style. This can reduce the amount of code and make the code more concise. For example:
border: 1px solid #e5e5e5;<p>This style defines a composite style composed of three attribute values: border width is 1px, border style is solid line, and border color is #e5e5e5. <p>6. Use CSS preprocessor <p>The CSS preprocessor is a tool that compiles CSS code into a more advanced CSS language. They extend the functions of CSS, including variables, functions, nesting, etc., making writing CSS more convenient and faster. Currently, the more popular CSS preprocessors include Sass, Less, Stylus, etc. <p>7. Summary <p>The above are some basic points on how to write CSS well. Mastering basic syntax, selectors, common attributes, style inheritance and composite styles can enable us to write concise, flexible and easy-to-maintain CSS code.
The above is the detailed content of How to write good css. For more information, please follow other related articles on the PHP Chinese website!