gpt4 book ai didi

html - 如何将子 div 配置为仅在父 div 百分比设置需要时才显示滚动条

转载 作者:太空宇宙 更新时间:2023-11-04 05:30:53 26 4
gpt4 key购买 nike

我正在处理一个非常标准的(或者我认为的)页面。我想做的是允许页面的宽度由 <body> 决定标签和顶级 .page我在我的 CSS 中定义的样式。我的站点有一个左侧导航 Pane ,它始终具有一定的宽度。 “内容”区域随页面延伸,应由占用用户屏幕的剩余空间决定。

我想在与我的 float 区域宽度不“保持和平”的内容上显示滚动条。例如,我最近遇到了一系列需要显示滚动条的子元素,以防止页面一直延伸到永恒。

这是一个简单的图像,展示了不需要滚动条时的样子: alt text

这是我的子内容比页面宽时的样子: alt text

我想做的是在红色部分达到允许的空间(包括边距和填充)时在红色部分显示滚动条。我希望所有内容都很好地填充蓝色部分,这样黄色和白色部分就不会重叠。

我附上了我用来组成这个显示的 html,因为我想不出更好的方式来描述我的方法:

<html>
<head>
<style type="text/css">
body
{
background-color: gray;
}

.page /* page is always centered on screen */
{
background-color: white;
width: 90%;
margin-left: auto;
margin-right: auto;
padding: 10px;
}

.nav-pane /* navigation pane is always 100px */
{
width: 100px;
height: 100%;
background-color: green;
}

.nav-pane > div
{
width: 100px;
}

.content-pane /* content pane should size with page */
{
background-color: yellow;
margin: 10px;
}

.content-pane > div /* !!! SHOULD NOT STRETCH !!! */
{
position: relative;
overflow: auto;
background-color: blue;
padding: 10px;
margin: 10px;
color: white;
}

.content-pane > div > *
{
clear: both;
}

.content /* content div holds tables, images, paragraphs, etc.. */
{
background-color: red;
float: left;
margin-bottom: 20px;
}

#small /* one is small */
{
width: 200px;
}

#big /* one is BIG! */
{
width: 4000px;
}
</style>
</head>
<body>
<div class="page">
<table width="100%" style="border: solid black">
<tr>
<td class="nav-pane">
<div>
I am the nav-pane
</div>
</td>
<td class="content-pane">
<div>
I am the content pane

<h2>I am a heading</h2>
<div class="content">
<div id="small">Scrollbar not needed</div>
</div>

<h2>I too am a heading</h2>
<div class="content">
<div id="big">I require a scroll bar to keep this page pretty... :(</div>
</div>
</div>
</td>
</tr>
</table>
</div>
</body>
</html>

如果不使用 javascript,我尝试做的事情是否可行?

提前感谢您的任何见解...

最佳答案

向您的 table 添加一个 table-layout: fixed 样式应该可以实现我相信您想要的。将它添加为内联(就像你已经在做的那样)或作为你的 css 的一部分(我认为更好的方法)。

说明:您的问题在于 div 位于 table 中,默认情况下,表格单元格会扩展以适合其内容。使用 table-layout 属性设置表格以使其保持固定大小可防止表格增加大小。当然,它产生的滚动条实际上​​会在table上,而不是在div上。

关于html - 如何将子 div 配置为仅在父 div 百分比设置需要时才显示滚动条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3231683/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com