What is the best way to make SVGs of varying sizes appear uniform in height?

I am encountering an issue with a RestAPI that provides SVG images of different sizes. While some images fit perfectly, others do not. You can check the examples using this link: https://i.sstatic.net/XJwKU.png

The main challenge is that I do not have information about all the picture sizes. How can I address this issue without changing the card height to 100%?

<div className="countries__card card">
    <div className="card__flag">
        <LazyLoad
            height={windowWidth >= 614 ? "210px" : '173px"'}
            once
        >
            <img
                width={windowWidth >= 614 ? "320px" : "100%"}
                height={windowWidth >= 614 ? "210px" : '100%"'}
                src="https://restcountries.eu/data/afg.svg"
                alt="flag"
            />
        </LazyLoad>
    </div>
    <div className="card__body">
        <div className="card__body-name">
            <h5>Lorem</h5>
        </div>
        <div className="card__body-infos">
            <span className="country-capital">Capital: Lorem</span>
            <span className="country-currencies">Currency: Lorem</span>
            <span className="country-region">Region: Lorem</span>
        </div>
    </div>
    <footer className="card__footer">
        <a href="#!">Go to detailed information.</a>
    </footer>
</div>
...
.card {
    border: 2px solid black;
    text-align: center;
    margin-bottom: rem(50px);
    width: 320px;
    overflow: hidden;
    animation: showCard 0.7s ease-out forwards;

    img {
        vertical-align: middle;
        border-top-left-radius: 8px;
        border-top-right-radius: 8px;
    }

    &__body,
    &__footer {
        background: $skyblue;
    }

    &__body {
        &-name {
            color: $text-navy;
            padding: rem(10px) 0;
            font-size: rem(20px);
            font-weight: $font-bold;
            font-weight: normal;
            border-bottom: 4px solid $text-navy;
        }

        &-infos {
            padding: rem(15px) 0;
            display: flex;
            justify-content: center;
            color: $text-navy;

            span {
                white-space: nowrap;
                font-size: rem(14.3px);
            }

            span:not(:last-child) {
                padding-right: rem(5px);
                border-right: 3px solid $text-navy;
            }

            span:not(:first-child) {
                padding-left: rem(5px);
            }
        }
    }

    &__footer {
        border-top: 3px solid $text-navy;
        padding: rem(15px) 0;
        background: $text-navy;
        cursor: pointer;
        border-bottom-left-radius: 8px;
        border-bottom-right-radius: 8px;
    }
}

Answer №1

Flag dimensions do not have a standard measurement. RestCountries offers flags in their original dimensions.

I have developed a compact 29 kB Single File Web Component that displays all SVG flags in fixed dimensions, similar to most SVG flag repositories.
It can utilize RestCountries (or any other SVG flag repository) as an alternative source:

Unfortunately, I have not yet created all the crests. Interestingly, you selected the exact two I have not completed: Afghanistan and Andorra for the screenshot. FlagMeister will default to RestCountries unless specified otherwise with the detail setting.

<script src="https://flagmeister.github.io/elements.flagmeister.min.js"></script>
<style>
div {
display:grid;
grid-template-columns:repeat(6,100px);
gap:10px;
}
[nodetail] {
--flagmeisterdetail:99999;
}
</style>
<h3>RestCountries & FlagMeister (forced to no-detail)</h3> 
<div>
<flag-af></flag-af>
<flag-af nodetail></flag-af>
<flag-by></flag-by>
<flag-by nodetail></flag-by>
<flag-ad></flag-ad>
<flag-ad nodetail></flag-ad>
</div>

Answer №2

If you're willing to store local copies of all the flags, you can add the following attribute to all SVGs. This will allow them to stretch to fit the width and height of your <img> tag.

preserveAspectRatio="none"

For example, the Belarus flag will look like this:

svg {
  width: 300px;
  height: 300px;
}
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="900" height="450" viewBox="0 0 1098 549" preserveAspectRatio="none">
<title>Flag of Belarus</title>
<rect fill="#C8313E" width="1098" height="549"/>
<rect y="366" fill="#4AA657" width="1098" height="183"/>
<rect fill="#FFF" width="122" height="549"/>
<g id="h">
<g id="q" fill="#C8313E" fill-rule="evenodd" transform="scale(5.304347826,9)">
<path d="M4,0h3v1h1v1h1v1h1v1h1v1h-1v1h-1v1h-1v1h-1v1h-1v1h-1v1h-1v-1h-1v-1h-1v-1h-1v-1h-1v-1h-1v-1h-1v-1h1v-1h1v-1h1v-1h1zM5,2h1v1h1v1h1v1h-1v1h-1v1h-1v-1h-1v-1h-1v-1h1v-1h1zM5,4h1v1h-1zM0,1h1v1h-1zM0,7h1v1h-1zM11,0h0.6v2h-.6zM11,7h.6v2h-.6zM2,9h1v1h1v1h1v1h-1v1h-1v1h-1v-1h-1v-1h-1v-1h-1v-1h1v-1h1zM2,11h1v1h-1zM8,9h1v1h1v1h1v1h-1v1h-1v1h-1v-1h-1v-1h-1v-1h1v-1h1zM8,11h1v1h-1zM0,15h1v1h-1zM11,14h.6v2h-.6z"/>
<path d="M0,18h1v-1h1v-1h1v-1h1v-1h1v-1h1v1h1v1h1v1h1v1h1v1h1v1h.6v4h-.6v1h-1v1h-1v1h-1v1h-1v1h-1v1h-1v2.6h-2v-0.6h-1v-1h-1v-1h-1v-1h-1 v-3h1v1h1v1h1v1h1v-1h1v-1h1v-1h1v-1h1v-1h1v-1h-1v-1h-1v-1h-3v1h2v1h-1v1h-1v1h-1v-1h-1v-1h-1v-1h-1zM0,22h1v1h-1zM11,25h.6v1h-.6zM9,27h1v1h1v1h.6v1.6h-.6v-.6h-1v-1h-1zM7,30h1v.6h-1z"/>
</g>
<use xlink:href="#q" transform="translate(122,0) scale(-1,1)"/>
</g>
<use xlink:href="#h" transform="translate(0,549) scale(1,-1)"/>
</svg>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Looking to increase the resolution of a 512x512 heightmap array of pixels in a png image to 2017x2017 using JavaScript for implementation as a heightmap in Unreal Engine 4?

I have a task of converting a 512 x 512 32-bit RGB PNG image with encoded height values into a 16-bit grayscale PNG representing height values for a height map. Below is the code used for this conversion: The image conversion process utilizes image-js li ...

Tips for creating an input box that only accepts floating point numbers:

I have a custom component - a text box that I am using in two different places. In one location, it accepts integers and in another, floats. For the integer validation (where dataType=2), I have successfully implemented it. However, for the float validat ...

Injected code from a self-built extension may not be applied by Google Chrome on certain websites until the scroll wheel is turned

I have developed two unique Chrome extensions designed to enhance web pages with CSS and SVG filters. Here are the links to the source code: CB Enhancer (adjusts colors for color-blind users) https://drive.google.com/open?id=1hvJIn3kPAMjaIWrAiUtIEVkEesQ1c ...

How can I adjust the vertical position of Material-UI Popper element using the popper.js library?

https://i.stack.imgur.com/ZUYa4.png Utilizing a material-ui (v 4.9.5) Popper for a pop-out menu similar to the one shown above has been my recent project. The anchorElement is set as the chosen ListItem on the left side. My goal is to have the Popper alig ...

Creating a Shadow Effect on CSS Triangles

I have created a unique design that resembles a large arrow pointing to the right. <div style=" font-size: 0px; line-height: 0%; width: 100px; border-bottom: 80px solid #8cb622; border-right: 62px solid #dadbdf; "></div> <div style=" font ...

Breaking the border between columns in CSS columns

To see a demonstration of my question, please check out this fiddle. In short, I am seeking a solution for making the purple border extend to the height of the green border, overlapping it. I am open to any creative solutions and hacks. Specifically, wit ...

Show a stylish container when the server encounters an HTTP error with status code 500

My approach for error handling involves a straightforward ErrorHandler class structured like this: export class AppErrorHandler implements ErrorHandler { handleError(error : any){ //console.log('test error ', error.status); ...

Utilize XQuery to manipulate both outer elements and inner elements

As a newcomer to XQuery, I am seeking assistance with the following XML example: <lg> <l n="1">Z nutz vnd heylsamer ler / verma⸗ </l> <l n="2">nung vnd ervolgung der wyßheit / ver </l> <l n="3"> ...

Leveraging jQuery's Append functionality

Struggling with using jQuery's .append() method. Check out my code: $('#all ul li').each(function() { if ($(this).children('.material-icons').hasClass('offline-icon') == false) { $('#online ul').append ...

ng-grid defines different cellClass based on the value of the field

I am currently using the ng-grid and I want the column to display in a different color based on different values. I have tried, but not succeeded so far... $scope.gridOptions = { ........ columnDefs: [ { field: "status", displayName: "St ...

Submitting a form using jQuery and processing the response

Can a form be submitted using jQuery without utilizing json, ajax, or other methods for handling the result? For example: <form id="loginform"> //some input fields and a submit button. </form> And then with jQuery: $("#loginform").sub ...

Generating random values from an array in PHP without the need to refresh the page

Lately, I've been delving into the world of PHP for the first time in a project. However, I've encountered a problem that has me stumped – despite scouring various questions on Stack Overflow. My issue involves randomizing values from an array ...

Creating a drop-down menu that aligns perfectly under the bar in Material-UI: What you need to know

While working with Material-UI, I encountered a problem with my drop-down menu. Every time I click on it, it covers the bar instead of appearing below it (see image links below). https://i.stack.imgur.com/1Y8CL.jpg https://i.stack.imgur.com/emf87.jpg Is ...

The script for tracking cursor coordinates is incompatible with other JavaScript code

<html> <script language="javascript"> document.onmousemove=function(evt) { evt = (evt || event); document.getElementById('x').value = evt.clientX; document.getElementById('y').value = evt.clientY; document.ge ...

Tips for choosing a particular list item using jQuery: retrieve the attribute value of 'value' and showcase it on the webpage

I have a task that requires the following: Implement an event listener so that when you click on any list item, the value of its "value" attribute will be shown next to this line. In my HTML, I have an ordered list with 8 list items. The values range fro ...

What is causing the dysfunction of angular2 form when used with the HTML <form> tag?

Can someone explain the strange behavior of the <form> element in ng2 to me? I have noticed something odd and need some clarification :) To demonstrate, I have created a simple Plunker example at this link: https://plnkr.co/edit/vdrHJBNdd26y6YhPXTHD ...

Challenges in Converting HTML String into a jQuery Object and Navigating It

I have created a small script that takes an autoresponder code from a textarea for email marketing. When the textarea loses focus, it should parse the pasted code, extract certain attributes and elements, and place them in separate input fields. However, ...

What is the best method for creating a fade effect on a div background?

I am trying to animate a div with the id #test from a 0 opacity background to an opacity of 0.7 using CSS rgba values, but for some reason, the .animate function is not working as expected. Here is my CSS: #test { background-color: rgba(0, 0, 0, 0); ...

Scroll to make the div slide in from the bottom

I'm trying to achieve a similar effect like the one shown in this website (you need to scroll down a bit to see the divs sliding in). Although I'm not very proficient in JS, I was able to create a code that makes the divs fade in from 0 opacity ...

How to effectively use flexbox to resize child elements in a CSS layout

My goal is to create a layout where a child element fills the entire flex box of a flex layout. Here is an example of the HTML structure I am using: <div class="parent"> <div class="child1"> should have 100px height </div&g ...