当第 2 列不存在时,将第 1 列扩展到最大宽度
这是我的标记:
<div class="wrapper">
<div class="content">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed mi ipsum, gravida quis eleifend at, vestibulum at nibh. Nunc faucibus pellentesque nunc, vitae ultricies nibh interdum eu. Proin a est sed eros suscipit pretium ac sit amet tortor.</p>
<p>Vivamus feugiat, neque non tincidunt iaculis, dolor ipsum convallis libero, condimentum malesuada leo nulla a turpis. Praesent sed metus ipsum. Cras semper condimentum mauris. Nulla eleifend blandit facilisis. Phasellus gravida tempus eros, molestie cursus nisi imperdiet non. Donec dapibus.</p>
</div>
<div class="adverts"></div>
<div class="clear"></div>
</div>
Fiddle here -- 随意修改和测试
我想要一个 CSS 解决方案,使内容宽度等于最大可用宽度或最大可用宽度 - 120px,具体取决于广告 div 是否存在。 div 可能存在也可能不存在,具体取决于页面是否应该显示广告。该解决方案必须适用于旧版本的 IE。
Here is my markup:
<div class="wrapper">
<div class="content">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed mi ipsum, gravida quis eleifend at, vestibulum at nibh. Nunc faucibus pellentesque nunc, vitae ultricies nibh interdum eu. Proin a est sed eros suscipit pretium ac sit amet tortor.</p>
<p>Vivamus feugiat, neque non tincidunt iaculis, dolor ipsum convallis libero, condimentum malesuada leo nulla a turpis. Praesent sed metus ipsum. Cras semper condimentum mauris. Nulla eleifend blandit facilisis. Phasellus gravida tempus eros, molestie cursus nisi imperdiet non. Donec dapibus.</p>
</div>
<div class="adverts"></div>
<div class="clear"></div>
</div>
Fiddle here -- feel free to modify and test
I want a CSS solution that makes the content width equal to max available width or max available width - 120px depending on whether adverts div is present or not. The div may or may not be present depending on whether the page is supposed to show ads or not. The solution must work in older versions of IE.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
@Salman,如果您重新排列两个 div 的顺序,那么您可以在没有任何宽度的情况下执行此操作,只需
浮动:右
“广告”div 并且不浮动 根据其他答案之一的内容,您无法在 IE 中的未浮动 div 之后正确浮动 div 以达到相同的效果,而不涉及宽度/边距
添加:这是您更新的示例小提琴:http://jsfiddle.net/clairesuzy/EqYnw/8/
Example Fiddle
jQuery 只是切换实际的广告元素以显示如果广告 div 中没有内容,它将崩溃,代码中解释了溢出用法。
我不确定您的“不存在”div 是否是实际的“广告”div 或其中的元素,但这应该适用于两者,因为内容将默认为 100% 剩余的信息
代码:
CSS:
HTML:
@Salman, if you rearrange the order of the two divs then you can do this without any widths, you just
float:right
the "adverts" div and don't float the contentas per one of the other answers you cannot right float a div after an unfloated one in IE to achieve this same effect without a width/margin being involved
added: Here's Your example fiddle updated : http://jsfiddle.net/clairesuzy/EqYnw/8/
Example Fiddle
The jQuery just toggles the actual ad-element to show that if there no content in the adverts div it will collapse the overflow usage is explained in the code.
I'm not sure if your "not present" div is the actual "adverts" div or the elements inside it, but this should work for both as the content will default to 100% of whatever is left over
Code for info:
CSS:
HTML:
我假设第 1 列是
div.content
的内容,当第二列 (div.adverts
) 为空的。您可以让包装和内容不浮动,并让广告向右浮动。非浮动 div 应使用所有可用空间,并且正确的浮动应使文本环绕其周围。只要广告 div(当不为空时)比内容高,它就会显示为两列。 (我认为清除 div 是浮动清除黑客或其他什么?)
您还可以使用表格(政治上不正确,但更容易使其在旧版浏览器中工作)。让表格的宽度为 100%,并且不为表格单元格指定任何宽度。空单元格应使用零空间。 (即使长度不同,没有任何复杂的 css,这也会给出两列布局)
在任何情况下:为了避免为空元素定义样式导致其可见的错误,请改为设置子元素的样式(如果它们不可见)目前的 css 无论如何都不会应用,如下所示:
I assume column 1 is the content of
div.content
, there's at leas two ways you can get this to use max available space when the second column (div.adverts
) is empty.You can let the wrapper and content be non-floating, and let the adverts one float to the right. The non-floated divs should use all available space, and the right float should cause the text to wrap around it. As long as the adverts div (when not empty) is taller than the content it will appear as two columns. (I assume the clear div is a float clearing hack or something?)
You could also use a table (not politically correct, but a lot easier to make it work in older browsers). Let the table be 100% widht, and don't specify any widht for the table cells. An empty cell should use zero space. (This will give a two column layout even if the lenght is different without any complicated css)
In any case: To avoid bugs where defining styles for an empty element causes it to be visible, style the sub elements instead, if they're not present the css will not apply anyway, like this:
对于
.advert
div 根本不在 HTML 中的情况,您可以使用通用同级选择器+
。让
.advert
向右浮动,并将其放在 HTML 中的.content
div 之前(对于 CSS 选择器)如果
>.adverts
div 存在。这不能与
show()
和hide()
动态配合使用。如果您动态需要,则必须从 DOM 中删除.advert
。这是一个 fiddle ,其中使用
detach()
而不是进行了演示>hide()
(在 IE6 中的虚拟机上工作)。You could use the general sibling selector
+
for the case the.advert
div is not in the HTML at all.Let the
.advert
float right, and have it before your.content
div in the HTML (for CSS selector)Use the sibling selector to define the smaller width if the
.adverts
div exists.This will not work dynamically with
show()
andhide()
. If you require that dynamically, you have to remove the.advert
from the DOM.Here is a fiddle with a demonstration using
detach()
instead ofhide()
(worked on a VM in IE6).