国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
Pseudo-class selector
Home Web Front-end CSS Tutorial css pseudo-selector learning pseudo-class selector analysis

css pseudo-selector learning pseudo-class selector analysis

Aug 03, 2022 am 11:26 AM
css Pseudo element Selector Pseudo class

In the previous article "css pseudo-selector learning: Pseudo-element selector analysis", we learned about pseudo-element selectors, and today we learn more about pseudo-class selectors. I hope it will be useful to everyone. Helps!

css pseudo-selector learning pseudo-class selector analysis

Pseudo-class selector

Pseudo-class selector is a type of selector that allows passing code that is not included in HTML Element state information to locate the usage of HTML elements. Pseudo-class selector The specific usage is to add keywords to the existing selector to represent the status information of the positioned HTML element. [Recommended learning: css video tutorial]

Through pseudo-classes, developers can set the dynamic state of elements, such as hover (hover), click (active), and other elements that cannot be passed in the document. The elements selected by the selector (these elements have no ID or class attributes), such as the first child element (first-child) or the last child element (last-child).

For example:hoverThe pseudo-class selector can be used to change the color of the button when the user hovers the mouse over the button. As shown in the following sample code:

/*?所有用戶指針懸停的按鈕?*/??
?button:hover?{??
??color:?blue;??
?}

The name of the pseudo-class is not case-sensitive, but needs to start with a colon :. In addition, pseudo-classes need to be used in conjunction with selectors in CSS. The syntax format is as follows:

選擇器:偽類?{??
??屬性?:?屬性值;??
?}

The specific syntax format of pseudo-class selector is :pseudo-class, Don’t forget : here.

CSS provides a variety of pseudo-classes, as shown in the following table:

##:first- of-typep:first-of-type Matches the first

element in the parent element

:focusinput:focus Matches the focused <input> element:hovera:hover Match the element on which the mouse is hovering :in-rangeinput:in-rangeMatch elements with the specified value range <input> element:invalidinput:invalid Matches all <input> elements#:lang(language):last-child:last-of-type:link:not(selector) :nth-child(n):nth-last-child(n):nth-last-of-type(n)##:nth-of-type(n)p:nth-of-type(2)match The second child element of the parent element

:only-of-typep:only-of-type matches the parent The only

element in the element

:only-childp:only-child matches the only child element in the parent element

:optionalinput:optional Matches <input> elements without the "required" attribute:out-of-rangeinput:out-of-range Matches <input> elements with values ??outside the specified range:read-onlyinput:read-only Matches <input> elements that specify the "readonly" attribute:read-writeinput:read-write Matches <input> elements without the "readonly" attribute :requiredinput:required Matches <input> elements that specify the "required" attribute:root root matches the root element of the element. In HTML, the root element is always HTML:target#news:target Matches the currently active #news element (clicked on a URL containing this anchor name) :validinput:valid Matches all An <input> element with a valid value:visiteda:visitedmatches all visited links

Classification of pseudo-class selectors

The CSS version has evolved from the first version to the third version, providing pseudo-class selectors## The number of # is already huge. In particular, the CSS3 version has added a large number of pseudo-class selectors.

There are so many pseudo-class selectors. In order to better sort out the pseudo-class selectors, we can divide them into the following 5 types according to their uses:

    <li>User behavior pseudo-classes: refers to some pseudo-classes related to user behavior, for example, hover

    :hover, press:active And get focus:focus etc.

    <li>URL positioning pseudo-class: used to locate elements in HTML pages

    <li>Input pseudo-class: pseudo-class related to form controls

    <li> Structure pseudo-class: Mainly used to locate target elements

    <li>Logical combination pseudo-class: Used for logical operations, such as

    :not( ), it means it is not a certain element.

1. User behavior pseudo-class - dynamic pseudo-class selector

The reason why it is called dynamic pseudo-class Selectors, because they match elements based on changing conditions, are relative to the fixed state of the document. As JavaScript is widely used to modify document content and element state, the boundaries between dynamic selectors and static selectors are becoming increasingly blurred. However, dynamic pseudo-class selectors are still a special type of selector.

: The link selector matches hyperlinks, and the :visited selector matches hyperlinks that the user has visited.

Usage: There are not many attributes that the visited selector can be applied to link elements. You can change the colors and fonts, but that's about it.

Tips: The visited selector matches any link whose href attribute is a URL that the user has visited on all pages, not just your page. The most common use of :visited is to apply a certain style to visited links to distinguish them from unvisited links. For example, when we watch the news, the status of the news that we have read and the news that we have not read are different in the homepage list, which makes it easier for us to distinguish.

: hover selector

: The hover selector matches any element that the user mouse is hovering over.

: active selector

: The active selector matches the element currently activated by the user (usually the element is clicked by the mouse).

: focus selector

: The focus selector matches the element that has received focus, and is often used for input elements.

2. Structural pseudo-class selector

Use the structural pseudo-class selector to select elements based on their position in the document. This type of selector is prefixed by a colon character (:), for example: empty. They can be used alone or in combination with other selectors, such as p:empty.

Structure pseudo-class selector The content contained in the class selector is as shown in the following table:

Selector Example Example description
:active a :active match clicked link
:checked input:checked match the selected
:disabled input:disabled matches every disabled <input> element
:empty p:empty Matches any

element that has no child elements

:enabled input:enabled Matches every enabled <input> element
:first-child p:first-child Matches the first child element

in the parent element,

must be the first child element in the parent element

## that have an invalid value
p:lang(it) Match every

element whose lang attribute value starts with "it"

p:last-child Matches the last child element

in the parent element,

must be The last child element in the parent element

p:last-of-type matches the The last

element

a:link matches all unvisited links
:not(p) Match every element that is not a

element

p:nth-child(2) Matches the second child element in the parent element

p:nth-last-child(2) Matches the penultimate child element of the parent element

p:nth-last-of-type(2) Matches the penultimate element of the parent element Child element

Pseudo-class selector Function is used to locate a group of sibling elements The first element of is used to locate the last element in a group of sibling elementsUsed to locate the nth element in a group of sibling elementsUsed to locate the nth element in a group of sibling elements in reverse order Used to locate the first element in a group of sibling elements of the same typeUsed to locate the last element in a group of sibling elements of the same typeUsed to locate the nth element in a group of sibling elements of the same type Used to locate the nth element in reverse order among a group of sibling elements of the same typeUsed to locate an element without any sibling elements Used to locate an element that does not have any sibling elements of the same type Used to locate an element An element that has no child elements and does not have any text content is used to locate the root of the HTML page Element (

CSS中的結(jié)構(gòu)偽類選擇器是根據(jù)HTML頁面中元素之間的關(guān)系來定位HTML元素,從而減少對HTML元素的id屬性和class屬性的依賴。

:first-child與:last-child

:first-child偽類用來定義一組兄弟元素的第一個元素而:last-child偽類則是定位一組兄弟元素的最后一個元素。

如下示例代碼展示了:first-child偽類和:last-child偽類的用法:

HTML結(jié)構(gòu)如下:

    ????<li>涂山紅紅 ????<li>涂山蘇蘇 ????<li>涂山蓉蓉 ????<li>涂山雅雅

CSS代碼如下:

li:first-child?{
????color:?red;
}
li:last-child?{
????color:?blue;
}

代碼運行結(jié)果如下圖所示:

css pseudo-selector learning pseudo-class selector analysis

:first-child 偽類可以使用:nth-child(n)偽類改寫為:nth-child(1),而:last-child偽類可以使用:nth-last-child(n)偽類改寫為:nth-last-child(1)。

:first-child偽類和:last-child偽類經(jīng)常會引起誤解。例如 li:first-child 是用來定位所有<li>元素中第一個作為子級元素的,而不是定位<li>元素的第一個子級元素。

:first-of-type與:last-of-type

:first-of-type偽類和:last-of-type偽類一個用于定位一組元素中的第一個兄弟元素,一個用來定位最后一個。

如下示例代碼展示了:first-of-type偽類和:last-of-type偽類的用法:

HTML結(jié)構(gòu)如下:

<h3>狐妖小紅娘</h3>
<p>涂山紅紅</p>
<p>涂山蘇蘇</p>

CSS代碼如下:

p:first-of-type?{
????color:?red;
}

p:last-of-type?{
????color:?blue;
}

代碼運行結(jié)果如下圖所示:

css pseudo-selector learning pseudo-class selector analysis

:first-of-type偽類與:last-of-type偽類的用法一定要和:first-child偽類與:last-child偽類的用法區(qū)分開。以:first-of-type偽類和:first-child偽類為例來說明:

    <li>

    :first-of-type偽類是定位一組同類型的兄弟元素中的第一個元素,不管這個元素在兄弟元素中的位置如何。

    <li>

    :first-child偽類是定位一組兄弟元素中的第一個元素,這些兄弟元素不一定是同類型的。

如果將上述示例代碼中的:first-of-type偽類改寫為:first-child偽類的話,將不會生效。

:nth-child(n)與:nth-last-child(n)

:nth-child(n)偽類和:nth-last-child(n)偽類都是CSS3中新增的選擇器,這兩個選擇器的用法基本上是一致的。區(qū)別在于:nth-last-child(n)偽類是倒序方式定位元素,也就是說,:nth-last-child(n)偽類是從一組元素的結(jié)尾開始的。

接下來,主要以:nth-child(n)偽類為例進行講解。:nth-child(n)偽類中的n參數(shù)的含義具有3種情況:

    <li>

    數(shù)字值:任意一個大于 0 的正整數(shù)。例如 #example td:nth-child(1) 表示定位ID為example的父元素下所有

selector:first-child
selector:last-child
selector:nth-child(n)
selector:nth-last-child(n)
selector:first-of-type
selector:last-of-type
selector: nth-of-type(n)
selector: nth-last-of-type(n)
selector:only-child
selector:only-of-type
selector:empty
selector:root )
元素中的第一個元素。<li>

關(guān)鍵字:odd表示奇數(shù),等同于:nth-child(2n);even表示偶數(shù),等同于:nth-child(2n+1)。

<li>

格式為(an+b)公式:a表示周期的長度(步長 ),n表示計數(shù)器(從 0 開始 ),而b則表示偏移值。

如下示例代碼展示了:nth-child(n)偽類(實現(xiàn)表格隔行換色效果)的用法:

nbsp;html>



????<meta>
????<meta>
????<meta>
????<title>nth-child偽類</title>
????<style>
        table {
            border-collapse: collapse;
            border-spacing: 0;
            width: 100%;
        }

        th,
        td {
            border-top: 1px solid lightcoral;
            text-align: center;
        }

        /* 最后一行單元格在底部增加一個邊框效果 */
        tr:last-child td {
            border-bottom: 1px solid lightcoral;
        }

        /* 實現(xiàn)隔行換色 */
        tr:nth-child(even) {
            background-color: aquamarine;
        }
    </style>



????
???????? ???????????? ???????????? ???????? ???????? ???????????? ???????????? ???????? ???????? ???????????? ???????????? ???????? ???????? ???????????? ???????????? ???????? ???????? ???????????? ???????????? ???????? ????
姓名區(qū)域
梵云飛西西域
歡都落蘭南國
石寬北山
涂山紅紅涂山

代碼運行結(jié)果如下圖所示:

css pseudo-selector learning pseudo-class selector analysis

:nth-child(n)偽類的n參數(shù)用法中比較復(fù)雜的是使用(an+b)公式用法,如下示例列舉了一些公式用法:

    <li>

    :nth-child(5n):定位元素的序號是5 [=5×1]、10 [=5×2]、15 [=5×3]等。

    <li>

    :nth-child(3n+4):定位元素的序號是4 [=(3×0)+4]、7 [=(3×1)+4]、10 [=(3×2)+4]、13 [=(3×3)+4] 等。

    <li>

    :nth-child(-n+3):定位元素的序號是3 [=-0+3]、2 [=-1+3]、1 [=-2+3]。

:nth-child(n)偽類與:nth-last-child(n)偽類和:nth-of-type(n)偽類與:nth-last-of-type(n)偽類的區(qū)別,類似于:first-of-type偽類與:last-of-type偽類和:first-child偽類與:last-child偽類的區(qū)別。

:empty

:empty偽類是用來定位沒有任何子級元素或文本內(nèi)容的元素,其中文本內(nèi)容包含了空白。但是HTML的注釋是不影響:empty偽類定位元素的。

如下示例代碼展示了:empty偽類的用法:

nbsp;html>



????<meta>
????<meta>
????<meta>
????<title>empty偽類</title>
????<style>
        body {
            /* 開啟flex布局 */
            display: flex;
        }

        .box {
            background: pink;
            height: 80px;
            width: 80px;
            margin: 0 20px;
        }

        .box:empty {
            background: lime;
        }
    </style>



????<div></div>
????<div>這個元素的背景是粉色的</div>
????<div>
????????<!-- 這是一個注釋 -->
????</div>


代碼運行結(jié)果如下圖所示:

css pseudo-selector learning pseudo-class selector analysis

:root

CSS中的:root偽類選擇器比較簡單,它代表的就是元素。

如下代碼展示的:root偽類的用法:

:root?{
????height:?100vh;
????width:?100vw;
????background-color:?dodgerblue;
}

代碼運行結(jié)果如下圖所示:

css pseudo-selector learning pseudo-class selector analysis

3、UI元素狀態(tài)偽類選擇器

使用UI偽類選擇器可以根據(jù)元素的狀態(tài)匹配元素,下方列表將簡單總結(jié)這類選擇器:

選擇器 說明
:enabled 選擇啟用狀態(tài)的元素
:disabled 選擇禁用狀態(tài)的元素
:checked 選擇被選中的input元素(只用于單選按鈕和復(fù)選框)
:default 選擇默認元素
:valid 根據(jù)輸入驗證選擇有效或者無效的input元素
:in-range/:out-of-range 選擇在制定范圍之內(nèi)或者職位受限的input元素
:required/:optional 根據(jù)是否允許:required屬性選擇input元素

4、輸入偽類選擇器

關(guān)于表單輸入的偽類,主要介紹三種常用的,具體如下:

    <li>

    :enabled:disabled

    <li>

    :read-only:read-write

    <li>

    :checked

:enabled和:disabled

:enabled:disabled一這組偽類選擇器分別表示禁用狀態(tài)與可用狀態(tài),這組為了使完全對立的。

:enabled偽類的實際用處并不大,因為大多元素默認都是可用的,所以寫不寫意義并不大。

如下代碼展示了:enabled:disabled的用法:

nbsp;html>



??<meta>
??<meta>
??<meta>
??<title>:enabled和:disabled的用法</title>
??<style>
    input:enabled {
      outline: none;
    }

    input:disabled {
      /* 禁用狀態(tài)背景為灰色 */
      background-color: gray;
    }
  </style>



??<input>
??<input>


代碼運行結(jié)果如下所示:

css pseudo-selector learning pseudo-class selector analysis

由上圖我們看到禁用狀態(tài)的<input>的背景顏色為灰色。

:read-only和:read-write

:read-only:read-write一這組偽類選擇器分別表示只讀和可寫狀態(tài),同樣的:read-write也很雞肋,因為默認就是可讀寫,示例代碼如下所示:

nbsp;html>



??<meta>
??<meta>
??<meta>
??<title>:read-only和:read-write</title>
??<style>
    input:read-write {
      outline: none;
    }

    /* 只讀狀態(tài) */
    input:read-only {
      color: red;
      outline: none;
    }
  </style>



??<input>
??<input>


代碼運行結(jié)果如下所示:

css pseudo-selector learning pseudo-class selector analysis

我們可以看到,只讀的<input>的文字顏色為紅色。

:checked

:checked偽類可以說是眾多偽類選擇器中使用頻率很高的一個偽類選擇器,該選擇器表示選中的狀態(tài),就比如下面這個例子:

nbsp;html>



??<meta>
??<meta>
??<meta>
??<title>checked偽類</title>
??<style>
    input:checked {
      /* 為選中的增加陰影 */
      box-shadow: 2px 2px 2px 2px lightcoral;
    }
  </style>



??<input>
??<input>


css pseudo-selector learning pseudo-class selector analysis

關(guān)于:checked偽類,最佳實踐是配合<label></label>元素來實現(xiàn),現(xiàn)在我們就通過:checked<label></label>元素來實現(xiàn)一個開關(guān)的效果。

示例代碼如下:

nbsp;html>



??<meta>
??<meta>
??<meta>
??<title>開關(guān)</title>
??<style>
    [type="checkbox"] {
      width: 44px;
      height: 26px;
      position: absolute;
      opacity: 0;
      pointer-events: none;
    }

    /* 開關(guān)樣式 */
    .cs-switch {
      display: inline-block;
      width: 44px;
      height: 26px;
      border: 2px solid;
      border-radius: 26px;
      background-color: currentColor;
      box-sizing: border-box;
      color: silver;
      transition: all .2s;
      cursor: pointer;
    }

    .cs-switch::before {
      content: "";
      display: block;
      width: 22px;
      height: 22px;
      border-radius: 50%;
      background-color: #fff;
      transition: margin-left .2s;
    }


    :checked+.cs-switch {
      color: blueviolet;
    }

    /* 選中移動 */
    :checked+.cs-switch::before {
      margin-left: 18px;
    }

    /* 禁用狀態(tài) */
    :disabled+.cs-switch {
      opacity: .4;
      cursor: not-allowed;
    }
  </style>



??<!-- 普通狀態(tài) -->
??<input>
??<label></label>
??<!-- 選中狀態(tài) -->
??<input>
??<label></label>
??<!-- 禁用狀態(tài) -->
??<input>
??<label></label>
??<!-- 選中禁用狀態(tài) -->
??<input>
??<label></label>


運行效果如下所示:

css pseudo-selector learning pseudo-class selector analysis

5、邏輯組合偽類

:not()否定的偽類

    <li>

    優(yōu)先級為0,優(yōu)先級由括號中的表達式?jīng)Q定; :not(p)由p決定

    <li>

    可以不斷的級聯(lián);
    input:not(:disabled):not(:read-only) {} ;表示處于不禁用,也不處于只讀的狀態(tài)

    <li>

    不可出現(xiàn)多個表達式,也不支持選擇符;
    li:not(li, od); 尚未支持

:not()的巨大的用處在于告別重置的問題;

重置web中的樣式,就好比我們在項目中經(jīng)常使用到的:添加.active類名來控制樣式的顯示與隱藏/改變樣式,往常的寫法都是:

.cs_li?{
	display:?none;
}
.cs_li.active?{
	display:?block;
}

而我們可以使用:not()偽類,可以更好的實現(xiàn):

.cs_li:not(.active)?{
	display:?none;
}

在列表中的設(shè)置<li>的邊框時也可使用其:not()

.cs_li:not(:nth-of-type(5n)){
	margin-right:?10px;?//?除5的倍數(shù)項都設(shè)置右邊的外邊距
}

:is()的作用是簡化選擇器

平時我們開發(fā)中經(jīng)常會用到類似下面的語法:

.cs_li_a?>?img,
.cs_li_b?>?img,
.cs_li_c?>?img?{
	display:?none;
}

使用:is()簡化可寫成:

:is(.cs_li_a,?.cs_li_b,?.cs_li_c)?>?img?{
	display:?none;
}

還有一種嵌套之間的關(guān)系,相互嵌套,交叉組合得出結(jié)論;如下方所示

ol?ol?li,
ol?ul?li,?
ul?ul?li,
ul?ol?li?{
	margin-left:?20px;
}

使用:is()偽類強化,則只需要幾行代碼:

:is(ol,?ul)?:is(ol,?ul)?li{
	margin-left:?20px;
}

<strong>:where()</strong>

:where()與上方的:is()相同,唯一不同的是級別永遠為0,也不受括號里面的表達式影響;

使用的方法與:is()完全相同,但優(yōu)先級永遠是0;底下的括號中的優(yōu)先級完全被忽略,倆句是同一個優(yōu)先級,并且級別等同于.conten選擇器

:where(.article,?section)?.conten?{}
:where(#article,?#section)?.conten?{

(學(xué)習(xí)視頻分享:web前端入門

The above is the detailed content of css pseudo-selector learning pseudo-class selector analysis. For more information, please follow other related articles on the PHP Chinese website!

Statement of this 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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

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.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is 'render-blocking CSS'? What is 'render-blocking CSS'? Jun 24, 2025 am 12:42 AM

CSS blocks page rendering because browsers view inline and external CSS as key resources by default, especially with imported stylesheets, header large amounts of inline CSS, and unoptimized media query styles. 1. Extract critical CSS and embed it into HTML; 2. Delay loading non-critical CSS through JavaScript; 3. Use media attributes to optimize loading such as print styles; 4. Compress and merge CSS to reduce requests. It is recommended to use tools to extract key CSS, combine rel="preload" asynchronous loading, and use media delayed loading reasonably to avoid excessive splitting and complex script control.

What are the key differences between inline, block, inline-block, and flex display values? What are the key differences between inline, block, inline-block, and flex display values? Jun 20, 2025 am 01:01 AM

Choosing the correct display value in CSS is crucial because it controls the behavior of elements in the layout. 1.inline: Make elements flow like text, without occupying a single line, and cannot directly set width and height, suitable for elements in text, such as; 2.block: Make elements exclusively occupy one line and occupy all width, can set width and height and inner and outer margins, suitable for structured elements, such as; 3.inline-block: has both block characteristics and inline layout, can set size but still display in the same line, suitable for horizontal layouts that require consistent spacing; 4.flex: Modern layout mode, suitable for containers, easy to achieve alignment and distribution through justify-content, align-items and other attributes, yes

What is Autoprefixer and how does it work? What is Autoprefixer and how does it work? Jul 02, 2025 am 01:15 AM

Autoprefixer is a tool that automatically adds vendor prefixes to CSS attributes based on the target browser scope. 1. It solves the problem of manually maintaining prefixes with errors; 2. Work through the PostCSS plug-in form, parse CSS, analyze attributes that need to be prefixed, and generate code according to configuration; 3. The usage steps include installing plug-ins, setting browserslist, and enabling them in the build process; 4. Notes include not manually adding prefixes, keeping configuration updates, prefixes not all attributes, and it is recommended to use them with the preprocessor.

How can you animate an SVG with CSS? How can you animate an SVG with CSS? Jun 30, 2025 am 02:06 AM

AnimatingSVGwithCSSispossibleusingkeyframesforbasicanimationsandtransitionsforinteractiveeffects.1.Use@keyframestodefineanimationstagesforpropertieslikescale,opacity,andcolor.2.ApplytheanimationtoSVGelementssuchas,,orviaCSSclasses.3.Forhoverorstate-b

What is the conic-gradient() function? What is the conic-gradient() function? Jul 01, 2025 am 01:16 AM

Theconic-gradient()functioninCSScreatescirculargradientsthatrotatecolorstopsaroundacentralpoint.1.Itisidealforpiecharts,progressindicators,colorwheels,anddecorativebackgrounds.2.Itworksbydefiningcolorstopsatspecificangles,optionallystartingfromadefin

What is the scope of a CSS Custom Property? What is the scope of a CSS Custom Property? Jun 25, 2025 am 12:16 AM

The scope of CSS custom properties depends on the context of their declaration, global variables are usually defined in :root, while local variables are defined within a specific selector for componentization and isolation of styles. For example, variables defined in the .card class are only available for elements that match the class and their children. Best practices include: 1. Use: root to define global variables such as topic color; 2. Define local variables inside the component to implement encapsulation; 3. Avoid repeatedly declaring the same variable; 4. Pay attention to the coverage problems that may be caused by selector specificity. Additionally, CSS variables are case sensitive and should be defined before use to avoid errors. If the variable is undefined or the reference fails, the fallback value or default value initial will be used. Debug can be done through the browser developer

CSS tutorial focusing on mobile-first design CSS tutorial focusing on mobile-first design Jul 02, 2025 am 12:52 AM

Mobile-firstCSSdesignrequiressettingtheviewportmetatag,usingrelativeunits,stylingfromsmallscreensup,optimizingtypographyandtouchtargets.First,addtocontrolscaling.Second,use%,em,orreminsteadofpixelsforflexiblelayouts.Third,writebasestylesformobile,the

What is the :target pseudo-class and how can it be used? What is the :target pseudo-class and how can it be used? Jun 22, 2025 am 12:48 AM

CSS's :target pseudo-class is used to style the target element based on the URL fragment identifier. It works by the browser scrolling to the element when the URL contains a # symbol and element id and allows a specific style to be applied to the element via :target. Common use cases include highlighting the page section after navigation, creating tabs or slideshows without JavaScript, and improving accessibility to long pages. Usage tips include ensuring that id is unique, combining transition or animation effects, switching content visibility with display attributes, and considering compatibility with older browsers.

See all articles