search
HomeWeb Front-endCSS TutorialSome tips for writing css
Some tips for writing cssJan 23, 2017 pm 03:08 PM
css

Javascripty has embarked on the road of engineering. Various mvm and mvvm frameworks have been dizzying. We will not discuss js here. Let me talk about some of my experience in writing CSS in actual work. Of course, many people have also summarized this experience. What I am talking about is definitely not as good as those written by some big names. I just simply bring out my work experience and compare it with Share it with everyone.

If you want to do your job well, you must first sharpen your tools

When writing CSS, you need to master at least one development tool, whether it is SASS, It's still LESS, essentially they are the same, but the syntax is a little different. If you are still writing CSS by hand, please learn about them as soon as possible, and choose one of them according to your own habits and use them. Personally, I prefer sass, which is more in line with my writing habits. There are also many people who like Less. They think the less syntax is more convenient.

What SASS/LESS

SASS (LESS) is an extension of CSS3, adding rule nesting, variables, mixing, selector inheritance, etc. Convert it into standard, well-formed CSS code by using command line tools or WEB framework plug-ins.

Why do we need SASS/LESS

As a development tool, they provide many convenient writing methods, which greatly saves designers’ time and makes CSS development easier. Be simple and maintainable.
They are also the cornerstone of writing modular, maintainable CSS.

How to use

There are a lot of tutorials about SASS/LESS on the Internet, so I won’t waste space on the basic syntax here.
You can search online by yourself. They are all similar. Once you learn one, you can basically use it:

I use SASS as an example for the following content. The content is not limited to SASS, LESS is also applicable!

God said, there is no rule without rules

Yes, there is no rule without rules. You need to understand a css naming standard or make your own standard (it is not recommended to do it yourself) Definition, not conducive to teamwork);

Why is there a need for a naming convention?

When you have written a lot of CSS, you will find that there is no effective naming convention and it will make you miserable. This feeling is especially noticeable if you are working on a slightly larger project, or in the process of developing it in collaboration with others. Because when you name the css, you will find that the name is used elsewhere, or a teammate has already used it, and you must rename it. Over time, the naming of CSS will become messy, smelly and long, and it is impossible to guess the meaning of the naming at a glance.

BEM naming convention

Various naming conventions are different, and the wise have their own opinions. Here I will introduce the BEM naming convention. What I introduce may not be suitable for you. , you need to think about which naming convention suits you. .

BEM is a CSS Class naming method proposed by the Yandex team, aiming to better create CSS/Sass modules.

BEM means block, element, and modifier.

●block: can be understood as a region, a component or a block-level element. The specific distinction needs to be analyzed based on the actual situation;

●block__element: It is an element in the above block , for example, if there is an a tag (a: element) in the navigation (nav: block), it is an element. Block and element are linked by two underscores;

●block__element–modifier: My understanding is state or attribute. For example, the a tag in element has three states: active, hover, and normal. These three states are modifiers. The midifier is connected by two "-" horizontal lines.

For the example mentioned above, I will use the actual code to demonstrate:

<!-- HTML结构 --><nav class="nav">
  <a href="#" class="nav__item nav__item--active">当前页:active</a>
  <a href="#" class="nav__item nav__item--hover">假设鼠标在这里要加个hover的class</a>
  <a href="#" class="nav__item nav__item--normal">假设需要加个normar的状态</a></nav>
// SASS写法.nav{
  &__item{
    &--active{
      <!-- active样式 -->
    }
    &--hover{
      <!-- hover样式 -->
    }
    &--normal{
      <!-- normal样式 -->
    }
  }}
/* 编译后的css */
.nav{ }
.nav__item{ }
.nav__item--active{ }
.nav__item--hover{ }
.nav__item--normal{ }

From the above example, you can see the css at a glance Meaning, and the compiled css does not have any nesting, but the structure of SASS is very clear and can be seen at a glance.

It can be seen that using SASS and BEM can write a code that is easy to read, maintainable, and modular;

God said, I don’t know you

A readable SASS must have a description. Every file and function needs a description.

For a SASS file, you need to explain at least two points, whether the SASS is public or private, which page and which part

@charset "utf-8"/** 预定义函数* author:xxx* time:xxxx-xx-xx*//** 清除浮动* use: @include clearfix();* author: xxx* time: xxxx-xx-xx*/@mixin clearfix(){
  *zoom: 1;
  &:before,
  &:after {
      content: "";
      display: table;
  }
  &:after {
      clear: both;
      overflow: hidden;
  }}

God said the world must be unified

●reset

css reset is essential. There are many css reset codes on the Internet. Compass also has a reset module. You only need to @import "compass/reset". If you think these codes are too redundant, you need to at least ensure that your css has margin and padding reset, so that you can ensure that the css has the same style in each browser.

*{
  margin: 0;
  padding: 0;}

●Space/Line Spacing/Margin

●Font Size

●Color

●Level

●Height

......

Why variables are needed


Using a separate variable has many benefits. The biggest one is maintainability. Sex, who knows who uses it!

Maintainability

●When you finish the development, take it to the designer for acceptance. The designer says this is not good, so change the color! ——It’s okay, I’ll change the variable!

●产品说,这里不好,列表间距太大了,小屏幕只显示那么一点点!—— 没事,我改个变量!

●来来来,产品说我们换一套皮肤! —— 没事,我重新定义一份变量!

……

这些例子可以让你明白有一份variables是多么重要了吧。其实这些只是在可维护方面的好处。作为一名前端,我们是最接近用户的工程师,我们不能仅仅停留在代码层面,更需要的是站在用户体验的角度思考问题,而variables可以让我们有一套规范,确保页面一致性

一致性

FE是面向用户的,我们需要对用户负责。设计师在设计页面的时候,不能所有的像素的都很精确,不可能每次的颜色都是毫无差错的。所以在这时候,就需要规范了,如果设计师没有规范,那我们自己制定一套规范。例如:

●同一个项目,一个页面的列表高度是20px,另一个页面的列表是21px,这时我们无需理会,直接使用我们之前定义的列表高度进行开发。

●同一个页面,有两个error的颜色#dc4c4c和#d84a4a,我们也无需理会,使用统一的error颜色变量;

这些是用户体验的细节,通过一份variables可以让我们保持页面的一致性。
这里只是略微提下用户体验,之后我再写一篇《前端工程师必须重视的用户体验》来详细讲解下用户体验。

module(模块化,基于SASS/LESS)

模块化在js中经常听到,对于css来说,模块化对于易读性和可维护性同样重要。那么如何来做模块化呢?

多文件夹

建立多个文件夹,分类存放sass文件。例如:将variables、mixin、公共样式、私有样式分成多个文件夹存放;

多文件

同一个文件夹的sass可以按模块、功能等等分成多个文件,最终用@import 导入

这样说的有点粗糙,我举个例子吧(下划线开头的sass文件不会编译单独css文件)

——sass
  ——config                //基本变量
  ——mixin                 //函数
  ——common                //公用
    ——header
    ——aside
      ——_list.scss
      ——_nav.scss
      ——_base.scss
  ——compoment             //组件样式
    ——dropdown
    ——lightbox
  ——page
    ——index               //首页
      ——_ad.scss           //广告样式
      ——_content.scss      //内容信息
      ——_info.scss         //个人信息样式
      ——_base.scss         //index样式,@import &#39;ad&#39;;@import &#39;content&#39;;@import &#39;info&#39;;
    ——write               
    ——preview        
      ——_aside.scss       //preview页面独有侧边栏
    ——about
  ——main.scss             //导入所需要的样式,最终生成一个main.css

如上面所示的目录结构,能让人一目了然sass的目录的结构,维护的时候可以快速准确的找到文件。
如果是多页面的项目,也可以最大限度复用代码,而且可以导出公用样式,利用缓存提高加载速度,不用每个页面都重复写一样的代码。

注意:common文件夹的公共样式必须是其他页面所共有的样式,如果有些页面有特殊的样式,应该将会覆盖的css抽取出来,在页面中分别导入不同的私有样式。

根据命名规则限定使用样式

比如说preview页面有一个私有aside样式,可以在这样写preview里面的_aside.scss:

@charset "utf-8"/** 预览页面侧边栏* author:xxx* time:xxxx-xx-xx*/@import &#39;../../common/aside/base&#39;.preview{
  .aside{
    /* css */
    &__item{
      /* css */
    }
  }}

这里需要注意的是,css覆盖会导致重新渲染,影响性能。

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
利用CSS怎么创建渐变色边框?5种方法分享利用CSS怎么创建渐变色边框?5种方法分享Oct 13, 2021 am 10:19 AM

利用CSS怎么创建渐变色边框?下面本篇文章给大家分享CSS实现渐变色边框的5种方法,希望对大家有所帮助!

css ul标签怎么去掉圆点css ul标签怎么去掉圆点Apr 25, 2022 pm 05:55 PM

在css中,可用list-style-type属性来去掉ul的圆点标记,语法为“ul{list-style-type:none}”;list-style-type属性可设置列表项标记的类型,当值为“none”可不定义标记,也可去除已有标记。

css与xml的区别是什么css与xml的区别是什么Apr 24, 2022 am 11:21 AM

区别是:css是层叠样式表单,是将样式信息与网页内容分离的一种标记语言,主要用来设计网页的样式,还可以对网页各元素进行格式化;xml是可扩展标记语言,是一种数据存储语言,用于使用简单的标记描述数据,将文档分成许多部件并对这些部件加以标识。

css3怎么实现鼠标隐藏效果css3怎么实现鼠标隐藏效果Apr 27, 2022 pm 05:20 PM

在css中,可以利用cursor属性实现鼠标隐藏效果,该属性用于定义鼠标指针放在一个元素边界范围内时所用的光标形状,当属性值设置为none时,就可以实现鼠标隐藏效果,语法为“元素{cursor:none}”。

css怎么实现英文小写转为大写css怎么实现英文小写转为大写Apr 25, 2022 pm 06:35 PM

转换方法:1、给英文元素添加“text-transform: uppercase;”样式,可将所有的英文字母都变成大写;2、给英文元素添加“text-transform:capitalize;”样式,可将英文文本中每个单词的首字母变为大写。

rtl在css是什么意思rtl在css是什么意思Apr 24, 2022 am 11:07 AM

在css中,rtl是“right-to-left”的缩写,是从右往左的意思,指的是内联内容从右往左依次排布,是direction属性的一个属性值;该属性规定了文本的方向和书写方向,语法为“元素{direction:rtl}”。

css怎么设置i不是斜体css怎么设置i不是斜体Apr 20, 2022 am 10:36 AM

在css中,可以利用“font-style”属性设置i元素不是斜体样式,该属性用于指定文本的字体样式,当属性值设置为“normal”时,会显示元素的标准字体样式,语法为“i元素{font-style:normal}”。

怎么设置rotate在css3的旋转中心点怎么设置rotate在css3的旋转中心点Apr 24, 2022 am 10:50 AM

在css3中,可以用“transform-origin”属性设置rotate的旋转中心点,该属性可更改转换元素的位置,第一个参数设置x轴的旋转位置,第二个参数设置y轴旋转位置,语法为“transform-origin:x轴位置 y轴位置”。

See all articles

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 agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools