html - CSS:无法设置包装内联元素的适当宽度

标签 html css

我有两个内联元素,一个图像和一个 anchor 。我希望 anchor 中的文本根据拖动条换行。我的问题是我希望文本在 anchor 元素下而不是图像下开始的下一行中继续。我可以通过指定 display: inline-table; 来实现规则。它工作得很好,只是现在文本的宽度超出了指定的宽度,被拖动条隐藏了:

What I want:                              What I get:
                       |                                         |                  
                       |                                         |
|__| This is the anchor|                  |__| This is the anchor|####
     element that needs|                       element that needs|####
     to get wrapped.   |                       et wrapped.       |####
                       |                                         |
                       |                                         |

HTML:

<div class:"container">
   <img src='someIcon.png'> <a href="">This is the anchor element that
   needs to get wrapped </a> 
</div>

CSS:

.container {
  white-space: normal;
  display: inline-table;
  word-wrap: break-word;
  word-break: break-all;
}

如果我禁用 display: inline-table;我得到:

                       |                
                       |
|__| This is the anchor|
element that needs to g|
et wrapped.            |
                       |
                       | 

正如所见,文本向右移动了 <img> 的宽度。

最佳答案

保持相同的 HTML 结构,您可以使用 display: inline-flex 实现。

.container {
  white-space: normal;
  display: inline-flex;
  word-wrap: break-word;
  word-break: break-all;
}
<div class="container">
   <img src='someIcon.png'> <a href="">This is the anchor element that needs to get wrapped </a> 
</div>

关于html - CSS:无法设置包装内联元素的适当宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39914701/

相关文章:

javascript - 如何使用javascript读取表单数据?

css - 使用 flex 属性组织列

javascript - 如何在JQuery 中只获取无序列表菜单中的第一个列表元素?

jquery - 如何使用 jQuery 分配 innertext?

css - 表格行的背景颜色 IE7 不起作用

javascript - 从 html 中的另一个 javascript 调用函数

javascript - 显示/隐藏 div 加载

jquery - 如何在移动网络应用程序的横向锁定 ipad 方向

javascript - 将文本区域中的特定文本设为粗体

iphone - 在 iPhone 上不停止 iTunes 音频的情况下播放 HTML5 音频