当文本换行到两行时,块元素占据全宽

前端之家收集整理的这篇文章主要介绍了当文本换行到两行时,块元素占据全宽前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_404_1@我基本上只是对此感到好奇,因为我一直都看到这一点,而且我所说过的任何人似乎都不知道是否有解决方案.

通常当我遇到这个时,它是一个带有背景的花式外观按钮,是显示块或内联块.

问题是这样的:假设你在div中有一个具有特定宽度的按钮,比方说160px,你内部有一个显示块或内联块,如果倾斜内的文本都适合它包裹在一行上两个正如你所料,但现在它在两行上它不再需要占用div的全宽,但确实如此!我发现这种情况并不奇怪,但我想知道是否有人知道css甚至JS解决方案可以解决这个问题?

码:

<div style="width: 160px; padding: 10px; background: blue;">
  <a href="#" style="background: red; display: block;">Test with a longwrappingword</a>
</div>

JSFiddle here

解决方法

this对你有用吗?
<div style="width: 160px; padding: 10px; background: blue;">
  <a href="#" style="background: red; display: table; width: 1%">Test with a longwrappingword</a>
</div>
<span style="padding-left:130px">^ Where the element COULD end if it wanted to</span>
原文链接:https://www.f2er.com/html/226905.html

猜你在找的HTML相关文章