[英]Place div to the right side of the text
I have a div with a fixed width.我有一个固定宽度的 div。 It has two children:
它有两个孩子:
So I want to achieve something like this:-所以我想实现这样的目标:-
I want the second child to be located right next to the text of the first child我希望第二个孩子位于第一个孩子的文本旁边
I've tried to solve this issue by adding display: flex
to the parent, but the problem is that the first child takes width more than its contents (example in the snippet below)我试图通过向父级添加
display: flex
来解决这个问题,但问题是第一个子级的宽度超过了它的内容(下面代码片段中的示例)
I've tried to add flex-basis: 0
to the first child, but then each word starts with a new line, even if there is enough place to display the whole text in one line.我尝试将
flex-basis: 0
添加到第一个子项,但随后每个单词都以新行开头,即使有足够的地方在一行中显示整个文本。
Maybe there is another solution?也许还有另一种解决方案? I've tried to solve the issue using absolute positioned child but failed also.
我试图使用绝对定位的孩子来解决这个问题,但也失败了。
.parent { width: 150px; background: lightblue; display: flex; align-items: center; }.first-child { outline: 1px solid red; }.first-child span { outline: 1px solid white; }.second-child { outline: 1px solid blue; width: 20px; height: 10px; flex-shrink: 0; }
<div class="parent"> <div class="first-child"> <span>Loooooooong teeeeeext</span> </div> <div class="second-child"> </div> </div> <br /><br /> <div class="parent"> <div class="first-child"> <span>Short text</span> </div> <div class="second-child"> </div> </div>
You can only use width
as min-content;
您只能使用
width
作为min-content;
for your text element.为您的文本元素。
For example:例如:
width: min-content;
Maybe you can try to solve with the js code after documentReady!或许你可以尝试用documentReady后的js代码来解决!
document.querySelectorAll('.first-child').forEach(t => {t.style.width=t.querySelector('span').offsetWidth+'px'})
document.querySelectorAll('.first-child').forEach(t => {t.style.width=t.querySelector('span').offsetWidth+'px'})
.parent { width: 150px; background: lightblue; display: flex; align-items: center; }.first-child { outline: 1px solid red; }.first-child span { outline: 1px solid white; }.second-child { outline: 1px solid blue; width: 20px; height: 10px; flex-shrink: 0; }
<div class="parent"> <div class="first-child"> <span>Loooooooong teeeeeext</span> </div> <div class="second-child"> </div> </div> <br /><br /> <div class="parent"> <div class="first-child"> <span>Short text</span> </div> <div class="second-child"> </div> </div>
Simply use display: inline
propert in right text.只需在右侧文本中使用
display: inline
属性。 and use display: block
property in parent div.并在父 div 中使用
display: block
属性。
The solution would be to add "flex: 0 0 auto;"解决方案是添加“flex: 0 0 auto;” on the ".first-child".
关于“.first-child”。
.parent { width: 150px; background: lightblue; display: flex; align-items: center; }.first-child { outline: 1px solid red; flex: 0 0 auto; }.first-child span { outline: 1px solid white; }.second-child { outline: 1px solid blue; width: 20px; height: 10px; flex-shrink: 0; }
<div class="parent"> <div class="first-child"> <span>Loooooooong <br>teeeeeext</span> </div> <div class="second-child"> </div> </div> <br /><br /> <div class="parent"> <div class="first-child"> <span>Short text</span> </div> <div class="second-child"> </div> </div>
This solution uses an addition to the HTML contents, which won't work for dynamic contents, but I'll post it nevertheless – for fixed content it works:该解决方案使用了 HTML 内容的附加内容,它不适用于动态内容,但我还是会发布它——对于固定内容它有效:
If you just add a linebreak / <br/>
tag beween the words in your long text, it does what you want (no other changes to your code necessary):如果您只是在长文本中的单词之间添加一个换行符/
<br/>
标记,它会执行您想要的操作(无需对您的代码进行其他更改):
.parent { width: 150px; background: lightblue; display: flex; align-items: center; }.first-child { outline: 1px solid red; }.first-child span { outline: 1px solid white; }.second-child { outline: 1px solid blue; width: 20px; height: 10px; flex-shrink: 0; }
<div class="parent"> <div class="first-child"> <span>Loooooooong<br/>teeeeeext</span> </div> <div class="second-child"> </div> </div> <br /><br /> <div class="parent"> <div class="first-child"> <span>Short text</span> </div> <div class="second-child"> </div> </div>
I hope this is what you are looking for to make the 2nd div to take the right end you can use justify-content: space-between;
我希望这就是您要使第二个 div 位于右端的方法,您可以使用
justify-content: space-between;
Update: I misread the question, I've made changes in code and it works as expected using CSS.更新:我误读了这个问题,我已经对代码进行了更改,并且它使用 CSS 按预期工作。
.parent { width: 150px; background: lightblue; display: flex; align-items: center; justify-content: flex-start; }.first-child { outline: 1px solid red; flex: .5; }.first-child span { outline: 1px solid white; }.second-child { outline: 1px solid blue; width: 20px; height: 10px; flex-shrink: 0; }
<div class="parent"> <div class="first-child"> <span>Loooooooong teeeeeext</span> </div> <div class="second-child"> </div> </div> <br /><br /> <div class="parent"> <div class="first-child"> <span>Short text</span> </div> <div class="second-child"> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.