绝对定位的 div 上的边距折叠?

发布于 2024-10-05 05:45:27 字数 1407 浏览 2 评论 0原文

我总共有 4 个 div:

1 个容器 1 个子菜单 1 内容 1 侧边栏

它们的设置如下:

<container>
     <submenu></submenu>
     <content></content>
     <sidebar></sidebar>
</container>

容器具有position:relative和overflow:hidden

子菜单,内容具有float:left,用于定位

侧边栏的边距绝对位于右侧(浮动将不起作用,因为子栏会影响布局。

我有在侧边栏内垂直列出的图像,但如果我浮动或将位置设置为相对位置,它们会被容器截断,我已经在侧边栏之后尝试了清晰的修复,但事实并非如此。这

是我经常遇到的问题之一

-----编辑------。

<div id="content">
    <div id="submenu">
    </div>
    <div id="contentBox">   
    </div>
    <div id="sidebar">
    </div>
</div>

#content
{
    margin: -22px auto 0 auto;
    overflow: hidden;
    padding: 0 10px 10px 10px;
    position: relative;
    text-align: left;
    width: 961px;
}

#submenu
{
    background-color: #001a28;
    border: 5px #008da8 solid;
    float: left;
    margin: -20px 0 0 -10px;
    padding-top: 20px;
    width: 700px;
}

#contentBox 
{
    background-color: #ffffff;
    float: left;
    margin: 10px 0 10px -10px;
    width: 710px;
}

#sidebar
{
    border-left: 5px #008da8 solid;
    border-right: 5px #008da8 solid;
    background-color: #ffffff;
    display: block;
    height: 100%;
    position: relative;
    float: right;
    text-align: center;
    right: 10px;
    top: -10px;
    width: 207px;   
}

I have 4 div's total:

1 container
1 submenu
1 content
1 sidebar

They are set up as so:

<container>
     <submenu></submenu>
     <content></content>
     <sidebar></sidebar>
</container>

The container has position:relative and overflow:hidden

submenu and conent has float:left with margins used to position

sidebar is absolutely positioned to the right (float will not work since the subbar affects the layout.

I have images that are listed vertically inside the sidebar but they are cut off by the container. if I float or set position to relative it will contain the sidebar properly. I've tried a clear fix after the side bar but that doesn't work.

This is one of those issues I always see to have problems with. Any suggestions?

-----edit------

<div id="content">
    <div id="submenu">
    </div>
    <div id="contentBox">   
    </div>
    <div id="sidebar">
    </div>
</div>

#content
{
    margin: -22px auto 0 auto;
    overflow: hidden;
    padding: 0 10px 10px 10px;
    position: relative;
    text-align: left;
    width: 961px;
}

#submenu
{
    background-color: #001a28;
    border: 5px #008da8 solid;
    float: left;
    margin: -20px 0 0 -10px;
    padding-top: 20px;
    width: 700px;
}

#contentBox 
{
    background-color: #ffffff;
    float: left;
    margin: 10px 0 10px -10px;
    width: 710px;
}

#sidebar
{
    border-left: 5px #008da8 solid;
    border-right: 5px #008da8 solid;
    background-color: #ffffff;
    display: block;
    height: 100%;
    position: relative;
    float: right;
    text-align: center;
    right: 10px;
    top: -10px;
    width: 207px;   
}

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

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

发布评论

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

评论(1

咿呀咿呀哟 2024-10-12 05:45:27

我还没有找到一种简单的方法来使 div 为父元素高度的 100%。我过去曾经能够作弊:而不是尝试使拉伸到 100% 高度,我在中使用垂直背景拉伸在侧边栏所在位置创建一条条纹,并使bg-透明。这样,无论的高度是多少,也就是说,总会有一种错觉,认为它是<容器>高度的100%。

一旦你有了这个,只要确保你的图像没有像你描述的那样被裁剪,它应该是相同的效果。不过,如果不了解您想要实现的目标,我无法判断这是否对您有用。希望这有帮助!

I haven't found an easy way to make a div 100% of the parent element's height. I've been able to cheat in the past: instead of trying to make the <sidebar> stretch to 100% height, I use a vertical background stretch in the <container> to create a stripe where the sidebar sits, and make the <sidebar> bg-transparent. That way, no matter what the height of the <sidebar> is, there will always be an illusion of it being 100% of the <container>'s height.

Once you have that, just make sure your images aren't being cropped like you described, and it should be the same effect. Though, I can't tell if this will be useful to you without knowing more about what you're trying to accomplish. Hope this helps!

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