博客列表 >伪类选择器和盒模型

伪类选择器和盒模型

三九三伏
三九三伏原创
2022年07月10日 00:15:01339浏览

一、伪类选择器

1. 位置类伪类选择器

1.1 选择某个元素

  1. HTML示例基础代码如下:
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8">
  6. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  7. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  8. <title></title>
  9. <link rel="stylesheet" href="test.css">
  10. </head>
  11. <body>
  12. <ul class="list">
  13. <li class="first">item1</li>
  14. <li>item2</li>
  15. <li>item3</li>
  16. <li>item4</li>
  17. <li>item5</li>
  18. <li>item6</li>
  19. <li>item7</li>
  20. <li>item8</li>
  21. </ul>
  22. </body>
  23. </html>
  24. test.css文件内容
  25. .list>li:nth-of-type(1){
  26. background-color: blue;
  27. }

效果如下:

1.2 选中一组元素

  1. .list>li:nth-of-type(1n){
  2. background-color: blue;
  3. }

效果如下:

1.3 选择一组中第三个元素后所有元素

  1. .list>li:nth-of-type(1n+3){
  2. background-color: blue;
  3. }

效果如下:

1.4 选择一组中前三个

  1. .list>li:nth-of-type(-1n+3){
  2. background-color: red;
  3. }

效果如下:

1.5 选择一组中的偶数项

  1. .list>li:nth-of-type(2n){
  2. background-color: red;
  3. }
  4. 或者
  5. .list>li:nth-of-type(even){
  6. background-color: red;
  7. }

效果如下:

1.6 选择一组中的奇数项

  1. .list>li:nth-of-type(2n+1){
  2. background-color: red;
  3. }
  4. 或者
  5. .list>li:nth-of-type(odd){
  6. background-color: red;
  7. }

效果如下:

使用2n-1也可以选择奇数项

1.7 选择倒数3个

  1. .list>li:nth-last-of-type(-n+3){
  2. background-color: red;
  3. }

效果如下:

1.8 永远选择第一个和最后一个元素

  1. .list>li:first-of-type{
  2. background-color: red;
  3. }
  4. .list>li:last-of-type{
  5. background-color: red;
  6. }

效果如下:

2. 状态类伪类选择器

HTML示例基础代码如下:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title></title>
  8. <link rel="stylesheet" href="test.css">
  9. </head>
  10. <body>
  11. <form action="">
  12. <fieldset style="width: 512px;">
  13. <legend>用户注册</legend>
  14. <label>用户名<input type="text"></label>
  15. <br/>
  16. <label for="tips">警告:</label>
  17. <input type="text" id="uname" value="一旦注册不能注销" style="border: none;" disabled/>
  18. <div class="gender">
  19. <input type="radio" id="male" name="sex" value="male" checked/>
  20. <label for="male"></label>
  21. <input type="radio" id="female" name="sex" value="female" />
  22. <label for="female"></label>
  23. <br />
  24. <button>提交</button>
  25. </div>
  26. </fieldset>
  27. </form>
  28. </body>
  29. </html>

2.1 获取input文本元素的disable属性

改变其样式

  1. input:disabled{
  2. color: red;
  3. background-color: yellow;
  4. }

效果如下:

2.2 获取input 单选框的选中属性

找到相邻元素,改变其样式。

  1. input:checked +label{
  2. color:red;
  3. }

效果如下:

单选框被选中时,“男”变为红色。

2.3 获取提交按钮的悬停属性

更改按钮样式

  1. button{
  2. height: 30;
  3. border: none;
  4. outline: none;
  5. background-color: seagreen;
  6. color: white;
  7. }

效果如下:

获取悬停属性并改变其样式

  1. button:hover{
  2. cursor: pointer;
  3. background-color: coral;
  4. }

效果如下:

鼠标悬停指针变为小手(受截图限制未看到效果),悬停在按钮上颜色会变为橘红色。

二、 盒子模型常用属性

  1. HTML示例基础代码如下:
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8">
  6. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  7. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  8. <title></title>
  9. <link rel="stylesheet" href="test.css">
  10. </head>
  11. <body>
  12. <div class="box">
  13. </div>
  14. </body>
  15. </html>

来看下一个块元素的盒子模型常用属性,如下图:

块元素占一行,宽度随页面变化自动调整,自动铺满一行。div元素当前外边距、边框、内边距都是没有值的。
当前div盒子高度为0,因此不可见,我们做些设置来使其可见。
调整下宽度和高度,并增加可见属性,如背景颜色。

  1. .box{
  2. width: 300px;
  3. height: 300px;
  4. background-color: coral;
  5. }

增加边框效果

  1. .box{
  2. width: 300px;
  3. height: 300px;
  4. background-color: coral;
  5. border: 5px solid blue;
  6. }

内边距和外边距属于不可见属性,改变不能被直接看到。且不可见属性,只能设置其宽度,不能设置特征(如颜色,实线等)。

  1. .box{
  2. width: 300px;
  3. height: 300px;
  4. background-color: coral;
  5. border: 5px solid blue;
  6. padding: 5px;
  7. margin: 5px;
  8. }

让内边距可见,使用背景裁切,间接实现内边距可见。为了显示效果将内边距调整为20px。

  1. .box{
  2. width: 300px;
  3. height: 300px;
  4. background-color: coral;
  5. background-clip: content-box;
  6. border: 5px solid blue;
  7. padding: 20px;
  8. margin: 5px;
  9. }

让盒子的大小包含边框和内边距

  1. .box{
  2. width: 300px;
  3. height: 300px;
  4. background-color: coral;
  5. background-clip: content-box;
  6. border: 5px solid blue;
  7. padding: 20px;
  8. margin: 5px;
  9. box-sizing: border-box;
  10. }

box-sizing: border-box;即表示让盒子的大小包含边框和内边距,可以看到盒子设置的高和宽都是300.

盒子的大小已经包含了边框和内边距。

使用如下方式单独设置某一条边框

  1. border-top: 10px dashed red;

设置内边距的普通方法

  1. padding-left: 5px;
  2. padding-right: 10px;
  3. padding-top: 7px;
  4. padding-bottom: 3px;

设置内边距的简化方法

简化为四个参数,从左至右为顺时针,分别表示内边距的上右下左。

  1. padding: 5px 7px 10px 3px;

简化为三个参数,分别表示内边距的上 ,右(左),下。

  1. padding: 5px 7px 10px;

简化为两个参数,分别表示内边距的上(下)和 右(左)。

  1. padding: 5px 7px;

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议