CSS Advanced (leveling up in CSS)

Source: Internet
Author: User

Original [: Leveling up in CSS]

CSS seems easy at first. After all, it's just styling, right?
But, give it time. Soon, CSS would show you the true depths of its complexity.
There is four things you can does to stay sane and using CSS at Scale:use proper semantics, modularize, adopt a naming C Onvention, and follow the single responsibility principle.

CSS at first looks very simple, after all, it is just style, right?
But, given the time, CSS will soon show you the true depth of its complexity.
In the large-scale use of CSS at the time to maintain a rational, there are four things to do: the appropriate semantics , modularity , the adoption of a naming convention , and follow the principle of single responsibility .

1. Proper semantics (use proper semantics)

There is a concept of semantic tagging in HTML and CSS. Semantics means the relationship between words and meanings. In the context of HTML, it means using the appropriate markup, which has a classic example:

<!-- bad --><div class=”footer”></div><!-- good --><footer></footer>

Semantic HTML is pretty straightforward. On the Other-hand, semantic CSS are much more abstract and subjective. Writing Semantic CSS means choosing class names that convey structural meaning and function. Come up with class names is easy to understand. Make sure they aren ' t too specific. That's the, you can reuse your classes.

Semantic HTML is fairly straightforward, and on the other hand, semantic CSS is fairly abstract and subjective. Writing semantic CSS means communicating the meaning and responsibilities of the structure and proposing a class name that is easy to understand. Make sure it's not very specific, so you can reuse your class name.

To enumerate the good semantic class names, here is a simple example of a CSS method:

<div class="Stream">  <div class="Streamitem">    <article class="Postarticle">      <div class="Postarticle-content">        <!--content --      </div>    </Article>  </div></div>

From the code, you can immediately discern structure, role, and meaning. The parent class is stream, a list of articles. The child class was streamitem, an actual article within the list. It's clear how the parent and child relate to one another. Furthermore, those classes is used on every page that features articles.

From the code above, you can immediately see the structure, the role, and the meaning. The parent class is stream, a list of articles, a subclass of Streamitem, and an article within a list. The relationship between the parent and child classes is clear. And such a class can be used in any page that has an article.
You should is able to the read HTML and CSS like a book. It should tell-a story. A story has characters and relationships between them. More semantic CSS would ultimately make your code more maintainable.

For further reading, check out what makes for Semantic Class Names, naming CSS Stuff are really hard, and semantics and Sen Sibility. For a longer read, see About HTML semantics and front-end architecture.

You can read HTML and CSS like a book, and it should tell you a story that includes personas and their relationships. A good semantic CSS will fundamentally make your code better maintained.

Advanced reading, please move the semantics of the class name, the name of the CSS is really difficult, and semantic and recognition capabilities. For a longer reading, move on to the semantic and front-end architecture of HTML.

2, modular (modularize)

In the age of component-based libraries like React, Modularization is king. Think of composable modules created by deconstructing interfaces. Below is Product Hunt ' s front page stream. As an exercise, let's break the stream to various components.
in the age of component-based libraries such as react, modularity is the kingly way. The
think component is a composable module created by the structural interface that flows down the front page of the product, and we do an exercise to break it down into different components.

Each colored outline represent a component. The stream has many stream items.
Each color profile represents a component that contains many flow items.

<div class="stream">    <div class="streamItem">        <!-- product info -->    </div></div>

Most of the can is broken down to even smaller components.
Most components can be disassembled into smaller components, like this:

Each stream item is a thumbnail and information about a featured product.
Each flow project has a brief picture and information about a particular product

<!--STREAM COMPONENT --<div class="Stream">  <div class="Streamitem">    <!--POST COMPONENT ---    <div class="POST">        <img src="Thumbnail.png" class="Postthumbnail"/ >        <div class="Content">        <!--product Info -        </div>    </div>  </div></div>

Because The stream component is independent of it children and vice versa, you can easily adjust or switch out the post C Lass without making significant changes to the stream class.
The flow component is independent of its children, and vice versa, and you can easily adjust or replace the post class without changing the convection classes a lot.
Thinking in components would help you make your decouple code. The more decoupled your code is, the lower the interdependence between your classes. This makes your code easier to modify and work with the long run.

Consider a component that can help decouple your code. The higher the degree of decoupling, the lower the interdependence between your classes. This will make your code easy to modify and run for a long time.

When modularizing your CSS, the start off by breaking your design to component. You can do the paper and pencil or in a program like Illustrator or Sketch. Identifying components would give you a idea of what to name your classes and what they relate to one another.

To read more about component driven CSS, check out CSS architectures:scalable and Modular approaches, Writing Modular CSS With Sass, and modularizing Your front-end Code for Long term maintainability and sanity.

When it comes to modular CSS, start splitting your design into components, you can use your paper and pens or use software like Illustrator or Sketch, and the components will give you a way to name your classes and how they relate to each other.

More about the component-driven CSS article, please go to CSS architecture-scalable and modular approach, with sass to write modular CSS, for long-term maintenance and reasonable sound modularity of your front-end code.

3. Choose a good naming convention (Choose a good naming convention)

There is dozens of CSS naming conventions out there. Some people swear by their choice of convention, claiming theirs is better than others. In truth, the best naming convention are different for each person. The best advice I ever received on this is:choose the naming convention so makes the most sense to you.
Here's a short list of some of the naming conventions people use:

    • Object oriented CSS Oocss
    • Block element modifier (BEM)
    • Scalable and modular architecture for CSS (SMACSS)
    • Atomic

One of my favorite naming conventions is BEM. BEM stands for block, element, and modifier. Yandex, the Russian equivalent of Google, came up with it to issues they had with their CSS codebase at scale.

Choose a reliable naming convention (Choose a good naming convention)

There are a lot of naming conventions for CSS, and some people claim that they choose the naming convention, claiming that they are better than others. In fact, a good naming convention is different for everyone, and the best advice I receive is this: choose the naming convention that is most reasonable for you.
About naming conventions used by other people there is a list here:

    • Object-oriented CSS Oocss
    • Block-level element decoration (BEM)
    • Extensible and Modular Architecture for CSS (SMACSS)
    • Atomic Nature

One of my favorite naming conventions is that BEM,BEM represents blocks, elements, and adornments. Yandex, the Russians ' Google, figured out the solution when they were dealing with their own size code base.


BEM is one of the Simplest-yet strictest-of the naming conventions.
Bem is a simple and strict naming convention.

.block{}.block__element{}.block--modifier{}

Blocks represent higher level classes. Elements is children of blocks. and modifiers represent different states.
A block represents a high-level class, an element is a child of a class, and a decorator represents a different state.

<div  class  = "search" ;  <input  type  =  "search__btn search__btn--active" />  </div ;   

In the example above, the class search was the block and search button is an its element. If we want to modify the "the" button, we can add a modifier like active.
One thing to remember about naming conventions are that regardless of which CSS naming convention you prefer, you'll ofte N times inherit or work on codebases with different standards. Be open to learning new standards and alternative ways of the thinking about CSS.
You can read more about BEM in Getting your head ' round BEM syntax, BEM 101, and Intro to BEM. For general reading about different conventions, check outoocss, ACSS, BEM, Smacss:what is they? What should I use?
In this example, the search class is a block, the searching button is an element, and if we want to modify the state of the button, we can add a modifier like active.
Ignoring the CSS naming conventions you are familiar with, you will often inherit or work in different code base standards. Take an open mind to learn new standards and alternative ways to think about CSS.
More reading about BEM began to understand Bem's grammar, BEM101, Bem Introduction. About the different conventions of general reading, oocss,acss,bem,smacss what are they? Which one should I use?

4. Follow the principle of sole responsibility (follow the single responsibility principle)

The single responsibility principles states that every module or class should has responsibility over a single part of The functionality provided by the software, and that responsibility should is entirely encapsulated by the class.
within the context of CSS, the responsibility principles means that pieces of code, classes and modules should Do only one thing. When applied-to-CSS file organization, this means, which self-contained components like carousels and navigation bars should Have their own CSS file.

The single responsibility principle states that each module or class should be responsible for a single part of a supporting software function that should be completely encapsulated in the class. In the context of CSS, the principle of single responsibility means that a piece of code, a class, or a module should do one thing. When applied to the organization of CSS, this represents a standalone component, like the navigation bar and carousel should have their own CSS files, just like this:

Another common pattern in file organization are grouping files by functionality. For example, with the snippet above, all the files related to the Carousel component is grouped together. Adopting this approach makes finding files much easier.
In addition to separating component styles, it's good to separate global style using the single responsibility principle.

Another common pattern of organizing files is to group files by function. For example, in the code snippet above, so the files associated with the carousel are organized together. Using such a method makes finding files quite simple.
In addition, for separating the component styles, it is good to draw the global style out of the single responsibility principle.

The example, each of the style concern is separated to its own file. This is the if you want to update your colors, and you know exactly where to look.
Regardless of which file organization convention you use, let the a responsibility principle help guide your decision S. If one file starts getting bloated, consider partitioning it out based on what makes logical sense.
For more on file structures and CSS architecture, read aesthetic Sass 1:architecture and Style Organization and scalable and maintainable CSS Architecture.
When the a single responsibility principle are applied to individual CSS classes, it means this each class should has only O Ne function. In the other words, separate out the styles into different classes based on concerns. Here is a classic example:
In this example, each design style is separated in its own file, so that if you want to update your color, it should be clear where to look.
No matter which document you use to organize your agreement, the single Responsibility principle helps you guide your decisions, and if a file becomes bloated, consider dividing the file from a logical level.
More about file structure and CSS architecture, reading aesthetic sass, architecting and organizing CSS and scalable as well as maintainable architectural styles.

When the single responsibility principle is adopted by the CSS class, this means that each class must have only one function, in other words, to divide the styles into different classes based on the relationship, here is a classic case:

. Splash { background: #f2f2f2  ; color: #fffff  ; margin: px ; padding: px ; Border-radius: 4px ; position: Absolute ; top: 0  ;Right  : 0  ; Bottom: 0  ;Left  : 0  ;}

In the example above, we is mixing concerns. The Splash class isn't only contains presentation and styling logic for itself, but is for its children as well. To remedy this, we can split the code into the separate classes.
In the example above, we mix different points, and this class contains not only the presentation and its own style logic, but also its own elements, in order to save it, we can separate the style code in two separate classes.

. Splash {  position: absolute;   top: 0;  Right  : 0;   Bottom: 0; Left  : 0; }. Splash__content {  background: #f2f2f2;   color: #fffff;   padding: px;  Border-radius: 4px; }

Now we have a splash and splash content. We can use the splash as a generic full-bleed class that takes any child. All of the concerns of the splash content, is decoupled from the parent.
Now we have slash and splash__content. We can use splash as a normal class that can accommodate any subclass, all of which are related to subclasses, and in this case are fully decoupled from the parent class.

You can read more about applying the responsibility approach to styling and classes in the single responsibility PR Inciple applied to CSS and a single Responsibility.
You can read more about writing styles and classes in a single-responsibility way, the one responsibility principle applied to CSS and one responsibility.

Simple beyond complexity (simplicity over complexity)

Ask any good front-end developer or CSS architect and they would tell you the they ' ve never been fully satisfied with thei R code. Writing Good CSS is a iterative process. Start simple, follow basic CSS conventions and style guides, and iterate from there.
If you enjoyed this article, you might like the article I wrote that teaches you five important characteristics of good de Sign through K-pop.
I would love to know how do you approach CSS. What is your favorite naming convention? How does you organize your code? Feel free to leave a note or Tweet to me.

Ask any good front-end developers or CSS architects who will tell you that they have never really been completely satisfied with their code. A well-written CSS is a iterative process, starting with a simple beginning, following basic CSS conventions and style guides, and starting with iterations from there.

CSS Advanced (leveling up in CSS)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.