神奇的CSS3选择器_html/css_WEB-ITnose
话说园子里也混迹多年了,但是基本没写过blog,写点基础的,那就从css3选择器开始吧。
Css3选择器
先说下,为什么提倡使用选择器。
- 使用选择器可以将样式与元素直接绑定起来,在样式表中什么样式与什么元素匹配一目了然,修改起来也很方便。
- 减少样式表的代码量。
属性选择器
1.[att*=val]属性选择器
意义:表示元素用att表示的属性的属性值包含用val表示的字符,则该元素使用这个样式
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title></title> <style type="text/css"> [id*=demo] { width: 100px; height: 100px; background-color: #000099; } </style></head><body><div id="demo"></div></body></html>
2.[att^=val]属性选择器
意义:表示元素用att表示的属性的属性值以val表示的字符串开头,则该元素使用这个样式。
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title></title> <style type="text/css"> [id^=demo] { width: 100px; height: 100px; background-color: #000099; margin: 10px; } </style></head><body><div id="demo"></div><div id="demo1"></div></body></html>
3.[att$=val]属性选择器
意义:表示元素用att表示的属性的属性值以val表示的字符串结尾,则该元素使用这个样式
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title></title> <style type="text/css"> [id$=o] { width: 100px; height: 100px; background-color: #000099; margin: 10px; } </style></head><body><div id="demo"></div><div id="demooo"></div></body></html>
结构性伪类选择器
伪类选择器是指已经定义好的选择器,不能随便起名。
例如:a:link,a:visited,a:hover,a:active.
伪元素选择器是指已经定义好的为元素使用的选择器。
- first-line伪元素选择器
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title></title> <style type="text/css"> p:first-line { color: red; } </style></head><body> <p> hello world <br/> 你好 </p></body></html>
2.first-letter 伪元素选择器
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title></title> <style type="text/css"> p:first-letter { color: red; } </style></head><body> <p> hello world </p> <p> 你好</p></body></html>
<strong>befor伪元素选择器</strong>
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title></title> <style type="text/css"> li:before { content: '*'; } </style></head><body> <ul> <li>demo1</li> <li>demo1</li> <li>demo1</li> <li>demo1</li> <li>demo1</li> </ul></body></html>
after伪元素选择器
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title></title> <style type="text/css"> li:after { content: '*'; } </style></head><body> <ul> <li>demo1</li> <li>demo1</li> <li>demo1</li> <li>demo1</li> <li>demo1</li> </ul></body></html>
root选择器
root选择器将样式绑定到页面的根元素。在使用:root与body元素的背景时,根据不同的条件,显示效果不同
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title></title> <style type="text/css"> :root { background-color: #003300; } body { background-color: yellow; } </style></head><body><p>你好</p></body></html>
not 选择器
排除结构元素下面子结构元素,使他不使用该元素
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title></title> <style type="text/css"> body *:not(h1) { background-color: yellow; } </style></head><body><h1 id="大家好">大家好</h1><p>你好</p></body></html>
empty选择器
当元素内容为空时使用的样式。
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title></title> <style type="text/css"> td:empty { background-color: yellow; } </style></head><body><table border="1"> <tr> <td width="100px">1</td> <td width="100px">2</td> <td width="100px"></td> </tr></table></body></html>
target选择器
使用target选择器给页面中的target元素使用样式
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title></title> <style type="text/css"> :target { background-color:yellow; } </style></head><body><table border="1"> <a href="#text3">示例1</a> <div id="text1"> <h1 id="你好">你好</h1> <p>你好你好你好你好你好你好你好你好你好你好你好你好你好你好</p> </div> <div id="text2"> <h1 id="你好">你好</h1> <p>你好你好你好你好你好你好你好你好你好你好你好你好你好你好</p> </div> <div id="text3"> <h1 id="你好">你好</h1> <p>你好你好你好你好你好你好你好你好你好你好你好你好你好你好</p> </div></table></body></html>
first-child、last-child选择器
指定第一个子元素和最后一个子元素的样式
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title></title> <style type="text/css"> li:first-child { background-color: yellow; } li:last-child { background-color: #009999; } </style></head><body><table border="1"> <ul> <li>1</li> <li>2</li> <li>3</li> <li>1</li> </ul></table></body></html>
nth-child、nth-last-child选择器
针对父元素中某个指定序号的子元素来指定样式。
也可以使用Nth-child(even)对偶数子元素指定样式,Nth-child(odd)对奇数元素指定样式
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title></title> <style type="text/css"> li:nth-child(2) { background-color: yellow; } li:nth-last-child(2) { background-color: #009999; } </style></head><body><table border="1"> <ul> <li>1</li> <li>2</li> <li>3</li> <li>1</li> </ul></table></body></html>
nth-of-type nth-last-of-type选择器
这两个选择器是为了弥补nth-child、nth-last-child选择器的缺陷,这两个选择器只针对同类元素指定样式。
UI元素状态选择器
E:horver,E:active,E:focus选择器
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title></title> <style type="text/css"> input[type="text"]:hover { background-color: yellow; } input[type="text"]:focus { background-color: green; } input[type="text"]:active { background-color: red; } </style></head><body><input type="text" name="name"></body></html>
E:enabled,E:disabled,E:read-only,E:read-write选择器
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title></title> <style type="text/css"> input[type="text"]:disabled { background-color: green; } input[type="text"]:read-only { background-color:darkgrey; } </style></head><body><input type="text" disabled><br><input type="text" ><br><br><input type="text" readonly="readonly" ></body></html>
E:checked、E:default选择器
E:checked指定复选框选取时的样式
E:default 指定默认选取框的样式
E::selection选择器
指定元素处于选中状态时的样式
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title></title> <style type="text/css"> p::selection { background-color: goldenrod; } </style></head><body> <p>测试测试</p></body></html>

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제











공식 계정 웹 페이지 업데이트 캐시, 이것은 간단하고 간단하며 냄비를 마시기에 충분히 복잡합니다. 공식 계정 기사를 업데이트하기 위해 열심히 노력했지만 사용자는 여전히 기존 버전을 열었습니까? 이 기사에서는이 뒤에있는 비틀기와 회전을 살펴 보고이 문제를 우아하게 해결하는 방법을 살펴 보겠습니다. 읽은 후에는 다양한 캐싱 문제를 쉽게 처리 할 수있어 사용자가 항상 가장 신선한 콘텐츠를 경험할 수 있습니다. 기본 사항에 대해 먼저 이야기 해 봅시다. 액세스 속도를 향상시키기 위해 브라우저 또는 서버는 일부 정적 리소스 (예 : 그림, CSS, JS) 또는 페이지 컨텐츠를 저장합니다. 다음에 액세스 할 때 다시 다운로드하지 않고도 캐시에서 직접 검색 할 수 있으며 자연스럽게 빠릅니다. 그러나 이것은 또한 양날의 검입니다. 새 버전은 온라인입니다.

이 기사에서는 브라우저에서 직접 사용자 입력을 검증하기 위해 필요한, Pattern, Min, Max 및 Length 한계와 같은 HTML5 양식 검증 속성을 사용하는 것에 대해 설명합니다.

기사는 HTML5 크로스 브라우저 호환성을 보장하기위한 모범 사례에 대해 논의하고 기능 감지, 점진적 향상 및 테스트 방법에 중점을 둡니다.

이 기사는 CSS를 사용한 웹 페이지에 효율적인 PNG 테두리 추가를 보여줍니다. CSS는 JavaScript 또는 라이브러리에 비해 우수한 성능을 제공하며, 미묘하거나 눈에 띄는 효과를 위해 테두리 너비, 스타일 및 색상 조정 방법을 자세히 설명합니다.

이 기사는 HTML & LT; Datalist & GT에 대해 논의합니다. 자동 완성 제안을 제공하고, 사용자 경험을 향상시키고, 오류를 줄임으로써 양식을 향상시키는 요소. 문자 수 : 159

이 기사는 HTML & lt; meter & gt에 대해 설명합니다. 범위 내에 스칼라 또는 분수 값을 표시하는 데 사용되는 요소 및 웹 개발의 일반적인 응용 프로그램. & lt; meter & gt; & lt; Progress & Gt; 그리고 Ex

이 기사는 html5 & lt; time & gt; 시맨틱 날짜/시간 표현 요소. 인간이 읽을 수있는 텍스트와 함께 기계 가독성 (ISO 8601 형식)에 대한 DateTime 속성의 중요성을 강조하여 Accessibilit를 향상시킵니다.

이 기사는 HTML & lt; Progress & Gt에 대해 설명합니다. 요소, 그 목적, 스타일 및 & lt; meter & gt의 차이; 요소. 주요 초점은 & lt; progress & gt; 작업 완료 및 & lt; meter & gt; Stati의 경우
