【问题标题】:SVG Responsive TextSVG 响应式文本
【发布时间】:2016-03-21 19:28:02
【问题描述】:

我在网页中有一个 SVG,它由图像 + 文本组成

<object data="/infographic/timeline.svg" type="image/svg+xml">    
  <img src="/infographic/timeline.svg" alt="Timeline">
</object>

所有的图像都是响应式的,但文本不是,所以文本变得非常非常小。

SVG的sn-p(它的海量)

  <defs>
    <style>
      .cls-1 {
        font-size: 60.014px;
      }

  .cls-1, .cls-10 {
    opacity: 0.69;
  }

  .cls-1, .cls-10, .cls-4, .cls-5, .cls-7, .cls-8, .cls-9 {
    fill: #ffffff;
  }

  .cls-1, .cls-10, .cls-3, .cls-4, .cls-5, .cls-6, .cls-7, .cls-9 {
    text-anchor: middle;
  }

  .cls-1, .cls-3, .cls-6 {
    font-family: "Roboto";
  }

  .cls-2 {
    font-size: 32.014px;
  }

  .cls-3 {
    font-size: 14.089px;
  }

  .cls-3, .cls-6 {
    fill: #db7426;
  }

  .cls-4, .cls-6 {
    font-size: 32px!important;
  }

  .cls-10, .cls-4, .cls-5, .cls-7, .cls-8, .cls-9 {
    font-family: Roboto;
  }

  .cls-5 {
    font-size: 24px;
  }

  .cls-5, .cls-8, .cls-9 {
    font-weight: 400;
  }

  .cls-6 {
    font-weight: 600;
  }

  .cls-10, .cls-7 {
    font-size: 18.75px;
    font-weight: 300;
  }

  .cls-7 {
    opacity: 0.4;
  }

  .cls-8, .cls-9 {
    font-size: 22px;
  }
 </style>
</defs>

<text id="Who_are_you_what_do_you_do_what_s_your_why_What_s_been_keepi" data-name="Who are you, what do you do, what’s your why? What’s been keepi" class="cls-8" x="397.706" y="535.325">Who are you, what do you do, what’s your why?<tspan x="397.706" dy="26.4">What’s been keeping you lying awake at night. </tspan></text>

是否可以让文本大小随着 SVG/屏幕宽度变小而增加?

任何帮助将不胜感激。

【问题讨论】:

  • 您是否尝试过不以像素为单位指定文本大小?
  • 我在 CSS 中,或者你的意思是在 标签上?
  • 通常文本会随 SVG 中的其他所有内容缩放 (example)。您似乎以某种方式禁用了此行为。我只是想弄清楚如何。
  • 如果有帮助,SVG 位于页面底部(长信息图)outhouse.scottishbordersdesign.co.uk/approach 当您将浏览器窗口移入时,文本变得难以阅读,我需要文本大小上去,或者你有任何其他建议会很棒。
  • 哦,我明白了。当 SVG 缩小时,您希望文本保持清晰的大小。我认为这是不可能的——你不能让文本在 SVG 中重新包装。切换到 HTML 或将整个 SVG 用作位置为 center top 的背景图像,以便在较小的视口上剪掉非文本区域。

标签: svg responsive-design responsiveness


【解决方案1】:

这可以通过在 html 上下文 中使用 foreignObject svg 元素以及对 viewBow 进行一些调整来实现。

在这个演示中,文本保持可选:

.demo {
  overflow: auto;
  resize: both;
  border:1px black solid;
  width: 230px;
  height: 130px
}

.svgtext {
  font-size: 28rem;
  height:100%;
  width:100%
}
<div class="demo">

  <svg x="0" y="30" viewBox="0 0 100 100" width="100%" height="100%">
    <foreignObject x="12" y="23" height="100%" width="100%">
      <div class"svgtext">
        Hello world!
       </div>
     </foreignObject>
  </svg>

</div>

使用preserveAspectRatio 控制调整大小的行为:

.demo {
  overflow: auto;
  resize: both;
  border:1px black solid;
  width: 230px;
  height: 130px
}

.svgtext {
  font-size: 28rem;
  height:100%;
  width:100%
}
<div class="demo">

  <svg preserveAspectRatio="none" x="0" y="30" viewBox="0 0 100 100" width="100%" height="100%">
    <foreignObject x="12" y="23" height="100%" width="100%">
      <div class"svgtext">
        Hello world!
       </div>
     </foreignObject>
  </svg>

</div>

【讨论】:

    【解决方案2】:

    使用纯 SVG 是不可能的(至少现在还没有)。唯一的解决方案是:

    1. 内联 SVG 并使用 javascript 操作文本的大小。

    2. 内联 SVG 并使用媒体查询控制文本的大小(见下文)。

    3. 将 CSS 添加到 SVG 并在其中使用媒体查询(见下文)。

    4. 当页面变小时使用媒体查询来切换 SVG

    选项 2 示例:将媒体查询与内联 SVG 一起使用

    text {
      font-size: 10px;
    }
    
    @media (max-width: 400px) {
      text {
        font-size: 20px;
      }
    }
    <svg viewBox="0 0 100 100" width="100%" height="100%">
      <circle cx="50" cy="50" r="50" fill="orange"/>
      <text x="50" y="60" text-anchor="middle">Testing</text>
    </svg>

    选项 3 的示例:在 SVG 中使用 CSS 中的媒体查询

    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" width="100%" height="100%">
      <style>
        text {
          font-size: 10px;
        }
    
        @media (max-width: 400px) {
          text {
            font-size: 20px;
          }
        }
      </style>
      <circle cx="50" cy="50" r="50" fill="orange"/>
      <text x="50" y="60" text-anchor="middle">Testing</text>
    </svg>
    

    【讨论】:

    猜你喜欢
    • 2013-09-07
    • 2019-12-11
    • 2023-04-10
    • 2014-11-14
    • 2019-08-24
    • 2019-05-06
    • 2016-06-25
    • 2016-01-08
    • 2015-12-22
    相关资源
    最近更新 更多