详解MySQL 索引+explain

mysql视频教程栏目今天着重介绍索引+explain,为需要面试的准备。

详解MySQL 索引+explain

免费推荐:mysql视频教程

一、索引的介绍

在mysql中,索引就是数据结构,已经在文件中按照索引进行排序好的结构.使用索引可以加快我们的查询速度,但是对我们的数据增删改效率会降低.因为一个网站大部分都是查询,我们主要优化select语句.

二、MySQL中索引的分类

普通索引 key唯一索引 unique key unique key 别名 别名可忽略 别名可忽略主键索引 primary key(字段)全文索引myisam引擎支持(只对英文进行索引,mysql版本5.6也支持),sphinx(中文搜索)混合索引 多个字段组成的索引.如 key key_index(title,email)

三、索引的基本操作

1、给表添加索引

create table t_index(    id int not null auto_increment,    title varchar(30) not null default '',    email varchar(30) not null default '',    primary key(id),    unique key uni_email(email) ,    key key_title(title))engine=innodb charset=utf8;

查看表

desc tablename

mysql> desc t_index;+-------+-------------+------+-----+---------+----------------+| Field | Type        | Null | Key | Default | Extra          |+-------+-------------+------+-----+---------+----------------+| id    | int(11)     | NO   | PRI | NULL    | auto_increment || title | varchar(30) | NO   | MUL |         |                || email | varchar(30) | NO   | UNI |         |                |+-------+-------------+------+-----+---------+----------------+3 rows in set (0.01 sec)

查看表的创建语句

show create table tbalename/G

mysql> show create table t_index/G;ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '/G' at line 1mysql> show create table t_indexG;*************************** 1. row ***************************       Table: t_indexCreate Table: CREATE TABLE `t_index` (  `id` int(11) NOT NULL AUTO_INCREMENT,  `title` varchar(30) NOT NULL DEFAULT '',  `email` varchar(30) NOT NULL DEFAULT '',  PRIMARY KEY (`id`),  UNIQUE KEY `uni_email` (`email`),  KEY `key_title` (`title`)) ENGINE=InnoDB DEFAULT CHARSET=utf81 row in set (0.00 sec)ERROR: No query specified

2、删除索引

删除主键索引

alter table table_name drop primary key;

注意:

mysql> alter table t_index drop primary key;ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key

主键不一定是自增长,但是自增长一定是主键。

删除逐渐之前先要把主键索引的自增长去掉。

mysql> alter table t_index modify  id int not null;Query OK, 0 rows affected (0.05 sec)Records: 0  Duplicates: 0  Warnings: 0

再来删除主键

mysql> alter table t_index drop primary key;Query OK, 0 rows affected (0.04 sec)Records: 0  Duplicates: 0  Warnings: 0
删除普通和唯一的索引

alter table table_name drop key ‘索引的别名’

实际操作

mysql> alter table t_index drop key uni_email;Query OK, 0 rows affected (0.03 sec)Records: 0  Duplicates: 0  Warnings: 0
mysql> alter table t_index drop key key_title;Query OK, 0 rows affected (0.02 sec)Records: 0  Duplicates: 0  Warnings: 0

3、添加索引

alter table t_index add key key_title(title);alter table t_index add key uni_email(email);alter table t_index add primary key(id);

4、有无索引对比

create table article(id int not null auto_increment,no_index int,title varchar(30) not null default '',add_time datetime,primary key(id));

插入数据

mysql> insert into article(id,title,add_time) values(null,'ddsd1212123d',now());mysql> insert into article(title,add_time) select title,now() from article;Query OK, 10 rows affected (0.01 sec)Records: 10  Duplicates: 0  Warnings: 0mysql> update article set no_index=id;

有无索引查询数据对比

mysql> select * from article where no_index=1495298;+---------+----------+-----------+---------------------+| id      | no_index | title     | add_time            |+---------+----------+-----------+---------------------+| 1495298 |  1495298 | ddsd1123d | 2019-05-15 23:13:56 |+---------+----------+-----------+---------------------+1 row in set (0.28 sec)
mysql> select * from article where id=1495298;+---------+----------+-----------+---------------------+| id      | no_index | title     | add_time            |+---------+----------+-----------+---------------------+| 1495298 |  1495298 | ddsd1123d | 2019-05-15 23:13:56 |+---------+----------+-----------+---------------------+1 row in set (0.01 sec)

表结构

mysql> show create table articleG;*************************** 1. row ***************************       Table: articleCreate Table: CREATE TABLE `article` (  `id` int(11) NOT NULL AUTO_INCREMENT,  `no_index` int(11) DEFAULT NULL,  `title` varchar(30) NOT NULL DEFAULT '',  `add_time` datetime DEFAULT NULL,  PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=1572824 DEFAULT CHARSET=utf81 row in set (0.00 sec)ERROR: No query specified

四、explain分析

使用explain可以对sql语句进行分析到底有没有使用到索引查询,从而更好的优化它.

我们只需要在select语句前面加上一句explain或者desc.

1、语法

explain|desc select * from tablename G;

2、分析

用刚才的两个有无索引对比看看

mysql> mysql> explain select * from article where no_index=1495298G;*************************** 1. row ***************************           id: 1  select_type: SIMPLE//单表查询        table: article//查询的表名   partitions: NULL         type: ALL//索引的类型,从好到坏的情况是:system>const>range>index>Allpossible_keys: NULL//可能使用到的索引          key: NULL//实际使用到的索引      key_len: NULL//索引的长度          ref: NULL         rows: 1307580//可能进行扫描表的行数     filtered: 10.00        Extra: Using where1 row in set, 1 warning (0.00 sec)ERROR: No query specified
mysql> explain select * from article where id=1495298G;*************************** 1. row ***************************           id: 1  select_type: SIMPLE        table: article   partitions: NULL         type: const//当对主键索引进行等值查询的时候出现constpossible_keys: PRIMARY          key: PRIMARY//实际使用到的所有primary索引      key_len: 4//索引的长度4 = int占4个字节          ref: const         rows: 1//所扫描的行数只有一行     filtered: 100.00        Extra: NULL1 row in set, 1 warning (0.00 sec)ERROR: No query specified

3、explain的type项分析

type项从优到差依次排序:

system:一般系统表只有一行记录的时候才会出现const:当对主键值进行等值查询的时候会出现,如where id=666666range:当对索引的值进行范围查询的时候会出现,如 where id<100000index:当我们查询的字段恰好是我们索引文件中的值,就会出现All:最差的一种情况,需要避免.

实际测试

mysql> use mysql;mysql> explain select * from userG;*************************** 1. row ***************************           id: 1  select_type: SIMPLE        table: user   partitions: NULL         type: ALLpossible_keys: NULL          key: NULL      key_len: NULL          ref: NULL         rows: 3     filtered: 100.00        Extra: NULL1 row in set, 1 warning (0.00 sec)
mysql> use test;mysql> explain select * from article where id=666666G;*************************** 1. row ***************************           id: 1  select_type: SIMPLE        table: article   partitions: NULL         type: constpossible_keys: PRIMARY          key: PRIMARY      key_len: 4          ref: const         rows: 1     filtered: 100.00        Extra: NULL
mysql> explain select * from article where id>666666G;mysql> explain select * from article where id<666666G;
mysql> explain select id  from article G;*************************** 1. row ***************************           id: 1  select_type: SIMPLE        table: article   partitions: NULL         type: indexpossible_keys: NULL          key: PRIMARY      key_len: 4          ref: NULL         rows: 1307580     filtered: 100.00        Extra: Using index1 row in set, 1 warning (0.00 sec)ERROR: No query specified

如果查询的字段在索引文件存在,那么就会直接从索引文件中进行查询,我们把这种查询称之为索引覆盖查询。

出现all,我们需要避免,因为进行全面扫描。

对于出现all的,可以给该字段增加普通索引查询

mysql> alter table article add key key_no_index(no_index);Query OK, 0 rows affected (1.92 sec)Records: 0  Duplicates: 0  Warnings: 0type为ref,应该是关联,但是ref是constmysql> explain select * from article where no_index=666666G;*************************** 1. row ***************************           id: 1  select_type: SIMPLE        table: article   partitions: NULL         type: refpossible_keys: key_no_index          key: key_no_index      key_len: 5          ref: const         rows: 1     filtered: 100.00        Extra: NULL1 row in set, 1 warning (0.00 sec)速度飞跃mysql> select * from article where no_index=666666;+--------+----------+-----------+---------------------+| id     | no_index | title     | add_time            |+--------+----------+-----------+---------------------+| 666666 |   666666 | ddsd1123d | 2019-05-15 23:13:55 |+--------+----------+-----------+---------------------+1 row in set (0.00 sec)

4、使用索引的场景

1、 经常出现在where后面的字段,我们需要给他加索引
2、order by 语句使用索引的优化
mysql> explain select * from article order by idG;*************************** 1. row ***************************           id: 1  select_type: SIMPLE        table: article   partitions: NULL         type: indexpossible_keys: NULL          key: PRIMARY      key_len: 4          ref: NULL         rows: 1307580     filtered: 100.00        Extra: NULL1 row in set, 1 warning (0.00 sec)ERROR: No query specifiedmysql> explain select * from article where id >0  order by idG;*************************** 1. row ***************************           id: 1  select_type: SIMPLE        table: article   partitions: NULL         type: rangepossible_keys: PRIMARY          key: PRIMARY      key_len: 4          ref: NULL         rows: 653790     filtered: 100.00        Extra: Using where1 row in set, 1 warning (0.01 sec)ERROR: No query specified

可以看出,即使是使用了索引但是几乎还是全表扫描。

加了where就少了一半

3、针对like的模糊查询索引的优化

where title like ‘%keyword%’ ====>全表扫描

where title like ‘keyword%’ ===>会使用到索引查询

给title加上铺索引

Sveil开源商城 Sveil开源商城

Sveil开源商城是专业和创新的开源在线购物车的解决方案,是基于osCommerce 3 alpha 5 独立开发的项目。环境为PHP+MYSQL,使用了先进的AJAX技术和富互联网应用(RIA)的框架ExtJS,由Sveil.com提供重要的可用性改善及与网站交互界面速度更快,更高效。VERSION 1.0–修复bug1、网站在维护2、当搜索引擎被激活,与我们联系功能不起作用。3、当SEO被激

Sveil开源商城 6 查看详情 Sveil开源商城

mysql> alter table article  add key key_index(title);Query OK, 0 rows affected (2.16 sec)Records: 0  Duplicates: 0  Warnings: 0mysql> show create table articleG;*************************** 1. row ***************************       Table: articleCreate Table: CREATE TABLE `article` (  `id` int(11) NOT NULL AUTO_INCREMENT,  `no_index` int(11) DEFAULT NULL,  `title` varchar(30) NOT NULL DEFAULT '',  `add_time` datetime DEFAULT NULL,  PRIMARY KEY (`id`),  KEY `key_no_index` (`no_index`),  KEY `key_index` (`title`)) ENGINE=InnoDB AUTO_INCREMENT=1507299 DEFAULT CHARSET=utf81 row in set (0.00 sec)

因为%没有出现在like关键字查询的最左边,所以可以使用到索引查询

只要是like左边出现了%,就是全表查询

mysql> explain select * from article where title like 'a%'G;*************************** 1. row ***************************           id: 1  select_type: SIMPLE        table: article   partitions: NULL         type: range//范围查询possible_keys: key_index          key: key_index      key_len: 92//          ref: NULL         rows: 1     filtered: 100.00        Extra: Using index condition1 row in set, 1 warning (0.00 sec)mysql> explain select * from article where title like '%a%'G;*************************** 1. row ***************************           id: 1  select_type: SIMPLE        table: article   partitions: NULL         type: ALL//全表查询possible_keys: NULL          key: NULL      key_len: NULL          ref: NULL         rows: 1307580     filtered: 11.11        Extra: Using where1 row in set, 1 warning (0.00 sec)
4、limit语句的索引使用优化

针对于limit语句的优化,我们可以在它前面加order by 索引字段

如果order by的字段是索引,会先去索引文件中查找指定行数的数据

mysql> explain select sql_no_cache  * from article limit 90000,10 G;*************************** 1. row ***************************           id: 1  select_type: SIMPLE        table: article   partitions: NULL         type: ALL//全表possible_keys: NULL          key: NULL      key_len: NULL          ref: NULL         rows: 1307580     filtered: 100.00        Extra: NULL1 row in set, 2 warnings (0.00 sec)ERROR: No query specifiedmysql> explain select sql_no_cache  * from article order by id  limit 90000,10 G;*************************** 1. row ***************************           id: 1  select_type: SIMPLE        table: article   partitions: NULL         type: indexpossible_keys: NULL          key: PRIMARY//使用到了索引      key_len: 4          ref: NULL         rows: 90010     filtered: 100.00        Extra: NULL1 row in set, 2 warnings (0.00 sec)ERROR: No query specified

另外一种针对于limit的优化方法:

索引覆盖+延时关联

原理:主要利用索引覆盖查询,把覆盖索引查询返回的id作为与我们要查询记录的id进行相关联,

mysql> select sql_no_cache  * from article limit 1000000,10;+---------+----------+----------------+---------------------+| id      | no_index | title          | add_time            |+---------+----------+----------------+---------------------+| 1196579 |  1196579 | ddsd12123123ad | 2019-05-15 23:13:56 || 1196580 |  1196580 | ddsd121231ad   | 2019-05-15 23:13:56 || 1196581 |  1196581 | ddsd1212123d   | 2019-05-15 23:13:56 || 1196582 |  1196582 | ddsd1123123d   | 2019-05-15 23:13:56 || 1196583 |  1196583 | ddsd1123d      | 2019-05-15 23:13:56 || 1196584 |  1196584 | ddsd1123d      | 2019-05-15 23:13:56 || 1196585 |  1196585 | ddsd1123d      | 2019-05-15 23:13:56 || 1196586 |  1196586 | ddsd1123d      | 2019-05-15 23:13:56 || 1196587 |  1196587 | ddsd1123d      | 2019-05-15 23:13:56 || 1196588 |  1196588 | ddsd1123d      | 2019-05-15 23:13:56 |+---------+----------+----------------+---------------------+10 rows in set, 1 warning (0.21 sec)mysql> select t1.* from article as t1 inner join (select id as pid from article  limit 10000,10) as t2 on t1.id=t2.pid;+-------+----------+----------------+---------------------+| id    | no_index | title          | add_time            |+-------+----------+----------------+---------------------+| 13058 |    13058 | ddsd12123123ad | 2019-05-15 23:13:49 || 13059 |    13059 | ddsd121231ad   | 2019-05-15 23:13:49 || 13060 |    13060 | ddsd1212123d   | 2019-05-15 23:13:49 || 13061 |    13061 | ddsd1123123d   | 2019-05-15 23:13:49 || 13062 |    13062 | ddsd1123d      | 2019-05-15 23:13:49 || 13063 |    13063 | ddsd1123d      | 2019-05-15 23:13:49 || 13064 |    13064 | ddsd1123d      | 2019-05-15 23:13:49 || 13065 |    13065 | ddsd1123d      | 2019-05-15 23:13:49 || 13066 |    13066 | ddsd1123d      | 2019-05-15 23:13:49 || 13067 |    13067 | ddsd1123d      | 2019-05-15 23:13:49 |+-------+----------+----------------+---------------------+10 rows in set (0.00 sec)
5、复合(多列)索引的最左原则(面试经常问)

只要查询的时候出现复合索引的最左边的字段才会使用到索引查询

把article表的no_index和title建立复合索引:

//给no_index和title创建一个复合索引mysql> alter table article add key index_no_index_title(no_index,title);Query OK, 0 rows affected (1.18 sec)Records: 0  Duplicates: 0  Warnings: 0//查看创建后的结构mysql> show create table articleG;*************************** 1. row ***************************       Table: articleCreate Table: CREATE TABLE `article` (  `id` int(11) NOT NULL AUTO_INCREMENT,  `no_index` int(11) DEFAULT NULL,  `title` varchar(30) NOT NULL DEFAULT '',  `add_time` datetime DEFAULT NULL,  PRIMARY KEY (`id`),  KEY `key_no_index` (`no_index`),  KEY `key_index` (`title`),  KEY `index_no_index_title` (`no_index`,`title`)) ENGINE=InnoDB AUTO_INCREMENT=1507299 DEFAULT CHARSET=utf81 row in set (0.00 sec)//删除no_index和title的索引mysql> alter table article drop key key_index;Query OK, 0 rows affected (0.05 sec)Records: 0  Duplicates: 0  Warnings: 0mysql> alter table article drop key key_no_index;Query OK, 0 rows affected (0.03 sec)Records: 0  Duplicates: 0  Warnings: 0mysql> show create table articleG;*************************** 1. row ***************************       Table: articleCreate Table: CREATE TABLE `article` (  `id` int(11) NOT NULL AUTO_INCREMENT,  `no_index` int(11) DEFAULT NULL,  `title` varchar(30) NOT NULL DEFAULT '',  `add_time` datetime DEFAULT NULL,  PRIMARY KEY (`id`),  KEY `index_no_index_title` (`no_index`,`title`)) ENGINE=InnoDB AUTO_INCREMENT=1507299 DEFAULT CHARSET=utf81 row in set (0.00 sec)//复合索引使用情况mysql> explain select * from article where title='ddsd1123d' and no_index=77777G;*************************** 1. row ***************************           id: 1  select_type: SIMPLE        table: article   partitions: NULL         type: refpossible_keys: index_no_index_title          key: index_no_index_title      key_len: 97          ref: const,const         rows: 1     filtered: 100.00        Extra: NULL1 row in set, 1 warning (0.00 sec)mysql> explain select * from article where  no_index=77777G; *************************** 1. row ***************************           id: 1  select_type: SIMPLE        table: article   partitions: NULL         type: refpossible_keys: index_no_index_title          key: index_no_index_title      key_len: 5          ref: const         rows: 1     filtered: 100.00        Extra: NULL1 row in set, 1 warning (0.00 sec)

五、慢查询日志

1、介绍

我们可以定义(程序员)一个sql语句执行的最大执行时间,如果发现某条sql语句的执行时间超过我们所规定的时间界限,那么这条sql就会被记录下来.

2、慢查询具体操作

先开启慢日志查询

查看慢日志配置

mysql> show variables like '%slow_query%';+---------------------+--------------------------------------------------+| Variable_name       | Value                                            |+---------------------+--------------------------------------------------+| slow_query_log      | OFF                                              || slow_query_log_file | /usr/local/mysql/data/caredeMacBook-Pro-slow.log |+---------------------+--------------------------------------------------+2 rows in set (0.00 sec)

开启慢日志查询

mysql> set global slow_query_log=on;Query OK, 0 rows affected (0.00 sec)

再次检查慢日志配置

mysql> show variables like '%slow_query%';+---------------------+--------------------------------------------------+| Variable_name       | Value                                            |+---------------------+--------------------------------------------------+| slow_query_log      | ON                                               || slow_query_log_file | /usr/local/mysql/data/caredeMacBook-Pro-slow.log |+---------------------+--------------------------------------------------+2 rows in set (0.00 sec)

去mysql配置文件my.ini中指定sql语句的界限时间和慢日志文件的路径

慢日志的名称,默认保存在mysql目录下面的data目录下面

log-slow-queries = 'man.txt'

设置一个界限时间

long-query-time=5

重启

六、profile工具

1、介绍

通过profile工具分析一条sql语句的时间消耗在哪里

2、具体操作

开启profile

执行一条SQL,(开启之后执行的所有SQL语句都会被记录下来

,以查看某条sql语句的具体执行时间耗费哪里)

根据query_id查找到具体的SQL

实例:

//查看profile设置mysql> show variables like '%profil%';+------------------------+-------+| Variable_name          | Value |+------------------------+-------+| have_profiling         | YES   || profiling              | OFF   |//未开启状态| profiling_history_size | 15    |+------------------------+-------+3 rows in set (0.00 sec)//开启操作mysql> set profiling = on;Query OK, 0 rows affected, 1 warning (0.00 sec)//查看是否开启成功mysql> show variables like '%profil%';+------------------------+-------+| Variable_name          | Value |+------------------------+-------+| have_profiling         | YES   || profiling              | ON    |//开启成功| profiling_history_size | 15    |+------------------------+-------+3 rows in set (0.00 sec)

具体查询

mysql> select * from article where no_index=666666;+--------+----------+-----------+---------------------+| id     | no_index | title     | add_time            |+--------+----------+-----------+---------------------+| 666666 |   666666 | ddsd1123d | 2019-05-15 23:13:55 |+--------+----------+-----------+---------------------+1 row in set (0.02 sec)mysql> show profiles;+----------+------------+---------------------------------------------+| Query_ID | Duration   | Query                                       |+----------+------------+---------------------------------------------+|        1 | 0.00150700 | show variables like '%profil%'              ||        2 | 0.01481100 | select * from article where no_index=666666 |+----------+------------+---------------------------------------------+2 rows in set, 1 warning (0.00 sec)mysql> show profile for query 2;+----------------------+----------+| Status               | Duration |+----------------------+----------+| starting             | 0.000291 || checking permissions | 0.000007 || Opening tables       | 0.012663 |//打开表| init                 | 0.000050 || System lock          | 0.000009 || optimizing           | 0.000053 || statistics           | 0.001566 || preparing            | 0.000015 || executing            | 0.000002 || Sending data         | 0.000091 |//磁盘上的发送数据| end                  | 0.000004 || query end            | 0.000007 || closing tables       | 0.000006 || freeing items        | 0.000037 || cleaning up          | 0.000010 |+----------------------+----------+15 rows in set, 1 warning (0.01 sec)

以上就是详解MySQL 索引+explain的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/778030.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年11月26日 08:24:08
下一篇 2025年11月26日 08:29:33

相关推荐

  • Uniapp 中如何不拉伸不裁剪地展示图片?

    灵活展示图片:如何不拉伸不裁剪 在界面设计中,常常需要以原尺寸展示用户上传的图片。本文将介绍一种在 uniapp 框架中实现该功能的简单方法。 对于不同尺寸的图片,可以采用以下处理方式: 极端宽高比:撑满屏幕宽度或高度,再等比缩放居中。非极端宽高比:居中显示,若能撑满则撑满。 然而,如果需要不拉伸不…

    2025年12月24日
    400
  • 如何让小说网站控制台显示乱码,同时网页内容正常显示?

    如何在不影响用户界面的情况下实现控制台乱码? 当在小说网站上下载小说时,大家可能会遇到一个问题:网站上的文本在网页内正常显示,但是在控制台中却是乱码。如何实现此类操作,从而在不影响用户界面(UI)的情况下保持控制台乱码呢? 答案在于使用自定义字体。网站可以通过在服务器端配置自定义字体,并通过在客户端…

    2025年12月24日
    800
  • 如何在地图上轻松创建气泡信息框?

    地图上气泡信息框的巧妙生成 地图上气泡信息框是一种常用的交互功能,它简便易用,能够为用户提供额外信息。本文将探讨如何借助地图库的功能轻松创建这一功能。 利用地图库的原生功能 大多数地图库,如高德地图,都提供了现成的信息窗体和右键菜单功能。这些功能可以通过以下途径实现: 高德地图 JS API 参考文…

    2025年12月24日
    400
  • 如何使用 scroll-behavior 属性实现元素scrollLeft变化时的平滑动画?

    如何实现元素scrollleft变化时的平滑动画效果? 在许多网页应用中,滚动容器的水平滚动条(scrollleft)需要频繁使用。为了让滚动动作更加自然,你希望给scrollleft的变化添加动画效果。 解决方案:scroll-behavior 属性 要实现scrollleft变化时的平滑动画效果…

    2025年12月24日
    000
  • 如何为滚动元素添加平滑过渡,使滚动条滑动时更自然流畅?

    给滚动元素平滑过渡 如何在滚动条属性(scrollleft)发生改变时为元素添加平滑的过渡效果? 解决方案:scroll-behavior 属性 为滚动容器设置 scroll-behavior 属性可以实现平滑滚动。 html 代码: click the button to slide right!…

    2025年12月24日
    500
  • 如何选择元素个数不固定的指定类名子元素?

    灵活选择元素个数不固定的指定类名子元素 在网页布局中,有时需要选择特定类名的子元素,但这些元素的数量并不固定。例如,下面这段 html 代码中,activebar 和 item 元素的数量均不固定: *n *n 如果需要选择第一个 item元素,可以使用 css 选择器 :nth-child()。该…

    2025年12月24日
    200
  • 使用 SVG 如何实现自定义宽度、间距和半径的虚线边框?

    使用 svg 实现自定义虚线边框 如何实现一个具有自定义宽度、间距和半径的虚线边框是一个常见的前端开发问题。传统的解决方案通常涉及使用 border-image 引入切片图片,但是这种方法存在引入外部资源、性能低下的缺点。 为了避免上述问题,可以使用 svg(可缩放矢量图形)来创建纯代码实现。一种方…

    2025年12月24日
    100
  • 如何让“元素跟随文本高度,而不是撑高父容器?

    如何让 元素跟随文本高度,而不是撑高父容器 在页面布局中,经常遇到父容器高度被子元素撑开的问题。在图例所示的案例中,父容器被较高的图片撑开,而文本的高度没有被考虑。本问答将提供纯css解决方案,让图片跟随文本高度,确保父容器的高度不会被图片影响。 解决方法 为了解决这个问题,需要将图片从文档流中脱离…

    2025年12月24日
    000
  • 为什么 CSS mask 属性未请求指定图片?

    解决 css mask 属性未请求图片的问题 在使用 css mask 属性时,指定了图片地址,但网络面板显示未请求获取该图片,这可能是由于浏览器兼容性问题造成的。 问题 如下代码所示: 立即学习“前端免费学习笔记(深入)”; icon [data-icon=”cloud”] { –icon-cl…

    2025年12月24日
    200
  • 如何利用 CSS 选中激活标签并影响相邻元素的样式?

    如何利用 css 选中激活标签并影响相邻元素? 为了实现激活标签影响相邻元素的样式需求,可以通过 :has 选择器来实现。以下是如何具体操作: 对于激活标签相邻后的元素,可以在 css 中使用以下代码进行设置: li:has(+li.active) { border-radius: 0 0 10px…

    2025年12月24日
    100
  • 如何模拟Windows 10 设置界面中的鼠标悬浮放大效果?

    win10设置界面的鼠标移动显示周边的样式(探照灯效果)的实现方式 在windows设置界面的鼠标悬浮效果中,光标周围会显示一个放大区域。在前端开发中,可以通过多种方式实现类似的效果。 使用css 使用css的transform和box-shadow属性。通过将transform: scale(1.…

    2025年12月24日
    200
  • 为什么我的 Safari 自定义样式表在百度页面上失效了?

    为什么在 Safari 中自定义样式表未能正常工作? 在 Safari 的偏好设置中设置自定义样式表后,您对其进行测试却发现效果不同。在您自己的网页中,样式有效,而在百度页面中却失效。 造成这种情况的原因是,第一个访问的项目使用了文件协议,可以访问本地目录中的图片文件。而第二个访问的百度使用了 ht…

    2025年12月24日
    000
  • 如何用前端实现 Windows 10 设置界面的鼠标移动探照灯效果?

    如何在前端实现 Windows 10 设置界面中的鼠标移动探照灯效果 想要在前端开发中实现 Windows 10 设置界面中类似的鼠标移动探照灯效果,可以通过以下途径: CSS 解决方案 DEMO 1: Windows 10 网格悬停效果:https://codepen.io/tr4553r7/pe…

    2025年12月24日
    000
  • 使用CSS mask属性指定图片URL时,为什么浏览器无法加载图片?

    css mask属性未能加载图片的解决方法 使用css mask属性指定图片url时,如示例中所示: mask: url(“https://api.iconify.design/mdi:apple-icloud.svg”) center / contain no-repeat; 但是,在网络面板中却…

    2025年12月24日
    000
  • 如何用CSS Paint API为网页元素添加时尚的斑马线边框?

    为元素添加时尚的斑马线边框 在网页设计中,有时我们需要添加时尚的边框来提升元素的视觉效果。其中,斑马线边框是一种既醒目又别致的设计元素。 实现斜向斑马线边框 要实现斜向斑马线间隔圆环,我们可以使用css paint api。该api提供了强大的功能,可以让我们在元素上绘制复杂的图形。 立即学习“前端…

    2025年12月24日
    000
  • 图片如何不撑高父容器?

    如何让图片不撑高父容器? 当父容器包含不同高度的子元素时,父容器的高度通常会被最高元素撑开。如果你希望父容器的高度由文本内容撑开,避免图片对其产生影响,可以通过以下 css 解决方法: 绝对定位元素: .child-image { position: absolute; top: 0; left: …

    2025年12月24日
    000
  • CSS 帮助

    我正在尝试将文本附加到棕色框的左侧。我不能。我不知道代码有什么问题。请帮助我。 css .hero { position: relative; bottom: 80px; display: flex; justify-content: left; align-items: start; color:…

    2025年12月24日 好文分享
    200
  • 前端代码辅助工具:如何选择最可靠的AI工具?

    前端代码辅助工具:可靠性探讨 对于前端工程师来说,在HTML、CSS和JavaScript开发中借助AI工具是司空见惯的事情。然而,并非所有工具都能提供同等的可靠性。 个性化需求 关于哪个AI工具最可靠,这个问题没有一刀切的答案。每个人的使用习惯和项目需求各不相同。以下是一些影响选择的重要因素: 立…

    2025年12月24日
    300
  • 如何用 CSS Paint API 实现倾斜的斑马线间隔圆环?

    实现斑马线边框样式:探究 css paint api 本文将探究如何使用 css paint api 实现倾斜的斑马线间隔圆环。 问题: 给定一个有多个圆圈组成的斑马线图案,如何使用 css 实现倾斜的斑马线间隔圆环? 答案: 立即学习“前端免费学习笔记(深入)”; 使用 css paint api…

    2025年12月24日
    000
  • 如何使用CSS Paint API实现倾斜斑马线间隔圆环边框?

    css实现斑马线边框样式 想定制一个带有倾斜斑马线间隔圆环的边框?现在使用css paint api,定制任何样式都轻而易举。 css paint api 这是一个新的css特性,允许开发人员创建自定义形状和图案,其中包括斑马线样式。 立即学习“前端免费学习笔记(深入)”; 实现倾斜斑马线间隔圆环 …

    2025年12月24日
    100

发表回复

登录后才能评论
关注微信