使用滚动条无法使DIV宽度达到100% [英] Cannot make DIV width 100% with scrollbar

查看:227
本文介绍了使用滚动条无法使DIV宽度达到100%的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有DIV和TABLE的页面。 DIV是我的标题,即使在显示水平滚动条时,我也希望它的宽度为100%。由于某些原因,它仅占可视窗口的100%。



我的HTML代码是:

 <!DOCTYPE html>< html lang =en>< body> < div style =background-color:yellow;> 100%DIV< / div> < table style =width:100%; background-color:orange;> < THEAD> < TR> < th style =padding-left:20px; padding-right:20px;> Very_Long_Header_1< / th> < th style =padding-left:20px; padding-right:20px;> Very_Long_Header_2< / th> < th style =padding-left:20px; padding-right:20px;> Very_Long_Header_3< / th> < / TR> < / THEAD> < / body>< / html>  

p>当我滚动到右侧时,DIV不会使用所有宽度:



如何让DIV占用页面宽度的100%?理想情况下,我不想更改TABLE,因为它是生成的代码。

解决方案

没有办法修正任何宽度 - 并且使用纯CSS - 无JavaScript)来获取块级元素,以通过块级父元素继承同级块级元素的宽度。



解决方法是在父元素上放置 display:inline-block ,因为内联块会计算其<宽度 b
$ b

在您的情况下,不要放置 display:inline-在 body 元素上块。我建议在另一个包装元素中包装 100%div 元素,因此:



 <!DOCTYPE html>< html lang =en>< body> < div class =wrapperstyle =display:inline-block; min-width:100%;> < div style =background-color:yellow;> 100%DIV< / div> < table style =width:100%; background-color:orange;> < THEAD> < TR> < th style =padding-left:20px; padding-right:20px;> Very_Long_Header_1< / th> < th style =padding-left:20px; padding-right:20px;> Very_Long_Header_2< / th> < th style =padding-left:20px; padding-right:20px;> Very_Long_Header_3< / th> < / TR> < / THEAD> < /表> < / div>< / html>  

另外,在wrapper元素上放置 min-width:100%会使它与 display:block 在较大的屏幕尺寸上。


I have a page with a DIV and a TABLE. The DIV is my header and I want it to be 100% width even when horizontal scroll-bar displayed. For some reason it takes only 100% of visible window.

My HTML code is:

<!DOCTYPE html>
<html lang="en">
<body>

    <div style="background-color:yellow;">100% DIV</div>

    <table style="width:100%; background-color:orange;">
        <thead>
            <tr>
                <th style="padding-left:20px; padding-right:20px;">Very_Long_Header_1</th>
                <th style="padding-left:20px; padding-right:20px;">Very_Long_Header_2</th>
                <th style="padding-left:20px; padding-right:20px;">Very_Long_Header_3</th>
            </tr>
        </thead>
    </table>

</body>
</html>

When I scroll to the right the DIV does not use all the width:

How can I make the DIV to take 100% of the page width? Ideally I do not want to change the TABLE because it's a generated code.

解决方案

There is no way (perfectly dynamically--without fixing any widths--and with pure CSS--no JavaScript) to get a block-level element to inherit the width of a sibling block-level element via a block-level parent element.

The workaround is to put display: inline-block on the parent element, since an inline-block calculates its width property from its widest child.

In your case, don't put display: inline-block on the body element. I would suggest wrapping your 100% div and table elements in another wrapper element, thus:

<!DOCTYPE html>
<html lang="en">
<body>
  <div class="wrapper" style="display: inline-block; min-width: 100%;">
    <div style="background-color:yellow;">100% DIV</div>
    <table style="width:100%; background-color:orange;">
      <thead>
        <tr>
          <th style="padding-left:20px; padding-right:20px;">Very_Long_Header_1</th>
          <th style="padding-left:20px; padding-right:20px;">Very_Long_Header_2</th>
          <th style="padding-left:20px; padding-right:20px;">Very_Long_Header_3</th>
        </tr>
      </thead>
    </table>
  </div>
</body>
</html>

Also, putting min-width: 100% on the wrapper element will make it mimic an element with display: block on larger screen sizes.

这篇关于使用滚动条无法使DIV宽度达到100%的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆