简体   繁体   English

CSS表格填充问题

[英]CSS table padding issue

I need help with some CSS styling. 我需要一些CSS样式方面的帮助。

I have made a table with 2 rows, and 2 columns, but the first column in the first row has a rowspan of 2. That creates a table like this: http://i.imgur.com/UjdSwu5.png , which is fine. 我制作了一个包含2行2列的表格,但是第一行的第一列的行距为2。这将创建一个表格,如下所示: http : //i.imgur.com/UjdSwu5.png ,即精细。

My problem is that when I try to apply padding to the 'name' and 'id' cells (but not the image cell), only the name cell gets padded. 我的问题是,当我尝试将填充应用于“名称”和“ id”单元格(而不是图像单元格)时,仅填充名称单元格。 Here is a screenshot of no padding: http://i.imgur.com/0CGVhDL.png , and here is a screenshot of when I try to pad both cells: http://i.imgur.com/ipvHv2M.png 这是不填充的屏幕截图: http : //i.imgur.com/0CGVhDL.png ,这是我尝试填充两个单元格时的屏幕截图: http : //i.imgur.com/ipvHv2M.png

HTML: HTML:

<div id="body">
  <div>
    <div>
      <div>
        <div id="content">
          <div id="items">
            <ul class="list">
              <li>
                <table border="0">
                  <tr>
                    <td rowspan="2"><a href="index.html"><img
                      src="images/Stone.png" alt="Image" height="50" width="50"></a>
                    </td>
                    <td>
                      <h3 class="name">Stone</h3>
                    </td>
                  </tr>
                  <tr>
                    <td>
                      <p class="id">1</p>
                    </td>
                  </tr>
                </table>
              </li>
            </ul>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

CSS: CSS:

#content td h3 {
  padding:5px 1px 5px 30px;
}

#content td p {
  padding:5px 1px 5px 30px;
}

If I do the following then the id cell gets padded how i want it to, but it also pads the img cell. 如果我执行以下操作,则id单元将被填充为我希望的样子,但它也会填充img单元。

#content td {
  padding:5px 1px 5px 30px;
}

What am i doing wrong? 我究竟做错了什么?

you are simply applying padding to the wrong element. 您只是将padding应用于错误的元素。

you are applying a padding to the h3 and p elements inside #content td but what you really want to do is apply the padding to the cell which is td . 您正在p #content tdh3p元素应用填充,但是您真正想要做的是将填充应用于td单元格

In order to that properly, you need to identify your cells, like this: 为了正确执行此操作,您需要标识您的单元格,如下所示:

<td class="name">
  <h3>Stone</h3>
</td>

and

<td class="id">
    <p>1</p>
</td>

and the CSS should be something like this CSS应该是这样的

#content td.name {
  padding:5px 1px 5px 30px;
}

#content td.id {
  padding:5px 1px 5px 30px;
}

Also, a good practice would be not to name a class as id that could be very confusing afterwards. 同样,一个好的做法是不要将一个命名为id ,这以后可能会造成很大的混乱。

I would advise calling it item-id instead, for example. 例如,我建议将其称为item-id

<td class="item-id">
    <p>1</p>
</td>

#content td.item-id {
  padding:5px 1px 5px 30px;
}

First of all, I suggest using the class-name of the elements, to style them. 首先,我建议使用元素的类名来设置它们的样式。

Here's the css which should do what you want: 这是应该执行您想要的工作的CSS:

.name, .id{
    padding: 80px;
}

In this fiddle, you can see a working solution: http://jsfiddle.net/63eUh/ 在这个小提琴中,您可以看到一个有效的解决方案: http : //jsfiddle.net/63eUh/

As Kevin Smouts already said, you applied the padding to the wrong Element - which can easly happen, when you are adressing Elements in this way - it's difficult to read. 正如Kevin Smouts已经说过的那样,您将填充物应用于错误的Element上-当您以这种方式处理Elements时很容易发生这种情况-很难阅读。

Whenever you change your HTML-structure, you have to care about css and update it as well. 每当您更改HTML结构时,都必须关心CSS并对其进行更新。 So I really don't recommend putting all your html-tree inside css to reach the correct elements. 因此,我真的不建议您将所有的html-tree放入CSS中以获取正确的元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM