简体   繁体   English

如何获取行内显示的文本以换行并增加父容器的高度?

[英]How can I get my text that is displayed inline-block to wrap and increase the height of the parent container?

I have an image and next to it a div with 2 child divs. 我有一张图片,旁边有一个带有2个子div的div。 Both the image and container of the 2 divs are displayed inline-block. 这两个div的图像和容器都以内联块显示。 The 2 divs contain a line of text. 2个div包含一行文本。 I would like for the parent div to increase in height if the text overflows, but instead, the lines of text get pushed below the image. 如果文本溢出,我希望父div的高度增加,但是,文本行会被推送到图像下方。 Here are 2 examples. 这里有两个例子。 In the first, the text is correctly aligned next to the image. 首先,文本在图像旁边正确对齐。 In the 2nd, the text gets pushed below the image because it overflows. 在第二个中,文本会因为溢出而被推送到图像下方。

 .w-165{ width: 165px; } .soft-border{ border: solid 1px rgba(76, 84, 98, 0.3); } .h-70{ height: 70px; } .d-inline-block{ display: inline-block; } .align-middle{ vertical-align: middle; } 
 <div class="w-165 soft-border"> <div class="soft-border"> Notifications </div> <div> <img class="h-70 d-inline-block align-middle" src="https://a248.e.akamai.net/f/248/9086/10h/s7d2.scene7.com/is/image/Caterpillar/C820021?$cc-g$"> <div class="d-inline-block align-middle"> <div> New Issue </div> <div> Dec 15 </div> </div> </div> </div </div> 

 .w-165{ width: 165px; } .soft-border{ border: solid 1px rgba(76, 84, 98, 0.3); } .h-70{ height: 70px; } .d-inline-block{ display: inline-block; } .align-middle{ vertical-align: middle; } 
 <div class="w-165 soft-border"> <div class="soft-border"> Notifications </div> <div> <img class="h-70 d-inline-block align-middle" src="https://a248.e.akamai.net/f/248/9086/10h/s7d2.scene7.com/is/image/Caterpillar/C820021?$cc-g$"> <div class="d-inline-block align-middle"> <div> New Issues </div> <div> Dec 15 </div> </div> </div> </div </div> 

The easiest thing to do here is to define a width for the div in question. 此处最简单的方法是为所讨论的div定义宽度。 That will force text to wrap. 这将迫使文本换行。

width: 60px;

One of many solutions can be to use a table layout for your image and text container - so add this to your CSS: 其中很多解决方案都可以使用您的图像和文本容器中的表格布局 -所以这添加到您的CSS:

.w-165 > div:last-child {
  display: table;
}
.w-165 > div:last-child > * {
  display: table-cell;
}

See demo below: 请参见下面的演示:

 .w-165 { width: 165px; } .soft-border { border: solid 1px rgba(76, 84, 98, 0.3); } .h-70 { height: 70px; } .w-165 > div:last-child { display: table; } .w-165 > div:last-child > * { display: table-cell; } .d-inline-block { display: inline-block; } .align-middle { vertical-align: middle; } 
 <div class="w-165 soft-border"> <div class="soft-border"> Notifications </div> <div> <img class="h-70 d-inline-block align-middle" src="https://a248.e.akamai.net/f/248/9086/10h/s7d2.scene7.com/is/image/Caterpillar/C820021?$cc-g$"> <div class="d-inline-block align-middle"> <div> New Issues </div> <div> Dec 15 </div> </div> </div> </div> 

I tried to adjust the code for make it works as you wanted, check this fiddle . 我试图调整代码以使其如您所愿地工作,请检查此小提琴

CSS 的CSS

I had to add width to the div s to make the text wraps, of course with this solution if you enter a word that is too long it will get out of the div . 我必须在div上增加宽度以使文本自动换行,当然,使用此解决方案,如果您输入的单词太长,它将脱离div

.h-70{
  width: 95px;
}

.d-inline-block div{
  width:60px;
}

Let me know if it helped please, thank you. 让我知道是否有帮助,谢谢。

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

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