两个并排的 div,且不换行,但当空间不足时,右侧的 div 会换行。

huangapple go评论78阅读模式
英文:

two divs side by side and nowrap, but the right one breaking down when not enough space

问题

我无法得到我想要的东西,尽管它看起来很简单。

<div class="around">
    <div class="first">
        <p>一些文字</p>
    </div>
    <div class="second">
        <p>其他文字</p>
    </div>
</div>

我有两个并排的div,每个div都有动态内容。

两个并排的div

当浏览器宽度变得太小以适应内容时,右侧的div应该换行。重要的是,右侧div中的内容应该右对齐。

两个div没有空间,但不换行

我已经阅读了其他帖子,但没有找到解决方法。我认为这是不可能的,因为我尝试过flex、grid、float等方法...但也许你们中的某个人有想法。


解决方案:

<style>
.around {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
}
.second {
    background: red;
}
.first,
.second {
    display: flex;
    flex-wrap: wrap;
    min-width:0;
    justify-content: space-between;
    flex-grow: 1;
}
</style>
英文:

i can't get what i want, although it seems simple.

    &lt;div class=&quot;around&quot;&gt;
        &lt;div class=&quot;first&quot;&gt;
            &lt;p&gt;Some words&lt;/p&gt;
        &lt;/div&gt;
        &lt;div class=&quot;second&quot;&gt;
            &lt;p&gt;other words&lt;/p&gt;
        &lt;/div&gt;
    &lt;/div&gt;

i have two divs side by side and each div has dynamic content.

two divs side by side

and when browserwidth is getting to small for the content, the right div should break down.Important is, that the content in the right div align-righted is.
two divs have no space, but not breaking

i have read the other threads, but found nothing. and it think this is not possible, because i tried flex, grid, float, etc.... but maybe one of you has an idea


The solution:

    &lt;style&gt;
    .around {
        display: flex;
        flex-wrap: wrap;
        justify-content: space-between;
    }
    .second {
        background: red;
    }
    .first,
    .second {
        display: flex;
        flex-wrap: wrap;
        min-width:0;
        justify-content: space-between;
        flex-grow: 1;
    }
&lt;/style&gt;

答案1

得分: 0

我会在父元素上使用display:flex,并在每个子元素上设置flex属性为1,使它们平均占据父元素的空间。

min-width:0;是一个在这种情况下有帮助的技巧,具体原因我不清楚。

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->
.around {
  display:flex;
}

.first,.second {
  flex:1;
  min-width:0;
}

/* for demostration styling */
.around {
border:green 2px solid;
}
.first {
background:red;
}
.second {
background:blue;
}

<!-- language: lang-html -->
<div class="around">
    <div class="first">
        <p>Some words</p>
    </div>
    
    <div class="second">
        <p>other words</p>
    </div>
</div>

<div class="around" style="width:100px">
    <div class="first">
        <p>Some words</p>
    </div>
    
    <div class="second">
        <p>other words</p>
    </div>
</div>

<!-- end snippet -->
英文:

I would use a display:flex on the parent, and set the flex property to 1 in each of the children, allowing them to each take up the same amount of space from the parent.

the min-width:0; is a hack that helps in these situations, idk why.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.around {
  display:flex;
}

.first,.second {
  flex:1;
  min-width:0;
}

/* for demostration styling */
.around {
border:green 2px solid;
}
.first {
background:red;
}
.second {
background:blue;
}

<!-- language: lang-html -->

&lt;div class=&quot;around&quot;&gt;
    &lt;div class=&quot;first&quot;&gt;
        &lt;p&gt;Some words&lt;/p&gt;
    &lt;/div&gt;
    
    &lt;div class=&quot;second&quot;&gt;
        &lt;p&gt;other words&lt;/p&gt;
    &lt;/div&gt;
&lt;/div&gt;

&lt;div class=&quot;around&quot; style=&quot;width:100px&quot;&gt;
    &lt;div class=&quot;first&quot;&gt;
        &lt;p&gt;Some words&lt;/p&gt;
    &lt;/div&gt;
    
    &lt;div class=&quot;second&quot;&gt;
        &lt;p&gt;other words&lt;/p&gt;
    &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 0

使用flexflex-wrap: wrap;,我已经实现了你想要实现的效果。
你可以设置任意大小的div,一旦屏幕宽度太小,两个div无法在同一行上,它们就会分开,并且红色容器会向下移动。

<!-- language: lang-css -->
.around {
  display: flex;
  flex-wrap: wrap;
}
.second {
  background: red;
}

<!-- language: lang-html -->
<div class="around">
    <div class="first">
        <p>Some words</p>
    </div>
    <div class="second">
        <p>other words</p>
    </div>
</div>

希望对你有帮助!

英文:

Using flex and flex-wrap: wrap; I got it to do what you were trying to achieve.
You can make the divs of any size and once the screen width is too small for both divs to be on the same line, it will break up and have the red container move down.
<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.around {
  display: flex;
  flex-wrap: wrap;
}
.second {
  background: red;
}

<!-- language: lang-html -->

&lt;div class=&quot;around&quot;&gt;
    &lt;div class=&quot;first&quot;&gt;
        &lt;p&gt;Some words&lt;/p&gt;
    &lt;/div&gt;
    &lt;div class=&quot;second&quot;&gt;
        &lt;p&gt;other words&lt;/p&gt;
    &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案3

得分: 0

默认情况下,插入的div会向下排列,尝试使用"display:flex"属性,并根据您的需求给它们添加间距。

英文:

Divs by default go down when inserted , try "display:flex" property and give them spacing according to your requirement.

答案4

得分: 0

解决方案:

<style>
.around,
.first,
.second {
   display: flex;
   flex-wrap: wrap;
 }
.first,
.second {
  flex-grow: 1;
}
英文:

The solution:

&lt;style&gt;
.around,
.first,
.second {
   display: flex;
   flex-wrap: wrap;
 }
.first,
.second {
  flex-grow: 1;
}

</style>

huangapple
  • 本文由 发表于 2023年8月8日 23:29:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76861035.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定