【问题标题】:How can I format all td elements contained in a table with class myclass in CSS?如何格式化包含在 CSS 中类 myclass 的表中的所有 td 元素?
【发布时间】:2010-03-09 10:06:39
【问题描述】:

如何在 CSS 中使用 myclass 类格式化表中包含的所有 td 元素?

我想要一个适用于<table class="myclass"><td>FORMAT THIS</td></table>. 的格式规则

【问题讨论】:

    标签: html css stylesheet


    【解决方案1】:

    带有descendant combinator(由空格字符表示):

    table.myclass td { — }
    

    【讨论】:

      【解决方案2】:

      这个简单的 css 不会做吗?

      .myclass td{}
      

      【讨论】:

      • 确实如此,但是如果将 myclass 添加到任何其他父元素(例如 div),则 td 将被设置样式。例如<div class="myclass"><table class="blueBorderOnCells"><tr><td>Example</td></tr></table></div> 示例将使用“蓝色边框”和 myclass 中的所有样式设置样式。
      【解决方案3】:
       table.myclass td { color: blue }
      

      顺便说一下,您的表格缺少tr 元素。

      【讨论】:

        【解决方案4】:

        据我所知你会这样做

        table.myclass td { styles in here }
        

        【讨论】:

          【解决方案5】:
          table.myclass td { ... }
          

          【讨论】:

            【解决方案6】:

            ...........

            table.myclass td{
             /* your styles */
            }
            

            Have a look at CSS selectors for more info.

            【讨论】:

              【解决方案7】:

              table#myid td { color: blue } //每个 td 在一个 id 为 myid 的单独表中

              表可以被删除,但我保留它是为了语义。

              #myid.myclass td { color: blue } *//id 为 myid 且类为 myclass 的任何内容中的每个 td。 “是一个” id(myid) “有一个” 类(myclass) ;-) *


              事情变得有趣,尤其是当一个人开始在“桌子”中玩 nth-child(x) 等时!

              这个问题似乎更多的是 "Scope" 的情况,而不是实际分配任何 css。 通过分配一个 idlimit 。即控制 范围 到一个独特的元素,当你打算使用 class 扩展范围到共享一个类的所有元素。 所以理论上你只需要指定 .myclass td{ etc } ,但我只是想确认你的意图和推理,快乐编码。

              但是我同意,请看一下 css 选择器和 jquery( Sizzle)。 这很容易,所以......强大。 :X

              CSS Selectors - INCLUDING THE CSS VERSION EACH NEEDS!

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2020-11-05
                • 2018-12-06
                • 2019-05-10
                • 1970-01-01
                • 2017-05-02
                • 1970-01-01
                • 2023-03-18
                相关资源
                最近更新 更多