html - 不可调整的表格单元格

标签 html css

我有一个表格,每个单元格中都有文本。我想在其中一些下面添加一个新文本(就像我对该段落所做的那样)但不移动“文本”,也不调整单元格的大小。我该怎么做?

HTML代码:

<!doctype html>
<html>
<head>
    <title>Table</title>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<table width="80%" align="center">
    <tr>
        <th></th>
        <th>Monday</th>
        <th>Tuesday</th>
        <th>Wednesday</th>
        <th>Thursday</th>
        <th>Friday</th>
        <th>Saturday</th>
        <th>Sunday</th>
    </tr>
    <tr>
        <th>09:00</th>

        <td>Text</td>
        <td>Text</td>
        <td></td>
        <td>Text</td>
        <td>Text</td>
        <td>Text</td>
        <td>Text</td>
    </tr>

    <tr>
        <th>10:00</th>

        <td>Text</td>
        <td>Text</td>
        <td>Text</td>
        <td>Text</td>
        <td>Text</td>
        <td>Text</td>
        <td>Text/td>
    </tr>

    <tr>
        <th>18:00</th>

        <td>Text</td>
        <td>Text</td>
        <td>Text</td>
        <td>Text
        <p>REZERV</p></td>
        <td>Text</td>
        <td>Text</td>
        <td>Text</td>
    </tr>

    <tr>
        <th>19:00</th>

        <td>Text</td>
        <td>Text</td>
        <td>Text</td>
        <td>Text
        <p>REZERV</p></td>
        <td>Text</td>
        <td>Text</td>
        <td>Text</td>
    </tr>

    <tr>
        <th>20:00</th>

         <td>Text</td>
         <td>Text</td>
         <td>Text</td>
         <td>Text</td>
         <td>Text</td>
         <td>Text</td>
         <td>Text</td>
    </tr>
</table>

CSS 代码:

body
{
    font-family: arial;
    background-image: url("back.jpg");
    background-repeat: no-repeat;
}
table{
    table-layout: fixed;
}
th:first-child { /* CHANGE THE SIZE OF THE FIRST TH AND THE TD BELOW IT WILL FOLLOW... */
    width: 7%;
    font-weight: normal;
}
td
{
    margin: 0;
    text-align: center;
    border-collapse: collapse;
    outline: 3px solid #e3e3e3;
    padding: 5px 10px;
    font-size: 20px;
    width: 30%;
}
tr:nth-child(even){
    background: rgba(200, 196, 196,0.9);
    color: white;
}
tr:nth-child(odd){
    background: rgba(255, 255, 255,0.9);
    color: grey;
}
td:after{
    content:'';
    display:block;
    margin-top:50%;
}
th
{
    background: #666;
    color: white;
    padding: 5px 10px;
    width: 20%;
}

td:hover
{
    cursor: pointer;
    background: lightblue;
    color: #666;
}
table tr:first-of-type {
    font-size:30px;
    font-style: italic;
}
p{
    background-color: purple;
    color: white;
}

最佳答案

参见:http://jsfiddle.net/mh7qfr63/1/

添加 vertical-align 以确保所有 tds 内容对齐到顶部。

vertical-align: top;

然后如果你想要一个特定的顶部填充然后添加:

padding-top: 40px // or some other value

如果您不希望 td 调整高度,则需要在 td 内明确设置高度。由于您的宽度是基于百分比的,因此宽度将根据您的页面宽度而变化。

综合:

td
{
    margin: 0;
    text-align: center;
    border-collapse: collapse;
    outline: 3px solid #e3e3e3;
    padding: 5px 10px;
    font-size: 20px;
    width: 30%;

    vertical-align: top;
    padding-top: 40px; // or other number
    height: 100px; // or other number
}

关于html - 不可调整的表格单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32849375/

相关文章:

php - 如何使一个 div 看起来部分覆盖在另一个方形 div 之上

javascript - 加载页面时如何防止关注表单(在页面底部)?

javascript - 基于计数器值的 Css3 div 渐变背景动画?

javascript - 防止浏览器缓存 JavaScript 文件的更好方法

php - MySQL UPDATE 列的值并用旧值添加新值

html - 点击 <a> 元素时如何禁用灰色 mask ?

html - PNG图像在IE7中扭曲混浊

css - 当页面宽度减小时,我怎样才能做到这一点,它最终会停止调整?

javascript - 在导入的文件中无法通过 id 找到模板

html - 我如何获得带有非透明文本的透明 div?