Make SVG icon get the 100% of grid area height and while width obeys to aspect ratio

Viewed 129

Target

  • The SVG icon's ({) height of must always fill the purple area's height, two merged rows of grid layout.
  • Width must automatically resize in compliance with icon's aspect ratio.

enter image description here

Initial listing and fiddle

Fiddle

<div class="Layout">
  <div class="TopLabel">Lorem ipsum dolor sit amet, consectetur adipiscing elit</div>
  <div class="BottomLabel">sed do eiusmod tempor incididunt ut labore et dolore magna aliqua</div>
  <div class="IconSlot">
    <svg class="OpeningBracketLabel" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 20">
      <path d="M4.38 15.12c0-.7 0-1.24-.13-1.78a2.85 2.85 0 00-3-2.64l-.81-.11V9.38c0-.05.17-.12.27-.13.4 0 .82 0 1.22-.1A2.86 2.86 0 004.31 6.2c.05-.92 0-1.84.07-2.75A3.53 3.53 0 018 0h1.63a.35.35 0 01.23.06.35.35 0 01.09.26c0 .52 0 .76-.12.88s-.38.11-.85.11c-.26 0-.52 0-.78.05a2.2 2.2 0 00-2.1 2.28v2a4 4 0 01-2.51 4 .74.74 0 00-.22.1c-.07.06-.18.14-.18.2s.11.15.19.2c.4.25.82.45 1.19.72a3.79 3.79 0 011.53 3.19v2a2.33 2.33 0 002.52 2.57h1c.34 0 .32.09.33.34 0 1 0 1-.95 1a10.63 10.63 0 01-1.79-.13 3.39 3.39 0 01-2.8-3.24c-.06-.51-.03-1.07-.03-1.47z"></path>
    </svg>
  </div>
</div>
.Layout {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 4px 6px;
}

.Layout + .Layout {
  margin-top: 12px;
}

.TopLabel {
 font-size: 14px;
 line-height: 18px;
 background: #BBDEFB;
}

.BottomLabel {
  font-size: 12px;
  line-height: 16px;
  background: #FFECB3;
  grid-row: 2;
}

.IconSlot {
  width: 20px;
  grid-row: span 2;
  order: 3;
  background: #E1BEE7;
}

.OpeningBracketLabel {
  width: 100%;
  height: 100%;
}

Some musings about this problem

I suppose the targets could not be reached without container for SVG icon (if I am wrong please let me know). So I added the .IconSlot.

Just

.OpeningBracketLabel { 
  height: 100%;
}

will not be enough - the layout will brake:

enter image description here

I added the width: 20px; for the .IconSlot temporary: without it, the icon will get too much place:

enter image description here

2 Answers

One posible solution to your problem would be using a simpler path: not a filled one but just the stroke. Now you can add preserveAspectRatio="none" to the svg so that it can stretch the image. The problem now is that the stroke would have an irregular stroke-width due to the deformation. In order to avoid the problem you can use vector-effect="non-scaling-stroke" for the path.

Since you want to have a dry code you may avoid repeating the code for the path by using a <use> element the second time you need it.

.Layout {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 4px 6px;
}

.Layout + .Layout {
  margin-top: 12px;
}

.TopLabel {
 font-size: 14px;
 line-height: 18px;
 background: #BBDEFB;
}

.BottomLabel {
  font-size: 12px;
  line-height: 16px;
  background: #FFECB3;
  grid-row: 2;
}

.IconSlot {
  width: 20px;
  grid-row: span 2;
  order: 3;
  background: #E1BEE7;
}

.OpeningBracketLabel {
  width: 100%;
  height: 100%;
}
<div class="Layout">
  <div class="TopLabel">Lorem ipsum dolor sit amet, consectetur adipiscing elit</div>
  <div class="BottomLabel">sed do eiusmod tempor incididunt ut labore et dolore magna aliqua</div>
  <div class="IconSlot">
    <svg class="OpeningBracketLabel" preserveAspectRatio="none" viewBox="3 0 6 19">
      <path id="brac" d="M9.86 .8c-.26 0-.52 0-.78.05a2.2 2.2 0 00-2.1 2.28v2a4 4 0 01-2.51 4 .74.74 0 00-.22.1c-.07.06-.18.14-.18.2s.11.15.19.2c.4.25.82.45 1.19.72a3.79 3.79 0 011.53 3.19v2a2.33 2.33 0 002.52 2.57h.35" fill="none" stroke="black" stroke-width="2" vector-effect="non-scaling-stroke"></path>
    </svg>
  </div>
</div>

<div class="Layout">
  <div class="TopLabel">Lorem ipsum dolor sit amet, consectetur adipiscing elit</div>
  <div class="BottomLabel">sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."</div>
   <div class="IconSlot">
    <svg class="OpeningBracketLabel" preserveAspectRatio="none"  viewBox="3 0 6 19">
     <use xlink:href="#brac"/>
    </svg>
  </div>
</div>

You could add a position:absolute to the svg and remove the width from the parent div

.Layout {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 4px 6px;
}

.Layout+.Layout {
  margin-top: 12px;
}

.TopLabel {
  font-size: 14px;
  line-height: 18px;
  background: #BBDEFB;
}

.BottomLabel {
  font-size: 12px;
  line-height: 16px;
  background: #FFECB3;
  grid-row: 2;
}

.IconSlot {
  position: relative;
  width: 50px;
  grid-row: span 2;
  order: 3;

}

.OpeningBracketLabel {
  position: absolute;
  height: 100%;
    background: #E1BEE7;
}
<div class="Layout">
  <div class="TopLabel">Lorem ipsum dolor sit amet, consectetur adipiscing elit</div>
  <div class="BottomLabel">sed do eiusmod tempor incididunt ut labore et dolore magna aliqua</div>
  <div class="IconSlot">
    <svg class="OpeningBracketLabel" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 20">
      <path d="M4.38 15.12c0-.7 0-1.24-.13-1.78a2.85 2.85 0 00-3-2.64l-.81-.11V9.38c0-.05.17-.12.27-.13.4 0 .82 0 1.22-.1A2.86 2.86 0 004.31 6.2c.05-.92 0-1.84.07-2.75A3.53 3.53 0 018 0h1.63a.35.35 0 01.23.06.35.35 0 01.09.26c0 .52 0 .76-.12.88s-.38.11-.85.11c-.26 0-.52 0-.78.05a2.2 2.2 0 00-2.1 2.28v2a4 4 0 01-2.51 4 .74.74 0 00-.22.1c-.07.06-.18.14-.18.2s.11.15.19.2c.4.25.82.45 1.19.72a3.79 3.79 0 011.53 3.19v2a2.33 2.33 0 002.52 2.57h1c.34 0 .32.09.33.34 0 1 0 1-.95 1a10.63 10.63 0 01-1.79-.13 3.39 3.39 0 01-2.8-3.24c-.06-.51-.03-1.07-.03-1.47z"></path>
    </svg>
  </div>
</div>

<div class="Layout">
  <div class="TopLabel">Lorem ipsum dolor sit amet, consectetur adipiscing elit</div>
  <div class="BottomLabel">sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore
    eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."</div>
  <div class="IconSlot">
    <svg class="OpeningBracketLabel" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 20">
      <path d="M4.38 15.12c0-.7 0-1.24-.13-1.78a2.85 2.85 0 00-3-2.64l-.81-.11V9.38c0-.05.17-.12.27-.13.4 0 .82 0 1.22-.1A2.86 2.86 0 004.31 6.2c.05-.92 0-1.84.07-2.75A3.53 3.53 0 018 0h1.63a.35.35 0 01.23.06.35.35 0 01.09.26c0 .52 0 .76-.12.88s-.38.11-.85.11c-.26 0-.52 0-.78.05a2.2 2.2 0 00-2.1 2.28v2a4 4 0 01-2.51 4 .74.74 0 00-.22.1c-.07.06-.18.14-.18.2s.11.15.19.2c.4.25.82.45 1.19.72a3.79 3.79 0 011.53 3.19v2a2.33 2.33 0 002.52 2.57h1c.34 0 .32.09.33.34 0 1 0 1-.95 1a10.63 10.63 0 01-1.79-.13 3.39 3.39 0 01-2.8-3.24c-.06-.51-.03-1.07-.03-1.47z"></path>
    </svg>
  </div>
</div>

Related