简体   繁体   English

将 SVG 缩放到相邻元素的高度

[英]Scale SVG to height of an adjacent element

I'm trying to scale an SVG with a fixed aspect ratio to match the height of an adjacent element (a div containing a range of different elements).我正在尝试缩放具有固定纵横比的 SVG 以匹配相邻元素的高度(包含一系列不同元素的 div)。

For demonstration purposes, here I have simply set the height of the SVG ( #triangle ) to 10rem , which just happens to match the height of the div ( #text ) on the right.出于演示目的,这里我简单地将 SVG ( #triangle ) 的高度设置为10rem ,这恰好与右侧 div ( #text ) 的高度相匹配。 But if you resize the window, everything changes.但是,如果您调整 window 的大小,一切都会改变。 (see code snippet at the end of this question) (请参阅此问题末尾的代码片段)

屏幕截图显示了一个三角形 SVG,其高度与相邻的文本部分匹配

What I would like to achieve is to be able to resize the window and as the text wraps (changing its height), the SVG's height adjusts to match:我想要实现的是能够调整 window 的大小,并且随着文本换行(改变其高度),SVG 的高度会调整以匹配:

显示相同三角形 SVG 和相邻文本部分的屏幕截图。文本已换行,使其更高。调整 SVG 的高度以匹配文本部分

I assumed that there would be a CSS grid or Flexbox solution to this, but I haven't found one so far.我假设会有一个 CSS 网格或 Flexbox 解决方案,但到目前为止我还没有找到。

(I'd also be very happy with a CSS solution instead of the SVG, eg. that uses clip-path or some other styled div , but the key issue is that it should have a fixed aspect ratio) (我也很高兴使用 CSS 解决方案而不是 SVG,例如使用剪辑路径或其他一些样式的div ,但关键问题是它应该具有固定的纵横比)

Creating a Javascript solution would be ok, but it is a little trickier than it first appears.创建一个 Javascript 解决方案是可以的,但它比最初出现的要复杂一些。

The difficulty is that the available space that determines whether #text wraps (and thus determines the height of #text ) is dependent on the width of #triangle .困难在于确定#text是否换行(从而确定#text的高度)的可用空间取决于#triangle的宽度。 #triangle has a fixed aspect ratio so its width is dependent on its height, which would be dependent on the height of #text ... #triangle有一个固定的纵横比,所以它的宽度取决于它的高度,这将取决于#text的高度......

A practical note: Obviously this kind of layout doesn't provide a huge amount of flexibility, and could only work with a particular type of content — a narrow SVG, and reasonably short text elements.实用说明:显然这种布局并不能提供很大的灵活性,并且只能用于特定类型的内容——狭窄的 SVG 和相当短的文本元素。 On desktop screens there would be plenty of empty space on the right hand side, with no wrapping, whereas on mobile it would take up the full width, with some wrapped text.在桌面屏幕上,右侧会有很多空白空间,没有换行,而在移动设备上,它会占据整个宽度,并带有一些换行的文本。

 #container { border: 1px goldenrod solid; padding: 1em; display: flex; flex-direction: row; justify-content: flex-start; gap: 1em; } #triangle { border: 1px steelblue solid; height: 10rem; } #text { border: 1px steelblue solid; display: flex; flex-direction: column; justify-content: flex-start; }
 <div id="container"> <svg id="triangle" viewBox="0 0 6 8"> <polygon points="0,8 3,0 6,8" fill="tomato"/> </svg> <div id="text"> <h1> Triangle </h1> <p> A polygon with three edges and three vertices. </p> <a href="https://en.wikipedia.org/wiki/Triangle"> Triangle on Wikipedia </a> </div> <div> </body>

This is not the perfect solution, but maybe somebody else can extend it.这不是完美的解决方案,但也许其他人可以扩展它。

The main trick is that the SVG ( svg ) has the height of 0, but will grow ( flex-grow: 1; ) according to the parent element ( .triangle ).主要技巧是 SVG ( svg ) 的高度为 0,但会根据父元素 ( .triangle ) 增长 ( flex-grow: 1; )。

The problem in this solution is that the share between .triangle and .text is fixed ( grid-template-columns: 1fr 4fr; ).此解决方案中的问题是.triangle.text之间的份额是固定的( grid-template-columns: 1fr 4fr; )。 This share can be adjusted, but if the use cases differ a lot in the text height it will be a problem.这个份额可以调整,但如果用例在文本高度上有很大差异,那将是一个问题。

 #container { border: 1px goldenrod solid; padding: 1em; display: grid; grid-template-columns: 1fr 4fr; grid-column-gap: 1em; }.triangle { display: flex; align-items: stretch; flex-direction: column; }.triangle svg { border: 1px steelblue solid; flex-grow: 1; height: 0; }.text { border: 1px steelblue solid; }
 <div id="container"> <div class="triangle"> <svg viewBox="0 0 6 8"> <polygon points="0,8 3,0 6,8" fill="tomato"/> </svg> </div> <div class="text"> <h1>Triangle</h1> <p>A polygon with three edges and three vertices.</p> <a href="https://en.wikipedia.org/wiki/Triangle"> Triangle on Wikipedia </a> </div> </div>

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

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