>  Q&A  >  본문

SVG를 배경 이미지로 사용

<p>如何将SVG作为CSS中的<code>background-image</code>使用?</p> <p>我尝试使用它,但是我感到困惑。</p> <p>我知道它应该是以CSS格式存在的,但我该如何做到这一点</p> <pre class="brush:html;toolbar:false;"><svg width="1318" height="800" xmlns="http://www.w3.org/2000/svg"><defs><linearGradient x1="-45.25%" y1="-88.077%" x2="97.789%" y2="100%" id="a"><stop stop-color="#FF52C1" offset="0%"/><stop stop-color="#F952C5" offset="4.09%"/><stop stop-color="#9952FF" stop-opacity="0" offset="100%"/></linearGradient><linearGradient x1="-64.06%" y1="-121.906%" x2="97.789%" y2="100%" id="b"><stop stop-color="#FF52C1" offset="0%"/><stop stop-color="#F952C5" offset="4.09%"/><stop stop-color="#9952FF" stop-opacity="0" offset="100%"/></linearGradient><linearGradient x1="100%" y1="111.373%" x2="-24.893%" y2="-55.159%" id="c"><stop stop-color="#FF52C1" offset="0%"/><stop stop-color="#9952FF" offset="100%"/></linearGradient><linearGradient x1="21.681%" y1="5.006%" x2="145.861%" y2="145.591%" id="d"><stop stop-color="#FF52C1" offset="0%"/><stop stop-color="#9952FF" offset="100%"/></linearGradient><linearGradient x1="6.375%" y1="-15.195%" x2="91.754%" y2="105.701%" id="e"><stop stop-color="#FF52C1" offset="0%"/><stop stop-color="#9952FF" offset="100%"/></linearGradient></defs><g fill="none" fill-rule="evenodd"><path fill="url(#a)" transform="matrix(-1 0 0 1 834.817 0)" d="M0 0h409.224l425.593 376v156.83z"/><path fill="url(#b)" transform="rotate(180 528.65 584)" d="M0 367l641.153.138L1057.3 673.299V801z"/><circle stroke="url(#c)" stroke-width="17" cx="704" cy="563" r="49"/><rect fill="url(#d)" opacity=".558" transform="rotate(45 1107.87 708.87)" x="1088.87" y="689.87" width="38" height="38" rx="3"/><rect fill="url(#d)" opacity=".503" transform="rotate(45 1279.598 103.598)" x="1251.598" y="75.598" width="56" height="56" rx="3"/><rect fill="url(#d)" opacity=".558" transform="rotate(45 934.627 63.627)" x="918.627" y="47.627" width="32" height="32" rx="3"/><rect fill="url(#d)" opacity=".558" transform="rotate(45 703.627 186.627)" x="687.627" y="170.627" width="32" height="32" rx="3"/><rect fill="url(#d)" opacity=".387" transform="rotate(45 1237.02 606.02)" x="1228.521" y="597.521" width="17" height="17" rx="1"/><path d="M91.477 739.477v-16.5a5 5 0 0 1 10 0v16.5h16.5a5 5 0 0 1 0 10h-16.5v16.5a5 5 0 1 1-10 0v-16.5h-16.5a5 5 0 1 1 0-10h16.5z" fill="url(#e)" opacity=".211" transform="rotate(45 96.477 744.477)"/></g></svg> </pre> <p><br /></p>
P粉986937457P粉986937457432일 전561

모든 응답(1)나는 대답할 것이다

  • P粉258083432

    P粉2580834322023-08-15 16:48:02

    당연하죠! HTML에서 SVG를 배경 이미지로 사용하고 CSS를 통해 설정하는 것은 매우 쉽습니다. 단계를 알려드리겠습니다.

    SVG를 CSS에 직접 포함:

    SVG 코드가 있는 경우 데이터 URL을 사용하여 CSS에 직접 삽입할 수 있습니다. 예:

    으아악

    SVG 콘텐츠(예: <svg> ... </svg>之间的所有内容)不包含可能与CSS语法冲突的任何字符。这包括像#";와 같은 문자)를 확인해야 문제를 방지할 수 있습니다.

    SVG 파일을 배경으로 사용:

    SVG 콘텐츠가

    와 같은 별도의 파일에 있는 경우 다른 이미지처럼 참조할 수 있습니다. background.svg 으아악

    HTML 및 CSS로 구현:

    이것은 간단한 예입니다. SVG를

    : background.svg이라는 파일에 저장한다고 가정해 보겠습니다.

    HTML(index.html):

    으아악

    CSS(): styles.css 으아악

    참고:

    SVG를 표시하려면 요소(이 경우

    ) 또는 크기를 제공할 만큼 충분한 콘텐츠가 있어야 한다는 점을 항상 기억하세요. my-element)应具有指定的widthheight 등을 사용하여 원하는 SVG 배경의 위치와 크기를 조정하세요. 이제 background-sizebackground-position div의 배경을 열면. index.html时,您应该看到SVG作为my-element

    회신하다
    0
  • 취소회신하다