CSS div/overflow:为什么第一个 HTML 文件可以工作,但第二个却不行?

发布于 2024-08-27 11:32:39 字数 8934 浏览 6 评论 0原文

请注意当您水平调整浏览器大小时第一个 HTML/CSS 的工作原理。它的缩小不会超过 800 像素左右,但只要您拖动浏览器的右边缘,它就会扩大。它还会正确地溢出顶部的表格并水平滚动。

我不喜欢第一个代码片段的地方是滚动条的位置。我希望它显示在字段集的边框内,因此即使我将浏览器缩小到 800 像素宽,我也可以看到字段集边框的左侧和右侧。

第二个代码片段与第一个代码片段完全相同,只是我在字段集内部和网格周围添加了另一个 div 标签。请注意,当您缩小浏览器的视口时,顶部字段集的宽度不会正确缩小。关于为什么它不起作用的任何想法,我可以做些什么来让它像第一个代码片段一样工作?

我认为我没有清楚地描述这一点,但是如果您并排运行两者,并扩展和收缩浏览器窗口的水平边缘,您就会看到两者之间的差异。

我对 CSS 和 HTML 布局还很陌生,所以我对 CSS 在某些情况下处理大小的方式的理解仍然让我感到困惑。

工作 HTML 文件:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <meta http-equiv="Content-Style-Type" content="text/css"></meta>

    <style type="text/css">
        #divBody {
            margin-top: 5px;
            top:24px;
            margin-top: 10px;
        }            
        #divContainer 
        {
            top: 5px;
            position:relative;
            min-height:100%;
            #width:expression(document.body.clientWidth < 830? "800": "90%" );
            width:90%;
            min-width: 800px;
            padding-bottom:70px;
        }
        #divMasterGrid {
            position:relative;
            margin:5px;
            top:5px;
            width:99%;
            margin:0 auto;
            overflow-x:scroll;
        }
        #divRadioButtonArea {
            position:relative;
            top:20px;
            height:51px;
            font-size: 12px;
            width:99%;
            margin:5px;
        }
    </style>
    <title>TEST TEST</title>
</head>


<body id="divBody">
    <div id="divContainer" class="gridRegion">

        <div id="divMasterGrid">
            <fieldset style="margin: 5px;">
                <legend style="font-size: 12px; color: #000;">Numbers</legend>
                <table border="1px">
                <tr>
                    <td>One
                    </td>
                    <td>Two
                    </td>
                    <td>Three
                    </td>
                    <td>Fout
                    </td>
                    <td>Five
                    </td>
                    <td>Six
                    </td>
                    <td>Seven
                    </td>
                    <td>Eight
                    </td>
                    <td>Nine
                    </td>
                    <td>Ten
                    </td>
                    <td>Eleven
                    </td>
                    <td>Twelve
                    </td>
                    <td>Thirteen
                    </td>
                    <td>Fourteen
                    </td>
                    <td>Fifteen
                    </td>
                    <td>Sixteen
                    </td>
                    <td>Seventeen
                    </td>
                    <td>Eighteen
                    </td>
                    <td>Nineteen
                    </td>
                    <td>Twenty
                    </td>                        
                </tr>
                </table>
            </fieldset>
        </div>


        <div id="divRadioButtonArea">
            <fieldset style=" padding-left: 5px;">
                <legend style="color: #000;  height:auto">Colors</legend>
                <table style="width:100%;padding-left:5%;padding-right:5%;">
                    <tr>
                        <td>
                            <input type="radio" name="A" value="Y"/><label>Red</label>
                        </td>
                        <td>
                            <input type="radio" name="O" value="O"/><label>White</label>
                        </td>
                        <td>
                            <input type="radio" name="W"/><label>Blue</label>
                        </td>
                    </tr>
                </table>
            </fieldset>
        </div>

    </div>
</body>
</html>

损坏的 HTML 文件:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <meta http-equiv="Content-Style-Type" content="text/css"></meta>
    <style type="text/css">
        #divBody {
            margin-top: 5px;
            top:24px;
            margin-top: 10px;
        }            
        #divContainer 
        {
            top: 5px;
            position:relative;
            min-height:100%;
            #width:expression(document.body.clientWidth < 830? "800": "90%" );
            width:90%;
            min-width: 800px;
            padding-bottom:70px;
        }
        #divTopFieldSet {
            position:relative;
            margin:5px;
            top:5px;
            width:99%;
        }
        #divRadioButtonArea {
            position:relative;
            top:20px;
            height:51px;
            font-size: 12px;
            width:99%;
            margin:5px;
        }
        #divTable {
            position:relative;
            width:99%;
            margin:5px auto;
            overflow-x:scroll;
        }

    </style>
    <title>TEST TEST</title>
</head>


<body id="divBody">
    <div id="divContainer" class="gridRegion">

        <div id="divTopFieldSet">
            <fieldset style="margin: 5px;">
                <legend style="font-size: 12px; color: #000;">Numbers</legend>
                <div id="divTable">
                    <table border="1px">
                    <tr>
                        <td>One
                        </td>
                        <td>Two
                        </td>
                        <td>Three
                        </td>
                        <td>Fout
                        </td>
                        <td>Five
                        </td>
                        <td>Six
                        </td>
                        <td>Seven
                        </td>
                        <td>Eight
                        </td>
                        <td>Nine
                        </td>
                        <td>Ten
                        </td>
                        <td>Eleven
                        </td>
                        <td>Twelve
                        </td>
                        <td>Thirteen
                        </td>
                        <td>Fourteen
                        </td>
                        <td>Fifteen
                        </td>
                        <td>Sixteen
                        </td>
                        <td>Seventeen
                        </td>
                        <td>Eighteen
                        </td>
                        <td>Nineteen
                        </td>
                        <td>Twenty
                        </td>                        
                    </tr>
                    </table>
                </div>
            </fieldset>
        </div>


        <div id="divRadioButtonArea">
            <fieldset style=" padding-left: 5px;">
                <legend style="color: #000;  height:auto">Colors</legend>
                <table style="width:100%;padding-left:5%;padding-right:5%;">
                    <tr>
                        <td>
                            <input type="radio" name="A" value="Y"/><label>Red</label>
                        </td>
                        <td>
                            <input type="radio" name="O" value="O"/><label>White</label>
                        </td>
                        <td>
                            <input type="radio" name="W"/><label>Blue</label>
                        </td>
                    </tr>
                </table>
            </fieldset>
        </div>

    </div>
</body>
</html>

Notice how the first HTML/CSS works when you re-size the browser horizontally. It will shrink no further than around 800 pixels, but it will expand as far as you drag the right edge of the browser. It will also correctly overflow the table at the top and scroll it horizontally.

The thing I don't like about the first code snippet is where the scrollbar is. I want it to show up within the borders of the fieldset, so even if I narrow the browser down to 800 pixels wide, I can see both the left and right sides of the fieldset's border.

The second code snippet is exactly the same as the first except I add another div tag to the mix, inside of the field set and around the grid. Notice how the top fieldset's width won't correctly shrink when you make the viewport of your browser narrower. Any ideas on why it doesn't work, what I can do to get it to work like the first code snippet?

I don't think I'm describing this clearly, but if you run the two side by side, and expand and contract the horizontal edge of your browser windows, you'll see the differences between the two.

I'm pretty new to CSS and HTML layout, so my understanding of why CSS handles sizing the way it does in some situations is still really confusing to me.

Working HTML file:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <meta http-equiv="Content-Style-Type" content="text/css"></meta>

    <style type="text/css">
        #divBody {
            margin-top: 5px;
            top:24px;
            margin-top: 10px;
        }            
        #divContainer 
        {
            top: 5px;
            position:relative;
            min-height:100%;
            #width:expression(document.body.clientWidth < 830? "800": "90%" );
            width:90%;
            min-width: 800px;
            padding-bottom:70px;
        }
        #divMasterGrid {
            position:relative;
            margin:5px;
            top:5px;
            width:99%;
            margin:0 auto;
            overflow-x:scroll;
        }
        #divRadioButtonArea {
            position:relative;
            top:20px;
            height:51px;
            font-size: 12px;
            width:99%;
            margin:5px;
        }
    </style>
    <title>TEST TEST</title>
</head>


<body id="divBody">
    <div id="divContainer" class="gridRegion">

        <div id="divMasterGrid">
            <fieldset style="margin: 5px;">
                <legend style="font-size: 12px; color: #000;">Numbers</legend>
                <table border="1px">
                <tr>
                    <td>One
                    </td>
                    <td>Two
                    </td>
                    <td>Three
                    </td>
                    <td>Fout
                    </td>
                    <td>Five
                    </td>
                    <td>Six
                    </td>
                    <td>Seven
                    </td>
                    <td>Eight
                    </td>
                    <td>Nine
                    </td>
                    <td>Ten
                    </td>
                    <td>Eleven
                    </td>
                    <td>Twelve
                    </td>
                    <td>Thirteen
                    </td>
                    <td>Fourteen
                    </td>
                    <td>Fifteen
                    </td>
                    <td>Sixteen
                    </td>
                    <td>Seventeen
                    </td>
                    <td>Eighteen
                    </td>
                    <td>Nineteen
                    </td>
                    <td>Twenty
                    </td>                        
                </tr>
                </table>
            </fieldset>
        </div>


        <div id="divRadioButtonArea">
            <fieldset style=" padding-left: 5px;">
                <legend style="color: #000;  height:auto">Colors</legend>
                <table style="width:100%;padding-left:5%;padding-right:5%;">
                    <tr>
                        <td>
                            <input type="radio" name="A" value="Y"/><label>Red</label>
                        </td>
                        <td>
                            <input type="radio" name="O" value="O"/><label>White</label>
                        </td>
                        <td>
                            <input type="radio" name="W"/><label>Blue</label>
                        </td>
                    </tr>
                </table>
            </fieldset>
        </div>

    </div>
</body>
</html>

Broken HTML file:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <meta http-equiv="Content-Style-Type" content="text/css"></meta>
    <style type="text/css">
        #divBody {
            margin-top: 5px;
            top:24px;
            margin-top: 10px;
        }            
        #divContainer 
        {
            top: 5px;
            position:relative;
            min-height:100%;
            #width:expression(document.body.clientWidth < 830? "800": "90%" );
            width:90%;
            min-width: 800px;
            padding-bottom:70px;
        }
        #divTopFieldSet {
            position:relative;
            margin:5px;
            top:5px;
            width:99%;
        }
        #divRadioButtonArea {
            position:relative;
            top:20px;
            height:51px;
            font-size: 12px;
            width:99%;
            margin:5px;
        }
        #divTable {
            position:relative;
            width:99%;
            margin:5px auto;
            overflow-x:scroll;
        }

    </style>
    <title>TEST TEST</title>
</head>


<body id="divBody">
    <div id="divContainer" class="gridRegion">

        <div id="divTopFieldSet">
            <fieldset style="margin: 5px;">
                <legend style="font-size: 12px; color: #000;">Numbers</legend>
                <div id="divTable">
                    <table border="1px">
                    <tr>
                        <td>One
                        </td>
                        <td>Two
                        </td>
                        <td>Three
                        </td>
                        <td>Fout
                        </td>
                        <td>Five
                        </td>
                        <td>Six
                        </td>
                        <td>Seven
                        </td>
                        <td>Eight
                        </td>
                        <td>Nine
                        </td>
                        <td>Ten
                        </td>
                        <td>Eleven
                        </td>
                        <td>Twelve
                        </td>
                        <td>Thirteen
                        </td>
                        <td>Fourteen
                        </td>
                        <td>Fifteen
                        </td>
                        <td>Sixteen
                        </td>
                        <td>Seventeen
                        </td>
                        <td>Eighteen
                        </td>
                        <td>Nineteen
                        </td>
                        <td>Twenty
                        </td>                        
                    </tr>
                    </table>
                </div>
            </fieldset>
        </div>


        <div id="divRadioButtonArea">
            <fieldset style=" padding-left: 5px;">
                <legend style="color: #000;  height:auto">Colors</legend>
                <table style="width:100%;padding-left:5%;padding-right:5%;">
                    <tr>
                        <td>
                            <input type="radio" name="A" value="Y"/><label>Red</label>
                        </td>
                        <td>
                            <input type="radio" name="O" value="O"/><label>White</label>
                        </td>
                        <td>
                            <input type="radio" name="W"/><label>Blue</label>
                        </td>
                    </tr>
                </table>
            </fieldset>
        </div>

    </div>
</body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终止放荡 2024-09-03 11:32:39

我希望这就是您的意思:

我已将溢出 x 和宽度规则移至 #divTopFieldSet - 当正文变得太窄而无法包含表格时,div 的内容会正确滚动。

说实话,我并不能 100% 确定造成这种情况的根本原因。当具有百分比宽度的元素设置为溢出:滚动时,它不会自动收缩以裁剪其内容 - 看起来元素的子元素发挥了作用。因为表格有自己的最小宽度(由单元格的内容决定),所以周围的容器不会收缩超过表格的外边缘。最重要的是,还有百分比宽度的计算 - width:99%; 表示“父容器宽度的 99%” - 如果父容器不基于视口的大小,当您调整大小时,99% 也不会改变。

不过,将溢出规则应用于树上的容器 (divTopFieldSet) 确实有效。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <meta http-equiv="Content-Style-Type" content="text/css"></meta>
    <style type="text/css">
        #divBody {
            margin-top: 5px;
            top:24px;
            margin-top: 10px;
        }            
        #divContainer 
        {
            top: 5px;
            position:relative;
            min-height:100%;
            #width:expression(document.body.clientWidth < 830? "800": "90%" );
            width:90%;
            min-width: 800px;
            padding-bottom:70px;
        }
        #divTopFieldSet {
            position:relative;
            margin:5px;
            top:5px;
            width:99%;
            overflow-x:scroll;
        }
        #divRadioButtonArea {
            position:relative;
            top:20px;
            height:51px;
            font-size: 12px;
            width:99%;
            margin:5px;
        }
        #divTable {
            margin:5px auto;
        }

    </style>
    <title>TEST TEST</title>
</head>


<body id="divBody">
    <div id="divContainer" class="gridRegion">

        <div id="divTopFieldSet">
            <fieldset style="margin: 5px;">
                <legend style="font-size: 12px; color: #000;">Numbers</legend>
                <div id="divTable">
                    <table border="1px">
                    <tr>
                        <td>One
                        </td>
                        <td>Two
                        </td>
                        <td>Three
                        </td>
                        <td>Fout
                        </td>
                        <td>Five
                        </td>
                        <td>Six
                        </td>
                        <td>Seven
                        </td>
                        <td>Eight
                        </td>
                        <td>Nine
                        </td>
                        <td>Ten
                        </td>
                        <td>Eleven
                        </td>
                        <td>Twelve
                        </td>
                        <td>Thirteen
                        </td>
                        <td>Fourteen
                        </td>
                        <td>Fifteen
                        </td>
                        <td>Sixteen
                        </td>
                        <td>Seventeen
                        </td>
                        <td>Eighteen
                        </td>
                        <td>Nineteen
                        </td>
                        <td>Twenty
                        </td>                        
                    </tr>
                    </table>
                </div>
            </fieldset>
        </div>


        <div id="divRadioButtonArea">
            <fieldset style=" padding-left: 5px;">
                <legend style="color: #000;  height:auto">Colors</legend>
                <table style="width:100%;padding-left:5%;padding-right:5%;">
                    <tr>
                        <td>
                            <input type="radio" name="A" value="Y"/><label>Red</label>
                        </td>
                        <td>
                            <input type="radio" name="O" value="O"/><label>White</label>
                        </td>
                        <td>
                            <input type="radio" name="W"/><label>Blue</label>
                        </td>
                    </tr>
                </table>
            </fieldset>
        </div>

    </div>
</body>
</html>

如果这不是您需要的,请告诉我们...

I hope this is what you mean:

I've moved the overflow-x and width rules to #divTopFieldSet - the contents of the div correctly scrolls when the body becomes too narrow to contain the table.

To tell you the truth, I'm not 100% sure of the root cause of this. When an element with a percentage width is set to overflow:scroll, it doesn't automatically shrink to crop its contents - it seems like the children of the element play a part. Because the table has a minimum width of its own (determined by the contents of the cells), the surrounding container will not shrink past the outer edge of the table. On top of that, there is the calculation of percentage widths - width:99%; means '99% of the parent container's width' - if the parent container isn't based on the size of the viewport, that 99% won't change when you resize, either.

Applying the overflow rule to a container further up the tree (divTopFieldSet) does work, though.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <meta http-equiv="Content-Style-Type" content="text/css"></meta>
    <style type="text/css">
        #divBody {
            margin-top: 5px;
            top:24px;
            margin-top: 10px;
        }            
        #divContainer 
        {
            top: 5px;
            position:relative;
            min-height:100%;
            #width:expression(document.body.clientWidth < 830? "800": "90%" );
            width:90%;
            min-width: 800px;
            padding-bottom:70px;
        }
        #divTopFieldSet {
            position:relative;
            margin:5px;
            top:5px;
            width:99%;
            overflow-x:scroll;
        }
        #divRadioButtonArea {
            position:relative;
            top:20px;
            height:51px;
            font-size: 12px;
            width:99%;
            margin:5px;
        }
        #divTable {
            margin:5px auto;
        }

    </style>
    <title>TEST TEST</title>
</head>


<body id="divBody">
    <div id="divContainer" class="gridRegion">

        <div id="divTopFieldSet">
            <fieldset style="margin: 5px;">
                <legend style="font-size: 12px; color: #000;">Numbers</legend>
                <div id="divTable">
                    <table border="1px">
                    <tr>
                        <td>One
                        </td>
                        <td>Two
                        </td>
                        <td>Three
                        </td>
                        <td>Fout
                        </td>
                        <td>Five
                        </td>
                        <td>Six
                        </td>
                        <td>Seven
                        </td>
                        <td>Eight
                        </td>
                        <td>Nine
                        </td>
                        <td>Ten
                        </td>
                        <td>Eleven
                        </td>
                        <td>Twelve
                        </td>
                        <td>Thirteen
                        </td>
                        <td>Fourteen
                        </td>
                        <td>Fifteen
                        </td>
                        <td>Sixteen
                        </td>
                        <td>Seventeen
                        </td>
                        <td>Eighteen
                        </td>
                        <td>Nineteen
                        </td>
                        <td>Twenty
                        </td>                        
                    </tr>
                    </table>
                </div>
            </fieldset>
        </div>


        <div id="divRadioButtonArea">
            <fieldset style=" padding-left: 5px;">
                <legend style="color: #000;  height:auto">Colors</legend>
                <table style="width:100%;padding-left:5%;padding-right:5%;">
                    <tr>
                        <td>
                            <input type="radio" name="A" value="Y"/><label>Red</label>
                        </td>
                        <td>
                            <input type="radio" name="O" value="O"/><label>White</label>
                        </td>
                        <td>
                            <input type="radio" name="W"/><label>Blue</label>
                        </td>
                    </tr>
                </table>
            </fieldset>
        </div>

    </div>
</body>
</html>

Let us know if this isn't what you need...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文