You need to enable JavaScript to run this app. Magento 2.4站点CSS选择器未区分header导致ul/ol样式异常
优惠活动
大模型
产品
解决方案
定价
更多

Magento 2.4站点CSS选择器未区分header导致ul/ol样式异常

问题背景

  • 调整产品展示模块时使用了全局ul、ol .row选择器,导致头部巨型菜单(Megamenu)的<ol>内容被CMS页面的<ul>标签样式影响,头部内容出现异常放大问题
  • 站点运行环境为 Magento 2.4
  • 现有CSS代码如下:
ul,
ol .col-3 {
  margin-top: 0rem;
  margin-bottom: 1rem;
  font-size: medium;
}

.col-3 {
  text-align: left;
  width: 100%;
  position: relative;
  vertical-align: -webkit-baseline-middle;
  max-width: 100%;
  display: inline-block;
  padding-bottom: 4rem;
}

.col-12 {
  text-align: left;
  width: 100%;
  position: relative;
  vertical-align: -webkit-baseline-middle;
  max-width: 100%;
  display: inline-block;
}

.button {
  transition: all .3s;
  margin: 5px;
  text-decoration: none;
  display: inline-block;
  background: white;
  height: 50%;
  width: 100%;
  line-height: 25px;
  text-align: center;
  color: black;
  margin: auto;
}

.button:hover {
  box-shadow: 40px;
  border: solid;
}

h3 {
  margin-top: 0rem;
  margin-bottom: 2rem;
  position: relative;
  max-width: 100%;
  margin-bottom: 2rem;
  font-weight: bold;
  padding-left: 1.5rem;
}
  • 现有产品模块HTML代码如下:
<div class="row">
  <div class="col-3">
    <div class="col-12">
      <h3 class="size-25"><a href="{{config path=" web/secure/base_url "}}/brands/shepherd-casters/casters/razer-series.html"><span style="color: #007dbd;">Razer</span></a></h3>
      <div class="col-content" style="text-align: center; padding-top: 3%; padding-bottom: 3%;">
        <a href="https://df239c5e7e.nxcli.net/brands/shepherd-casters/casters/razer-series.html"><img src="https://casterdepot.com/media/Shepherd/RZ03TPP090SWTP05.png" alt="" width="200px;" height="250px;"></a>
      </div>
    </div>
    <div class="col-12">
      <div class="col-content" style="text-align: left; padding-top: 3%; padding-bottom: 3%;">
        <ul>
          <li>Dynamic Capacity</li>
          <li>Bearing / Raceways</li>
          <li>Finish</li>
        </ul>
        <a href="https://df239c5e7e7e.nxcli.net/brands/shepherd-casters/casters/razer-series.html">
          <button class="button 3" type="button">Read More</button>
        </a>
      </div>
    </div>
  </div>
</div>

解决方案

Magento 2 默认所有页面主体内容都会包裹在带有.page-main类的容器内,头部区域属于.page-header的后代,不会被.page-main包含,只要给需要限制范围的CSS选择器加上.page-main前缀即可:

  1. 首先修正原选择器的语法错误:原ul, ol .col-3的实际生效规则是「所有ul元素」+「所有ol元素后代中的.col-3元素」,如果你的需求是匹配主体内的所有ul、ol、.col-3元素,修改后的CSS如下:
/* 仅匹配主体内的ul、ol、.col-3元素 */
.page-main ul,
.page-main ol,
.page-main .col-3 {
  margin-top: 0rem;
  margin-bottom: 1rem;
  font-size: medium;
}

.page-main .col-3 {
  text-align: left;
  width: 100%;
  position: relative;
  vertical-align: -webkit-baseline-middle;
  max-width: 100%;
  display: inline-block;
  padding-bottom: 4rem;
}

.page-main .col-12 {
  text-align: left;
  width: 100%;
  position: relative;
  vertical-align: -webkit-baseline-middle;
  max-width: 100%;
  display: inline-block;
}

.page-main .button {
  transition: all .3s;
  margin: 5px;
  text-decoration: none;
  display: inline-block;
  background: white;
  height: 50%;
  width: 100%;
  line-height: 25px;
  text-align: center;
  color: black;
  margin: auto;
}

.page-main .button:hover {
  box-shadow: 40px;
  border: solid;
}

.page-main h3 {
  margin-top: 0rem;
  margin-bottom: 2rem;
  position: relative;
  max-width: 100%;
  margin-bottom: 2rem;
  font-weight: bold;
  padding-left: 1.5rem;
}
  1. 如果你使用的自定义主题修改了主内容容器的类名,可以用浏览器开发者工具(F12)检查主体内容最外层的容器类,替换上面的.page-main即可。

内容的提问来源于stack exchange,提问作者Kameron

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.10 18:09:04