Technology Sharing

CSS [Practical Tutorial] (2024 Latest Edition)

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

Introduction to CSS

CSS isCascading Style Sheets(Cascading Style Sheets), which is used to precisely control the style of HTML pages in order to better display graphic information or produce a cool/friendly interactive experience.

没有必要让所有浏览器都显示得一模一样的,好的浏览器有更好的显示,糟糕的浏览器就只有普通的显示,这才是对用户更负责任的做法。
  • 1

The suffix of CSS files is .css

Notes

/* 注释的内容 */
  • 1

Three ways to add styles

1. style Inline styles

<span style="color: red">红色文字</span>
  • 1

insert image description here

2. Add via CSS style selector

https://blog.csdn.net/weixin_41192489/article/details/140226382

3. Inheritance

Elements within a container will get the styles added to the container

  <div style="color: red">
    <p>我是容器内的元素,并没有直接添加样式,但也会变红!</p>
  </div>
  • 1
  • 2
  • 3
  • The properties of text styles are all inherited, including color, those starting with text-, line-, and font-.
  • The properties of box, positioning and layout cannot be inherited.

Who decides the final style?

Because the styles on an element come from many sources, including the browser's default style, built-in styles, styles added by various CSS selectors, and styles inherited from parent elements, the final style will be determined by the style with the highest priority among the many styles.

CSS style priority

https://blog.csdn.net/weixin_41192489/article/details/139720467

Essential knowledge of style

CSS Length Units

https://blog.csdn.net/weixin_41192489/article/details/140236423

CSS Functions

https://blog.csdn.net/weixin_41192489/article/details/140318834

Box Model

https://blog.csdn.net/weixin_41192489/article/details/102411612

  • Both height and line height can expand the box, but background images cannot expand the box.

Notes on inline elements

  • Inline elements can only show inner and outer margins at the beginning and end, and should be avoided as much as possible, so do not set inner and outer margins for inline elements.

  • When an inline element wraps, the borders will be displayed in an irregular manner, so do not set borders for inline elements!

  • When you need to add borders/margins to an inline element, convert it to an inline box.

    display:inline-block
    
    • 1

What styles can be added?

General Style

Almost every HTML element can have styles added to it.

Width Height

  • width: default is the width of the content (not the width of the box)
  • Height: Defaults to the content height (not the box height)

By using the following styles, you can change the element's default standard box model to the IE box model.

box-sizing:border-box;
  • 1

at this time

  • width: the width of the box (left border + left padding + content width + right padding + right border)
  • Height: The height of the box (top border + top padding + content height + bottom padding + bottom border)

Padding

  • Negative values ​​are not supported
  • The percentage value is calculated relative to the width, whether horizontally or vertically.
padding: 10px;
  • 1

Padding: 10px on top, bottom, left and right

padding:10px 5px;
  • 1

The top and bottom padding is 10px

The right and left padding are 5px

padding:10px 5px 15px;
  • 1

The top padding is 10px

The right and left padding are 5px

The bottom padding is 15px

padding:10px 5px 15px 20px;
  • 1

The top padding is 10px

The right padding is 5px

The bottom padding is 15px

The left padding is 20px

border

Related styles include

  • border-radius
  • border-image
  • outline

See https://blog.csdn.net/weixin_41192489/article/details/140325106

Margin

https://blog.csdn.net/weixin_41192489/article/details/115140348

Overflow

  • visible [default value] The excess content will not be cut or have scroll bars added, and all of it will be displayed.
  • Hidden: Hide the scroll bar and do not display content that exceeds the object size, but it can still be scrolled.
  • auto: If the content does not exceed, no scroll bar is displayed; if the content exceeds, the scroll bar is displayed.
  • scroll: On Windows, scroll bars are always displayed regardless of whether the content exceeds the limit. On Mac, this is the same as the auto attribute.

Containers with overflow values ​​of auto and scroll are scroll containers (containers with scroll bars). It is not recommended to use padding for their blank space because of compatibility issues. Therefore, you can only use the margin-bottom of the child element to achieve the bottom blank space of the scroll container.

opacity

The attribute value ranges from 0.0 (completely transparent) to 1.0 (completely opaque). If it exceeds the range of 0 to 1, the final calculated value is the boundary value.

    opacity: -1;    /* 解析为 0, 完全透明 */
    opacity: 2;     /* 解析为 1, 完全不透明 */
  • 1
  • 2

Background

https://blog.csdn.net/weixin_41192489/article/details/140301618

box-shadow

The shadow will not change the size of the box, will not affect the layout of its sibling elements, and you can also set multiple border shadows to achieve better effects and enhance the three-dimensional effect.

box-shadow: 水平偏移 垂直偏移 模糊程度 阴影大小 阴影颜色(默认值为color的颜色值)  内/外阴影
  • 1
  • Horizontal offset: positive value to the right, negative value to the left.
  • Vertical offset: positive values ​​go downwards, negative values ​​go upwards.
  • Blur degree: cannot be negative
  • Inner/outer shadow - inset is inner shadow, not written is outer shadow
box-shadow: 15px 21px 48px -2px #666;
  • 1

insert image description here
Common shadows

box-shadow: 0 2px 6px 0 rgba(0,0,0,.4);
  • 1

insert image description here

For more usage, see
https://developer.mozilla.org/zh-CN/docs/Web/CSS/box-shadow

  • [Actual] Override the background color of the input box auto-fill

    input:-webkit-autofill {
        -webkit-box-shadow: inset 0 0 0 1000px #fff;
        background-color: transparent;
    }
    
    • 1
    • 2
    • 3
    • 4
  • [Actual] Batch modify button press color

    button:active {
        box-shadow: inset 0 0 0 999px rgba(0, 0, 0, .1);
    }
    
    • 1
    • 2
    • 3
  • [Actual Combat]The masking effect of the novice guide

    .guide {
        box-shadow: 0 0 0 9999px rgba(0, 0, 0, .75);
        border-radius: 50%;
    }
    
    • 1
    • 2
    • 3
    • 4

Clipping clip-path

https://blog.csdn.net/weixin_41192489/article/details/121341551

transform

https://blog.csdn.net/weixin_41192489/article/details/140314866

Zoom

Firefox browser does not support it. The following attribute values ​​are allowed:

  • Percentage value.zoom:50%, which means shrinking to half of its original size.
  • Numeric value.zoom:0.5, which means shrinking to half of its original size.
  • normalKeywords.zoom:normalEquivalent tozoom:1, is the default value.
  • resetKeywords.zoom:reset, which means that when the user presses Ctrl and − or Ctrl and + to zoom in and out, the element does not zoom in and out. However, this keyword has poor compatibility and is only supported by Safari browsers.

Comparison between zoom and scale() functions

  • zoomThe center coordinates of the attribute scaling are relative to the upper left corner of the element and cannot be modified.transformTransformingscale()The default center coordinate of the scaling function is the center point of the element.
  • Zooming with the zoom attribute will change the size of the element in real time and trigger redrawing and recalculation. Therefore, the performance of zooming with the zoom attribute is worse than that of zooming with the scale() function.
  • Applying the zoom property to an element does not create a stacking context, does not affect the positioning of fixed elements, does not affect the overflow hiding of absolutely positioned elements by the overflow property, and does not change the containing block of absolutely positioned elements.

Mask

https://blog.csdn.net/weixin_41192489/article/details/121158821

Gradient

https://blog.csdn.net/weixin_41192489/article/details/140316024

Unique styles for different types of elements

text

https://blog.csdn.net/weixin_41192489/article/details/140264311

Hyperlink

Pseudo-class styles - must be written in the following fixed order:

  • :link "link": before the hyperlink is clicked - applies to all hyperlinks with href attributes (excluding anchors) - can be omitted and abbreviated in the a tag
  • :visited "visited": after the link has been visited - can be omitted, abbreviated in the a tag
  • :hover: When the mouse is placed on the label
  • :active “Active”: When the mouse clicks on the label but does not release it.
    /*让超链接点击之前是红色*/
    a:link{
        color:red;
    }

    /*让超链接点击之后是绿色*/
    a:visited{
        color:orange;
    }

    /*鼠标悬停,放到标签上的时候*/
    a:hover{
        color:green;
    }

    /*鼠标点击链接,但是不松手的时候*/
    a:active{
        color:black;
   }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

picture

Image filter

https://www.runoob.com/cssref/css3-pr-filter.html

icon

sheet

Hover row highlight
/*鼠标悬停时,让当前行显示#868686这种灰色*/
table tr:hover{
      background: #868686;
}
  • 1
  • 2
  • 3
  • 4
Alternating color rows

The alternating colors between rows of data can reduce the user's reading fatigue.

table tr:nth-child(odd){
   background:#fff
}
table tr:nth-child(even){
   background:blue
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

scroll

Mouse cursor

valuedescribe
urlURL for the custom cursor.
defaultThe default cursor (usually an arrow)
autoDefault. The cursor set by the browser.
crosshairThe cursor appears as a crosshair.
pointer【common】The cursor is rendered as a pointer (a hand) indicating a link
moveThis cursor indicates that an object can be moved.
e-resizeThis cursor indicates that the edge of the rectangle can be moved to the right (east).
ne-resizeThis cursor indicates that the edge of the rectangular box can be moved up and to the right (north/east).
nw-resizeThis cursor indicates that the edge of the rectangular box can be moved up and left (north/west).
n-resizeThis cursor indicates that the edge of the rectangle can be moved upward (north).
se-resizeThis cursor indicates that the edge of the rectangular box can be moved down and to the right (south/east).
sw-resizeThis cursor indicates that the edge of the rectangular box can be moved down and left (south/west).
s-resizeThis cursor indicates that the edge of the rectangle can be moved downward (south).
w-resizeThis cursor indicates that the edge of the rectangular box can be moved to the left (west).
textThis cursor indicates text.
waitThis cursor indicates that the program is busy (usually a watch or hourglass).
helpThis cursor indicates that help is available (usually a question mark or a balloon).

cursor

cursor color caret-color
Set the color of the input box insertion cursor (not supported by IE and Edge browsers yet)

input {
    caret-color: red;
}
  • 1
  • 2
  • 3

Show and hide related styles

Without the following styles that can hide elements, the elements are displayed (some are invisible only because they are covered by the stack):

  • display: none [Common] Hidden elements, do not occupy space, will load resources, DOM accessible (adding hidden to the html tag will change the tag's display to none)
  • visibility: hidden Hidden elements, occupy space, load resources, DOM accessible
    • If the parent element sets visibility:hidden, the child elements will also be invisible (inheritance)
    • If visibility:visible is set for a child element, the child element will be displayed again.
  • opacity: 0 The element is invisible, takes up space, and can be clicked and selected.
  • clip: rect(0 0 0 0) The element is invisible, not clickable, and does not take up space, but is keyboard accessible
  • Negative z-index values ​​hide the element, which is invisible and cannot be clicked, but occupies space and is keyboard accessible.
    .lower {
     position: relative;
     z-index: -1;
    }
    
    • 1
    • 2
    • 3
    • 4

Position-dependent styles

The position of an element on a page is determined by many styles, including positioning, display, float, margin, etc.

When position, display, and float exist at the same time, the final display effect of the element will be determined according to the logic described in the link below.

https://blog.csdn.net/weixin_41192489/article/details/119009647

Positioning

https://blog.csdn.net/weixin_41192489/article/details/140242430

Layout display

https://blog.csdn.net/weixin_41192489/article/details/140250775

Margin

https://blog.csdn.net/weixin_41192489/article/details/115140348

Alignment

https://blog.csdn.net/weixin_41192489/article/details/140255028

Cascading z-index

https://blog.csdn.net/weixin_41192489/article/details/140297366

Interaction-related styles

Smooth scrolling scroll-behavior: smooth

https://blog.csdn.net/weixin_41192489/article/details/121365831

Stretch resize

https://blog.csdn.net/weixin_41192489/article/details/121396794

Device adaptation related styles

Media Inquiries@media

https://blog.csdn.net/weixin_41192489/article/details/126028971

【Actual combat】Mobile terminal adaptation solutions (three types)

https://blog.csdn.net/weixin_41192489/article/details/120999355

Custom CSS styles (CSS variables/custom properties)

https://blog.csdn.net/weixin_41192489/article/details/140317369

Improve the efficiency of writing CSS

CSS Preprocessors

CSS preprocessor is a special programming language used to add some programming features to CSS (CSS itself is not a programming language). You don't need to consider browser compatibility issues, because the final compilation and output of CSS preprocessor is still standard CSS style. You can use basic programming skills such as variables, simple logical judgment, functions, etc. in CSS preprocessor.

The mainstream CSS preprocessors are Sass (Scss), Less and Stylus.
(SASS was renamed SCSS starting from version 3.0)

CSS Frameworks

CSS Coding Standards

  • Reset browser default styles
  • Try to avoid using style
  • The unit may be omitted only if the property value is zero.
  • The values ​​are usually even numbers, such as 6px instead of 5px (especially for line height and font size, so that their difference can be divided by 2, so that the text is centered in the line)
  • The class name should be semantic, so that you can recognize at a glance which element the class name applies to/the style effect achieved by the class name.
/* 给左侧的盒子定义的样式 */
.leftBox{
}
/* 给内部为菜单的盒子定义的样式 */
.menuBox{
}
/* 将子元素排列为一行的样式 */
.row{
   display:flex
}
/* 设置颜色为红色 */
.red{
  color:red
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

Recommended text styles

  • Use font-size: medium; for the text container (when you change the default browser size, the text can be enlarged or reduced accordingly)
  • Text within a text container, using relative units - percentages or ems
  • Text margins are measured in em units

High performance animation

The three elements of CSS high-performance animation refer to absolute positioning, opacity attribute and transform attribute. Therefore, for the same animation effect, the transform attribute should be used first. For example, the element movement animation should use the transform attribute instead of the margin attribute.