在主居中的 div 旁边浮动另一个 div
好吧,我有一个网站,整个东西都包装在一个容器中。 该容器以 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
搭载@NickAllen,您想要使用绝对定位,以便侧边栏的宽度不包含在主容器的居中中。
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.
也许我误解了你的问题,但这不是你想要的:
Maybe I'm misunderstanding your question, but isn't this what you want:
给容器div赋予属性position:relative; 将您的浮动 div 作为容器 div 的第一个子级并给它
我认为这会起作用,但未经测试
好的,所以测试了它并且它起作用
Give the container div the property position: relative; place your floating div as the first child of the container div and give it
I think that will work, but untested
Okay so tested it and it works
老问题,但贡献,因为其他答案有点缺乏那个“东西”,而且它仍然在谷歌之上。 实现这一目标的最简单、最干净的方法是使用两个包装器。
使用以下 CSS:
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.
With the following css:
浮动是实现这一目标的方法。 它们总是粘在一起,除非容器小于它们宽度的总和。
提示:确保容器足够宽以容纳两个内部 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.