首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试将svg扩展到父容器

尝试将svg扩展到父容器
EN

Stack Overflow用户
提问于 2020-02-11 21:55:09
回答 1查看 49关注 0票数 1

我一直在尝试扩展SVG,但失败了,我的头撞得够多了,但似乎没有一个解决方案有效。所以基本上它是一个以SVG为背景的中心模式。

我正在尝试SVG以正确地匹配父容器的边框,边到边。附上下面的设计和代码链接。

设计

CodePen Link

代码语言:javascript
复制
.modal {
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  position: absolute;
  z-index: 99999999;
  max-height: 100%;
  width: 80%;
  height: 80%;
  max-width: 100%;
  border: 2px solid black;
  box-shadow: 0 10px 20px rgba(5, 31, 52, 0.19), 0 6px 6px rgba(5, 31, 52, 0.2);
}

.svg-container {
  position: relative;
  height: 0;
  width: 100%;
  padding: 0;
  padding-bottom: 100%;
}

svg {
  position: absolute;
  height: 100%;
  width: 100%;
  left: 0;
  top: 0;
}
代码语言:javascript
复制
<div class="modal-hidden">
  <div class="modal">
    <div class="svg-container">
      <svg viewBox="0 0 844 688" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
          <defs>
              <rect id="path-1" x="0" y="0" width="844" height="688" rx="10"></rect>
              <linearGradient x1="50%" y1="0%" x2="50%" y2="98.5140414%" id="linearGradient-3">
                  <stop stop-color="#8333D4" stop-opacity="0.499535621" offset="0%"></stop>
                  <stop stop-color="#CE5DCB" stop-opacity="0.110276442" offset="100%"></stop>
              </linearGradient>
          </defs>
          <g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
              <g id="Sezzle-Desktop-Modal" transform="translate(-298.000000, -97.000000)">
                  <g id="Modal-Popup" transform="translate(298.000000, 97.000000)">
                      <g id="Desktop-Modal">
                          <g id="Modal-Background">
                              <mask id="mask-2" fill="white">
                                  <use xlink:href="#path-1"></use>
                              </mask>
                              <use id="Mask" fill="#FFFFFF" xlink:href="#path-1"></use>
                              <polygon id="Path-2" fill="#FCD7B6" mask="url(#mask-2)" points="705.472656 734 844 327 844 734"></polygon>
                              <polygon id="Path-2" fill="#FCD7B6" mask="url(#mask-2)" transform="translate(69.263672, 158.500000) rotate(-180.000000) translate(-69.263672, -158.500000) " points="0 362 138.527344 -45 138.527344 362"></polygon>
                              <circle id="Oval" fill="#F08570" mask="url(#mask-2)" cx="10.5" cy="71.5" r="105.5"></circle>
                              <circle id="Oval" fill="url(#linearGradient-3)" mask="url(#mask-2)" cx="808.5" cy="581.5" r="95.5"></circle>
                          </g>
                      </g>
                  </g>
              </g>
          </g>
      </svg>
    </div>
  </div>
</div>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-02-12 02:49:35

我相信您正在寻找的关键属性是preserveAspectRatio="none",以及在svg元素上设置的宽度/高度。我修改了CSS,将其缩减为您需要的内容。你需要在你想要的地方设置大小。我也简化了你的svg文件

代码语言:javascript
复制
html,
body,
.modal-hidden {
  width: 100%;
  height: 100%;
}

.modal-hidden {
  display: flex;
  justify-content: center;
  align-items: center;
}

.modal {
  box-shadow: 0 10px 20px rgba(5, 31, 52, 0.19), 0 6px 6px rgba(5, 31, 52, 0.2);
  height: 40%;
  width: 40%;
}

.svg-container {
  height: 100%;
  width: 100%;
  box-sizing: border-box;
}
代码语言:javascript
复制
<div class="modal-hidden">
  <div class="modal">
    <div class="svg-container">
      <svg preserveAspectRatio="none" viewBox="0 0 844 688" height="100%" width="100%" xmlns="http://www.w3.org/2000/svg">

        <defs>
          <rect id="path-1" x="0" y="0" width="844" height="688" rx="10"/>
            <linearGradient x1="0.5" y1="0" x2="0.5" y2="0.985140380859375" id="linearGradient-3">
              <stop stop-color="#8333D4" stop-opacity="0.499535621" offset="0"/>
              <stop stop-color="#CE5DCB" stop-opacity="0.110276442" offset="1"/>
            </linearGradient>
          </defs>

          <mask id="mask-2" fill="white" stroke="none" stroke-width="1" fill-rule="evenodd">
            <rect width="844" height="688" rx="10"/>
          </mask>

          <rect id="Mask" width="844" height="688" rx="10" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd"/>
          <polygon id="Path-2" fill="#FCD7B6" mask="url(#mask-2)" points="705.5 734 844 327 844 734" stroke="none" stroke-width="1" fill-rule="evenodd"/>
          <polygon id="Path-2" fill="#FCD7B6" mask="url(#mask-2)" points="138.5 -45 0 362 0 -45" stroke="none" stroke-width="1" fill-rule="evenodd"/>
          <circle id="Oval" fill="#F08570" mask="url(#mask-2)" cx="10.5" cy="71.5" r="105.5" stroke="none" stroke-width="1" fill-rule="evenodd"/>
          <circle id="Oval" fill="url(#linearGradient-3)" mask="url(#mask-2)" cx="808.5" cy="581.5" r="95.5" stroke="none" stroke-width="1" fill-rule="evenodd"/>
      </svg>
    </div>
  </div>
</div>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60170493

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档