CSS绝对位置元素延伸背景

发布于 2024-08-10 00:14:46 字数 730 浏览 7 评论 0原文

我有一个绝对位置 div,它与容器背景重叠,因为它具有较大的高度。该 div 与位于其左侧的 body div 共享容器。

有没有办法将容器扩展到绝对定位的 div 的高度,而不是主体内容的高度?

或者我应该并排浮动 div 并在两者的底部放置一个

?似乎是一个让容器扩展的混乱黑客:/

编辑:评论似乎不喜欢代码结构。所以我也将其编辑到这里。

布局是:

<div id="content">
   <div class="container">
      <div id="header"></div>
      <div id="main">
         <div id="column-1"></div>
         <div id="column-2"></div>
      </div>
   </div>
</div>

#content有重复的背景,#container设置页面的固定宽度。 #header 存放链接,#main 存放包含页面主要内容的两列。我无法让这两列彼此相邻(浮动/绝对),同时让 #content's 背景在它们下方重复

I have a absolutely position div that is overlapping a containers background due to it having a larger height. This div is sharing the container with a body div that's sitting happily to the left of it.

Is there a way to extend the container to be the height of the absolutely positioned div, rather than the body content?

Or should I just float the divs side by side and chuck a <div style="clear: both"></div> at the bottom of the two? Seems like a messy hack to get a container to extend :/

EDIT: Comments don't seem to like code structure. So I'll edit it into here as well.

The layout is:

<div id="content">
   <div class="container">
      <div id="header"></div>
      <div id="main">
         <div id="column-1"></div>
         <div id="column-2"></div>
      </div>
   </div>
</div>

#content has a repeated background and #container sets the fixed width of the page. #header sits up to for the links and #main holds the two columns which have the main content for the page. I can't get those two columns to sit next to each other (float / absolutely) whilst having the #content's background repeat down below them

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

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

发布评论

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

评论(1

带上头具痛哭 2024-08-17 00:14:46

使用此布局:

<div id="content">
  <div class="container">
    <div id="header"></div>
    <div id="main">
      <div id="column-1"></div>
      <div id="column-2"></div>
    </div>
  </div>
</div>

您的基本 CSS 应该类似于:

html, body, div { margin: 0; padding: 0; border: 0 none; }
body, #content { height: 100%; }
#main { overflow: hidden; }
#column-1 { float: left; width: 300px; }
#column-2 { float: left; width: 600px; }

您说您希望背景图像出现在内容下方。由此我假设您的意思是您希望页面为全屏高度(最小值)。

绝对定位的要点在于,它从正常流中删除了元素,因此您不能让它的“容器”扩展以包含它,因为从技术上讲它没有容器。

绝对定位有其一席之地,但十分之九的情况是我使用基于浮动的布局获得了更好的结果。但如果没有更多信息,我真的无法说更多。

With this layout:

<div id="content">
  <div class="container">
    <div id="header"></div>
    <div id="main">
      <div id="column-1"></div>
      <div id="column-2"></div>
    </div>
  </div>
</div>

your basic CSS should be something like:

html, body, div { margin: 0; padding: 0; border: 0 none; }
body, #content { height: 100%; }
#main { overflow: hidden; }
#column-1 { float: left; width: 300px; }
#column-2 { float: left; width: 600px; }

You said you wanted the background image appearing below the content. From this I assume you mean you want the page to be full screen height (minimum).

The point of absolute positioning is that it removes the element from the normal flow so no you can't have it's "container" extend to include it because technically it has no container.

Absolute positioning has its place but 9 times out of 10 I get better results with a float-based layout. But I can't really say more without more information.

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