Home Web Front-end CSS Tutorial Detailed explanation of CSS principles for front-end development

Detailed explanation of CSS principles for front-end development

Mar 13, 2017 am 09:56 AM

This article mainly introduces relevant information about the detailed explanation of CSS principles for front-end development. Friends in need can refer to

Detailed explanation of CSS principles for front-end development

People who are engaged in Web front-end development have dealt with CSS a lot. Some people may not know how CSS works. How does the written CSS be parsed by the browser? When this becomes a bottleneck for us to improve CSS level, should we know more about it?

  1. Browser Development and CSS

Web browsers mainly connect to web servers through the HTTP protocol to obtain web pages. HTTP allows web browsing The server sends data to the web server and retrieves the web page. Currently the most commonly used HTTP is HTTP/1.1, which is fully defined in RFC2616. HTTP/1.1 has its own set of standards that Internet Explorer does not fully support, but many other modern web browsers fully support these standards. The location of the web page is indicated by the URL (Uniform Resource Locator), which is the address of the web page; anything starting with http: means logging in through the HTTP protocol. Many browsers also support other types of URLs and protocols, such as ftp: for FTP (File Transfer Protocol), gopher: for Gopher and https: for HTTPS (HTTP encrypted with SSL).

Early web browsers only supported a simplified version of HTML. The rapid development of proprietary software browsers has led to the creation of non-standard HTML code. But as HTML grew, it gained many display features in order to meet the requirements of designers. As these capabilities increase, foreign languages ​​for defining styles make less and less sense.

The original proposal of CSS was proposed by Hakun Lee in 1994. BertBos was designing a browser called Argo, and they decided to work together on CSS.

There had been some proposals for style sheet languages, but CSS was the first to include the idea of ​​"cascading". In CSS, a file's styles can be inherited from other style sheets. The reader can use his or her own preferred style in some places and inherit or "layer" the author's style in other places. This layering method allows both the author and the reader to flexibly add their own designs and mix their own preferences. .

In early 1997, W3C organized a working group specifically responsible for CSS, and its leader was Chris Lilley. This working group began discussing issues not covered in the first edition, which resulted in the publication of the second edition requirements in May 1998. As of 2007, the third edition is not yet complete.

2. How the browser renders and loads the page

Why do some websites load very slowly when they are opened, and the entire page is loaded? Pages are displayed simultaneously, while some websites are displayed gradually from top to bottom? To understand this, you can start with the following general process:

1. The order of browser downloading is from top to bottom, and the order of rendering is also from top to bottom. Downloading and rendering are performed at the same time. of.
 2. When rendering to a certain part of the page, all parts above it have been downloaded (this does not mean that all associated elements have been downloaded).
 3. If you encounter a semantically interpretable tag embedded file (JS script, CSS style), then the IE download process will enable a separate connection for downloading.
 4. And perform parsing after downloading. During the parsing process, stop downloading of all downward elements of the page.
 5. After the style sheet is downloaded, it will be parsed together with all previously downloaded style sheets. After the parsing is completed, all previous elements (including previously rendered ones) will be re-rendered.
 6. If there is redefinition in JS or CSS, the later-defined function will overwrite the previously-defined function.

The key points here are the three points 2-5. Rendering efficiency is related to the following three points:

 1. Query positioning efficiency of CSS selector
 2. Browser renderingmodeand algorithm
 3. To The size of the rendered content

  3. What is CSS and the advantages of CSS

 What is CSS?

  1. CSS is the abbreviation of Cascading Style Sheets.

  2. CSS language is a markup language that does not require compilation and can be directly interpreted and executed by the browser (it is a browser interpreted language).

  3. In standard web design, CSS is responsible for the presentation of web content (XHTML).

  4. The CSS file can also be said to be a text file, which contains some CSS tags. The CSS file must use css as the file name suffix.

  5. We can change the overall presentation of the web page by simply changing the CSS file, which can reduce our workload, so it is a required course for every web designer.

  6. CSS is produced and maintained by the W3C CSS Working Group.

Use CSS+p to reconstruct web pages. Compared with the traditional TABLE web page layout, it has the following three significant advantages:

 1. Separate performance and content. Separate the design part and put it in a separate style file, and only store text information in the HTML file. Such pages are more friendly to search engines.
 2. Improve page browsing speed. For the same page visual effect, the page capacity reconstructed using CSS+p is much smaller than the page file capacity encoded by TABLE. The former is generally only 1/2 the size of the latter. The browser doesn't have to compile a lot of lengthy tags.


 3. Easy to maintain and revise. You can redesign the entire website by simply modifying a few CSS files.


4. Browser CSS matching principle


Browser CSS matching does not search from left to right, but from right to left to search. For example, as mentioned before, p#pBox p span.red{color:red;}, the browser's search sequence is as follows: first search for all span elements with class='red' in the html, and after finding them, search whether there are any in their parent elements. p element, and then determine whether there is a p element with the id pBox in the parent element of p. If both exist, the CSS will match.


The advantage of the browser searching from right to left is to filter out some irrelevant style rules and elements as early as possible. Firefox calls this search method keyselector (keyword query). The so-called keyword is the last (rightmost) rule in the style rules. The key above is span.red.


 

5. Optimize your CSS


The so-called efficient CSS is to allow the browser to do as little as possible when looking for elements matching style. Search, here are some common inefficient mistakes we make when writing CSS:


1. Do not use the tag name

# before the ID selector ## Normal way of writing: p#pBox

Better way of writing: #pBox

Explanation: Because the ID selector is unique, adding p will add unnecessary unnecessary CSS matching.

2. Do not use the tag name

before the class selector. General writing method: span.red

A better way to write it: .red

Explanation: Same as the first one, but if you define multiple .red and the styles are different under different elements, they cannot be removed, such as your css The definition in the file is as follows:


 p.red{color:red;}
  span.red{color:#ff00ff}
Copy after login


If it is defined in this way, do not remove it, otherwise it will be confusing, but it is recommended It is best not to write like this

3. Use hierarchical relationships as little as possible

General writing: #pBoxp.red{color:red;}

Better way to write: .red{..}

4. Use class instead of hierarchical relationship

General way to write: #pBox ul li a{display:block; }

Better way to write: .block{display:block;}

5. In CSS rendering efficiency, the efficiency of id and class is basically the same

 class will be
cached

in the first load, which will have a better effect in cascading. It will be better to use id in the root element (id has a subtle speed advantage ).

Thank you for reading, I hope it can help everyone, thank you for your support of this site!

The above is the detailed content of Detailed explanation of CSS principles for front-end development. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Adding Box Shadows to WordPress Blocks and Elements Adding Box Shadows to WordPress Blocks and Elements Mar 09, 2025 pm 12:53 PM

The CSS box-shadow and outline properties gained theme.json support in WordPress 6.1. Let's look at a few examples of how it works in real themes, and what options we have to apply these styles to WordPress blocks and elements.

Create a JavaScript Contact Form With the Smart Forms Framework Create a JavaScript Contact Form With the Smart Forms Framework Mar 07, 2025 am 11:33 AM

This tutorial demonstrates creating professional-looking JavaScript forms using the Smart Forms framework (note: no longer available). While the framework itself is unavailable, the principles and techniques remain relevant for other form builders.

Demystifying Screen Readers: Accessible Forms & Best Practices Demystifying Screen Readers: Accessible Forms & Best Practices Mar 08, 2025 am 09:45 AM

This is the 3rd post in a small series we did on form accessibility. If you missed the second post, check out "Managing User Focus with :focus-visible". In

Create an Inline Text Editor With the contentEditable Attribute Create an Inline Text Editor With the contentEditable Attribute Mar 02, 2025 am 09:03 AM

Building an inline text editor isn't trivial. The process starts by making the target element editable, handling potential SyntaxError exceptions along the way. Creating Your Editor To build this editor, you'll need to dynamically modify the content

Working With GraphQL Caching Working With GraphQL Caching Mar 19, 2025 am 09:36 AM

If you’ve recently started working with GraphQL, or reviewed its pros and cons, you’ve no doubt heard things like “GraphQL doesn’t support caching” or

Making Your First Custom Svelte Transition Making Your First Custom Svelte Transition Mar 15, 2025 am 11:08 AM

The Svelte transition API provides a way to animate components when they enter or leave the document, including custom Svelte transitions.

Comparing the 5 Best PHP Form Builders (And 3 Free Scripts) Comparing the 5 Best PHP Form Builders (And 3 Free Scripts) Mar 04, 2025 am 10:22 AM

This article explores the top PHP form builder scripts available on Envato Market, comparing their features, flexibility, and design. Before diving into specific options, let's understand what a PHP form builder is and why you'd use one. A PHP form

File Upload With Multer in Node.js and Express File Upload With Multer in Node.js and Express Mar 02, 2025 am 09:15 AM

This tutorial guides you through building a file upload system using Node.js, Express, and Multer. We'll cover single and multiple file uploads, and even demonstrate storing images in a MongoDB database for later retrieval. First, set up your projec

See all articles