gpt4 book ai didi

html - 使背景颜色延伸到溢出区域

转载 作者:行者123 更新时间:2023-11-27 23:19:24 24 4
gpt4 key购买 nike

如果父级的总内容高度为 10,000px,但 overflow: auto 元素呈现的高度为 700px 怎么办我强制 aside 子元素动态呈现为 10,000px 而不是 默认的 700px?您可以在开始滚动时看到白色背景 the Fiddle .

  • 不能再添加任何元素(::after::before 是可以接受的)。
  • aside 元素的内容必须通过 #body 元素与 main 元素的内容一起滚动(没有 position: fixed; ).
  • aside 元素的 background-color 必须从最顶部 0px 延伸到最底部(例如 5,000px 或 10,000px)下方 最初可见的折叠。
  • aside 元素不能有它自己的 overflow: auto;
  • 动态(对于知识较少的人)意味着我们可以设置静态高度,例如height: 10000px 因为我们不知道渲染的高度是多少。

在我的示例中,当您开始滚动绿色 background-color 结尾时,我想让 aside 元素一直延伸到内容底部。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Overflow Flex Box Issue</title>
<style type="text/css">
* {border: 0; margin: 0; padding: 0;}

aside
{
background-color: #afa;
order: 2;
width: 20%;
}

body
{
display: flex;
flex-direction: column;
height: 100%;
}

body > header
{
align-self: stretch;
background-color: #faa;
flex: 0 1 auto;
min-height: 56px;
order: 1;
}

body > footer
{
align-self: auto;
background-color: #aaf;
flex: 0 1 auto;
min-height: 36px;
order: 2;
}

html {height: 100%;}

main
{
background-color: #cfc;
order: 1;
width: 80%;
}

#body
{
display: flex;
order: 2;
overflow: auto;
}

</style>
</head>

<body>

<div id="body">
<main>
<article>
<p>article</p>
<p>article</p>
<p>article</p>
<div style="height: 10000px;">10,000px</div>
</article>
</main>
<aside><p>&#60;aside&#62;, 100% height only of visible area, it <em>should</em> be <em>100% of total parent height</em>.</p></aside>
</div>

<header>The body &#62; header element; element 2, order: 1;.</header>
<footer>The body &#62; footer element; element 3, order: 3;.</footer>

</body>
</html>

最佳答案

除了绝对定位,这是 CSS 做不到的。您需要使用 JavaScript。

问题是:

第一部分:background-color

您没有为内容元素定义高度 (#body)。

这意味着高度是由内容驱动的。

背景色只会覆盖元素的宽度和高度。您可以在演示中看到这一点。一旦滚动开始,背景颜色就会结束。那是因为溢出区域在元素的宽/高区域之外。

来自规范:

14.2 The background

Authors may specify the background of an element (i.e., its rendering surface) as either a color or an image. In terms of the box model, "background" refers to the background of the content, padding and border areas.

因此 CSS 背景属性旨在覆盖元素边界的区域。它们不覆盖边缘区域。它们不会溢出。


第二部分:溢出

这是截断背景颜色的另一个原因。

overflow 属性仅适用于内容。它与背景无关。

来自规范:

11.1.1 Overflow: the overflow property

This property specifies whether content of a block container element is clipped when it overflows the element's box.


由于存在这两个障碍,CSS 无法解决此问题(除了可能的 hack)。使背景颜色填充动态大小容器的整个长度的唯一方法是使用脚本。

关于html - 使背景颜色延伸到溢出区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58100020/

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