html - HTML 标签可以继承父/封闭标签的类吗?

标签 html css inheritance

有没有办法专门为包装标签内的标签设置样式?例如,我只想设置样式 <td>在“宽”类的表中。

HTML:

<table class="wide">
<tr>
<td>Entry 1</td>
<td>Entry 2</td>
</tr>
<tr>
...
</tr>
</table>

CSS(或类似的东西):

td.wide {
    /* styling */
}

我想要所有的<td> “宽”表格的元素样式化但像 <table class="small"> ... </table> 这样的表格不会有它的 <td>元素样式。我想避免添加 class="wide"<td>所有宽表中的元素。

注意:我不打算专门针对 <table> 执行此操作和 <td>元素。我想知道通常执行此操作的正确方法。该示例可以为所有 <p> 设置样式包含在 <div> 中的元素某个类(class)的。

最佳答案

Can HTML tags inherit the class of a parent/enclosing tag?

没有。类不是继承的。

Is there a way to specifically style only the tags inside a wrapping tag?

A descendant combinator

selector-of-container [space] selector-of-target-within-container

I would like to avoid adding class="wide" to every <td> element in all the wide tables.

table.wide td { /* rules */ }

关于html - HTML 标签可以继承父/封闭标签的类吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19006656/

相关文章:

javascript - CSS 不会出现在 HTML 和 Node 中

javascript - 如何将当前日期的两天添加到隐藏输入的值

html - 修复了在 Safari 中滚动回到顶部时菜单与内容重叠的问题

javascript - jquery 中 ajax 加载的 h1 或 h2 元素的样式不起作用

css - 除了 Web 浏览器之外,CSS 是否在任何地方使用?

javascript - 使 antd 的 Sider 响应式

css - 溢出滚动不适用于绝对定位的元素

c++ - 抽象类的 std::shared_ptr 实例化派生类

c++ - 为什么 pA、pB、pC 不相等?

javascript - 类型错误 - 引用 Javascript 类