Home  >  Article  >  Web Front-end  >  How to write good css

How to write good css

PHPz
PHPzOriginal
2023-04-06 12:45:27784browse
<p>CSS (Cascading Style Sheets) is a language used to control the layout and style of web pages. How to write CSS is a skill that every front-end developer needs to master. This article will introduce how to write CSS well.

<p>1. Understand basic syntax

<p>Before starting to learn CSS, we need to understand some basic syntax. CSS can be divided into internal style sheets, external style sheets and inline style sheets. Among them, internal style sheets are written directly in HTML files, external style sheets are written in independent CSS files, and inline style sheets are written directly in HTML tags.

<p>For example:

<!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:

  1. tag selector
<p>The tag selector is one of the most commonly used selectors. For example:

p {
  color: red;
}
<p>This selector will select all <p> elements.

  1. Class selector
<p>The class selector is identified by . 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".

  1. ID Selector
<p>The ID selector is identified by the # number and applies to unique HTML elements. For example:

#header {
  background-color: gray;
}
<p>This selector will select the HTML element with id="header".

  1. Descendant selector
<p>The descendant selector uses spaces to select child elements in the parent element. For example:

div p {
  color: red;
}
<p>This selector selects all <p> elements within the <div> element.

  1. Pseudo-class selector
<p>Pseudo-class selector is used to select HTML elements in a special state or position. For example:

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:

  1. Background attribute
<p>The background attribute is used to control the background of the element, including color, picture, repeatability, etc. For example:

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.

  1. Font properties
<p>Font properties are used to control the font, font size, color, etc. of text. For example:

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.

  1. Border attribute
<p>The border attribute is used to control the border of the element. For example:

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.

  1. Size attributes
<p>The size attributes are used to control the size of HTML elements, including width, height, outer spacing, inner spacing, etc. For example:

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn