Home Web Front-end CSS Tutorial CSS writing specifications

CSS writing specifications

Dec 15, 2016 pm 03:29 PM

CSS Writing Specifications [Transfer]

1. The default attributes of elements in different browsers are different. Use Reset to reset some default attributes of browser elements to achieve browser compatibility. /**Clear inner and outer margins **/ body, h1, h2, h3, h4, h5, h6, hr, p, blockquote, /* structural elements structural elements*/ dl, dt, dd, ul, ol, li, /* list elements list elements*/PRe, /* text formatting elements text formatting elements*/ form, fieldset, legend, button, input, textarea, /* form elements form elements*/ th, td, /* table elements table elements*/ img/* img elements Picture elements*/ { border:medium none; margin: 0; padding: 0; } /**Set default font **/body,button, input, select, textarea { font: 12px/1.5 '宋体' ,tahoma, Srial, helvetica, sans-serif; } h1, h2, h3, h4, h5, h6 { font-size: 100%; } em{font-style:normal;} /**Reset list elements **/ ul , ol { list-style: none; } /**Reset hyperlink element **/a { text-decoration: none; color:#333;} a:hover { text-decoration: underline; color:#F40; } /* *Reset image elements **/img{ border:0px;} /**Reset table elements **/ table { border-collapse: collapse; border-spacing: 0; }

2. Good naming habits

3. Code abbreviation li{ font-family:Arial, Helvetica, sans-serif; font-size: 1.2em; line-height: 1.4em; padding-top:5px; padding-bottom:10px; padding-left:5px; } becomes li { font: 1.2em/1.4em Arial, Helvetica, sans-serif; padding:5px 0 10px 5px; }

4. Use CSS inheritance. If multiple child elements of the parent element on the page use the same style, it is best to separate them The same styles are defined on their parent elements, allowing them to inherit these CSS styles. This way you can maintain your code well and reduce the amount of code. Then the original code is like this: #container li{ font-family:Georgia, serif; } #container p{ font-family:Georgia, serif; } #container h1{font-family:Georgia, serif; } #container{ font- family:Georgia, serif; }

5. Using multiple selectors you can combine multiple CSS selectors into one if they have a common style. Doing so not only keeps the code concise but also saves you time and space. For example: h1{ font-family:Arial, Helvetica, sans-serif; font-weight:normal; } h2{ font-family:Arial, Helvetica, sans-serif; font-weight:normal; } h3{ font-family: Arial, Helvetica, sans-serif; font-weight:normal; } can be combined into h1, h2, h3{ font-family:Arial, Helvetica, sans-serif; font-weight:normal; }

6. Appropriate code Note

7. Sort your CSS code. If the properties in the code can be sorted alphabetically, it will be faster to find modifications:

/*** Style properties are sorted alphabetically ***/ div{ background-color:#3399cc ; color:#666; font:1.2em/1.4em Arial, Helvetica, sans-serif; height:300px; margin:10px 5px; padding:5px 0 10px 5px; width:30%; z-index:10; } 9 . Choose better style attribute values. Some attributes in CSS use different attribute values. Although the effects are similar, there are differences in performance. For example, the difference is that border:0 sets the border to 0px, although it is not visible on the page. , but according to the default value of border, the browser still renders border-width/border-color, that is, the memory value has been occupied. Border:none sets the border to "none", which means there is no border. When the browser parses "none", it will not perform rendering actions, that is, it will not consume memory values. Therefore, it is recommended to use border:none; Similarly, display:none hides the object browser without rendering and does not occupy memory. And visibility:hidden will.

10. Use instead of @import. First of all, @import does not belong to the XHTML tag and is not part of the Web standard. It is not very compatible with earlier browsers and has some negative effects on the performance of the website. Influence. For details, please refer to "High-Performance Website Design: Don't Use @import". So, please avoid using @import

11. Use external style sheets This principle is always a good design practice. Not only is it easier to maintain and modify, but more importantly, using external files can improve page speed, because CSS files can be cached in the browser. CSS built into the HTML document will be re-downloaded with the HTML document on each request. Therefore, in practical applications, there is no need to build CSS code into HTML documents:

12. Avoid using CSS expressions (Expression) CSS expressions are a powerful (but dangerous) way to dynamically set CSS properties.

13. Code Compression When you decide to deploy your website project to the Internet, you must consider compressing the CSS to remove comments and spaces to make the web page load faster. To compress your code, you can use some tools, such as YUI Compressor. You can use it to streamline CSS code, reduce file size, and obtain higher loading speed. The above is the content of CSS writing specifications. For more related articles, please pay attention to PHP Chinese website (www.php.cn)!

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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
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)

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.

Show, Don't Tell Show, Don't Tell Mar 16, 2025 am 11:49 AM

How much time do you spend designing the content presentation for your websites? When you write a new blog post or create a new page, are you thinking about

Building an Ethereum app using Redwood.js and Fauna Building an Ethereum app using Redwood.js and Fauna Mar 28, 2025 am 09:18 AM

With the recent climb of Bitcoin’s price over 20k $USD, and to it recently breaking 30k, I thought it’s worth taking a deep dive back into creating Ethereum

What the Heck Are npm Commands? What the Heck Are npm Commands? Mar 15, 2025 am 11:36 AM

npm commands run various tasks for you, either as a one-off or a continuously running process for things like starting a server or compiling code.

How do you use CSS to create text effects, such as text shadows and gradients? How do you use CSS to create text effects, such as text shadows and gradients? Mar 14, 2025 am 11:10 AM

The article discusses using CSS for text effects like shadows and gradients, optimizing them for performance, and enhancing user experience. It also lists resources for beginners.(159 characters)

Creating Your Own Bragdoc With Eleventy Creating Your Own Bragdoc With Eleventy Mar 18, 2025 am 11:23 AM

No matter what stage you’re at as a developer, the tasks we complete—whether big or small—make a huge impact in our personal and professional growth.

Let's use (X, X, X, X) for talking about specificity Let's use (X, X, X, X) for talking about specificity Mar 24, 2025 am 10:37 AM

I was just chatting with Eric Meyer the other day and I remembered an Eric Meyer story from my formative years. I wrote a blog post about CSS specificity, and

See all articles