【问题标题】:HTML Viewport not using full widthHTML 视口不使用全宽
【发布时间】:2021-08-08 06:02:24
【问题描述】:

我有问题。在我的网站上,我每秒刷新一次数据,但这会导致字体每秒随机调整大小。要解决这个问题,我需要在我的 html 页面中添加以下内容:

<meta name="viewport" content="initial-scale=1">

只有这对我有 1 个问题。在我的桌面上,它没有效果,但是当我切换到我的手机时,页面宽度只使用了 40%,而我有以下 css:

body {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 14px;
    background-color: #DADADA;
    width: 99%;
    margin: auto auto;
}

这是之前和之后的示例图像:

之前

之后

唯一的区别是那一行。我已经发现这篇文章看起来像同样的问题:html tag stops being full width on skinny viewports,但该解决方案似乎对我不起作用,因为当我尝试他的解决方案时没有任何改变。我页面底部的表格是搞砸的事情之一,所以我将分享表格的所有代码:

body {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 14px;
    background-color: #DADADA;
    width: 99%;
    margin: auto auto;
}

.title {
    display: block;
    text-align: center;
    color: #1D0F73;
    font-size: 22px;
    font-weight: bold;
    padding: 5px;
}

#orderHistory {
    margin-top: 30px;
    border-style: solid;
    border-color: black;
    border-width: 1px;
}

#orderHistoryContent {
    padding-top: 20px;
    width: 100%;
    padding: 0px;
}

#orderHistoryContent table {
    width: 100%;
    text-align: left;
    border-collapse: collapse;
    font-size: 14px
}

#orderHistoryContent table td, th {
    border-style: solid;
    border-color: black;
    border-width: 1px;
    padding: 3px 5px;
}


.priceCell {
    text-align: right;
}


.order-history-header {
    font-weight: bold;
}

.order-target-price {
    visibility: visible;
}

.order-limit-price {
    visibility: visible;
}

@media only screen and (max-width: 1100px) {

    .order-target-price {
        display: none;
    }

    .order-limit-price {
        display: none;
    }
}
<html>
    <head>
        <meta charset="UTF-8">

        <!-- This line prevents jumping fonts on a mobile device -->
        <meta name="viewport" content="initial-scale=1">

        <!-- JQUERY -->
        <script src="https://code.jquery.com/jquery-1.11.0.min.js"></script>
        <script src="https://ajax.aspnetcdn.com/ajax/jquery.templates/beta1/jquery.tmpl.js"></script>

        <!-- CSS FILES -->
        <link rel="stylesheet" href="css/main.css">
        <link rel="stylesheet" href="css/orderHistory.css">
    </head>
    <body>

        <div id="orderHistory">
          <label class="title">Order history</label>
          <div id="orderHistoryContent">
              <table>
                  <tbody>
                      <tr>
                          <th>Action</th>
                          <th>Market</th>
                          <th>Coin</th>
                          <th>DateTimeEntered</th>
                          <th class="priceCell">Quantity</th>
                          <th class="priceCell">Amount</th>
                          <th class="order-target-price">Target price</th>
                          <th class="order-limit-price">Limit price</th>
                          <th class="priceCell">Price</th>
                          <th>DateTimeExecuted</th>
                          <th>Status</th>
                          <th class="priceCell">Profit</th>
                      </tr>
                      <tr style="border-style: solid; border-color: black; border-width: 1px;">
                          <td style="background-color: rgb(251, 216, 255);">Buy</td>
                          <td style="background-color: rgb(255, 255, 255);">USDT</td>
                          <td style="background-color: rgb(173, 204, 255);">ETH</td>
                          <td style="background-color: rgb(255, 227, 214);">2021-05-19 02:56:01</td>
                          <td class="priceCell" style="background-color: rgb(251, 255, 191);">0.59904000</td>
                          <td class="priceCell" style="background-color: rgb(251, 255, 191);">2012.27120640</td>
                          <td class="order-target-price" style="background-color: rgb(255, 227, 214);">0.000000</td>
                          <td class="order-limit-price" style="background-color: rgb(255, 227, 214);">0.000000</td>
                          <td class="priceCell" style="background-color: rgb(251, 255, 191);">3359.160000</td>
                          <td style="background-color: rgb(251, 255, 191);">2021-05-19 02:56:01</td>
                          <td style="background-color: rgb(210, 255, 209);">Executed</td>
                      </tr>
                      <tr style="border-style: solid; border-color: black; border-width: 1px;">
                          <td style="background-color: rgb(229, 196, 146);">Sell</td>
                          <td style="background-color: rgb(229, 229, 229);">USDT</td>
                          <td style="background-color: rgb(198, 229, 173);">BTC</td>
                          <td style="background-color: rgb(229, 204, 192);">2021-05-16 15:48:33</td>
                          <td class="priceCell" style="background-color: rgb(225, 229, 171);">0.04112200</td>
                          <td class="priceCell" style="background-color: rgb(225, 229, 171);">2014.25425280</td>
                          <td class="order-target-price" style="background-color: rgb(229, 204, 192);">0.000000</td>
                          <td class="order-limit-price" style="background-color: rgb(229, 204, 192);">0.000000</td>
                          <td class="priceCell" style="background-color: rgb(225, 229, 171);">48982.400000</td>
                          <td style="background-color: rgb(225, 229, 171);">2021-05-16 15:48:34</td>
                          <td style="background-color: rgb(189, 229, 188);">Executed</td>
                          <td class="priceCell" rowspan="0" style="background-color: rgb(155, 237, 137);">
                              <b>
                                  +12.40$<br />
                                  +0.62%
                              </b>
                          </td>
                      </tr>
                      <tr style="border-style: solid; border-color: black; border-width: 1px;">
                          <td style="background-color: rgb(251, 216, 255);">Buy</td>
                          <td style="background-color: rgb(255, 255, 255);">USDT</td>
                          <td style="background-color: rgb(220, 255, 193);">BTC</td>
                          <td style="background-color: rgb(255, 227, 214);">2021-05-16 15:29:55</td>
                          <td class="priceCell" style="background-color: rgb(251, 255, 191);">0.04116400</td>
                          <td class="priceCell" style="background-color: rgb(251, 255, 191);">1999.96817068</td>
                          <td class="order-target-price" style="background-color: rgb(255, 227, 214);">0.000000</td>
                          <td class="order-limit-price" style="background-color: rgb(255, 227, 214);">0.000000</td>
                          <td class="priceCell" style="background-color: rgb(251, 255, 191);">48585.370000</td>
                          <td style="background-color: rgb(251, 255, 191);">2021-05-16 15:29:56</td>
                          <td style="background-color: rgb(210, 255, 209);">Executed</td>
                      </tr>
                  </tbody>
              </table>
          </div>
      </div>

    </body>
</html>

这个例子的问题是表格穿过了外div边框,而我将外div的宽度设置为100%。 这是什么原因造成的,我该如何解决?

【问题讨论】:

  • 你之前有哪个元标签? &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;?
  • 在那之前我什么都没有

标签: html css


【解决方案1】:

基本上,您的 div 确实是 100% 的视口,但您的表格比视口宽,导致问题...

您需要一些overflow,因为表格元素不是超级“移动友好”:)。

试试:

#orderHistoryContent {
    ...
    overflow: auto;
}

或者,如果您不喜欢滚动,您可以改为使用 word-break 打破表格内的内容...

#orderHistoryContent table td, th { 
      word-break: break-word; 
}

body {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 14px;
    background-color: #DADADA;
    width: 99%;
    margin: auto auto;
}

.title {
    display: block;
    text-align: center;
    color: #1D0F73;
    font-size: 22px;
    font-weight: bold;
    padding: 5px;
}

#orderHistory {
    margin-top: 30px;
    border-style: solid;
    border-color: black;
    border-width: 1px;
}

#orderHistoryContent {
    padding-top: 20px;
    width: 100%;
    padding: 0px;
}

#orderHistoryContent table {
    width: 100%;
    text-align: left;
    border-collapse: collapse;
    font-size: 14px
}

#orderHistoryContent table td, th {
    border-style: solid;
    border-color: black;
    border-width: 1px;
    padding: 3px 5px;
}

#orderHistoryContent.overflow { overflow: auto; }
#orderHistoryContent.word-break table td, th { word-break: break-word; }

.priceCell {
    text-align: right;
}


.order-history-header {
    font-weight: bold;
}

.order-target-price {
    visibility: visible;
}

.order-limit-price {
    visibility: visible;
}

@media only screen and (max-width: 1100px) {

    .order-target-price {
        display: none;
    }

    .order-limit-price {
        display: none;
    }
}
<html>
    <head>
        <meta charset="UTF-8">

        <!-- This line prevents jumping fonts on a mobile device -->
        <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1">

        <!-- JQUERY -->
        <script src="https://code.jquery.com/jquery-1.11.0.min.js"></script>
        <script src="https://ajax.aspnetcdn.com/ajax/jquery.templates/beta1/jquery.tmpl.js"></script>

        <!-- CSS FILES -->
        <link rel="stylesheet" href="css/main.css">
        <link rel="stylesheet" href="css/orderHistory.css">
    </head>
    <body>

        <div id="orderHistory">
          <label class="title">Order history (overflow)</label>
          <div id="orderHistoryContent" class="overflow">
              <table>
                  <tbody>
                      <tr>
                          <th>Action</th>
                          <th>Market</th>
                          <th>Coin</th>
                          <th>DateTimeEntered</th>
                          <th class="priceCell">Quantity</th>
                          <th class="priceCell">Amount</th>
                          <th class="order-target-price">Target price</th>
                          <th class="order-limit-price">Limit price</th>
                          <th class="priceCell">Price</th>
                          <th>DateTimeExecuted</th>
                          <th>Status</th>
                          <th class="priceCell">Profit</th>
                      </tr>
                      <tr style="border-style: solid; border-color: black; border-width: 1px;">
                          <td style="background-color: rgb(251, 216, 255);">Buy</td>
                          <td style="background-color: rgb(255, 255, 255);">USDT</td>
                          <td style="background-color: rgb(173, 204, 255);">ETH</td>
                          <td style="background-color: rgb(255, 227, 214);">2021-05-19 02:56:01</td>
                          <td class="priceCell" style="background-color: rgb(251, 255, 191);">0.59904000</td>
                          <td class="priceCell" style="background-color: rgb(251, 255, 191);">2012.27120640</td>
                          <td class="order-target-price" style="background-color: rgb(255, 227, 214);">0.000000</td>
                          <td class="order-limit-price" style="background-color: rgb(255, 227, 214);">0.000000</td>
                          <td class="priceCell" style="background-color: rgb(251, 255, 191);">3359.160000</td>
                          <td style="background-color: rgb(251, 255, 191);">2021-05-19 02:56:01</td>
                          <td style="background-color: rgb(210, 255, 209);">Executed</td>
                      </tr>
                      <tr style="border-style: solid; border-color: black; border-width: 1px;">
                          <td style="background-color: rgb(229, 196, 146);">Sell</td>
                          <td style="background-color: rgb(229, 229, 229);">USDT</td>
                          <td style="background-color: rgb(198, 229, 173);">BTC</td>
                          <td style="background-color: rgb(229, 204, 192);">2021-05-16 15:48:33</td>
                          <td class="priceCell" style="background-color: rgb(225, 229, 171);">0.04112200</td>
                          <td class="priceCell" style="background-color: rgb(225, 229, 171);">2014.25425280</td>
                          <td class="order-target-price" style="background-color: rgb(229, 204, 192);">0.000000</td>
                          <td class="order-limit-price" style="background-color: rgb(229, 204, 192);">0.000000</td>
                          <td class="priceCell" style="background-color: rgb(225, 229, 171);">48982.400000</td>
                          <td style="background-color: rgb(225, 229, 171);">2021-05-16 15:48:34</td>
                          <td style="background-color: rgb(189, 229, 188);">Executed</td>
                          <td class="priceCell" rowspan="0" style="background-color: rgb(155, 237, 137);">
                              <b>
                                  +12.40$<br />
                                  +0.62%
                              </b>
                          </td>
                      </tr>
                      <tr style="border-style: solid; border-color: black; border-width: 1px;">
                          <td style="background-color: rgb(251, 216, 255);">Buy</td>
                          <td style="background-color: rgb(255, 255, 255);">USDT</td>
                          <td style="background-color: rgb(220, 255, 193);">BTC</td>
                          <td style="background-color: rgb(255, 227, 214);">2021-05-16 15:29:55</td>
                          <td class="priceCell" style="background-color: rgb(251, 255, 191);">0.04116400</td>
                          <td class="priceCell" style="background-color: rgb(251, 255, 191);">1999.96817068</td>
                          <td class="order-target-price" style="background-color: rgb(255, 227, 214);">0.000000</td>
                          <td class="order-limit-price" style="background-color: rgb(255, 227, 214);">0.000000</td>
                          <td class="priceCell" style="background-color: rgb(251, 255, 191);">48585.370000</td>
                          <td style="background-color: rgb(251, 255, 191);">2021-05-16 15:29:56</td>
                          <td style="background-color: rgb(210, 255, 209);">Executed</td>
                      </tr>
                  </tbody>
              </table>
          </div>
          <label class="title">Order history (word-break)</label>
          <div id="orderHistoryContent" class="word-break">
              <table>
                  <tbody>
                      <tr>
                          <th>Action</th>
                          <th>Market</th>
                          <th>Coin</th>
                          <th>DateTimeEntered</th>
                          <th class="priceCell">Quantity</th>
                          <th class="priceCell">Amount</th>
                          <th class="order-target-price">Target price</th>
                          <th class="order-limit-price">Limit price</th>
                          <th class="priceCell">Price</th>
                          <th>DateTimeExecuted</th>
                          <th>Status</th>
                          <th class="priceCell">Profit</th>
                      </tr>
                      <tr style="border-style: solid; border-color: black; border-width: 1px;">
                          <td style="background-color: rgb(251, 216, 255);">Buy</td>
                          <td style="background-color: rgb(255, 255, 255);">USDT</td>
                          <td style="background-color: rgb(173, 204, 255);">ETH</td>
                          <td style="background-color: rgb(255, 227, 214);">2021-05-19 02:56:01</td>
                          <td class="priceCell" style="background-color: rgb(251, 255, 191);">0.59904000</td>
                          <td class="priceCell" style="background-color: rgb(251, 255, 191);">2012.27120640</td>
                          <td class="order-target-price" style="background-color: rgb(255, 227, 214);">0.000000</td>
                          <td class="order-limit-price" style="background-color: rgb(255, 227, 214);">0.000000</td>
                          <td class="priceCell" style="background-color: rgb(251, 255, 191);">3359.160000</td>
                          <td style="background-color: rgb(251, 255, 191);">2021-05-19 02:56:01</td>
                          <td style="background-color: rgb(210, 255, 209);">Executed</td>
                      </tr>
                      <tr style="border-style: solid; border-color: black; border-width: 1px;">
                          <td style="background-color: rgb(229, 196, 146);">Sell</td>
                          <td style="background-color: rgb(229, 229, 229);">USDT</td>
                          <td style="background-color: rgb(198, 229, 173);">BTC</td>
                          <td style="background-color: rgb(229, 204, 192);">2021-05-16 15:48:33</td>
                          <td class="priceCell" style="background-color: rgb(225, 229, 171);">0.04112200</td>
                          <td class="priceCell" style="background-color: rgb(225, 229, 171);">2014.25425280</td>
                          <td class="order-target-price" style="background-color: rgb(229, 204, 192);">0.000000</td>
                          <td class="order-limit-price" style="background-color: rgb(229, 204, 192);">0.000000</td>
                          <td class="priceCell" style="background-color: rgb(225, 229, 171);">48982.400000</td>
                          <td style="background-color: rgb(225, 229, 171);">2021-05-16 15:48:34</td>
                          <td style="background-color: rgb(189, 229, 188);">Executed</td>
                          <td class="priceCell" rowspan="0" style="background-color: rgb(155, 237, 137);">
                              <b>
                                  +12.40$<br />
                                  +0.62%
                              </b>
                          </td>
                      </tr>
                      <tr style="border-style: solid; border-color: black; border-width: 1px;">
                          <td style="background-color: rgb(251, 216, 255);">Buy</td>
                          <td style="background-color: rgb(255, 255, 255);">USDT</td>
                          <td style="background-color: rgb(220, 255, 193);">BTC</td>
                          <td style="background-color: rgb(255, 227, 214);">2021-05-16 15:29:55</td>
                          <td class="priceCell" style="background-color: rgb(251, 255, 191);">0.04116400</td>
                          <td class="priceCell" style="background-color: rgb(251, 255, 191);">1999.96817068</td>
                          <td class="order-target-price" style="background-color: rgb(255, 227, 214);">0.000000</td>
                          <td class="order-limit-price" style="background-color: rgb(255, 227, 214);">0.000000</td>
                          <td class="priceCell" style="background-color: rgb(251, 255, 191);">48585.370000</td>
                          <td style="background-color: rgb(251, 255, 191);">2021-05-16 15:29:56</td>
                          <td style="background-color: rgb(210, 255, 209);">Executed</td>
                      </tr>
                  </tbody>
              </table>
          </div>
      </div>

    </body>
</html>

【讨论】:

  • 添加overflow: auto;只是在外层div的边框处截断表格,但div仍然不是全宽,而是屏幕宽度的40%
  • 很难调试我们没有的代码,您只需提供图像...提供您的 HTML,请参阅Minimal, Reproducible Example...基本上,您的表格太宽,无法放入视口...尽可能避免使用表格
  • 我已经提供了重现问题的所有代码。我给出的代码是针对一个包含问题的元素。修复一个,修复所有。尝试在移动设备上运行我的代码或缩小窗口
  • 我可以看到您的问题,并且已经尝试为您指出正确的方向...因此,您的内容使表格对于视口来说太宽了。基本上,您的外部 div 确实是视口的 100%,但是您的表格比视口宽...oveflow 将所有内容都保留在视口内,但有一个滚动...或者您可以尝试word-break,例如:#orderHistoryContent table td, th { word-break: break-word; }...查看sn-p,希望对您有所帮助
猜你喜欢
  • 2020-12-07
  • 2021-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-08
  • 1970-01-01
  • 2019-03-07
  • 2011-09-11
相关资源
最近更新 更多