css 如何在第一列中应用文本左对齐,在其他列中应用文本右对齐

svujldwt  于 2023-03-14  发布在  其他
关注(0)|答案(3)|浏览(181)

我有一个要设置样式的表格。我如何只对第一列应用text-align:left;,而对其他列应用text-align:right;
下面是我的表格示例:
http://jsfiddle.net/gianlucaguarini/hAv7P/
此时,所有列的文本对齐都是左对齐。

kninwzqo

kninwzqo1#

你可能想看看:first-child伪类(W3C定义)--这正是它设计的目的,你可以这样使用它:

.table td {
    text-align: right;
}

.table td:first-child {
    text-align: left;
}​

:first-child伪类意味着选择器只匹配每个<tr>中的第一个<td>
示例如下:http://jsfiddle.net/xv5Cn/1/

1tuwyuhd

1tuwyuhd2#

您需要指定以下CSS:

.table td,
.table th
{
    text-align:left;
}

#right,
.table td + td,
.table th + th
{
    text-align:right;
}

An example is at JSFiddle .
您可以在 *5.7相邻同级选择器 * 阅读更多关于相邻同级CSS选择器的信息。

wlzqhblo

wlzqhblo3#

我已经更新了你的小提琴。请点击以下链接:
http://jsfiddle.net/hAv7P/9/
您所需要做的就是向要对齐的添加一个类,然后像摆弄一样添加CSS。

<h1>First Accordion</h1>
<div class="accordion" id="accordion2">
  <div class="accordion-group">
    <div class="accordion-heading">

      <table class="table">
        <thead>
          <tr>
            <th>Fondo/Sociedad</th>
            <th>Actual</th>
            <th>Rent. A.</th>
            <th>Patr. Actual</th>
            <th>Tae A.</th>
            <th>Reg. CNMV</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>Iurisfond, F1</td>
            <td>111,88</td>
            <td>0.13 %</td>
            <td>3.404.808,19 euro</td>
            <td>1.53%</td>
            <td class="rightalign">3015</td>
          </tr>
          <tr>
            <td>CAT OCC BOLSA ESPANOLA</td>
            <td>14,84</td>
            <td>1,55 %</td>
            <td>2.043.608,62 euro</td>
            <td>10,43 %</td>
            <td class="rightalign">3766</td>
          </tr>
          <tr>
            <td>INVESRIONS AGO, SICAV</td>
            <td>20,19</td>

            <td>3,75 %</td>
            <td>26.817.819,33 euro</td>
            <td>20,57 %</td>
            <td class="rightalign">3015</td>

          </tr>
          <tr>
            <td>DIXION INVESTMENT, SICAV**</td>
            <td>10,30</td>
            <td>0,006 %</td>
            <td>4.731.059,54 euro</td>
            <td>0,71 %</td>
            <td class="rightalign">2274</td>
          </tr>
          <tr>
            <td>GAMAVO, SICAV**</td>
            <td>7,20</td>
            <td>3,99 %</td>
            <td>20.469.027,45</td>
            <td>56,52 %</td>
            <td class="rightalign">11</td>
          </tr>
          <tr>
            <td>CAT OCC ESPANOLA</td>
            <td>1,12</td>
            <td>1,45 %</td>
            <td>4.297.723,66</td>
            <td>2,15 %</td>
            <td class="rightalign">321</td>
          </tr>
          <tr>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
          </tr>
        </tbody>
      </table>
    </div>
    <div id="collapseOne" class="accordion-body in collapse" style="height: auto; ">
      <div class="accordion-inner">
        <table class="table">
          <thead>
            <tr>
              <th>Fondo/Sociedad</th>
              <th>Actual</th>
              <th>Rent. A.</th>
              <th>Patr. Actual</th>
              <th>Tae A.</th>
              <th>Reg. CNMV</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>Iurisfond, F1</td>
              <td>111,88</td>
              <td>0.13 %</td>
              <td>3.404.808,19 euro</td>
              <td>1.53%</td>
              <td>3015</td>
            </tr>
            <tr>
              <td>CAT OCC BOLSA ESPANOLA</td>
              <td>14,84</td>
              <td>1,55 %</td>
              <td>2.043.608,62 euro</td>
              <td>10,43 %</td>
              <td>3766</td>
            </tr>
            <tr>
              <td>INVESRIONS AGO, SICAV</td>
              <td>20,19</td>

              <td>3,75 %</td>
              <td>26.817.819,33 euro</td>
              <td>20,57 %</td>
              <td>3015</td>

            </tr>
            <tr>
              <td>DIXION INVESTMENT, SICAV**</td>
              <td>10,30</td>
              <td>0,006 %</td>
              <td>4.731.059,54 euro</td>
              <td>0,71 %</td>
              <td>2274</td>
            </tr>
            <tr>
              <td>GAMAVO, SICAV**</td>
              <td>7,20</td>
              <td>3,99 %</td>
              <td>20.469.027,45</td>
              <td>56,52 %</td>
              <td>11</td>
            </tr>
            <tr>
              <td>CAT OCC ESPANOLA</td>
              <td>1,12</td>
              <td>1,45 %</td>
              <td>4.297.723,66</td>
              <td>2,15 %</td>
              <td>321</td>
            </tr>
            <tr>
              <td></td>
              <td></td>
              <td></td>
              <td></td>
              <td></td>
              <td></td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>
    <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseOne"> <i class="icon-arrow-up"></i>
      </a>
  </div>
</div>

相关问题