如何在不隐藏 DIV 部分的情况下使用 CSS 浮动

发布于 2024-08-07 16:39:36 字数 1707 浏览 2 评论 0原文

当在某个 DIV 上使用 CSS 浮动时,其他未浮动的 DIV 会继续占用浮动 DIV 的空间。虽然我确信这是故意的,但我不知道如何达到我想要的效果。请考虑这个例子:

<html>

<div style="width:400px">

<div style="width:150px;float:right;border:thin black solid">
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam.</div>

Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. 

<div style="background-color:red;border:thin black solid">Some sample text</div>

Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.
</div>

</html> 

如果您将此 HTML 粘贴到浏览器中(或检查其 jsfiddle),您将请注意,“某些示例文本”是红色的,并且红色背景一直延伸到浮动 DIV。虽然我确信有一种方法可以遮盖我不想要的红色背景部分,但它仍然会将边框裁剪掉并隐藏在 DIV 下方。理想情况下,我希望背景颜色和边框仅占据有效的文本空间,而不是在浮动 DIV 下方蔓延。这种效果可能吗?

请注意,我使用浮动属性不是作为列的替代,而是作为一个让文本围绕其流动的块。到目前为止,所提出的解决方案都没有考虑到这一点。为了清楚起见,这里有一些图像。

这就是我得到的:

Bad

这就是我想要的:

Good

您会注意到,在这两个示例中,当文本到达底部时,最终文本会环绕浮动部分。我的第二个例子可以通过直接指定包含“一些示例文本”的 div 的宽度来实现。我不想指定宽度。这似乎是多余的,因为我想要与其周围文本的宽度相同。但也许这是不可能的?

When CSS float is used on a DIV, other DIVs that are not floated continue to occupy the space of the floated DIV. While I am sure this is intentional, I do not know how to achieve the effect I am looking for. Please consider this example:

<html>

<div style="width:400px">

<div style="width:150px;float:right;border:thin black solid">
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam.</div>

Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. 

<div style="background-color:red;border:thin black solid">Some sample text</div>

Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.
</div>

</html> 

If you paste this HTML in a browser (or examine its jsfiddle), you'll notice that "Some sample text" is red, and that the red background extends all the way through the floated DIV. While I am sure there is a way to obscure the parts of the red background I don't want, it would still leave the border cropped off and hidden underneath the DIV. Ideally, I want the background color and border to only occupy valid text space, and not creep underneath the floated DIV. Is this effect possible?

Please note that I am using the float property not as a replacement for columns, but as a block that will have the text flow around it. So far none of the proposed solutions takes this into account. For clarity, here are some images.

This is what I get:

Bad

This is what I want:

Good

You'll notice that in both examples, the final text wraps around the floated part as the text reaches the bottom. My second example I can achieve by directly specifying the width of the div containing "Some sample text". I do not want to specify the width. It seems redundant since I want the same width as that of the text around it. But perhaps that isn't possible?

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

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

发布评论

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

评论(6

探春 2024-08-14 16:39:36

这里有一个解决方案:

<div style="background-color:red;border:thin black solid;overflow:hidden;">Some sample text</div>

基本上,这里的魔力是 overflow:hidden,它改变了 CSS 视觉格式化模型。查看 CSS 布局基础知识,第 5 部分:浮动

修复相邻框

让我们看看红色的段落边框
问题先。原因是
段落边框出现在后面
图像是因为浮点数没有
重新定位块盒
与他们相邻。在此示例中,
浮动图像只是推动线
段落内的方框。所以
文本被推到右侧,但是
实际的段落框仍然
延伸到整个宽度
容器。

此问题的修复来自
非常好的隐藏段落
CSS 2.1 的浮动部分
规格:

<块引用>

表格的边框,a
块级替换元素,或
正常流程中的元素
建立新的块格式
上下文
(例如带有
“可见”以外的“溢出”)必须
不重叠相同的任何浮动
块格式化上下文作为
元素本身。

所以为了防止我们的段落块
重叠浮动内容,
解决方案是创建一个“新块
格式化上下文”,在 CSS 中
规范作者术语。
听起来很棘手,是吗?幸运的是,它
没那么难。新的块格式
上下文由任何块创建
满足这些标准:

  • 浮动
  • 绝对定位
  • 具有一种更不寻常的风格的显示属性值
    (内联块、表格单元格等)
  • 将其溢出属性设置为不可见的值。

最后一个选项是
大多数情况下最容易做的事情:设置
溢出:我们段落中的自动
创建一个新的“块格式
context”,并渲染该段落
边框在正确的位置。

Here's one solution:

<div style="background-color:red;border:thin black solid;overflow:hidden;">Some sample text</div>

Basically the magic here is overflow: hidden, which changes the CSS visual formatting model. Take a look at CSS layout fundamentals, part 5: floats:

Fixing adjacent boxes

Let’s look at the red paragraph border
problem first. The reason the
paragraph border appears behind the
image is because floats don’t
reposition block boxes that are
adjacent to them. In this example, the
floated image is only pushing the line
boxes inside the paragraph across. So
the text is pushed to the right, but
the actual paragraph box still
stretches across the full width of the
container.

The fix for this issue comes from a
very well-hidden paragraph in the
floats section of the CSS 2.1
specification:

The border box of a table, a
block-level replaced element, or an
element in the normal flow that
establishes a new block formatting
context
(such as an element with
‘overflow’ other than ‘visible’) must
not overlap any floats in the same
block formatting context as the
element itself.

So to prevent our paragraph block from
overlapping the floated content, the
solution is to create a “new block
formatting context”, in CSS
specification author terminology.
Sounds tricky, eh? Fortunately, it
isn’t that hard. New block formatting
contexts are created by any block
which meets these criteria:

  • is floated
  • is absolutely positioned
  • has a display property value of one of more unusual flavours
    (inline-block, table-cell, etc.)
  • has its overflow property set to something other than visible.

The last option is the one that is
easiest to do in most cases: setting
overflow: auto on our paragraph will
create a new “block formatting
context”, and render the paragraph
border in the right place.

帅的被狗咬 2024-08-14 16:39:36

如果您在 div 中添加一个 span 并像这样设置样式(当然,我实际上通常不会使用内联样式):

<div style="overflow: hidden;">
  <span style="background-color:red;
               border:thin black solid;
               display: inline-block;">
   Some sample text
   </span>
</div>

那么您可以获得 此小提琴中显示的效果。请注意,div 本身上的 overflow:hidden 是为了适应长文本字符串的换行。

If you add a span in your div and style that like so (of course, I would not actually normally use inline styling):

<div style="overflow: hidden;">
  <span style="background-color:red;
               border:thin black solid;
               display: inline-block;">
   Some sample text
   </span>
</div>

Then you can get this effect shown in this fiddle. Note that the overflow:hidden on the div itself is to accommodate line wrapping for long text strings.

独自唱情﹋歌 2024-08-14 16:39:36

看来这根本不可能如我所希望的那样。正如 Cletus 解释的那样,“没有什么允许这样做。您可以将文本环绕在右侧浮动周围,但这也会将块元素向右扩展。根据定义,块元素是一个正方形,您似乎想要一个顶部带有正方形的正方形。”右切并沿着那条线的边界做不到。”

(我赞成 Cletus 的回答,但不想将其标记为正确的“答案”,因为我相信未来的 Google 员工应该很快就会失望。)

It appears this is simply not possible the way I had hoped. As Cletus explained, "Nothing allows that. You can wrap text around the right float but that will extend the block element to the right as well. A block element is, by definition, a square and you seem to want a square with the top right cut out and the border to follow that line. Can't do it."

(I upvoted Cletus' answer, but didn't want to mark it as the correct "answer" because I believe future Googlers should be let down quickly.)

长发绾君心 2024-08-14 16:39:36

您应该将另一列指定为左浮动。现在的方式是,左侧的文本将包裹右侧的浮动文本。指定左侧浮动对象的大小也是明智的做法。

<html>

<div style="width:400px">

<div style="width:150px;float:right;border:thin black solid">
     Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim  veniam.</div>

     Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. 

<div style="background-color:red; width: 250px; border:thin black solid">Some sample text

     Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.
</div></div>

</html>

另外,考虑使用CSS,它将使这段代码更容易长期维护<<

编辑:我以为你希望所有内容都向左浮动,我想我误解了你的问题。你想做的事是不可能的。您可以做的最好的事情是指定向右浮动的对象的边距等于 400-150 (250px),或者指定“某些文本”对象的宽度等于 (250px)。

You should specify the other column as float left. The way that you have it right now the text on the left is going to wrap the floated text on the right. It would also be wise to specify the size of the floated object on the left too.

<html>

<div style="width:400px">

<div style="width:150px;float:right;border:thin black solid">
     Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim  veniam.</div>

     Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. 

<div style="background-color:red; width: 250px; border:thin black solid">Some sample text

     Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.
</div></div>

</html>

Also, consider using CSS it will make this code much easier to maintain long-term<<

EDIT: I thought you wanted everything to be floated to the left I think I misunderstood your question. What you are trying to do is not possible. The best thing you could do is either specify the margin of the object floated to the right to be equal to 400-150 (250px), or specify the width of the "some text" object to be equal to (250px).

从﹋此江山别 2024-08-14 16:39:36

我认为您只需要做一件事:

  • 给“示例文本”div 一个右边距,大于右浮动元素的宽度

就可以了。

编辑:根据您的编辑,我建议您使用 span(内联元素)而不是 div(块元素)。如果您始终需要将其放在单独的行上,则可以为该元素及其后面的元素指定一个clear:left。

I think there is only one thing you need to do:

  • Give the "sample text" div a right margin, greater than the width of the right-floated element

That should do it.

Edit: Based on your edit I would suggest you use a span (an inline element) instead of a div (a block element). If you always need it to be on a separate line, you can give both that element and the element after it a clear:left.

对不⑦ 2024-08-14 16:39:36

我这么晚才知道这个问题,无论如何我正在解决你的问题。请参阅此 Fiddle

您需要定义 display: table;

如下所示:

<div style="background-color:red;border:thin black solid;display: table;">Some sample text</div>

注意 display: block; 不会像 display: table; 一样工作,您也可以使用 display: table-cell;

你可能也喜欢这个问题: css inline-块与表格单元格

I came to know this question so late, anyway I am fixing your problem. See this Fiddle

You need to define display: table;

As this:

<div style="background-color:red;border:thin black solid;display: table;">Some sample text</div>

Note display: block; won't work as display: table; and you could also use display: table-cell;

You may also like this question: css inline-block vs table-cell

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