使用 :after 伪元素重复图像

发布于 2024-12-11 10:08:07 字数 488 浏览 0 评论 0原文

我想在我的网站上添加这样的标题: 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

假情假意假温柔 2024-12-18 10:08:07

有点 hacky,需要将 overflow-x:hidden; 添加到父元素,但应该可以解决问题:

h2 {
    position: relative;
    padding-right: 10px;
    float: left;
    }
h2::after {
    content: '';
    position: absolute;
    left: 100%;
    right: 9999px;
    background: url(image.gif);
    height: 10px;
    width: 9999px;
    }

A little hacky and will involve adding overflow-x:hidden; to the parent element, but should do the trick:

h2 {
    position: relative;
    padding-right: 10px;
    float: left;
    }
h2::after {
    content: '';
    position: absolute;
    left: 100%;
    right: 9999px;
    background: url(image.gif);
    height: 10px;
    width: 9999px;
    }
墨离汐 2024-12-18 10:08:07

您可以为 after 伪元素设置任何属性。我要做的是将内容设置为“”(空),然后设置伪元素的宽度和高度。然后您可以像往常一样将背景设置为重复的 gif。

h2:after {
  content: "";
  height:20px;
  width:400px;
  background:url('img/imagehere.gif') repeat-x top left;
}

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.

h2:after {
  content: "";
  height:20px;
  width:400px;
  background:url('img/imagehere.gif') repeat-x top left;
}
丑丑阿 2024-12-18 10:08:07

当您使用 content:url() 按下类时,创建一个 img 元素。在这种情况下,您需要使用背景。正如埃里克所说。

但是,如果标题中文本的大小未知(也称为动态内容),那么伪元素并不是一个好的解决方法。您可以使用如下标记:

<h1><span>your text content</span></h1>

然后将重复背景添加到 h

h1{ background:'url(img/imagehere.gif) repeat-x';}

要隐藏文本出现的部分背景,请使 span 的背景为纯色

span{background:white} 

更新:

如果您的 h1 下有一个背景图片,那么您可以执行以下操作:

相同的 HTML:

您的文本content

CSS:

假设您的 body 有一个背景图像:

body{background-image:(foo)}

那么您希望 bar 成为您在标题。您应该这样做:

h1{background:url(bar)}

并将 body 所具有的相同背景添加到包含文本的 span 中:

h1 span{background-image:(foo)}

这将解决您的问题。看看这个 Fiddle 看看实际情况。它不取决于您的文字大小或其他任何因素。

注意:如果您使用 span 那么您应该将其设为 dispaly:inline-block

更新:

根据您的要求,我重新考虑这一点。这次我使用了tables。没有解释的代码:

HTML

<table>
    <tbody>
        <tr>
            <td><h1>Heading</h1></td>
            <td class="pattern"></td>
        </tr>
    </tbody>
</table>

CSS

body{background:url(foo)}
table, tbody, tr{width:100%;}
.pattern{background:url(bar); width:100%;}

查看实际操作< ;

When you use content:url() pressed class create an img element. You need to use background 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:

<h1><span>your text content</span></h1>

And then add the repetitive background to h

h1{ background:'url(img/imagehere.gif) repeat-x';}

To hide background in part that text apears make the span's background a solid color

span{background:white} 

Update:

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:

body{background-image:(foo)}

then you want bar to be your image to repeat after the heading. You should do this:

h1{background:url(bar)}

And add same background your body have to the span containing your text:

h1 span{background-image:(foo)}

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 it dispaly:inline-block

Update:

Based on your request I rethink on this. I used tables this time. Code without explanation:

HTML

<table>
    <tbody>
        <tr>
            <td><h1>Heading</h1></td>
            <td class="pattern"></td>
        </tr>
    </tbody>
</table>

CSS

body{background:url(foo)}
table, tbody, tr{width:100%;}
.pattern{background:url(bar); width:100%;}

See in action<

咆哮 2024-12-18 10:08:07

如果您添加“display:block”声明,上面 Erik Hinton 的答案将起作用,如下所示:

h2:after {
  content: "";
  display:block;
  height:20px;
  width:400px;
  background:url('img/imagehere.gif') repeat-x top left;
}

The answer above from Erik Hinton will work if you add "display:block" declaration, as below:

h2:after {
  content: "";
  display:block;
  height:20px;
  width:400px;
  background:url('img/imagehere.gif') repeat-x top left;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文