简体   繁体   English

将 div 扩展到段落子级增长的右侧

[英]Expand div to the right on paragraph child growth

If i have a paragraph inside a div, is it possible to make the div expand to the left instead of to the right, when the paragraph gets longer?如果我在 div 中有一个段落,当段落变长时,是否可以使 div 向左扩展而不是向右扩展? I know there are several threads on this, and i have tried with direction: rtl etc, but nothing seems to work for me.我知道这有几个线程,我已经尝试过方向:rtl 等,但似乎没有什么对我有用。

Here is some code.这是一些代码。

https://codepen.io/haa123/pen/oNxaqwE https://codepen.io/haa123/pen/oNxaqwE

HTML: HTML:

<div id="container">  
  <div id="container1">
    <p>Lorem ipsum</p>
</div>
<br>
<div id="container2">
    <p>Expanding to the right</p>
</div>

CSS: CSS:

#container {
  position: absolute; left: 50%;
}

#container1 {
  background-color: red;
  display: inline-block;
}

#container2 {
  margin-top: 5px;
  background-color: red;
  display: inline-block;
}

Here are some photos to illustrate.这里有一些照片来说明。

This is what I have at the moment:这就是我目前所拥有的:

左侧的两个红色框

This is what I want to achieve:这就是我想要实现的目标:

右手边的两个红框

Thanks!谢谢!

use css grid:使用 css 网格:

<div class='container'>
  <div></div>
  <div><p>lorem ipsum</p></div>
  <div><p>expand</p>
</div>

You can use display flex in your container您可以在容器中使用 display flex

 #container { position: absolute; left: 50%; display: flex; flex-direction: column; align-items: flex-end; } #container1 { background-color: red; display: inline-block; } #container2 { margin-top: 5px; background-color: red; display: inline-block; }
 <div id="container"> <div id="container1"> <p>Lorem aaabcdedfqafasafasfafasf</p> </div> <br> <div id="container2"> <p>Expanding to the right asadasdsaqweqweqweqe</p> </div> </div>

I do not know why you used Position:absolute;我不知道你为什么使用Position:absolute; But if it is really necessary to use it, just by giving text-aligen: right;但是如果真的需要使用的话,只要给text-aligen: right; to your #container , your paragraph will be rtl.到您的#container ,您的段落将是 rtl。

 #container { position: absolute; left: 50%; text-align: right; } #container1 { background-color: red; display: inline-block; } #container2 { margin-top: 5px; background-color: red; display: inline-block; }
 <div id="container"> <div id="container1"> <p>Lorem ipsum</p> </div> <br> <div id="container2"> <p>Expanding to the right</p> </div>

.bricklet {text-align: right}
.bricklet .content {display: inline-block}
.bricklet .content.hidden {display: none}

<div class="bricklet">
  <div>Expander</div>
  <div class="content hidden">Expanded content</div>
</div>

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

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