如何将样式类应用于td类?

时间:2011-03-15 12:45:14

标签: html css

我想要找到的是在下表中为每个td应用某种样式的正确语法:

<section id="shows">
<!-- HTML5 section tag for the shows 'section' -->

<h2 class="gig">Shows</h2>

<ul class="gig">

    <!-- Start the table -->
    <table>
        <tr>
            <!-- Setup the header row -->
            <th>When</th>
            <th>Where</th>
            <th>Start</th>
            <th>Finish</th>
        </tr>

        <?php
            // some PHP to fetch all the gig entries from the shows table
            $shows_query = "SELECT * FROM shows ORDER BY date ASC";
            $shows = mysql_query($shows_query);
            // a loop to place all the values in the appropriate table cells
            while ($show = mysql_fetch_array($shows)){
            //begin the loop...
            ?>

        <!-- Start the row -->
        <tr>

            <!-- Format the date value from the database and print it: -->
            <td class="when"><?php 
            $date = date("l, F j, Y", strtotime($show['date'])); 
            echo "$date";
            ?></td>

            <td class="venue"><?php
            echo $show['venue'];
            ?></td>

            <!-- Format the the start and end times and print them: -->
            <td class="start"><?php
            $time = date("G:i", strtotime($show['time'])); 
            echo "$time";
            ?></td>

            <td class="finish"><?php
            $until = date("G:i", strtotime($show['until']));
            echo "$until";
            ?></td>

            <!-- Finish this row -->
        </tr>

        <!-- Some space before the next row -->
        <div class="clear"></div>

        <?php 
            // close the loop:
             }
             ?>
        <!-- Finish the table -->
    </table>
</ul>

</section>

我目前的造型是:

#shows table.gig { font-size: 25px; }
#shows td.finish { margin-left: 50px;}

我确实有一个桌子本身,但不确定是否有必要。

字体大小有效,但我无法弄清楚如何将样式应用于td,th,tr元素等。我尝试了几件事,但似乎无法让它工作!

任何帮助都非常感激。

感谢。

8 个答案:

答案 0 :(得分:58)

为表格提供一个类名,然后使用以下内容定位td:

table.classname td {
    font-size: 90%;
}

答案 1 :(得分:8)

如果我记得很清楚,那么您应用于table的某些CSS属性不会按预期继承。因此,您确实应该将样式直接应用于tdtrth元素。

如果您需要为每列添加样式,请使用表格中的<col>元素。

请在此处查看示例:http://jsfiddle.net/GlauberRocha/xkuRA/2/

注意:margin中没有td。请改用padding

答案 2 :(得分:5)

您可以使用:nth-​​child(N)CSS选择器,如:

table td:first-child {}  //1
table td:nth-child(2) {} //2
table td:nth-child(3) {} //3
table td:last-child {}   //4

答案 3 :(得分:4)

试试这个

table tr td.classname
{
 text-align:right;
 padding-right:18%;
}

答案 4 :(得分:3)

定位td更明确的方法是table tr td { }

答案 5 :(得分:2)

table.classname td {
    font-size: 90%;
}

为我工作。感谢。

答案 6 :(得分:0)

当使用反应式引导程序表时,我没有找到

table.classname td {

语法起作用,因为根本没有<table>标记。像这样的模块通常不使用外部标记,而是直接潜入,可能最多使用<thead><tbody>进行分组。

尽管

,但只需指定这样的效果很好
td.classname {
    max-width: 500px;
    text-overflow: initial;
    white-space: wrap;
    word-wrap: break-word;
}

因为它直接覆盖<td>,并且只能用于您想要更改的元素。也许在你的情况下使用

thead.medium td {
  font-size: 40px; 
}
tbody.small td {
  font-size:25px;
}

使用更大的标题进行一致的字体大小调整。

答案 7 :(得分:0)

只需创建一个类名,然后像这样定义你的风格:

table.tdfont td {
  font-size: 0.9em;
}