html - 如何防止表格高度溢出容器div?

标签 html css layout html-table css-tables

我需要将表格包含在其容器 div 中。

样式宽度完美,但高度完全没有响应。

我搜索了很多类似的帖子。我试过 table-layout:fixed、相对定位容器、最大/最小高度,但似乎没有任何效果。

    <!DOCTYPE html>
<html>

  <head>
    <title> Aisha-Flix </title>
    <script src="https://code.jquery.com/jquery.min.js"></script>
    <link href="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet" type="text/css" />
    <script src="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" type="text/css" />

    <!-- Fonts -->
    <link href="https://fonts.googleapis.com/css?family=Prompt:300,600" rel="stylesheet"> 

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">




    <style>

    html{height:100%}

    body{
      padding: 0;
      margin: 0;
      color: #f5f5f5;
      font-family: 'Prompt', sans-serif;
      height: 100%;
    }


    #content{
      height: 100vh;
      border: 1px solid black;
      position: relative;
      background-color: black;

    }

    #movieContainer{
      position: absolute;
      width: 100%;
      height: 50vh;
      top: 33vh;
      background: url(giphy3.gif);
      background-size: 100% 100%;
      border: 1px solid green;
    }

    table{
      width: 100%;
      height: 90%;
      border: 1px solid red;
      table-layout: fixed;

    }

    #number{
      font-size: 16vh;
      padding-top: 16vh;

    }

    #number span{
      font-size: 30vh;
      font-family: Gill Sans;
    }

    td{
      border: 1px solid orange;
    }

    .anim{
      background: url(static.gif) !important;
      background-size: cover !important;
      background-repeat: no-repeat;

    }

    td:nth-child(2){
      vertical-align: top;
    }

    p:first-child{
      font-family: Gill Sans;
      font-size: 1.7em;
    }

    #rating{
      text-align: right;
      vertical-align: top;
    }




    </style>
  </head>

  <body>

    <div id="content">
      <button id="test"> </button>

      <div class="col-xs-12" id="movieContainer">
        <table>
               <tr>
          <td id="number" width="66%" rowspan="2">#<span>7</span></td>
          <td>
            <p> 2001: A Space Odyssey</p>
            <p><b>Director</b></p>
            <p style="margin-top: -10px;"> Stanley Kubrick - 1968</p>
            <p><b>DP </b></p>
            <p style="margin-top: -10px;"> Geoffrey Unsworth </p>
          </td>
        </tr> 

            <tr> <td id="rating"> <p> &#11088 &#11088 &#11088 </p></td></tr>

        </table>
      </div>

      <div id="addMovie"></div>
    </div>

    <div id="footer"> </div>


     <script type="text/javascript">

        var windowHeight =  $(window).height();

        $("#landingPage").height(windowHeight + "px"); 

        $("#test").click(function(){
          $("#movieContainer").addClass("anim");
        });




      </script>


  </body>


</html>

JSBIN:http://output.jsbin.com/zafodepozu

最佳答案

出现问题是因为 div .movi​​eContainer 的高度固定 如果将其高度更改为自动,它将起作用

    var windowHeight = $(window).height();

    $("#landingPage").height(windowHeight + "px");

    $("#test").click(function() {
      $("#movieContainer").addClass("anim");
    });
    html {
      height: 100%
    }
    
    body {
      padding: 0;
      margin: 0;
      color: #f5f5f5;
      font-family: 'Prompt', sans-serif;   height: 100%;
    }
    
    #content {
      height: 100vh;
      border: 1px solid black;
      position: relative;
      background-color: black;
    }
    
    #movieContainer {
      position: absolute;
      width: 100%;
      height: auto;
      top: 33vh;
      background: url(giphy3.gif);
      background-size: 100% 100%;
      border: 1px solid green;
    }
    
    table {
      width: 100%;
      height: 90%;
      border: 1px solid red;
      table-layout: fixed;
    }
    
    #number {
      font-size: 16vh;
      padding-top: 16vh;
    }
    
    #number span {
      font-size: 30vh;
      font-family: Gill Sans;
    }
    
    td {
      border: 1px solid orange;
    }
    
    .anim {
      background: url(static.gif) !important;
      background-size: cover !important;
      background-repeat: no-repeat;
    }
    
    td:nth-child(2) {
      vertical-align: top;
    }
    
    p:first-child {
      font-family: Gill Sans;
      font-size: 1.7em;
    }
    
    #rating {
      text-align: right;
      vertical-align: top;
    }
  <script src="https://code.jquery.com/jquery.min.js"></script>
  <link href="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet" type="text/css" />
  <script src="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
  <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" type="text/css" />

  <!-- Fonts -->
  <link href="https://fonts.googleapis.com/css?family=Prompt:300,600" rel="stylesheet">
 





<body>

  <div id="content">
    <button id="test"> </button>

    <div class="col-xs-12" id="movieContainer">
      <table>
        <tr>
          <td id="number" width="66%" rowspan="2">#<span>7</span></td>
          <td>
            dgdsgdsg
          </td>
        </tr>

        <tr>
          <td id="rating">
             &#11088 &#11088 &#11088
          </td>
        </tr>

      </table>
    </div>

    <div id="addMovie"></div>
  </div>

  <div id="footer"> </div>


 

 

关于html - 如何防止表格高度溢出容器div?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38165527/

相关文章:

javascript - 引用像 src ="//www.example.com/blah.js"这样的 url

HTML:如何在元素的类属性中使用 href

c++ - 如何布局C++代码: namespaces and similar file names in visual studio 2010

html - 有没有办法在 Css 中重新排列 block (flex)的顺序

html - 设置多个备用表格行的样式

java - 使用 Java 和 jsoup 进行网站抓取; html无法读取;空指针异常

css - 在下拉菜单选项下显示文本

html - 使容器 "clear"成为包含元素的 margin-top 而不会溢出 :hidden?

javascript - 悬停在图表上时工具提示闪烁

java - 我可以在Java中的CardLayout中添加多个框架吗?