html - 如何垂直对齐动态长度的文本,同时让它环绕浮动图像?
问题描述
我正在尝试设置一个 div,其中包含一个浮动到左上角的图像和一个未知长度的文本。目标是如果文本小于图像,则将文本置于 div 的中间,或者如果图像足够,则将其环绕在图像周围。
我的总体布局是:
<div class="wrapper">
<img src="http://placekitten.com/50/50" class="image" style="float: left">
<p class="text">
Text goes here
</p>
</div>
我尝试过显示表格和弹性框,但它们将图像和文本视为不同的块,并防止长文本环绕图像。固定高度也不起作用,因为文本的长度是可变的,并且在开始环绕图像之前可能需要垂直对齐几行。
我已经为我想要得到的结果设置了一个小提琴。它使用两种不同的硬编码样式,但我试图找到一个无论提供多少文本都可以工作的单一解决方案。
没有某种javascript hack有没有办法解决这个问题?
解决方案
解决这个问题的方法是用包装器包装内容,这样它就不会继承 flex 属性作为直接后代。这也为您在需要时使用 flex 提供了一些灵活性。
.wrapper {
border: 1px solid black;
padding: 0;
overflow: auto;
display: flex;
}
.image {
vertical-align: middle;
}
.text {
margin: 0;
}
.content {
flex: 1;
}
.content p {
display: inline;
vertical-align: middle;
}
<p>
This text should be centered vertically:
</p>
<div class="wrapper">
<div class="content">
<img src="http://placekitten.com/50/50" class="image">
<p class="text">
A little bit of text!
</p>
</div>
</div>
<p>
This text should wrap around the image, going underneath it:
</p>
<div class="wrapper">
<div class="content">
<img src="http://placekitten.com/50/50" class="image">
<p class="text">
A lot of text! I mean a whole lot! Like tons and tons! You won't believe how much text. It's still going even. How is it still going? Who knows, but it's doing it! Oh wow it just isn't stopping. So much text! It's still going even. How is it still going?
Who knows, but it's doing it! Oh wow it just isn't stopping. So much text! It's still going even. How is it still going? Who knows, but it's doing it! Oh wow it just isn't stopping. So much text! It's still going even. How is it still going? Who
knows, but it's doing it! Oh wow it just isn't stopping. So much text! It's still going even. How is it still going? Who knows, but it's doing it! Oh wow it just isn't stopping. So much text! Oh wait, it's over.
</p>
</div>
</div>
<p>
This is what I'm trying to avoid:
</p>
<div class="wrapper">
<div class="content">
<img src="http://placekitten.com/50/50" class="image">
<p class="text">
A lot of text! I mean a whole lot! Like tons and tons! You won't believe how much text. It's still going even. How is it still going? Who knows, but it's doing it! Oh wow it just isn't stopping. So much text! It's still going even. How is it still going?
Who knows, but it's doing it! Oh wow it just isn't stopping. So much text! It's still going even. How is it still going? Who knows, but it's doing it! Oh wow it just isn't stopping. So much text! It's still going even. How is it still going? Who
knows, but it's doing it! Oh wow it just isn't stopping. So much text! It's still going even. How is it still going? Who knows, but it's doing it! Oh wow it just isn't stopping. So much text! Oh wait, it's over.
</p>
</div>
</div>
推荐阅读
- javascript - Angular:如何防止组件模板同时闪烁两个条件语句?
- angular - 将可拖动 div 的一部分设置为不可拖动
- angularjs - 动态输入编译
- arrays - 滚动数组
- javascript - Angular 4初始页面加载可以停用路由保护不起作用
- rss - RSS 提要无效:
暂停服务 当我尝试验证我的 rss 提要时,我得到了这个结果(使用 w3c 提要验证器)
此提要未验证。
第 1 行,第 39 列:未定义的根元素:状态 [帮助]
<?xml version="1.0" encoding="UTF-8" ?><status>Service unavailable.</status>
- c - 如何在c中从文件中读取多种数据类型
- excel - 是否可以参数化从 Excel 到 SSAS 的连接?(MDX 查询)
- tfs - 按迭代开始日期对 TFS 查询进行排序
- swift - 如何在 TVOS 的 TVDigitEntryViewController 的完成处理程序中获取输入的引脚?