Home  >  Article  >  Web Front-end  >  "CSS Design Guide" Study Notes 1_html/css_WEB-ITnose

"CSS Design Guide" Study Notes 1_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 11:40:311041browse

This article is about some study notes and experiences after reading Charles Wyke-Smit's "CSS Design Guide" these days. The author seems to have started contacting web design when he was a freshman. I am not a computer major, so I learned everything by myself. I remember that I basically didn’t know anything about CSS at the time, not even how to select a certain class and id in CSS, so I just downloaded some source code to look at it. , and later I saw that there were . and # in these CSS files, and then I learned how to use them without even guessing. Then when I saw something in the source code that I didn’t understand, I went directly to W3School to find the API. This gradually It allowed me to master the commonly used parts of CSS, and most of the problems when making web pages can be solved. Because my attitude was wavering during this period, I also wanted to learn PHP, Java, Android, etc. However, as you can imagine, the result was that I didn't stick to it. Now that my junior year is almost over and I am about to enter the stage of quasi-senior year, I feel that I can't do this. I have been thinking about a question some time ago, what do I like, and then I think about where I have spent the most time since college. The final result is... the front end. Although the front end involves a lot of knowledge, it does not Can't stop my passion for it. The first thing after deciding was to systematically consolidate my knowledge system, and then I had the study notes of this "CSS Design Guide". This book is also an introduction to CSS that is recommended by many people and has many positive reviews. Book. I have said so much nonsense but it is of no use. Let’s get to the point quickly. The following is mainly the author’s summary of certain chapters, not every chapter. It also contains some of the author’s personal views and some expansions on the content of the book. .

Since the article is relatively long, I will divide the article into multiple parts. The first part of this article

Chapter 1 HTML Markup and Document Structure
Since the object that CSS acts on is HTML, so the author mainly talks about the usage and structure of some basic HTML tags in this chapter.

1.2 HTML Document Analysis
In this section, the author mainly talks about the most basic document structure required for an HTML page as follows:


< html>


Document




First of all, is a new document type in HTML5 Declaration syntax, HTML5 is greatly simplified compared to HTML4's lengthy document type declaration syntax.

1.2.2 Block-level elements and inline elements

The author introduces two important concepts in this section????Block-level elements and inline elements. By default, block-level elements are Elements will always occupy a line, but inline elements will not. Except for the special display attribute of the table element, the display attribute value of basically all HTML elements is either block or inline. One of the author's ideas is that no matter which HTML element you want to know, the first question to ask is: is it a block-level element or an inline element? Then when writing the markup, anticipate how this element will be positioned in the initial state. , so that you can further think about how to use CSS to reposition it in the future, because there is a big difference in positioning between block-level elements and inline elements, which will be explained in detail in the following expansion.

The block-level element box (a very important concept????box model, which will be explained in detail later) will expand to the same width as the parent element. This is why the block-level element will occupy one line. , because the parent element of all block-level elements is body, and its default width is the browser's viewport size, so by default the width of block-level elements is also as wide as the browser's viewport, so , there is no space next to one block-level element to accommodate another block-level element.

Compared with block-level elements that will expand to the same width as the parent element, inline elements behave exactly the opposite. They will try to "shrink-wrap" their content (also the concept of the box model). This That's why several inline elements will be displayed side by side on one line until they fill up a new line, and each block-level element will directly start a new line.

Expansion:

The author did not explain in detail some other characteristics of block-level elements and inline elements in this section. Here I will expand some knowledge of their characteristics. First, list some common block-level elements and inline elements:


div, form, table, header, aside, section, article , figure, figcaption, h1~h6, nav, p, pre, blockqoute, canvas, ol, ul, dl


span, a, img, label, input, select, textarea, br, i, em, strong, small, button, sub, sup, code
The author mentioned before that no matter which HTML element you want to know, the first question to ask is: is it a block-level element or an inline element, because their performance on the box model is very different, but in Before understanding their differences, we must first understand another concept????Replacement elements and non-replacement elements. Replacement elements refer to elements that the browser determines the specific content to be displayed based on the attributes of the element, such as the img tag. The browser reads the content contained in this element based on its src attribute value. Common replacement elements include input, textarea, select, object, iframe, video, etc. These elements have a common characteristic, which is The browser does not directly display its content, but displays specific content through the value of one of its attributes. For example, the browser will determine whether it should display radio buttons or multi-select buttons based on the value of the type attribute in the input. Is a text input box. For non-replaced elements, such as p, label elements, etc., the browser directly displays the content contained in the element. At this point you should have a rough idea of ​​what replacement elements and non-replacement elements are.

After you have a general understanding of the two concepts, you can understand the difference in performance between block and inline on the box model. The first is margin. This is how W3C defines the element objects it supports. :

Applies to: all elements except elements with table display types other than table-caption, table and inline-table
My English is not very good, so I don’t quite understand the meaning of this sentence. My understanding is this All elements support margin. All table display types except the display attribute value table-caption and table-inline, such as table-row-group, table-cell, table-row and table-header-group, etc., but in order to verify that I For my understanding, I would like to point out that elements with a display attribute value of table are also supported. Maybe my understanding of the original standard is wrong. But there is another thing to pay special attention to: margin-top and margin-bottom are two special attributes. They have no effect on non-replaced inline elements. The following is an introduction to the supported objects of margin-top and margin-bottom from W3C:

Applies to: all elements except elements with table display types other than table-caption, table and inline-table
These properties have no effect on non-replaced inline elements.
The previous sentence and the previous pair of margins The description is the same, there is no doubt that the following sentence means that these (margin-top and margin-bottom) properties have no effect on non-replaced inline elements such as a and span. Note that this is non-replaced inline elements and not just Is a non-replaced element or an inline element. For example, img is an inline element, margin-top and margin-bottom have an effect on it, because it is a replaced element rather than a non-replaced element, so "margin-top and margin-bottom have no effect on inline elements" This statement is incorrect.

W3C describes the support objects for padding as follows:

all elements except table-row-group, table-header-group, table-footer-group, table-row, table-column-group and table-column
The above sentence means that in addition to the table display types are table-row-group, table-header-group, table-footer-group, table-row, table-column- Group and table-column elements are not supported, but all other elements are supported.

But there are some special cases here that need to be noted. The effect of setting left and right padding on inline elements such as span and img is visible, but setting top and bottom padding on inline elements is not visible in some cases. , these situations are divided into whether it is a replacement element and whether the background color is set. In order to understand these concepts more intuitively, I made a table here:

padding-top, bottom is visible

So the statement that "padding-top and padding-bottom have no effect on inline elements" is also wrong, because they are only invisible for inline non-replaced elements that do not have a background color, and for inline replacement For elements, it will have an effect regardless of whether the background color is set, and it will expand the parent element.

Having said so many differences between block and inline-block, in fact, in addition to these two common display attributes, there is another attribute that is also very common, that is inline-block. Yes, these are the first two A combination of these situations, it has both the characteristics of block and inline. For example, after setting the attribute value of an element whose display attribute value is block or inline to inline-block, you can use text- which is only valid for inline elements. align: center; In addition to declaring to center it, you can also use padding-top and padding-bottom to set the top and bottom padding on the element without setting a background color, and can stretch the parent element.

This is the end of the expansion of block-level elements and inline elements. If there is anything unclear or wrong, please point it out.

1.3 Document Object Model
In this section, the author only introduces the Document Object Model (DOM, Document Object Model) corresponding to the HTML structure. DOM observes the elements in the page and each element from the perspective of the browser. Attributes of elements, from which a family tree of these elements can be derived. The relationship between each element in the document can be clearly seen through the DOM. For example, the DOM family tree of the following HTML code is as follows:



The Document Object Model


< ;p>The page's HTML markup structure defines the DOM.




DOM

The above is a very simple DOM Structure diagram, from which you can and intuitively see the relationship between each element in the HTML document flow, such as whether it is a parent-child element or a sibling element.

1.4 Summary
In this chapter, the author mainly explains how HTML tags provide structure for content, and what kind of boxes each element will generate on the screen, such as inline boxes or block-level boxes. Finally, I briefly explained what DOM is. It is the model of the document in the browser, and CSS can modify the style attributes of elements in the DOM, thereby modifying the layout and appearance of the page itself.

Chapter 2: How CSS works
In this chapter, the author mainly explains how CSS adds styles to HTML, and explains the working mechanism of cascading, such as when the same attribute of an element is styled multiple times. Which style should be chosen depends on the cascading mechanism of CSS to determine which style is ultimately applied.

Each HTML element has a set of style attributes that relate to different aspects of the element when it is displayed in the document flow, such as its position in the document flow, borders, background, color, etc. CSS is a mechanism that first selects HTML elements and then sets the CSS properties of the selected elements. The CSS selector and the style to be applied form a CSS rule.

2.2 Context Selector
The format of the context selector is as follows:

Tag 1 Tag 2 { Statement }
Among them, tag 2 is the target we want to select, and only in the tag It will be selected only if 1 is the ancestor element of tag 2 (not necessarily the parent element). Strictly speaking, the context selector should be called "Descendant Comninator Selector".

One more thing to note is that context selectors use spaces as delimiters while group selectors use commas as delimiters, don’t get confused.

2.3 Special context selector
The context selector introduced by the author in the previous section uses an ancestor element as the context, as long as the target element has such an ancestor element "upstream" in the DOM structure. , it doesn’t matter how many levels are separated between this ancestor element and the target element, but sometimes we need a more specific context than "an ancestor element", then we can use some special selectors, such as self-select selector>, immediate sibling selector, general sibling selector~ and universal selector*.

2.3.1 Sub-selector>

Tag 1 > Tag 2
The tag 2 here must be a child element of tag 1, that is to say, tag 1 must be tag 2 The parent element of tag 2 and not any other ancestor element of tag 2.

2.3.2 Immediately following sibling selector

Tag 1 Tag 2
Here tag 2 must immediately follow sibling tag 1, otherwise it will be invalid.

2.3.3 General sibling selector~

Tag 1 ~ Tag 2
Here tag 2 must follow (does not have to follow immediately, just after tag 1) ) after its sibling label 1.

2.3.4 Universal Selector*

*
Universal Selector* is a wildcard character that represents any element in the document flow, but Universal Selector* is usually paired with some Use other selectors, such as:

section > *
represents all sub-elements of section, but generally speaking, wildcards are rarely used to select all sub-elements under a certain element, because this involves When it comes to browser performance issues, it will affect the rendering time of the web page. When we write, we write from left to right, but the browser renders from right to left. As for the above code, the browser will first Traverse all elements, and then find out which elements have a section as their parent element. Another example, there is a selector:

div.container #main > .article {}
The browser is rendering When , first take out the elements containing article in all classes to form a collection, and then traverse the elements in each collection. If the id of the element's parent element is not main, delete the element from the collection. Then start searching upwards from the parent element of this element. If you don't find an element with the tag name div and container in the class name, delete the element from the collection until all conditions are matched, so you can do it without using wildcards. If so, try not to use it.

2.4 ID and class selectors
The author introduces the id and class selectors in this section, which provides us with another means of selecting elements. They can be used to ignore the hierarchical structure of elements in the document flow. As long as the id and class attributes and values ​​are added to the element, we can find the target element through their values.

You can set any value for the id and class attributes, but they cannot start with numbers or special symbols.
2.4.3 When to use id and when to use class

The purpose of id is to uniquely identify elements in the page, so each id attribute value in each page is unique. The purpose of class is to identify a group of elements with the same characteristics, which means that multiple identical classes can appear on a page.

The author’s view on when to use ids is:

Every top-level area should be added with an id to get a very clear context so that only nesting can be selected when writing CSS label in the corresponding area.
As for when to use class, since the purpose of class is to identify a group of elements with the same characteristics, if there is a group of elements in the page that have certain same characteristics, you should not hesitate to use class.

But you should also be careful not to use classes indiscriminately here to avoid flooding of classes, for example:

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