简体   繁体   English

将一个 div 附加到另一个 div 的右侧,但流到下一行

[英]Attach one div to the right of another div, but flow to the next line

I would like to place 3 divs (or spans) with variable length text side by size so that the text reads as if it was in a single element.我想放置 3 个带有可变长度文本的 div(或跨度),以便文本读起来就像在单个元素中一样。 I found this post on how to Attach div to the right of another div .我找到了这篇关于如何将 div 附加到另一个 div 右侧的帖子。 But if I follow the method in this post, it seems as if longer spans of text show up own their own new lines.但是如果我按照这篇文章中的方法,似乎更长的文本跨度会显示出自己的新行。 I want the box on the bottom line to start directly to the right of the second box (from right) on the top line.我希望底线的框直接从顶线的第二个框(从右起)的右侧开始。 I want whatever text does not fit on the first line to continue on the second line.我希望第一行不适合的任何文本在第二行继续。 I am not sure how to do this in CSS + HTML.我不知道如何在 CSS + HTML 中做到这一点。

<div style="position: relative;">

    <div style="font-size: 14px; display: inline; border: 1px solid green; float: left;">Lorem us turpis inasdf aadsf aasda ss</div>
    <div style="font-size: 14px; display: inline; border: 1px solid green; float: left;">Lorem us turpis </div>
    <div style="font-size: 14px; display: inline; border: 1px solid green; float: left;">Lorem us turpis inasdf aadsf aasda ss</div>

</div>

在此处输入图片说明

If you use a <span> element, it shouldn't break, but rather wrap as you'd like.如果您使用<span>元素,它不应该中断,而是按照您的意愿进行包装。

Demo tested in Safari and FF.演示在 Safari 和 FF 中测试。

 .text { font-size: 14px; border: 1px solid green; }
 <div class="container"> <span class="text">Lorem us turpis inasdf aadsf aasda ss</span> <span class="text">Lorem us turpis </span> <span class="text">Lorem us turpis inasdf aadsf aasda ss</span> </div>


Further, if you want to use a <div> element, remove position on the container element, remove float on the child elements and simply add display: inline .此外,如果您想使用<div>元素,请移除容器元素上的position ,移除子元素上的float并简单地添加display: inline

Demo演示

 .text { font-size: 14px; border: 1px solid green; display: inline; }
 <div class="container"> <div class="text">Lorem us turpis inasdf aadsf aasda ss</div> <div class="text">Lorem us turpis </div> <div class="text">Lorem us turpis inasdf aadsf aasda ss</div> </div>

This can be easily achieved by using <span> tags.这可以通过使用<span>标签轻松实现。 I switched out your <div> tags for <span> tags and placed all 3 spans inside a <p> tag.我将您的<div>标签换成<span>标签,并将所有 3 个跨度放在<p>标签内。 This works with and without the <p> tag.这适用于和不使用<p>标签。 I separated the style into a class named text for better readability.为了更好的可读性,我将样式分成了一个名为text的类。 Lastly, I increased the font size so that the text is more likely to wrap on your screen and prove the point.最后,我增加了字体大小,以便文本更有可能在您的屏幕上环绕并证明这一点。

 .text { font-size: 25px; border: 1px solid green; }
 <div style="position: relative;"> <p> <span class="text">Lorem us turpis inasdf aadsf aasda ss</span> <span class="text">Lorem us turpis</span> <span class="text">Lorem us turpis inasdf aadsf aasda ss</span> </p> </div>

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

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