[英]Clearing an inline-block element to the next line
I'm looking to clear an inline-block element (in this case an <a>
within a <p>
) to the next line, without having to set display:block
and defining a width. 我想要将内联块元素(在这种情况下是<p>
的<a>
清除到下一行,而不必设置display:block
并定义宽度。
Here's an example: http://jsfiddle.net/alecrust/zstKf/ 这是一个例子: http : //jsfiddle.net/alecrust/zstKf/
Here's the desired result (using display:block
and defining a width): http://jsfiddle.net/alecrust/TmwhU/ 这是期望的结果(使用display:block
并定义宽度): http : //jsfiddle.net/alecrust/TmwhU/
If you want to avoid setting an explicit width so you can style the background according to the actual length of the text, you can do the following: 如果要避免设置显式宽度,以便根据文本的实际长度设置背景样式,可以执行以下操作:
Wrap your link: 包裹您的链接:
<p>To stay up to date <span><a href="#">Follow Us</a></span></p>
Note that I have added a <span>
tag around the link. 请注意,我在链接周围添加了<span>
标记。
Style your wrapper with CSS: 使用CSS为您的包装器设置样式:
span {
display: inline-block;
width: 100%;
}
Setting the width to 100% forces the wrapper to take up the whole line. 将宽度设置为100%会强制包装器占据整条线。 Keeping the <a>
tag for the link set to inline-block allows it to have padding and a background applied while not having it expand to fit the container's width of 100%. 将链接集的<a>
标记保持为内联块允许它应用填充和背景,而不将其展开以适应容器的宽度100%。
Forked JSFiddle: http://jsfiddle.net/Cm9kZ/ 分叉JSFiddle: http : //jsfiddle.net/Cm9kZ/
It's a bit of a kludge, but it will work: 这有点像kludge,但它会起作用:
a {
display: inline-block;
padding: 5px 18px;
background-color: #8C4AD5;
text-decoration: none;
position:relative;
top:25px;
left:-30%
}
You'll have to fudge the left position, but that basically puts you back into setting a known value, just like the width issue in your display:block
example. 你必须捏造左边的位置,但这基本上会让你回到设置一个已知的值,就像你的display:block
示例中的宽度问题一样。 Not really any better, just a different approach. 不是更好,只是一种不同的方法。
This only works if you want to line break after the last element in the p. 这仅适用于要在p中的最后一个元素之后换行的情况。
I've experimented quite a bit and this works for me, in Safari 6: 我已经尝试了很多,这对我有用,在Safari 6中:
p.linebreak-after-last-element:after {
content: "";
display: inline-block;
width: 100%;
}
I have not tested this in other browsers, but it's so simple it should work in all browsers supporting display: inline-block
. 我没有在其他浏览器中测试过这个,但它很简单,它应该适用于支持display: inline-block
所有浏览器。
内联块元素后面的空<div/>
清除内联块。
With the requirements you have, I don't think it's possible. 根据您的要求,我认为这是不可能的。
I was hoping that this would help , but it doesn't because you don't have an element before your link. 我希望这会有所帮助 ,但事实并非如此,因为你的链接之前没有元素。
You should just change your HTML, for example: http://jsfiddle.net/thirtydot/zstKf/10/ 您应该只更改HTML,例如: http : //jsfiddle.net/thirtydot/zstKf/10/
Using the pseudo class :: after you could add content with a clear:both
; 使用伪类::你可以添加带有clear:both
内容clear:both
; property to it. 财产到它。
Not tested but should work in theory. 没有测试但应该在理论上工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.