I have a svg generated by javascript and included inline in the HTML page. On top (above, higher z-index) of some hexes (polygons defined in SVG) I want to display an image.
I was able to do it adding a div containing the image with css position absolute and top/left/width in pixels.
But, with the data I have to display, that means adding 473 div for 39 different images (473 polygons out of ~10k need an image, 74 lines and 134 columns of hexes). The image represents the map of the world and the hexes can have about 40 different colors. You can see it here.
I was wondering if there was a solution to make that image appear on top of the hex, ideally in svg or in css? like adding a class to the polygon, or a style?
I have tried putting a style to a polygon and giving it a background-image, contain the image within the polygon, but the color of the polygon prevails on top.
Here is the code with:
- the first polygon of the first line with an image over it thanks to the div solution
- the last polygon of the second line with a CSS changing color into orange but failing to add an image on top of the orange (and even with fill:none !?)
.imageOnTop {
fill:orange;
stroke:black;
background-image: url('https://www.gravatar.com/avatar/4bd89893bb55d58180443e45eca3163e?s=64&d=identicon&r=PG&f=1');
background-size: contain;
z-index: -1;
position: relative;
}
<div id="main">
<div id="mapBody">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="300" height="150">
<polygon points="0,90 15,81 30,90 30,108 15,117 0,108" fill="rgb(150,183,243)" stroke="rgb(150,183,243)" stroke-width="1"></polygon>
<polygon points="30,90 45,81 60,90 60,108 45,117 30,108" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="60,90 75,81 90,90 90,108 75,117 60,108" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="90,90 105,81 120,90 120,108 105,117 90,108" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="120,90 135,81 150,90 150,108 135,117 120,108" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="150,90 165,81 180,90 180,108 165,117 150,108" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="180,90 195,81 210,90 210,108 195,117 180,108" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="210,90 225,81 240,90 240,108 225,117 210,108" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="15,63 30,54 45,63 45,81 30,90 15,81" fill="rgb(150,183,243)" stroke="rgb(150,183,243)" stroke-width="1"></polygon>
<polygon points="45,63 60,54 75,63 75,81 60,90 45,81" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="75,63 90,54 105,63 105,81 90,90 75,81" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="105,63 120,54 135,63 135,81 120,90 105,81" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="135,63 150,54 165,63 165,81 150,90 135,81" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="165,63 180,54 195,63 195,81 180,90 165,81" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="195,63 210,54 225,63 225,81 210,90 195,81" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="225,63 240,54 255,63 255,81 240,90 225,81" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="0,36 15,27 30,36 30,54 15,63 0,54" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="30,36 45,27 60,36 60,54 45,63 30,54" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="60,36 75,27 90,36 90,54 75,63 60,54" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="90,36 105,27 120,36 120,54 105,63 90,54" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="120,36 135,27 150,36 150,54 135,63 120,54" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="150,36 165,27 180,36 180,54 165,63 150,54" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="180,36 195,27 210,36 210,54 195,63 180,54" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<!-- LOOK AT THIS ONE --> <polygon points="210,36 225,27 240,36 240,54 225,63 210,54" fill="black" stroke="rgb(83,126,162)" stroke-width="1" class="imageOnTop"></polygon> <!-- LOOK AT THIS ONE -->
<polygon points="15,9 30,0 45,9 45,27 30,36 15,27" fill="rgb(150,183,243)" stroke="rgb(150,183,243)" stroke-width="1"></polygon>
<polygon points="45,9 60,0 75,9 75,27 60,36 45,27" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="75,9 90,0 105,9 105,27 90,36 75,27" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="105,9 120,0 135,9 135,27 120,36 105,27" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="135,9 150,0 165,9 165,27 150,36 135,27" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="165,9 180,0 195,9 195,27 180,36 165,27" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="195,9 210,0 225,9 225,27 210,36 195,27" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="225,9 240,0 255,9 255,27 240,36 225,27" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
</svg>
</div>
</div>
<div><img src="https://www.gravatar.com/avatar/4bd89893bb55d58180443e45eca3163e?s=64&d=identicon&r=PG&f=1" style="width: 20px; position: absolute; top: 15px; left: 25px;" /></div>
Below is an update of the question with a solution proposed to do this via <defs><pattern ...><image ...>
Thanks @Jing-Xian!!
Here is the solution for the image to appear via CSS and not to repeat: (width has to match polygon width)
.imageOnTop {
fill:orange;
fill:url(#img1);
stroke:black;
}
<div id="main">
<div id="mapBody">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="300" height="150">
<defs>
<pattern id="img1" patternUnits="userSpaceOnUse" width="34px" height="34px">
<image href="https://i.ibb.co/0XGwpPz/resource-bison.png" x="4px" y="-5px" width="34px" height="34px" />
</pattern>
</defs>
<polygon points="0,90 15,81 30,90 30,108 15,117 0,108" fill="rgb(150,183,243)" stroke="rgb(150,183,243)" stroke-width="1"></polygon>
<polygon points="30,90 45,81 60,90 60,108 45,117 30,108" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="60,90 75,81 90,90 90,108 75,117 60,108" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="90,90 105,81 120,90 120,108 105,117 90,108" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="120,90 135,81 150,90 150,108 135,117 120,108" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="150,90 165,81 180,90 180,108 165,117 150,108" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="180,90 195,81 210,90 210,108 195,117 180,108" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="210,90 225,81 240,90 240,108 225,117 210,108" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="15,63 30,54 45,63 45,81 30,90 15,81" fill="rgb(150,183,243)" stroke="rgb(150,183,243)" stroke-width="1"></polygon>
<polygon points="45,63 60,54 75,63 75,81 60,90 45,81" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="75,63 90,54 105,63 105,81 90,90 75,81" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="105,63 120,54 135,63 135,81 120,90 105,81" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="135,63 150,54 165,63 165,81 150,90 135,81" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="165,63 180,54 195,63 195,81 180,90 165,81" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="195,63 210,54 225,63 225,81 210,90 195,81" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="225,63 240,54 255,63 255,81 240,90 225,81" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="0,36 15,27 30,36 30,54 15,63 0,54" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="30,36 45,27 60,36 60,54 45,63 30,54" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="60,36 75,27 90,36 90,54 75,63 60,54" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="90,36 105,27 120,36 120,54 105,63 90,54" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="120,36 135,27 150,36 150,54 135,63 120,54" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="150,36 165,27 180,36 180,54 165,63 150,54" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="180,36 195,27 210,36 210,54 195,63 180,54" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<!-- LOOK AT THIS ONE --> <polygon points="210,36 225,27 240,36 240,54 225,63 210,54" fill="orange" stroke="rgb(83,126,162)" stroke-width="1" class="imageOnTop"></polygon> <!-- LOOK AT THIS ONE -->
<polygon points="15,9 30,0 45,9 45,27 30,36 15,27" fill="rgb(150,183,243)" stroke="rgb(150,183,243)" stroke-width="1"></polygon>
<polygon points="45,9 60,0 75,9 75,27 60,36 45,27" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="75,9 90,0 105,9 105,27 90,36 75,27" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="105,9 120,0 135,9 135,27 120,36 105,27" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="135,9 150,0 165,9 165,27 150,36 135,27" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="165,9 180,0 195,9 195,27 180,36 165,27" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="195,9 210,0 225,9 225,27 210,36 195,27" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="225,9 240,0 255,9 255,27 240,36 225,27" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
</svg>
</div>
</div>
<div><img src="https://i.ibb.co/0XGwpPz/resource-bison.png" style="width: 30px; position: absolute; top: 11px; left: 22px;" /></div>
But one problem remains: if the image is displayed this way, the background color of the polygon disappears (despite the PNG being transparent).
Proposed by MXDVL: add a rect into the pattern.
But that rect appears above the image, not under. Plus I have 30 different colors for the hexes and 39 different images to add as overlay. That would mean more than 1000 pattern to be defined. :/
.imageOnTop {
fill:orange;
fill:url(#img1);
stroke:black;
}
<div id="main">
<div id="mapBody">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="300" height="150">
<defs>
<pattern id="img1" patternUnits="userSpaceOnUse" width="34px" height="34px">
<image href="https://i.ibb.co/0XGwpPz/resource-bison.png" x="4px" y="-5px" width="34px" height="34px" />
<rect width="20" height="20" fill="orange">
</pattern>
</defs>
<polygon points="0,90 15,81 30,90 30,108 15,117 0,108" fill="rgb(150,183,243)" stroke="rgb(150,183,243)" stroke-width="1"></polygon>
<polygon points="30,90 45,81 60,90 60,108 45,117 30,108" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="60,90 75,81 90,90 90,108 75,117 60,108" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="90,90 105,81 120,90 120,108 105,117 90,108" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="120,90 135,81 150,90 150,108 135,117 120,108" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="150,90 165,81 180,90 180,108 165,117 150,108" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="180,90 195,81 210,90 210,108 195,117 180,108" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="210,90 225,81 240,90 240,108 225,117 210,108" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="15,63 30,54 45,63 45,81 30,90 15,81" fill="rgb(150,183,243)" stroke="rgb(150,183,243)" stroke-width="1"></polygon>
<polygon points="45,63 60,54 75,63 75,81 60,90 45,81" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="75,63 90,54 105,63 105,81 90,90 75,81" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="105,63 120,54 135,63 135,81 120,90 105,81" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="135,63 150,54 165,63 165,81 150,90 135,81" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="165,63 180,54 195,63 195,81 180,90 165,81" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="195,63 210,54 225,63 225,81 210,90 195,81" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="225,63 240,54 255,63 255,81 240,90 225,81" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="0,36 15,27 30,36 30,54 15,63 0,54" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="30,36 45,27 60,36 60,54 45,63 30,54" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="60,36 75,27 90,36 90,54 75,63 60,54" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="90,36 105,27 120,36 120,54 105,63 90,54" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="120,36 135,27 150,36 150,54 135,63 120,54" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="150,36 165,27 180,36 180,54 165,63 150,54" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="180,36 195,27 210,36 210,54 195,63 180,54" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<!-- LOOK AT THIS ONE --> <polygon points="210,36 225,27 240,36 240,54 225,63 210,54" fill="orange" stroke="rgb(83,126,162)" stroke-width="1" class="imageOnTop"></polygon> <!-- LOOK AT THIS ONE -->
<polygon points="15,9 30,0 45,9 45,27 30,36 15,27" fill="rgb(150,183,243)" stroke="rgb(150,183,243)" stroke-width="1"></polygon>
<polygon points="45,9 60,0 75,9 75,27 60,36 45,27" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="75,9 90,0 105,9 105,27 90,36 75,27" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="105,9 120,0 135,9 135,27 120,36 105,27" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="135,9 150,0 165,9 165,27 150,36 135,27" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="165,9 180,0 195,9 195,27 180,36 165,27" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="195,9 210,0 225,9 225,27 210,36 195,27" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
<polygon points="225,9 240,0 255,9 255,27 240,36 225,27" fill="rgb(83,126,162)" stroke="rgb(83,126,162)" stroke-width="1"></polygon>
</svg>
</div>
</div>
<div><img src="https://i.ibb.co/0XGwpPz/resource-bison.png" style="width: 30px; position: absolute; top: 11px; left: 22px;" /></div>