简体   繁体   English

使用 SVG 和 CSS 的具有前景层的背景图像

[英]Background image with a foreground layer using SVG and CSS

First of all, thank you for reading this.首先,感谢您阅读本文。

Problem问题

I wonder if there exists a way to create some kind of 3d website background, for instance from a SVG file and some CSS codes ?我想知道是否有一种方法可以创建某种 3d 网站背景,例如从 SVG 文件和一些 CSS 代码? More precisely, is it possible to set some layers of a SVG background to be in front of the html body elements ?更准确地说,是否可以将 SVG 背景的某些层设置在 html body 元素的前面? Since it is not possible to use z-index in SVG file , I wonder if there exist a workaround to achieve this.由于不可能在 SVG 文件中使用 z-index ,我想知道是否存在解决方法来实现这一点。

Example例子

To give you a concrete example, I would really appreciate to do this on a SEO consultant website .给你一个具体的例子,我非常感谢在SEO 顾问网站上这样做。 For this specific example, how can I force the clouds (at the bottom of the background image) to be in foreground so that the text and every body elements appear from behind the cloud (including the sidebar) ?对于这个特定的示例,我如何强制云(在背景图像的底部)位于前景中,以便文本和每个正文元素从云后面(包括侧边栏)出现?

Illustrations: Current display vs. Desired display插图:当前显示所需显示

Notes笔记

(1) Currently the format is jpg (& webp), since I encountered compatibility issues with macOS devices and SVG background. (1) 目前格式为 jpg (& webp),因为我遇到了 macOS 设备和 SVG 背景的兼容性问题。 Moreover, I'm not sure that using SVG as website background is a good idea.此外,我不确定使用 SVG 作为网站背景是一个好主意。

(2) I initially tried to add the clouds as a separate image in a specific div but then I could not figure out how to apply identical responsive setting as the ones of the background img (set by the theme I'm using). (2) 我最初尝试将云作为单独的图像添加到特定的 div 中,但后来我无法弄清楚如何应用与背景 img 相同的响应设置(由我正在使用的主题设置)。

I'd be very grateful for any advice or idea you may have.如果您有任何建议或想法,我将不胜感激。

Thank you.谢谢你。 :) :)

You are right in that SVG elements cannot be positioned using z-index, but the SVG element itself can be styled/ordered so that it overlays the HTML.您是对的,SVG 元素不能使用 z-index 定位,但 SVG 元素本身可以设置样式/排序,以便覆盖 HTML。

 svg { position: fixed; width: 50%; bottom: 0; right: 0; }
 <section> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Eros in cursus turpis massa tincidunt. Mi sit amet mauris commodo quis imperdiet. Felis donec et odio pellentesque. Leo urna molestie at elementum eu facilisis. Pretium aenean pharetra magna ac placerat. Aenean pharetra magna ac placerat vestibulum lectus mauris ultrices. Nunc vel risus commodo viverra maecenas. Volutpat consequat mauris nunc congue nisi. Sit amet aliquam id diam maecenas. Nunc sed blandit libero volutpat sed cras. In est ante in nibh mauris cursus mattis molestie a. Quis lectus nulla at volutpat diam ut venenatis tellus in. Ut venenatis tellus in metus. Diam donec adipiscing tristique risus nec. Tellus at urna condimentum mattis pellentesque id. Purus in mollis nunc sed id semper risus in.</p> <p>Interdum velit laoreet id donec ultrices. Nibh tortor id aliquet lectus proin nibh nisl condimentum id. Eu facilisis sed odio morbi quis commodo. Et tortor at risus viverra adipiscing at. Gravida neque convallis a cras semper. Auctor urna nunc id cursus metus aliquam eleifend mi. Sit amet commodo nulla facilisi nullam vehicula ipsum a. Mi proin sed libero enim sed faucibus turpis. Fermentum leo vel orci porta non pulvinar neque. Varius sit amet mattis vulputate enim nulla aliquet. Semper eget duis at tellus at urna condimentum mattis. Viverra accumsan in nisl nisi scelerisque eu. Malesuada bibendum arcu vitae elementum. Lacinia quis vel eros donec ac. Turpis tincidunt id aliquet risus feugiat in. Non consectetur a erat nam at lectus urna duis convallis. Risus sed vulputate odio ut enim blandit. Vitae elementum curabitur vitae nunc sed velit. Faucibus nisl tincidunt eget nullam non nisi. Lacus suspendisse faucibus interdum posuere lorem ipsum dolor sit amet.</p> <section> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 4 3"> <circle r="1" cx="3" cy="3" fill="blue"/> <circle r=".5" cx="1.5" cy="3" fill="blue"/> </svg>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM