简体   繁体   English

如何定义表格单元格的高度(CSS / HTML)

[英]How to define table cell height (CSS/HTML)

I'm using standard CSS/HTML for designing my website. 我正在使用标准CSS / HTML来设计我的网站。 Using a table for the navigation, I assign my "#nav table" with a width of 100% and my "#nav th" with a height of 50px . 使用表格进行导航,我将宽度为100% "#nav table"分配为高度为50px "#nav th"

My issue is that it's not actually setting itself to what I define. 我的问题是,它实际上并未将自己设置为我定义的内容。 I had assumed it was as easy as just saying height: 50px but even when I define it as 0px it remains at 90.5px. 我以为它就像说height: 50px一样容易height: 50px但是即使我将其定义为0px它仍然保持在90.5px. In the HTML I did define my cells and what the table was, etc. 在HTML中,我确实定义了单元格以及表的内容,等等。

How can I fix this? 我怎样才能解决这个问题? Right now my site header is outrageously large because my table cells won't adjust to what I define them. 现在,我的网站标题非常大,因为我的表格单元格无法适应我定义的表格单元格。

Using just PHP, HTML, and CSS. 仅使用PHP,HTML和CSS。

CSS: CSS:

    #nav table {
        margin: 15px;
        width: 100%;
        position: fixed;
        }

    #nav th {
        height: 0px;
        border: 5px black solid;
        padding: 25px;
        text-align: center;
        white-space: nowrap;
        letter-spacing: 0px;
        word-spacing: 5px;
        }

HTML: HTML:

    <center>
    <table id="nav">
    <tr>
    <th><a href="LINK"><h1>LINK</h1></a></th>
    <th><a href="LINK1"><h1>LINK1</h1></a></th>
    <th><a href="LINK2"><h1>LINK2</h1></a></th>
    <th><a href="LINK3"><h1>LINK3</h1></a></th>
    <th><a href="LINK4"><h1>LINK4</h1></a></th>
    <th><a href="LINK5><h1>LINK5</h1></a></th>
    </tr>
    </table>
    </center>
    <p>
    Page Directory:

Using the code #nav table assumes an element with the ID "nav" with a table inside, and not a table element with the id "nav". 使用代码#nav table假定一个ID为“ nav”且内部带有table元素,而不是ID为“ nav”的一个table元素。

<style type="text/css">
  /* For a table with the id of 'nav'.  You could equally remove 'table'. */
  table#nav {
    margin: 15px;
    width: 100%;
    position: fixed;
  }
  #nav th {
    height: 0px;
    border: 5px black solid;
    padding:0 25px;
    text-align: center;
    white-space: nowrap;
    letter-spacing: 0px;
    word-spacing: 5px;
  }
  th h1 {
    margin:0;
  }
</style>

<center>
  <table id="nav">
    <tr>
      <th><a href="LINK"><h1>LINK</h1></a></th>
      <th><a href="LINK1"><h1>LINK1</h1></a></th>
      <th><a href="LINK2"><h1>LINK2</h1></a></th>
      <th><a href="LINK3"><h1>LINK3</h1></a></th>
      <th><a href="LINK4"><h1>LINK4</h1></a></th>
      <!-- Don't forget to close all hrefs with a double-quote: -->
      <th><a href="LINK5"><h1>LINK5</h1></a></th>
    </tr>
  </table>
</center>

https://jsfiddle.net/cz1o8a2n/1/ https://jsfiddle.net/cz1o8a2n/1/

Note that if you have an element inside of your th that is 90.5px tall, you will not be able to make the table header less tall using CSS (unless you use CSS to decrease the height of that element). 请注意,如果你有你的中的元素th即90.5px高,你将无法使表头使用CSS少高(除非你用CSS来降低该元素的高度)。

Edit : Consider using the more moder nav instead, with list-items. 编辑 :考虑使用带有列表项的更现代的nav Plus, you'll have an easier time making your site responsive if you like. 另外,如果您愿意,您将可以更轻松地使网站响应。 :) :)

<style type="text/css">
  nav {
    margin: 15px;
    width: calc(100% - 30px);
    position: fixed;
    text-align:center;
  }
  nav ul {
    padding-left:0;
    list-style:none;
  }
  nav li {
    display:inline-block;
    border: 3px black solid;
    padding:0 20px;
    text-align: center;
    white-space: nowrap;
    letter-spacing: 0px;
    word-spacing: 5px;
    margin:2px 0;
  }
</style>
<nav id="nav">
  <ul>
    <li><a href="LINK">LINK</a></li>
    <li><a href="LINK1">LINK1</a></li>
    <li><a href="LINK2">LINK2</a></li>
    <li><a href="LINK3">LINK3</a></li>
    <li><a href="LINK4">LINK4</a></li>
    <li><a href="LINK5">LINK5</a></li>
  </ul>
</nav>

https://jsfiddle.net/cz1o8a2n/2/ https://jsfiddle.net/cz1o8a2n/2/

HTML HTML
You're missing a " for LINK5. 您缺少LINK5的"

<center>
    <table id="nav">
        <tr>
            <th><a href="LINK"><h1>LINK</h1></a></th>
            <th><a href="LINK1"><h1>LINK1</h1></a></th>
            <th><a href="LINK2"><h1>LINK2</h1></a></th>
            <th><a href="LINK3"><h1>LINK3</h1></a></th>
            <th><a href="LINK4"><h1>LINK4</h1></a></th>
            <th><a href="LINK5"><h1>LINK5</h1></a></th>
        </tr>
    </table>
</center>

CSS CSS
If you want to apply css to a table with the id set to nav your selector needs to be table#nav . 如果要将css应用于ID设置为nav的表,则选择器必须是table#nav As for the table headings being larger than you like, the issue is the h1 tag inherently has a pretty big margin. 至于表标题大于您想要的,问题是h1标签固有地具有很大的边距。 This big margin is making your table headings larger than you'd like. 这个巨大的利润使您的表标题比您想要的要大。

table#nav {
    margin: 15px;
    width: 100%;
    position: fixed;
}

#nav th {
    height: 50px;
    border: 5px black solid;
    text-align: center;
    white-space: nowrap;
    overflow: hidden;
}

#nav th h1 {
    margin: 0;
}

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

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