在主居中的 div 旁边浮动另一个 div

发布于 2024-07-20 07:01:25 字数 174 浏览 3 评论 0原文

好吧,我有一个网站,整个东西都包装在一个容器中。 该容器以 margin:auto 居中。 我想将一段内容浮动到这个居中容器的右侧,并让它粘在它的一侧,无论用户是否调整浏览器窗口的大小等。我想知道是否有一种真正简单的方法执行此操作,而不是添加另一个巨大的 div,为其提供宽度并将居中部分浮动到左侧,将内容块浮动到右侧。 谢谢!

Ok so I have a website and the entire thing is wrapped in a container. This container is centered with margin:auto. I would like to float a piece of content to the right of this centered container and have it sort of stick to the side of it no matter if the user resizes the browser window, etc. I'm wondering if there's a real simple way to do this rather than adding another huge div, giving it width and floating the centered portion to the left and the piece of content to the right. Thanks!

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

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

发布评论

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

评论(5

少女七分熟 2024-07-27 07:01:25

搭载@NickAllen,您想要使用绝对定位,以便侧边栏的宽度不包含在主容器的居中中。

#container {
  position: relative;
  width: 500px;
  border: 1px solid #f00;
  margin: 0px auto;
}
#sidebar {
  position: absolute;
  width: 200px;
  right: -200px;
  border: 1px solid #0f0;
}
<div id="container">
  <div id="sidebar">
    [ sidebar content ]<br>
    [ sidebar content ]<br>
  </div>
  [content]<br>
  [content]<br>
  [content]<br>
</div>

Piggybacking on @NickAllen, you want to use absolute positioning so that the width of the sidebar isn't included in the centering on the primary container.

#container {
  position: relative;
  width: 500px;
  border: 1px solid #f00;
  margin: 0px auto;
}
#sidebar {
  position: absolute;
  width: 200px;
  right: -200px;
  border: 1px solid #0f0;
}
<div id="container">
  <div id="sidebar">
    [ sidebar content ]<br>
    [ sidebar content ]<br>
  </div>
  [content]<br>
  [content]<br>
  [content]<br>
</div>

沙沙粒小 2024-07-27 07:01:25

也许我误解了你的问题,但这不是你想要的:

    #container {
        width: 960px;
        margin: 0px auto;
    }
    
    #sidebar {
        float: right;
    }
<div id="container">
        <div id="sidebar">
        some content
        </div>
    </div>

Maybe I'm misunderstanding your question, but isn't this what you want:

    #container {
        width: 960px;
        margin: 0px auto;
    }
    
    #sidebar {
        float: right;
    }
<div id="container">
        <div id="sidebar">
        some content
        </div>
    </div>

南渊 2024-07-27 07:01:25

给容器div赋予属性position:relative; 将您的浮动 div 作为容器 div 的第一个子级并给它

#floatingDiv
{
    position: absolute;
    top: 0;
    right: -{widthOfFloatedDiv};
}

我认为这会起作用,但未经测试

好的,所以测试了它并且它起作用

<div style="position: relative; width: 980px; margin: 0 auto; border: 1px solid #000; height: 400px;">
    <div style="position: absolute; top: 0; right: -200px; width: 200px;">
        <p>Floated DIV</p>
    </div>
    <p>container div</p>
</div>

Give the container div the property position: relative; place your floating div as the first child of the container div and give it

#floatingDiv
{
    position: absolute;
    top: 0;
    right: -{widthOfFloatedDiv};
}

I think that will work, but untested

Okay so tested it and it works

<div style="position: relative; width: 980px; margin: 0 auto; border: 1px solid #000; height: 400px;">
    <div style="position: absolute; top: 0; right: -200px; width: 200px;">
        <p>Floated DIV</p>
    </div>
    <p>container div</p>
</div>
甜嗑 2024-07-27 07:01:25

老问题,但贡献,因为其他答案有点缺乏那个“东西”,而且它仍然在谷歌之上。 实现这一目标的最简单、最干净的方法是使用两个包装器

<div class="bigWrapper">
  <div class="sidebar">Hello, I'm your sidebar</div>      

  <div class="smallWrapper">
    Put the thing you want to center here.
  </div>
</div>

使用以下 CSS:

.bigWrapper { 
    width: 1000px;
    height: auto;
    margin: auto;
}

.smallWrapper {
    width: 500px;
    height: auto;
    margin: auto;
}

.sidebar {
    width: 250px;
    float: left;
    height: auto;
}

Old question, but contributing since other answers were kinda lacking that "something" and it's still on top of Google. The simplest, cleanest way to achieve this is with two wrappers.

<div class="bigWrapper">
  <div class="sidebar">Hello, I'm your sidebar</div>      

  <div class="smallWrapper">
    Put the thing you want to center here.
  </div>
</div>

With the following css:

.bigWrapper { 
    width: 1000px;
    height: auto;
    margin: auto;
}

.smallWrapper {
    width: 500px;
    height: auto;
    margin: auto;
}

.sidebar {
    width: 250px;
    float: left;
    height: auto;
}
情深已缘浅 2024-07-27 07:01:25

浮动是实现这一目标的方法。 它们总是粘在一起,除非容器小于它们宽度的总和。

提示:确保容器足够宽以容纳两个内部 div; 如果没有,并且用户的窗口较窄,他们将在另一个下方显示一个。

Floating is the way to go for this. They will always stick together, unless the container is smaller than the sum of their widths.

Tip: make sure your container is wide enough to hold both inner divs; if not, and the user has a narrower window, they will show one below the other.

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