简体   繁体   English

为什么“float-start”和“float-end”会断开我的<span>元素与 div 的连接?</span>

[英]why are "float-start" and "float-end" disconnecting my <span> elements from the div?

I'm running reactjs and attempting to use float-start and float-end to position "start" and "end" span elements on the left and right side of a div , respectively.我正在运行 reactjs 并尝试将float-startfloat-end分别用于div左侧和右侧的 position “开始”和“结束” span元素。

I've boiled it down to this basic demo:我把它归结为这个基本演示:

index.js : index.js

import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  <React.StrictMode>
    <div className="my-red-container">
      <span>things</span>
    </div>

    <div className="my-yellow-container">
      <span className='float-start'>left</span>
      <span className='float-end'>right</span>
    </div>
  </React.StrictMode>
);

index.css : index.css

.my-yellow-container {
  background-color: yellow;
}

.my-red-container {
  background-color: red;
}

When I use npm start to run this, I expect to see a red bar saying "things" followed by a yellow bar saying "left" on the left and "right" on the right.当我使用npm start运行时,我希望看到一个红色条表示“事物”,然后是一个黄色条表示左侧的“左”和右侧的“右”。 Instead I get this:相反,我得到这个:

在此处输入图像描述

If I move the red div below the yellow div like this:如果我像这样将红色 div 移动到黄色 div 下面:

    <div className="my-yellow-container">
      <span className='float-start'>left</span>
      <span className='float-end'>right</span>
    </div>

    <div className="my-red-container">
      <span>things</span>
    </div>

Then I expect to get a yellow bar saying "left" on the left and "right" on the right, and below that a red bar saying "things".然后我希望得到一个黄色的条,左边是“左”,右边是“右”,下面是一个红色的条,上面写着“东西”。 Instead I get this:相反,我得到这个:

在此处输入图像描述

But when I add an "unclassed" span to the yellow div, like this:但是当我向黄色 div 添加一个“未分类” span时,如下所示:

    <div className="my-yellow-container">
      <span>far left</span>
      <span className='float-start'>left</span>
      <span className='float-end'>right</span>
    </div>

    <div className="my-red-container">
      <span>things</span>
    </div>

Then I get this instead:然后我得到这个:

在此处输入图像描述

It's like the float-start and float-end completely disconnect the span from it's containing div and then all the text gets jumbled.这就像float-startfloat-end完全断开了span与其包含的div的连接,然后所有文本都变得混乱了。

Please help.请帮忙。 What did I mess up?我搞砸了什么? I've clearly overlooked something, but I don't know what.我显然忽略了一些东西,但我不知道是什么。

The float property is used to specify how an element should float within its parent element. float 属性用于指定元素应如何在其父元素中浮动。 When you apply the float property to an element, the element is taken out of the normal flow of the document and moved to the left or right of its parent element.当您将 float 属性应用于元素时,该元素将脱离文档的正常流动并移动到其父元素的左侧或右侧。 This can cause other elements to behave as if the floated element is not there, as if the element does not occupy any space.这可能会导致其他元素表现得好像浮动元素不存在一样,就好像该元素不占用任何空间一样。

The float-start and float-end values of the float property are used to specify which side of the parent element an element should float to. float 属性的 float-start 和 float-end 值用于指定元素应浮动到父元素的哪一侧。 The float-start value floats the element to the left in left-to-right languages, and the float-end value floats the element to the right in left-to-right languages. float-start 值在从左到右的语言中将元素浮动到左侧,而 float-end 值在从左到右的语言中将元素浮动到右侧。

If you have applied the float property to a span element and it is causing the element to be disconnected from the parent div, it could be because the div element does not have a set width and is not able to contain the floated element.如果您已将 float 属性应用于 span 元素并导致元素与父 div 断开连接,则可能是因为 div 元素没有设置宽度并且无法包含浮动元素。 You can fix this by giving the div element a set width, or by applying the overflow: hidden property to the div element to contain the floated element.您可以通过为 div 元素设置宽度或将 overflow: hidden 属性应用于 div 元素以包含浮动元素来解决此问题。

It is also possible that the float property is causing the span element to overlap with other elements on the page, which can cause it to appear disconnected from the div.浮动属性也可能导致 span 元素与页面上的其他元素重叠,这可能导致它看起来与 div 断开连接。 In this case, you can try applying the clear property to the div element to prevent it from overlapping with the floated element.在这种情况下,您可以尝试将 clear 属性应用于 div 元素,以防止它与浮动元素重叠。

Below is a code Sample下面是一个代码示例

<style>
  .floated-element {
    float: float-start; /* or float-end */
  }
  .containing-element {
    width: 500px;
  }
</style>

<div class="containing-element">
  <span class="floated-element">This element will be floated to the left (or right) within the containing element</span>
</div>

In this example, the float property is applied to the span element with the value of float-start, which will float the element to the left in left-to-right languages.在此示例中,将 float 属性应用于值为 float-start 的 span 元素,这将使该元素在从左到右的语言中向左浮动。 The containing-element div has a set width of 500 pixels, which will contain the floated element.包含元素的 div 设置了 500 像素的宽度,它将包含浮动元素。

If you wanted to float the element to the right instead, you could use the value float-end for the float property.如果您想改为将元素浮动到右侧,则可以为 float 属性使用值 float-end。

I hope this helps.我希望这有帮助。 Let me know if you have any questions or need further clarification.如果您有任何问题或需要进一步说明,请告诉我。

Discovered from a previous answer (that was somehow deleted before I could tag it as the answer) that I had to set the containing div to this:从以前的答案(在我将其标记为答案之前以某种方式被删除)发现我必须将包含的div设置为此:

<div style={{ overflow: "hidden" }}>
...
</div>

This seemed to do the trick.这似乎可以解决问题。 The (now-deleted) post explained that "float" intentionally disconnects the floating child from the dimension restrictions of the parent, but setting the style overflow: "hidden" forces them back into containment again.这篇(现已删除)的帖子解释说,“浮动”有意断开浮动子项与父项的尺寸限制的连接,但设置样式overflow: "hidden"会迫使它们再次进入收容状态。 I just wanted the "far left" and "far right" feature of float-start and float-end , so I'm fine forcing it the child element back into containment again.我只想要float-startfloat-end的“最左端”和“最右端”特性,所以我很好地强制它的子元素再次回到包含中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM