CSS修改未在网页生效,但浏览器控制台修改有效,求排查原因
问题
本地修改CSS文件后,修改内容未在网页上显示;但在浏览器开发者工具控制台中应用相同修改时,样式可正常生效。相关代码如下:
CSS代码
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@100;200;300;400;500;600&display=swap'); :root{ --main-color: #8e44ad; --black: #222; --white: #fff; --light-black: #777; --light-white: #fff9; --dark-bg:rgba(0,0,.7); --light-bg: #eee; --border: .1rem solid var(--black); --box-shadow:0 .5rem 1rem rgba(0,0,0,.1); --text-shadow:0 1.5rem 3rem rgba(0,0,0,.3); } *{ font-family: 'Poppins', sans-serif; margin: 0; padding: 0; box-sizing: border-box; outline: none; border: none; text-decoration: none; text-transform: capitalize; } html{ font-size: 62.5%; overflow-x: hidden; } html::-webkit-scrollbar{ width: 1rem; } html::-webkit-scrollbar-track{ background-color: var(--white); } html::-webkit-scrollbar-thumb{ background-color: var(--main-color); } section{ padding: 5rem 10%; } .packages .box-container{ display: grid; grid-template-columns: repeat(auto-fit, minmax(30rem, 1fr)); gap: 2rem; } .packages .box-container .box{ border: var(--black); box-shadow: var(--box-shadow); background: var(--white); } .packages .box-container .box .image{ height: 25rem; overflow: hidden; } .packages .box-container .box .image img{ height: 100%; width: 100%; object-fit: cover; } .packages .box-container .box .content{ padding: 2rem; text-align: center; } .packages .box-container .box .content h3{ font-size: 2.5rem; color: var(--black); } .packages .box-container .box .content p{ font-size: 1.5rem; color: var(--light-black); line-height: 2; padding: 1rem 0; } .packages .load-more{ text-align: center; margin-top: 2rem; }
HTML代码
<?php include 'config.php'; ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>home</title> <!--swiper css link--> <link rel="stylesheet" href="css/swiper-bundle.min.css"> <!-- font awesome cdn link--> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css"> <!-- custom css file link--> <link rel="stylesheet" href="css/style.css?v=<?=$version?>"> </head> <body> <section class="packeges"> <h1 class="heading-title">our packages</h1> <div class="box-container"> <div class="box"> <div class="image"> <img src="images/img-1.jpg" alt=""> </div> <div class="content"> <h3>adventure & tour</h3> <p>Do some adventures and go to tours and enjoy yourself</p> <a href="book.php" class="btn">book now</a> </div> </div> <!-- 其余box内容省略 --> </div> <div class="load-more"><a href="package.php" class="btn"> load more</a></div> </section> <!--swiper js link --> <script src="https://unpkg.com/swiper@7/swiper-bundle.min.js"></script> <!-- custom js file link--> <script src="js/script.js?v=<?=$version?>"></script> </body> </html>
问题原因与解决办法
核心问题是类名拼写不一致:
- HTML中section的class为
packeges(单词多了一个字母e) - CSS中使用的选择器为
.packages(单词正确拼写)
由于选择器与DOM元素的类名不匹配,本地CSS的样式无法作用到目标元素;而在开发者工具中修改时,是直接选中了class为packeges的DOM元素应用样式,因此能正常生效。
解决办法二选一:
- 方案一:修正HTML中的类名,将
<section class="packeges">改为<section class="packages">(推荐,保持单词正确拼写) - 方案二:修正CSS中的选择器,把所有
.packages替换为.packeges
内容的提问来源于stack exchange,提问作者user22148437
相关产品推荐
相关产品推荐

