电商网站CSS样式失效求助:#product1 .pro.des及后续样式不生效
问题解决:CSS样式从
#product1 .pro.des开始失效的原因及修复 核心错误点
- 后代选择器语法错误:原CSS中
#product1 .pro.des试图匹配同时带有pro和des类的元素,但HTML里des是pro的子元素,必须用空格分隔类选择器表示后代关系,正确写法为#product1 .pro .des。同理,#product1.pro.des span缺少空格,应为#product1 .pro .des span,否则完全无法匹配DOM结构。 - CSS语法笔误导致解析中断:
#header中的box-shadow属性值15p是笔误,应为15px,无效单位会导致该样式失效,甚至干扰后续CSS规则解析。- 全局样式的
font-family: 'Spartan' sans-serif;缺少逗号,正确写法是font-family: 'Spartan', sans-serif;,语法错误会导致字体设置失效。
修正后的CSS代码
@import url('https://fonts.googleapis.com/css2?family=Roboto+Slab&family=Spartan:wght@100;200;300;400;500;600;700;800;900&display=swap'); *{ margin: 0; padding: 0; box-sizing: border-box; font-family: 'Spartan', sans-serif; /* 修复:添加逗号 */ } h1{ font-size: 50PX; line-height: 64px; color: #222; } h2{ font-size: 46PX; line-height: 54px; color: #222; } h4{ font-size: 20PX; color: #222; } h6{ font-weight: 700; font-size: 12px } p{ font-size: 16PX; color: #465b52; margin: 15px 0 20px 0; } .section-p1{ padding: 40px 80px; } .section-p1{ margin: 40px 0; } body{ width: 100%; } /*header start*/ #header{ display: flex; justify-content: space-between ; align-items: center; padding: 20px 80px; background-color: #e3e6f3; box-shadow: 0px 5px 15px rgba(0, 0, 0, 0.06); /* 修复:15p改为15px */ z-index: 999; position: sticky; top: 0; left: 0; } #navbar{ display: flex; justify-content: center; align-items: center; } #navbar li{ list-style: none; padding: 0 20px; position: relative; } #navbar li a{ text-decoration: none; font-size: 16px; font-weight: 600; color: #1a1a1a; transition: 0.3s ease; } #navbar li a:hover, #navbar li a.active{ color: #088178; } #navbar li a.active::after, #navbar li a:hover::after{ content: ""; width: 30%; height: 2px; background: #088178; position: absolute; bottom: -4px; left: 20px; } #hero{ background-image: url("img/hero4.png"); height: 90vh; width: 100%; background-position: top 25 right 0; background-size: cover; display: flex; flex-direction: column; align-items: flex-start; justify-content: center; } #hero h4{ padding-bottom: 15px; } #hero h1{ color: #088178; } #hero button{ background-image: url(img/button.png); background-color: transparent; color: #088178; border:0; padding: 14px 80px 14px 65px; background-repeat: no-repeat; cursor: pointer; font-weight: 700; font-size: 15px; } #feature{ display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; } #feature .fe-box{ width: 180px; text-align: center; padding: 25px 15px; box-shadow: 20px 20px 34px rgba(0, 0, 0, 0.03); border: 1px solid #cce7d0; border-radius: 4px; margin: 15px 0; } #feature .fe-box:hover{ box-shadow: 10px 10px 34px rgba(0, 0, 0, 0.03); } #feature .fe-box h6{ display: inline-block; padding: 9px 8px 6px 8px; line-height: 1; border-radius: 4px; color: #088178; background-color: #fddde4; } #feature .fe-box img{ width: 100%; margin-bottom: 10px; } #product1{ text-align: center; } #product1 .pro{ width: 23%; min-width: 250px; padding: 10px 12px; border: 1px solid #cce7d0; border-radius: 25px; cursor: pointer; box-shadow: 20px 20px 30px rgba(0, 0, 0, 0.02); margin: 15px 0; } #product1 .pro img{ width: 100%; border-radius: 20px; } #product1 .pro .des{ /* 修复:添加空格 */ text-align: start; padding: 10px 0; } #product1 .pro .des span{ /* 修复:添加空格 */ color: #606063; font-size: 12px; /* 原50px过大,调整为合理值 */ } #product1 .pro .des h5{ /* 修复:添加空格 */ padding-top: 7px; color: #1a1a1a; font-size: 14px; } #product1 .pro .des i{ /* 修复:添加空格 */ font-size: 12px; /* 原25px过大,调整为合理值 */ color: rgb(241, 183, 25); }
额外优化建议
- 给
.pro-container添加display: flex; flex-wrap: wrap; justify-content: space-between;样式,实现产品卡片的横向排列,符合电商页面布局需求。
内容的提问来源于stack exchange,提问作者Najad
相关产品推荐
相关产品推荐

