This article will share some pure CSS layout and typesetting techniques that may not be known to everyone but are super practical, to achieve some common or special layouts.
Developing every web page is inseparable from layout and typesetting. Only by laying the foundation for a good layout and typesetting can we make subsequent Development is smoother. Of course, we cannot stay in the limited thinking of the IExplorer era. The layout and typesetting that cannot be solved are all implemented with JS. Today is different from the past. Modern CSS properties can better and quickly implement various layouts and layouts, saving more time to fish. However, according to the author's current understanding, most students still follow the
CSS JS
method to complete some even if they do not need to be compatible with IExplorer Common or special layout layout. From the perspective of the positioning of the three front-end Musketeers of HTML/CSS/JS, HTML maps the structure of the web page, and CSS maps the ## of the web page. #Performance, JSMap the behavior of the web page. Layout and typesetting
refers to adjusting the
position, size and other attributes of visual information elements such as graphics, text, images, and media on the page layout. The process of page layout becoming organized. Most students think that layout is just a few reasonable CSS properties put together casually, but in most cases there will be flaws even if implemented, and JS may be used to intervene. It can be seen from the characteristics of layout and typesetting
that it belongs to the
expression category, so the author believes that most layout and typesetting can use pure CSS Complete, no JS intervention required. This article adheres to the principle of giving priority to CSS
for effects that can be achieved using CSS, and explains how the author skillfully uses various pure CSS development techniques to complete some
common or special layouts typesetting. Attributes
Before entering the topic, the author summarizes Layout and typesetting
Some necessary attributes, these attributes can quickly build the overall effect, and then through Some common selectors are modified to achieve perfect results. It seems simple, but it may not be completely mastery to use.
Required attributes are some geometric attributes, mainly used to declare position
and
size .
Floating layout
:float##Positioning layout
:
position /left/right/top/bottom/z-indexFlexible layout
:
display:flex/inline-flex、 flex series propertiesBox model
:
box-sizing/margin/padding/border/width/heightThe number of selectors derived from CSS modules
is also large. If there is no special way to memorize the corresponding functions of these selectors, it will be difficult to use them to their maximum effect.
The author divides the selectors into eight categories based on their functions. The selectors of each category can be combined with each other in an application scenario. If you memorize the selectors of these categories, I believe you can maximize the use of the selectors. Function, it can also be applied to some common or special layouts with ease.
Layout may only apply to certain selectors, but it does not prevent everyone from memorizing it through the following classification methods. As an important part of CSS, selectors have more ways to play than attribute combinations.
Basic selector
Selector
Alias
Description
Version
Commonly used
##tag
Tag selector
Specify the type of tag
1
√
id
ID selector
Specifies the identity's tag
1
√
.class
Class selector
Specifies the tag of the class name
1
√
*
Wildcard selector
tags of all types
2
√
Hierarchical selector
Selector
Alias
Description
Version
Commonly used
elemP elemC
Descendant selector
##Descendant elements of
# element
1
√
##elemP>elemC
Children selector
#Children element of
element
2
√
elem1 elem2
Adjacent sibling selector
Element adjacent
Sibling element
2
√
##elem1~elem2
Universal sibling selector# The
sibling elements after the
## element
3√
Collection selector
##Selector
Alias
Description
Version
Commonly used
##elem1,elem2
Union selector
Multiple specified
elements
1
√
##elem.class
Intersection selector
Specifies the element of the class name
1
√
Conditional selector
SelectorDescription
Version
Commonly used
:lang
Specifies the element of the markup language
2
×
##:dir()
Element## that specifies the writing direction
#4
×
:has
element that contains the specified element
4
×
:is
# element# that specifies the condition
##4
×
:not
element with non-specified conditions
4
√
:where
# element specifying the condition
4
×
:scope
#Specify
element
As a reference point
4
×
:any-link
all containing
## link element of #href
4
×
##:local-linkAll
link elements that contain
href
and are absolute addresses
4
×
Status selector
Selector
Description
Version
Commonly used
:active
Mouse activated element
1
×
:hover
element
1
√
:link
Unvisited link element
1
×
:visited
Visited link element
1
×
:target
The element of the current anchor point
3
×
:focus
Enter the focused form element
2
√
##:required
Enter the required
form elements
3
√
:valid
Enter legal
form elements
3
√
:invalid
Input illegal
form element
3
√
:in-range
form within the input range Element
3
×
:out-of-range
Input range
Form elements other than
3
×
##:checked
Option selected
form element
3
√
##:optional
Option optionalForm elements
3
×
##:enabled
Event enabled form elements
3×
:disabled
Event disabled form elements
3√
:read-only
Read-only form elements
3×
:read- write
Readable and writable form element
3×
##:target-within
The
element within which the anchor element is activated
4
×
:focus-within
The internal form element is in the focused state
element
4
√
:focus-visible
Enter the focused
form element
4
×
:blank
The input is empty
form element
4
×
:user-invalid
Enter valid
form elements
4
×
:indeterminate
Options are undetermined
Form elements
4
×
##:placeholder-shown
placeholder-shown
form element
4
√
:current()
Element## in browsing
#4
×
:past()
Browsed
element
4
×
:future()
Unbrowsed
Element
4
×
:playing
Start playing
Media Elements
4
×
:paused
paused
Media Elements
4
×
Structure selector
Selector
Description
Version
Commonly used
:root
The root element of the document
3
×
:empty
element without child elements
3
√
:nth-child(n)
element## of the specified sequential index in the element
#3
√
:nth-last-child(n)
element
element specifying reverse index
3
×
##:first-child
The first
element in the
element
2
√
:last-child The element that ends in the
element
3
√
:only- child
The parent element only has the
element of this element
3
√
:nth-of-type(n)
The
tag specifying the sequential index in the tag
3
√
:nth-last-of-type(n)
##tag specifying the reverse index in the tag
3
×
##:first-of-type
The tag## that is the first of the tags
#3#√
:last-of-type
The tag is the tailtag
3√
##:only-of-type
The parent element only has the
tag of the tag
3
√
Attribute Selector
Selector
Description
Version
Commonly used
[attr]
The
element of the specified attribute
2
√
[attr=val]
The
element whose attribute is equal to the specified value
2
√
[attr*=val]
The
element whose attribute contains the specified value
3
√
[attr^=val]
The
element## whose attribute starts with the specified value #3
√
[attr$=val]
The attribute ends with the specified value The
element
3
√
##[attr~=val]
Attributes containing
elements with specified values (complete words)
(deprecated)
2
×
[attr|=val]
The
element whose attribute starts with the specified value (complete word)
(deprecated)
2
×
Pseudo-element
SelectorDescription
Version
Commonly used
::before
Before the element Inserted content
2
√
##::after
Content inserted after the element
2√
##::first-letter
#First letter of the
## element
1
×
::first-line#First line of
## element
1
×
::selection
The
element selected by the mouse
3
×
:: backdrop
elements in full screen mode
4
×
: :placeholder
## placeholder for form elements
4
√
Tips
With the above prerequisite knowledge, let’s follow the author to experience how to skillfully use various pure CSS development techniques to complete some common or special layoutsBar. In order to facilitate the browser to automatically calculate certain styles, box-sizing:border-box needs to be set globally. Before coding, please introduce reset.css organized by the author.
Main layout
Main layout refers to the placeholder layout that is common and has uniform characteristics in most situations. Mastering Main Layout is an essential front-end skill. To develop a rough plan of the overall layout by looking at design drawings, you must be familiar with the characteristics and structure of these Main Layout.
Full screen layout
Classic Full screen layout consists of top, bottom and The main body is composed of three parts, and its characteristics are the left and right full-screen stretching of the three parts, the top and bottom heights are fixed, and the main body height is adaptive. This layout is very common and is also the mainstream layout of most web application bodies. Usually three tags <header></header>, <footer></footer> and <main></main> are used for semantic typesetting, <main>## You can also insert </main>
left:0and right:0 Stretch it to the left and right to fill the screen; declare top:0 and bottom:0 at the top and bottom respectively to suck it to the top and bottom, and Declare the two heights as fixed values; declare the top and bottom of the body as the top height and bottom height respectively. The three parts are fixed at specific positions through absolute positioning so that they do not affect each other.
flex. display:flexBy default, the child nodes will be arranged horizontally, which needs to be declared flex-direction:columnChange the arrangement direction of the child nodes to vertical arrangement; the top and bottom heights are fixed, so the main body needs to be declaredflex:1Make height adaptive.
needs to be displayed in a scrollable state, do not declare overflow:auto to allow the container to scroll adaptively. Doing so may cause other problems The formatting context may cause adaptive scrolling to fail or produce other unknown effects. You need to insert a <div> within and declare it as follows.
div {
overflow: hidden;
height: 100%;
}
Copy after login
Two-column layout
Classic
Two-column layout is composed of left and right columns, and its characteristics areThe width of one column is fixed, The width of the other column is adaptive and The height of the two columns is fixed and equal. The following takes the left column width as fixed and the right column width as adaptive as an example, and vice versa.
float:leftand fixed width, Since float causes nodes to deflow, the right column needs to declare margin-left as the width of the left column to ensure that the two columns do not overlap.
Three-column layout is composed of left, middle and right columns, and its characteristics are: The width of two consecutive columns is fixed, the width of the remaining column is adaptive and the height of the three columns is fixed and equal. The following takes the fixed width of the left middle column and the adaptive width of the right column as an example, and vice versa. The overall implementation principle is consistent with the above two-column layout.
In the jQuery era there were many adsorption effect plug-ins, and now the three major front-end frameworks also have their own third-party adsorption effect components. They all have a common implementation principle: monitor the scroll event, determine the location range of scrollTop and target node, and if the conditions are met, target node's position is declared as fixed to position the target node relative to the window, making the user appear to be nailed to the specified position in the window.
There are a lot of JS codes to achieve the adsorption effect found online, not to mention that the author likes to play with CSS, so I won’t post the relevant JS code here. Here we recommend a rarely seen and rarely used CSS property position:sticky. A simple two lines of core CSS code can complete the functions of more than ten lines of core JS code, so why not do it.
A brief reviewpositionAttribute value, I won’t go into how to use it, everyone should be familiar with it.
The above is the detailed content of 8 Pure CSS Layout Techniques Worth Knowing. For more information, please follow other related articles on the PHP Chinese 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