使用 :after 伪元素重复图像
我想在我的网站上添加这样的标题: http://cl.ly/0m3F0j392e0G1n0s0T34 我理想的做法是使用文本作为标题,然后在其后水平重复一个 10 像素 x 10 像素的 gif。
编辑:我应该补充一点,我想使用带纹理的背景,这样我就无法为 h2 元素设置任何纯色。
我已经能够在标题后添加 gif,但即使我添加了重复 x,我也无法让它重复。这是我使用的代码:
h2:after {
content: 'url(img/imagehere.gif) repeat-x';
}
是否有任何解决方法或任何替代方法?我不想将整个标题分割为图像。我考虑过将标题浮动到左侧,然后将一个空的 div 浮动到右侧,并将 gif 作为重复的背景图像,但我认为这就是 :after 伪元素的用途,对吗?
I would like to put headlines in my site like this: http://cl.ly/0m3F0j392e0G1n0s0T34
What i'd ideally like to do is use text for the headline and then have a 10px by 10px gif repeat horizontally after it.
EDIT: I should add that I would like to use a textured background so I can't set any solid colours to the h2 element.
I have been able to add in the gif after the headline but I can't get it to repeat, even if i add repeat-x. Here's the code i used:
h2:after {
content: 'url(img/imagehere.gif) repeat-x';
}
Are there any workarounds for this or any alternate methods? I'd rather not resort to slicing the entire headline as an image. I've thought about floating the headline to the left then floating an empty div to the right with the gif as a repeating background image but I figure this is what the :after pseudo-element is for, right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有点 hacky,需要将
overflow-x:hidden;
添加到父元素,但应该可以解决问题:A little hacky and will involve adding
overflow-x:hidden;
to the parent element, but should do the trick:您可以为 after 伪元素设置任何属性。我要做的是将内容设置为“”(空),然后设置伪元素的宽度和高度。然后您可以像往常一样将背景设置为重复的 gif。
You can set any attributes to your after pseudo element. What I would do it set the content to "" (empty) and then set a width and height to the pseudo element. You can set the background to the repeated gif as normal then.
当您使用
content:url()
按下类时,创建一个img
元素。在这种情况下,您需要使用背景
。正如埃里克所说。但是,如果标题中文本的大小未知(也称为动态内容),那么伪元素并不是一个好的解决方法。您可以使用如下标记:
然后将重复背景添加到
h
要隐藏文本出现的部分背景,请使
span
的背景为纯色更新:
如果您的
h1
下有一个背景图片,那么您可以执行以下操作:相同的 HTML:
您的文本content
CSS:
假设您的
body
有一个背景图像:那么您希望
bar
成为您在标题。您应该这样做:并将
body
所具有的相同背景添加到包含文本的span
中:这将解决您的问题。看看这个 Fiddle 看看实际情况。它不取决于您的文字大小或其他任何因素。
注意:如果您使用
span
那么您应该将其设为dispaly:inline-block
更新:
根据您的要求,我重新考虑这一点。这次我使用了
tables
。没有解释的代码:HTML
CSS
查看实际操作< ;
When you use
content:url()
pressed class create animg
element. You need to usebackground
in this case. Like what Erik said.But if size of your text in heading is not known (aka dynamic content) then pseudo element is not a good work around. You can use a markup like this:
And then add the repetitive background to
h
To hide background in part that text apears make the
span
's background a solid colorUpdate:
if you have a background image under your
h1
, then you can do this:Same HTML:
<h1><span>your text content</span></h1>
CSS:
say your
body
have a background image:then you want
bar
to be your image to repeat after the heading. You should do this:And add same background your
body
have to thespan
containing your text:This would solve your problem. Look at this Fiddle to see in action. It's not depended on your text size or anything else.
Note: if you are using an
span
then you should make itdispaly:inline-block
Update:
Based on your request I rethink on this. I used
tables
this time. Code without explanation:HTML
CSS
See in action<
如果您添加“display:block”声明,上面 Erik Hinton 的答案将起作用,如下所示:
The answer above from Erik Hinton will work if you add "display:block" declaration, as below: