【问题标题】:CSS not inheriting parent class propertiesCSS 不继承父类属性
【发布时间】:2016-09-16 16:55:01
【问题描述】:

我有以下 HTML

<div id="borderContainer" class="scViewer"  data-dojo-type="dijit/layout/BorderContainer" data-dojo-props="design:'headline',gutters:false">
  <div id="buttonPagerContentPane" data-dojo-type="dijit/layout/ContentPane" align="center" data-dojo-props="region:'bottom'" class="buttonContentPane">
    <div id="buttonPagerTitle" class="ContentPaneTitle">
      Sheet Selector <br>
    </div>
      <button data-dojo-type="dijit/form/Button" type="button" data-dojo-attach-point="PreviousButtonAttachNode" id="previousButton" class="scViewButtonContent buttonContentPane">
        Previous
      </button>
      <button data-dojo-type="dijit/form/Button" type="button" data-dojo-attach-point="NextButtonAttachNode" id="nextButton" class="scViewButtonContent">
        Next
      </button>
  </div>
</div>

还有以下 CSS:

.scViewer {
  color: #2546ff;
}

.scViewer .buttonContentPane {
  padding: 5px 5px;
  color:#FFFFFF;
  border-radius: 5px;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.4), 0 1px 1px rgba(0, 0, 0, 0.2);
}

.scViewer .ContentPaneTitle{
  color: #2546ff;
  font-weight: bold;
}
.scViewer .buttonContentPane .scViewButtonContent{
  text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.4);
  text-decoration: none;
}

我的问题是两个上一个/下一个按钮不会继承 buttonContentPane 类而没有再次明确定义它,即使它在父 buttonPagerTitle &lt;div&gt; 中。为了证明上面的这一点,我明确定义了 nextButton 而没有buttonContentPane 属性,开发工具中生成的 HTML 不包含定义中的 buttonContentPane,但继承的部分包含 buttonContentPane,其属性显示为灰色:

我的总体目标是对 CSS 代码进行样板化,以便在我的组织内重复使用。我的语法错了吗?我是否不正确地构造了选择器?感谢您的宝贵时间

【问题讨论】:

  • 它们是否在某个 class="scViewer" 的下方?由于您只发布了其余代码的图片,因此我们无法通过ctrl-f 了解它们是否存在。一般提示:代码图片绝对没用。
  • @MarcB 是的,它被声明为根 div,我最初将其排除在外,但它在那里(见编辑)。图片只是为了证明该类确实没有在开发工具中定义。
  • 据我所知,CSS 属性不会被子元素继承。所以 .scViewer .buttonContentPane 只会影响 .buttonContentPane 类的元素。使用 .scViewer .buttonContentPane .scViewButtonContent 显式定义按钮是为其提供所需样式的一种选择,或者您可以尝试使用逗号的 .scViewer .buttonContentPane、.scViewer .buttonContentPane 按钮,这样您就不必编写相同的代码两次。编辑:只有特定的 CSS 样式可以由子级从父级继承(字体系列、文本对齐等)
  • @Benneb10 这些特定的 CSS 可继承元素是什么,它们是否在某处的规范中定义?另外,我不太清楚你的逗号方法是什么意思
  • @Rice 抱歉,我没有回复,如果您仍然感兴趣,这里有一个 stackOverflow 列出了继承的 CSS 属性 stackoverflow.com/questions/5612302/…

标签: html css web dojo


【解决方案1】:

我假设您希望您的“下一个”和“上一个”按钮继承这些属性:

.scViewer .buttonContentPane {
  padding: 5px 5px;
  color:#FFFFFF;
  border-radius: 5px;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.4), 0 1px 1px rgba(0, 0, 0, 0.2);
}

不幸的是(对您而言),并非所有属性都由元素的子/后代继承,也并非所有元素都会从其父/祖先继承。您遇到了这两个问题。

如果您希望按钮的样式正确(正如您在问题中提到的那样),您需要直接将类添加到按钮,或者您需要在 CSS 中编写明确声明按钮应从其父级继承属性。

下面是一个简单的例子,展示了如何明确地告诉一个元素从其父元素继承属性。单击“运行代码 sn-p”以查看生成的按钮。

.wrapper1,
.wrapper2 {
  color:red;
  padding: 20px;
  box-shadow: 0 0 3px rgba(0,0,0,0.4);
  border-radius: 10px;
  margin: 10px;
  width: 100px;
}

.wrapper2 button {
  color: inherit;
  padding: inherit;
  box-shadow: inherit;
  border-radius: inherit;
  border: none;
}
<div class="wrapper1">
  This button doesn't inherit.
  <button>My button</button>
</div>

<div class="wrapper2">
  This button does inherit.
  <button>My button</button>
</div> 

【讨论】:

  • 这非常有用,解决了我的问题。我对从 buttonContentPane 继承的 scViewButtonContent 内部所需的属性使用了“继承”,并且这些属性的工作方式与您的示例一样。再次感谢!
猜你喜欢
  • 1970-01-01
  • 2012-05-03
  • 1970-01-01
  • 1970-01-01
  • 2020-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-02
相关资源
最近更新 更多