亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

讓 SVG 充當塊元素

讓 SVG 充當塊元素

ibeautiful 2023-09-25 17:03:12
我一直在試圖弄清楚如何讓我的 SVG 元素充當 display: 塊。我想要一個位于 SVG 正下方的圖像,但它始終與 SVG 重疊。我嘗試將屬性更改為“display: block”以及在 SVG 元素本身周圍創建一個 div 容器,但似乎沒有任何效果。我確信有一個簡單的方法,我只是想不出來。#svg{        position: absolute;        left: 0;        right: 0;        margin: auto;        z-index: -1;        display: block;        margin-bottom: 10px;        display: block;    }    #svgContainer{        display: block;        width: 90%;        margin: auto;    }    #seasonImage{        background-image: url('images/summer.png');        width: 120%;        margin-left: -30px;        height: auto;        background-repeat:no-repeat;        background-size: contain;        height:200px;        display: block;        position: relative;    }<div id="svgContainer">  <svg id="svg" viewBox="-10 -10 220 220" width="90%">    <text x="80" y="106" id="currentTemp" font-size="18" font-weight="bold" style="fill: #C9AC68">{{currentTemp}}&#176;</text>    <circle class="background" cx="100" cy="100" r="35" stroke="#C9AC68" />    <circle class="background" cx="100" cy="100" r="55" stroke="#B25538" />    <circle class="background" cx="100" cy="100" r="75" stroke="#507282" />    <circle class="background" cx="100" cy="100" r="95" stroke="#7E8959" />                  <circle id="line1" class="overlayLine" cx="100" cy="100" r="35" stroke="#C9AC68" stroke-dasharray="0, 3000" stroke-dashoffset="126" transform="rotate(-90,100,100)" />    <circle id="line2" class="overlayLine" cx="100" cy="100" r="55" stroke="#B25538" stroke-dasharray="0, 3000" stroke-dashoffset="188" transform="rotate(-90,100,100)" />    <circle id="line3" class="overlayLine" cx="100" cy="100" r="75" stroke="#507282" stroke-dasharray="0, 3000" stroke-dashoffset="251" transform="rotate(-90,100,100)" />    <circle id="line4" class="overlayLine" cx="100" cy="100" r="95" stroke="#7E8959" stroke-dasharray="0, 3000" stroke-dashoffset="314" transform="rotate(-90,100,100)" />  </svg></div><div id="seasonImage"></div>
查看完整描述

1 回答

?
Helenr

TA貢獻1780條經驗 獲得超4個贊

您的 CSS 設置position: absolute為該svg元素。與靜態定位的元素不同,絕對定位的元素不占用其容器所需的空間。top瀏覽器將它們放置在您用、left、right和指定的坐標處bottom,無論它是在任何其他元素的下面還是上面。


從#seasonImage元素的角度來看,沒有任何東西占據其自身上方的空間。因此,瀏覽器將其放置在其容器的頂部,與絕對定位的 SVG 重疊。


如果我們注釋掉或刪除position: absolute;(及其相關屬性),瀏覽器會將圖像直接放置在 SVG 下方。


#svg {

  /*position: absolute;

      left: 0;

      right: 0;*/

  margin: auto;

  /*z-index: -1;*/

  display: block;

  margin-bottom: 10px;

  display: block;

}


#svgContainer {

  display: block;

  width: 90%;

  margin: auto;

}


#seasonImage {

  background-image: url('https://cataas.com/cat?26');

  width: 120%;

  margin-left: -30px;

  height: auto;

  background-repeat: no-repeat;

  background-size: contain;

  height: 200px;

  display: block;

  position: relative;

}

<div id="svgContainer">

  <svg id="svg" viewBox="-10 -10 220 220" width="90%">

            <text x="80" y="106" id="currentTemp" font-size="18" font-weight="bold" style="fill: #C9AC68">{{currentTemp}}&#176;</text>

            <circle class="background" cx="100" cy="100" r="35" stroke="#C9AC68" />

            <circle class="background" cx="100" cy="100" r="55" stroke="#B25538" />

            <circle class="background" cx="100" cy="100" r="75" stroke="#507282" />

            <circle class="background" cx="100" cy="100" r="95" stroke="#7E8959" />


            <circle id="line1" class="overlayLine" cx="100" cy="100" r="35" stroke="#C9AC68" stroke-dasharray="0, 3000" stroke-dashoffset="126" transform="rotate(-90,100,100)" />

            <circle id="line2" class="overlayLine" cx="100" cy="100" r="55" stroke="#B25538" stroke-dasharray="0, 3000" stroke-dashoffset="188" transform="rotate(-90,100,100)" />

            <circle id="line3" class="overlayLine" cx="100" cy="100" r="75" stroke="#507282" stroke-dasharray="0, 3000" stroke-dashoffset="251" transform="rotate(-90,100,100)" />

            <circle id="line4" class="overlayLine" cx="100" cy="100" r="95" stroke="#7E8959" stroke-dasharray="0, 3000" stroke-dashoffset="314" transform="rotate(-90,100,100)" />

        </svg>

</div>


<div id="seasonImage"></div>


查看完整回答
反對 回復 2023-09-25
  • 1 回答
  • 0 關注
  • 110 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號