在 2 列布局中,如果删除另一列,如何使一列扩展到总宽度?
用标记更好地解释:
HTML:
<div><!--both sidebar and content share width-->
<div id="content"><!--maybe at 30%-->
</div>
<div id="sidebar"><!--may be at 70%-->
</div>
</div>
CSS:
#sidebar{float:left;} /* no idea what width*/
#content{float:right;} /* no idea what width*/
我想要做的是,如果标记中不存在侧边栏,则内容 div 会展开以取代侧边栏:
<div>
<div id="content"><!--width now at 100%-->
</div>
</div>
Better explained with a markup:
HTML:
<div><!--both sidebar and content share width-->
<div id="content"><!--maybe at 30%-->
</div>
<div id="sidebar"><!--may be at 70%-->
</div>
</div>
CSS:
#sidebar{float:left;} /* no idea what width*/
#content{float:right;} /* no idea what width*/
What I want to do is that if the sidebar is not present in the markup, the content div expands to take the place of the sidebar:
<div>
<div id="content"><!--width now at 100%-->
</div>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
纯 CSS 方法将使用 CSS table-layout:
CSS:
HTML:
然而,上述内容不适用于 IE6/7 等较旧的浏览器。使用 javascript(如本文其他答案中提到的)作为后备将是理想的选择。
A pure CSS method will be using CSS table-layout:
CSS:
HTML:
The above however will not work for older browsers like IE6/7 etc. Using javascript (as mentioned by other answers to this post) as a fallback will be ideal.
如果您像这样翻转您的 div,就可以实现此目的:
这种方法的唯一缺点是,如果您想向内容 div 添加任何填充,您将需要知道侧边栏的宽度。这是可以解决的,但因为这不是你真正的问题,我不会告诉你如何吸鸡蛋,我相信你能解决;)
You can acheive this if you flip your divs around like so:
The only drawback with this approach is that you will need to know how wide your sidebar is if you want to add any padding to your content div. That's fixable, but as that wasn't really your question, I won't tell you how to suck eggs, I'm sure you can work it out ;)
这个很棘手:
如果你在侧边栏上设置宽度,使其浮动,并且不要在内容上设置宽度(另外,也不要浮动内容,因为这会导致 div“收缩包装”) 。然后该列将按照您的预期运行。
我想补充一点,这本质上是不稳定的,而且往往是有限制的。最好的选择是,如果您控制服务器端的布局(您有一些生成布局的脚本),则在渲染之前将 css 类附加到文档中。
纯 html 解决方案是可能的,但它也依赖于这种特定的行为。如果您想浮动#content div,它将不起作用(因为您将被迫指定尺寸以避免收缩包装效果)。
您也可以使用 JavaScript 来完成此操作。需要注意的是,当 javascript 启动时,您可能会得到 FOUC(无样式内容的闪存)。
使用 jQuery,您可以执行诸如计算相邻 div 之类的操作,即
然后使用结果执行以下操作:
This one is tricky:
If you set the width on the sidebar, float it, and don't set the width on the content (also, don't float the content either as this will cause the div to "shrink-wrap"). Then the column will behave like you desired.
I'd like to add that this is intrinsically flaky and often limiting. Your best bet is, if you control the layout on the server side (you've got some script that generates layouts) then append css classes to the document before it's rendered.
A pure html solution is possible, but it also relies on this particular behavior. If you want to float the #content div, it won't work (because you'll be forced to specify the dimensions to avoid the shrink wrapping effect).
You can also do this using javascript. The thing to watch out for there is you might get a FOUC (flash of unstyled content) as the javascript kicks in.
With jQuery you could do something like count the adjacent divs i.e.
Then use the result to do something like:
功能:
运行该功能
然后您可以随时添加样式块来隐藏所有内容或任何您需要执行的操作。
您也可以使用切换按钮,但这种方式使用 div 效果非常好。
然而你真的想去做这件事。
Function:
To run the function
Then you can always add in style block to hide all content or whatever you need to do also.
You could also use a toggle but this way can work really nice using div's.
However you want to go about it really.
查看此技术:
http://www.webdesignerwall.com/tutorials /css-clearing-floats-with-overflow/
它非常灵活,不需要服务器端代码。
IMO 事情应该分开,服务器端 - 用于服务器端任务,前端 - 用于前端任务。
祝你好运
Check out this technique:
http://www.webdesignerwall.com/tutorials/css-clearing-floats-with-overflow/
It's very flexible and does not require server side code.
IMO things should be separated, server side - for server side tasks, front end - for front end tasks.
Good Luck