svg背景色放网页看不见图_在网页背景中使用SVG:条纹

本文介绍了如何在网页背景中使用SVG创建条纹效果,指出SVG相对于CSS渐变的优势,如更好的浏览器支持和可创建更复杂的图案。通过调整SVG元素的尺寸和颜色,可以实现水平或垂直条纹,甚至创建格子和苏格兰格子图案,为网页设计提供灵活的背景解决方案。
摘要由CSDN通过智能技术生成

svg背景色放网页看不见图

Resolution independent, small in file size and easy to edit, vector images have several advantages when it comes to designing web page backgrounds over CSS gradients:

与分辨率无关,文件大小小且易于编辑,在设计通过CSS渐变设计的网页背景时,矢量图像具有多个优点:

  1. SVG syntax for stripes is easier and less verbose than CSS gradients.

    与CSS渐变相比,用于条纹的SVG语法更容易且更冗长。
  2. SVG backgrounds have better browser support (SVG is supported in IE9, but CSS gradients are not supported in Internet Explorer until v10).

    SVG背景具有更好的浏览器支持(IE9支持SVG,但Internet Explorer直到v10才支持CSS渐变)。
  3. SVG backgrounds can be made much more complex than those made with pure CSS.

    SVG背景可以比纯CSS背景复杂得多。

But to be effective, vector illustrations need to follow a few rules:

但是要有效,矢量插图需要遵循一些规则:

一般准则 (General Guidelines)

  • Just like repeated , you only need one seamless “tile” or <pattern> of an SVG for it to be usable: you don’t have to build the whole thing.

    就像重复的 ,您只需要使用一个无缝的SVG“平铺”或<pattern>即可使用它:不必构建整个事物。

  • Keep the SVG file as small, “light”, and editable as possible: this usually means a little hand-editing and tweaking.

    保持SVG文件尽可能小,“浅”并且可编辑:这通常意味着需要手工编辑和微调。
  • It’s a good idea to keep at least one of the background in your CSS for the greatest flexibility.

    最好在CSS中保留背景的至少一种 ,以实现最大的灵活性。

A few examples should be enough to get you started. Note that these concentrate on repeated backgrounds: giant single SVG backgrounds are treated much the same way as giant bitmap background images.

几个例子应该足以让您入门。 请注意,这些都集中在重复的背景上:巨型单个SVG背景与巨型位图背景图像的处理方式几乎相同。

简单的SVG条纹 (Simple SVG Stripes)

A simple “stripe” effect is nothing more than a filled area beside a blank space, repeated infinitely. In the case of SVG, the easiest way to make this filled area to use a rectangle element. Save the following code as stripe.svg:

一个简单的“条纹”效果就是无限重复的空白旁边的填充区域。 对于SVG,最简单的方法是使该填充区域使用矩形元素 。 将以下代码另存为stripe.svg

<svg viewBox="0 0 1 1" xmlns="http://www.w3.org/2000/svg">
    <rect width="0.5" height="1" />
</svg>

Note that the viewBox or the equivalent width and height attributes can be used to size the <svg> element for the purposes of creating background images.

注意,为了创建背景图像,可以使用viewBox 等效的widthheight属性来调整<svg>元素的大小。

This SVG file can be applied to the background of a web page using:

可以使用以下命令将此SVG文件应用于网页的背景:

body {
	background-image: url(stripe.svg);
}

By default, the SVG will expand to cover the entire space of the element. This effectively divides the page into half black (the default fill of the SVG rectangle) and half white (the default background color of the web page, as any area not explicitly colored in SVG is automatically transparent).

默认情况下,SVG将展开以覆盖元素的整个空间。 这样可以有效地将页面分为半黑(SVG矩形的默认填充)和半白(网页的默认背景色,因为任何未在SVG中显式着色的区域都是自动透明的)。

To make this SVG pattern repeat more than once, we can change the background-size in the CSS declaration: x width, followed by y height.

要使此SVG模式重复多次,我们可以在CSS声明中更改background-size :x宽度,后跟y高度。

body {
    background-image: url(stripe.svg);
    background-size: 20px 20px;
}

This produces:

这将产生:

One nice aspect of having a small “tight” SVG illustration is that it’s easy to change this from a vertical to a horizontal stripe by altering the background-size:

background-size SVG插图的一个不错的方面是,通过更改background-size ,可以很容易地将其从垂直条纹更改为水平条纹:

Horizontal Pinstripe: body { background-size: 1px 10px; }

水平细条纹: body { background-size: 1px 10px; } body { background-size: 1px 10px; }

Vertical Pinstripe:: body { background-size: 10px 1px; }

垂直细条纹:: body { background-size: 10px 1px; } body { background-size: 10px 1px; }

For more versatility, you could color the stripe using rgba or hsla, and address the background-color of the page in the CSS:

为了获得更多的通用性,您可以使用rgbahsla为条纹hsla ,并在CSS中处理页面的background-color

<svg viewBox="0 0 1 1" xmlns="http://www.w3.org/2000/svg">
	<rect width="0.5" height="1" fill="hsla(32, 42%, 50%, .5)" />
</svg>

The CSS:

CSS:

body {
	background-image: url(stripe.svg);
	background-size: 20px 20px;
	background-color: #669;
}

This SVG file is small enough to write inline into the CSS itself, saving an HTTP request:

这个SVG文件足够小,可以内联写入CSS本身,从而保存HTTP请求:

body {
	background-image:  url('data:image/svg+xml;utf8,<svg viewBox="0 0 1 1" xmlns="http://www.w3.org/2000/svg"><rect width="0.5" height="1" fill="hsla(32, 42%, 50%, .5)" /></svg>');
	background-size: 20px 20px;
}

Be careful with quotes when doing this! This SVG uses double quotes internally, meaning that single quotes are used on the outside to avoid confusing the browser.

这样做时请小心引号! 该SVG在内部使用双引号,这意味着在外部使用单引号以避免混淆浏览器。

You could also base64-encode the SVG file and use that inline in the CSS:

您还可以对SVG文件进行base64编码,并在CSS 内联使用:

body {
    background-image:  url('data:image/svg+xml;base64,PHN2ZyB2aWV3Qm94PSIwIDAgMiAxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHByZXNlcnZlQXNwZWN0UmF0aW89InhNaW5ZTWluIG1lZXQiPjxyZWN0IHdpZHRoPSIxIiBoZWlnaHQ9IjIiIGZpbGw9ImhzbGEoMzIsIDQyJSwgNTAlLCAuNSkiIC8+PC9zdmc+');
}

Doing so is optional, and does have three disadvantages:

这样做是可选的,确实有三个缺点:

  • the DataURI version takes up more space than the inline SVG

    DataURI版本比嵌入式SVG占用更多空间

  • converting the image into base64 makes it non-editable (to make any changes, you’d have to find the original SVG, edit it, convert the result into base64, and add the new code back into the CSS).

    将图像转换为base64使其不可编辑(要进行任何更改,您必须找到原始SVG,对其进行编辑,将结果转换为base64,然后将新代码添加回CSS中)。
  • unencoding base 64 is slower on mobile platforms.

    在移动平台上,未编码的base 64较慢。

However, using base-64 SVG inline does give you a little more cross-browser compatibility; ultimately, the decision is up to you.

但是,使用base-64 SVG内联确实为您提供了跨浏览器的兼容性。 最终,决定权取决于您。

分隔线 (Separating The Lines)

It’s easy to make the lines thicker and thinner in our example using background-size, but the one thing this won’t do is change the ratio between the thickness of the lines and the space between them. To alter that, we go back to the SVG and make the rectangle narrower or wider:

在我们的示例中,使用background-size可以使线条变粗和变细,但这很简单,但要做的一件事就是更改线条粗细与线条之间的间距之 。 要更改此设置,我们返回SVG,使rectangle变窄或变宽:

<svg viewBox="0 0 1 1" xmlns="http://www.w3.org/2000/svg">
	<rect width=".25" height="1" />
</svg>

Which produces:

产生:

制作更复杂的图案 (Making More Complex Patterns)

Once you have the basics, it’s easy to produce more complex variations of horizontal and vertical stripes in SVG. For example, a gingham pattern would be a &frac13; width rectangle overlaying a &frac13; height element:

一旦掌握了基础知识,就可以轻松地在SVG中产生水平和垂直条纹的更复杂的变化。 例如,方格图案应为&frac13;。 覆盖&frac13;的宽度矩形 高度元素:

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
  <g fill="rgba(0,0,0,0.3)">
    <rect width="100" height="33" />
    <rect width="33" height="100" x="67" />
  </g>
</svg>

You can see a variation on this technique at the top of this article, incorporating stroke-dasharray to provide further decoration: a good example of a pattern that is impossible to achieve with CSS gradients.

您可以在本文的顶部看到此技术的一种变体,它结合了stroke-dasharray来提供进一步的修饰:这是CSS渐变无法实现的模式的一个很好的例子。

The same concepts can be used to create plaids and tartans, although the more complex of these should probably use SVG’s <pattern> syntax, discussed in the next article.

可以使用相同的概念来创建格子和格子呢,尽管其中更复杂的应该使用SVG的<pattern>语法,这将在下一篇文章中进行讨论。

翻译自: https://thenewcode.com/1045/Using-SVG-in-Web-Page-Backgrounds-Stripes

svg背景色放网页看不见图

以下是一个使用SVG绘制的山水背景的代码示例: ```HTML <svg viewBox="0 0 100 100" style="background-color: #9AC8EB;"> <!-- 山 --> <path d="M0,80 Q30,60 50,70 Q70,80 100,60 V100 H0 Z" fill="#5E8A8C"/> <path d="M0,80 Q30,60 50,70 Q70,80 100,60" fill="none" stroke="#5E8A8C" stroke-width="2"/> <!-- 水 --> <rect x="0" y="70" width="100" height="30" fill="#9AC8EB"/> <rect x="0" y="70" width="100" height="5" fill="url(#water-pattern)"/> <defs> <pattern id="water-pattern" x="0" y="0" width="10" height="10" patternUnits="userSpaceOnUse"> <path d="M0,5 Q5,0 10,5 Q15,10 20,5 Q25,0 30,5 Q35,10 40,5 Q45,0 50,5 Q55,10 60,5 Q65,0 70,5 Q75,10 80,5 Q85,0 90,5 Q95,10 100,5" fill="#5E8A8C"/> </pattern> </defs> </svg> ``` 代码解释: - `viewBox` 属性定义了SVG视口的大小和位置,`background-color` 样式设置了SVG的背景颜色为淡蓝色。 - `path` 标签绘制了一座山,`d` 属性定义了山的形状,该路径由两个二次贝塞尔曲线和一条直线组成,`fill` 属性设置为 `#5E8A8C` 表示填充颜色为深绿色,`stroke` 属性设置为 `#5E8A8C` 表示边框颜色为深绿色,`stroke-width` 属性设置为 `2` 表示边框宽度为2个像素。 - `rect` 标签绘制了一条水,`x`、`y`、`width` 和 `height` 属性定义了水的位置和大小,`fill` 属性设置为 `#9AC8EB` 表示填充颜色为淡蓝色。 - 使用 `defs` 和 `pattern` 标签定义了一段水的纹理,`path` 标签定义了纹理的形状,使用 `fill` 属性填充颜色为深绿色,然后将该纹理应用于水的填充。 运行上述代码,将会在浏览器显示一个山水背景,如下所示: ![SVG山水背景](https://img-blog.csdnimg.cn/20210925203904987.svg)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值