I am working with a svg element as follows and I am using css to control the font-size of the svg text.
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1280 720">
<rect class="vBoxRect" width="1280" height="720" fill="none" stroke="red"></rect>
<rect class="boundRect" x="70" y="70" width="1160" height="600" fill="none" stroke="green"></rect>
<g class="bound" style="transform: translate(70px, 70px);">
<g class="yAxis">
<g class="yAxisLeft" fill="none" font-size="10" font-family="sans-serif" text-anchor="end">
<g class="tick" opacity="1" transform="translate(0,411.7647058823529)">
<line stroke="currentColor" x2="1160"></line>
<text fill="currentColor" x="-3" dy="0.32em" transform="matrix(1 0 0 1 13.1462 0)" text-anchor="middle">20%</text>
</g>
<g class="tick" opacity="1" transform="translate(0,223.52941176470583)">
<line stroke="currentColor" x2="1160"></line>
<text fill="currentColor" x="-3" dy="0.32em" transform="matrix(1 0 0 1 13.1462 0)" text-anchor="middle">40%</text>
</g>
<g class="tick" opacity="1" transform="translate(0,35.29411764705883)">
<line stroke="currentColor" x2="1160"></line>
<text fill="currentColor" x="-3" dy="0.32em" text-anchor="middle" transform="matrix(1 0 0 1 12.3462 0)">60%</text>
</g>
</g>
</g>
</g>
</svg>
However, when I increase the font-size, e.g.
.tick>text{
font-size: xx-large;
}
it changes to this
Is there anyway I can have the font-size increased, yet the text would be aligned to the green line? like below
As an alternative, I tried following
.tick>text{
transform-origin: left;
transform-box: fill-box;
transform: scaleY(2.5);
}
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1280 720">
<rect class="vBoxRect" width="1280" height="720" fill="none" stroke="red"></rect>
<rect class="boundRect" x="70" y="70" width="1160" height="600" fill="none" stroke="green"></rect>
<g class="bound" style="transform: translate(70px, 70px);">
<g class="yAxis">
<g class="yAxisLeft" fill="none" font-size="10" font-family="sans-serif" text-anchor="end">
<g class="tick" opacity="1" transform="translate(0,411.7647058823529)">
<line stroke="currentColor" x2="1160"></line>
<text fill="currentColor" x="-3" dy="0.32em" transform="matrix(1 0 0 1 13.1462 0)" text-anchor="middle">20%</text>
</g>
<g class="tick" opacity="1" transform="translate(0,223.52941176470583)">
<line stroke="currentColor" x2="1160"></line>
<text fill="currentColor" x="-3" dy="0.32em" transform="matrix(1 0 0 1 13.1462 0)" text-anchor="middle">40%</text>
</g>
<g class="tick" opacity="1" transform="translate(0,35.29411764705883)">
<line stroke="currentColor" x2="1160"></line>
<text fill="currentColor" x="-3" dy="0.32em" text-anchor="middle" transform="matrix(1 0 0 1 12.3462 0)">60%</text>
</g>
</g>
</g>
</g>
</svg>
.tick>text{
transform-origin: left;
transform-box: fill-box;
transform: scaleY(2.5);
}
but it is not applying the scaling from the desired origin.
Try positioning your test at x="0" instead of x="-3".
Also remove the transform and test-anchor="middle/end" attributes.
.tick>text{
transform-origin: left;
transform-box: fill-box;
transform: scaleY(2.5);
}
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1280 720">
<rect class="vBoxRect" width="1280" height="720" fill="none" stroke="red"></rect>
<rect class="boundRect" x="70" y="70" width="1160" height="600" fill="none" stroke="green"></rect>
<g class="bound" style="transform: translate(70px, 70px);">
<g class="yAxis">
<g class="yAxisLeft" fill="none" font-size="10" font-family="sans-serif">
<g class="tick" opacity="1" transform="translate(0,411.7647058823529)">
<line stroke="currentColor" x2="1160"></line>
<text fill="currentColor" x="0" dy="0.32em">20%</text>
</g>
<g class="tick" opacity="1" transform="translate(0,223.52941176470583)">
<line stroke="currentColor" x2="1160"></line>
<text fill="currentColor" x="0" dy="0.32em">40%</text>
</g>
<g class="tick" opacity="1" transform="translate(0,35.29411764705883)">
<line stroke="currentColor" x2="1160"></line>
<text fill="currentColor" x="0" dy="0.32em">60%</text>
</g>
</g>
</g>
</g>
</svg>
I have a bunch of SVG shapes(Hexagones) that render perfectly
on desktop screen. However when the screen is reduced to tablet and mobile
some of the hexagones do not render properly.
I have tried with media query but still not responsive.
BTW this snippet html is made manually so it has a bunch of duplicate code
with different x,y positions for hexagone, so do not be scared a lot of html codes.
I would appreciate any help.
body {
background-color: #333;
font-family: 'Luckiest Guy', cursive;
}
#hexGrid {
width: 80vw;
height: 80vh;
}
#media (max-width : 600px) {
#hexGrid {
width:84vw;
height: 100vh;
}
}
<svg id="hexGrid">
<svg viewBox="0 0 200 200">
<defs>
<g id="pod">
<polygon stroke="#ffffff" stroke-width=".5" points="7.43,-12.41 -8.11,-12.39 -15.87,1.07 -8.09,14.52 7.45,14.51 15.21,1.05" />
</g>
</defs>
</svg>
<!--ODD COLUMNS-->
<svg viewBox="0 0 200 200">
<g >
<use xlink:href="#pod" transform="translate(50, 94.5)" fill="transparent" />
<text transform="translate(50, 94.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">A1</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g >
<use xlink:href="#pod" transform="translate(50, 121.5)" fill="transparent" />
<text transform="translate(50, 121.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">A2</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g >
<use xlink:href="#pod" transform="translate(50, 148.5)" fill="transparent" />
<text transform="translate(50, 148.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">A3</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g >
<use xlink:href="#pod" transform="translate(97, 94.5)" fill="transparent" />
<text transform="translate(97, 94.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">C1</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g >
<use xlink:href="#pod" transform="translate(97, 121.5)" fill="transparent" />
<text transform="translate(97, 121.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">C2</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g>
<use xlink:href="#pod" transform="translate(97, 148.5)" fill="transparent" />
<text transform="translate(97, 148.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">C3</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g>
<use xlink:href="#pod" transform="translate(144, 94.5)" fill="transparent" />
<text transform="translate(144, 94.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">E1</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g >
<use xlink:href="#pod" transform="translate(144, 121.5)" fill="transparent" />
<text transform="translate(144, 121.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">E2</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g >
<use xlink:href="#pod" transform="translate(144, 148.5)" fill="transparent" />
<text transform="translate(144, 148.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">E3</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g>
<use xlink:href="#pod" transform="translate(191, 94.5)" fill="transparent" />
<text transform="translate(191, 94.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">G1</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g>
<use xlink:href="#pod" transform="translate(191, 121.5)" fill="transparent" />
<text transform="translate(191, 121.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">G2</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g>
<use xlink:href="#pod" transform="translate(191, 148.5)" fill="transparent" />
<text transform="translate(191, 148.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">G3</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g>
<use xlink:href="#pod" transform="translate(238, 94.5)" fill="transparent" />
<text transform="translate(238, 94.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">I1</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g>
<use xlink:href="#pod" transform="translate(238, 121.5)" fill="transparent" />
<text transform="translate(238, 121.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">I2</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g>
<use xlink:href="#pod" transform="translate(238, 148.5)" fill="transparent" />
<text transform="translate(238, 148.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">I3</text>
</g>
</svg>
<!--EVEN COLUMNS-->
<svg viewBox="0 0 200 200">
<g>
<use xlink:href="#pod" transform="translate(73.52, 54.5)" fill="transparent" />
<text transform="translate(73.52, 54.54)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">B1</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g>
<use xlink:href="#pod" transform="translate(73.52, 81.5)" fill="transparent" />
<text transform="translate(73.52, 81.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">B2</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g>
<use xlink:href="#pod" transform="translate(73.52, 108.5)" fill="transparent" />
<text transform="translate(73.52, 108.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">B3</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g>
<use xlink:href="#pod" transform="translate(73.52, 135.5)" fill="transparent" />
<text transform="translate(73.52, 135.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">B4</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g>
<use xlink:href="#pod" transform="translate(120.5, 54.5)" fill="transparent" />
<text transform="translate(120.5, 54.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">D1</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g>
<use xlink:href="#pod" transform="translate(120.5, 81.5)" fill="transparent" />
<text transform="translate(120.5, 81.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">D2</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g>
<use xlink:href="#pod" transform="translate(120.5, 108.5)" fill="transparent" />
<text transform="translate(120.5, 108.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">D3</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g>
<use xlink:href="#pod" transform="translate(120.5, 135.5)" fill="transparent" />
<text transform="translate(120.5, 135.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">D4</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g>
<use xlink:href="#pod" transform="translate(167.5, 54.5)" fill="transparent" />
<text transform="translate(167.5, 54.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">F1</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g>
<use xlink:href="#pod" transform="translate(167.5, 81.5)" fill="transparent" />
<text transform="translate(167.5, 81.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">F2</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g>
<use xlink:href="#pod" transform="translate(167.5, 108.5)" fill="transparent" />
<text transform="translate(167.5, 108.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">F3</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g>
<use xlink:href="#pod" transform="translate(167.5, 135.5)" fill="transparent" />
<text transform="translate(167.5, 135.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">F4</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g>
<use xlink:href="#pod" transform="translate(214.5, 54.5)" fill="transparent" />
<text transform="translate(214.5, 54.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">H1</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g>
<use xlink:href="#pod" transform="translate(214.5, 81.5)" fill="transparent" />
<text transform="translate(214.5, 81.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">H2</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g>
<use xlink:href="#pod" transform="translate(214.5, 108.5)" fill="transparent" />
<text transform="translate(214.5, 108.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">H3</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g>
<use xlink:href="#pod" transform="translate(214.5, 135.5)" fill="transparent" />
<text transform="translate(214.5, 135.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">H4</text>
</g>
</svg>
</svg>
I think the issue is to maintain the correct aspect ratio of the view box of the outer, parent SVG regardless of the window aspect ratio.
body {
background-color: #333;
font-family: 'Luckiest Guy', cursive;
}
#hexGrid {
width: 100%;
height: 100%;
}
#media (max-width : 600px) {
#hexGrid {
width: 100%;
height: 100%;
}
}
<svg id="hexGrid" viewBox="0 0 2000 1000">
<svg viewBox="0 0 200 200">
<defs>
<g id="pod">
<polygon stroke="#ffffff" stroke-width=".5" points="7.43,-12.41 -8.11,-12.39 -15.87,1.07 -8.09,14.52 7.45,14.51 15.21,1.05" />
</g>
</defs>
</svg>
<!--ODD COLUMNS-->
<svg viewBox="0 0 200 200">
<g >
<use xlink:href="#pod" transform="translate(50, 94.5)" fill="transparent" />
<text transform="translate(50, 94.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">A1</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g >
<use xlink:href="#pod" transform="translate(50, 121.5)" fill="transparent" />
<text transform="translate(50, 121.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">A2</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g >
<use xlink:href="#pod" transform="translate(50, 148.5)" fill="transparent" />
<text transform="translate(50, 148.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">A3</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g >
<use xlink:href="#pod" transform="translate(97, 94.5)" fill="transparent" />
<text transform="translate(97, 94.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">C1</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g >
<use xlink:href="#pod" transform="translate(97, 121.5)" fill="transparent" />
<text transform="translate(97, 121.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">C2</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g>
<use xlink:href="#pod" transform="translate(97, 148.5)" fill="transparent" />
<text transform="translate(97, 148.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">C3</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g>
<use xlink:href="#pod" transform="translate(144, 94.5)" fill="transparent" />
<text transform="translate(144, 94.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">E1</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g >
<use xlink:href="#pod" transform="translate(144, 121.5)" fill="transparent" />
<text transform="translate(144, 121.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">E2</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g >
<use xlink:href="#pod" transform="translate(144, 148.5)" fill="transparent" />
<text transform="translate(144, 148.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">E3</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g>
<use xlink:href="#pod" transform="translate(191, 94.5)" fill="transparent" />
<text transform="translate(191, 94.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">G1</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g>
<use xlink:href="#pod" transform="translate(191, 121.5)" fill="transparent" />
<text transform="translate(191, 121.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">G2</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g>
<use xlink:href="#pod" transform="translate(191, 148.5)" fill="transparent" />
<text transform="translate(191, 148.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">G3</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g>
<use xlink:href="#pod" transform="translate(238, 94.5)" fill="transparent" />
<text transform="translate(238, 94.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">I1</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g>
<use xlink:href="#pod" transform="translate(238, 121.5)" fill="transparent" />
<text transform="translate(238, 121.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">I2</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g>
<use xlink:href="#pod" transform="translate(238, 148.5)" fill="transparent" />
<text transform="translate(238, 148.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">I3</text>
</g>
</svg>
<!--EVEN COLUMNS-->
<svg viewBox="0 0 200 200">
<g>
<use xlink:href="#pod" transform="translate(73.52, 54.5)" fill="transparent" />
<text transform="translate(73.52, 54.54)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">B1</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g>
<use xlink:href="#pod" transform="translate(73.52, 81.5)" fill="transparent" />
<text transform="translate(73.52, 81.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">B2</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g>
<use xlink:href="#pod" transform="translate(73.52, 108.5)" fill="transparent" />
<text transform="translate(73.52, 108.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">B3</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g>
<use xlink:href="#pod" transform="translate(73.52, 135.5)" fill="transparent" />
<text transform="translate(73.52, 135.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">B4</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g>
<use xlink:href="#pod" transform="translate(120.5, 54.5)" fill="transparent" />
<text transform="translate(120.5, 54.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">D1</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g>
<use xlink:href="#pod" transform="translate(120.5, 81.5)" fill="transparent" />
<text transform="translate(120.5, 81.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">D2</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g>
<use xlink:href="#pod" transform="translate(120.5, 108.5)" fill="transparent" />
<text transform="translate(120.5, 108.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">D3</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g>
<use xlink:href="#pod" transform="translate(120.5, 135.5)" fill="transparent" />
<text transform="translate(120.5, 135.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">D4</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g>
<use xlink:href="#pod" transform="translate(167.5, 54.5)" fill="transparent" />
<text transform="translate(167.5, 54.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">F1</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g>
<use xlink:href="#pod" transform="translate(167.5, 81.5)" fill="transparent" />
<text transform="translate(167.5, 81.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">F2</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g>
<use xlink:href="#pod" transform="translate(167.5, 108.5)" fill="transparent" />
<text transform="translate(167.5, 108.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">F3</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g>
<use xlink:href="#pod" transform="translate(167.5, 135.5)" fill="transparent" />
<text transform="translate(167.5, 135.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">F4</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g>
<use xlink:href="#pod" transform="translate(214.5, 54.5)" fill="transparent" />
<text transform="translate(214.5, 54.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">H1</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g>
<use xlink:href="#pod" transform="translate(214.5, 81.5)" fill="transparent" />
<text transform="translate(214.5, 81.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">H2</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g>
<use xlink:href="#pod" transform="translate(214.5, 108.5)" fill="transparent" />
<text transform="translate(214.5, 108.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">H3</text>
</g>
</svg>
<svg viewBox="0 0 200 200">
<g>
<use xlink:href="#pod" transform="translate(214.5, 135.5)" fill="transparent" />
<text transform="translate(214.5, 135.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">H4</text>
</g>
</svg>
</svg>
First, i'm not sure it's a good practice to use nested SVG... (Edit: After some research it's seems not a bad practice...)
And your issue is related to viewbox of SVG. You can try with following viewbox: <svg viewBox="0 0 290 200">
And you can safely remove nested SVG
#hexGrid {
width: 80vw;
height: 80vh;
}
body {
background-color: #666;
font-family: 'Luckiest Guy', cursive;
}
<svg id="hexGrid" viewBox="0 0 290 200">
<defs>
<g id="pod">
<polygon stroke="#ffffff" stroke-width=".5" points="7.43,-12.41 -8.11,-12.39 -15.87,1.07 -8.09,14.52 7.45,14.51 15.21,1.05" />
</g>
</defs>
<!--ODD COLUMNS-->
<g >
<use xlink:href="#pod" transform="translate(50, 94.5)" fill="transparent" />
<text transform="translate(50, 94.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">A1</text>
</g>
<g >
<use xlink:href="#pod" transform="translate(50, 121.5)" fill="transparent" />
<text transform="translate(50, 121.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">A2</text>
</g>
<g >
<use xlink:href="#pod" transform="translate(50, 148.5)" fill="transparent" />
<text transform="translate(50, 148.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">A3</text>
</g>
<g >
<use xlink:href="#pod" transform="translate(97, 94.5)" fill="transparent" />
<text transform="translate(97, 94.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">C1</text>
</g>
<g >
<use xlink:href="#pod" transform="translate(97, 121.5)" fill="transparent" />
<text transform="translate(97, 121.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">C2</text>
</g>
<g>
<use xlink:href="#pod" transform="translate(97, 148.5)" fill="transparent" />
<text transform="translate(97, 148.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">C3</text>
</g>
<g>
<use xlink:href="#pod" transform="translate(144, 94.5)" fill="transparent" />
<text transform="translate(144, 94.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">E1</text>
</g>
<g >
<use xlink:href="#pod" transform="translate(144, 121.5)" fill="transparent" />
<text transform="translate(144, 121.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">E2</text>
</g>
<g >
<use xlink:href="#pod" transform="translate(144, 148.5)" fill="transparent" />
<text transform="translate(144, 148.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">E3</text>
</g>
<g>
<use xlink:href="#pod" transform="translate(191, 94.5)" fill="transparent" />
<text transform="translate(191, 94.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">G1</text>
</g>
<g>
<use xlink:href="#pod" transform="translate(191, 121.5)" fill="transparent" />
<text transform="translate(191, 121.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">G2</text>
</g>
<g>
<use xlink:href="#pod" transform="translate(191, 148.5)" fill="transparent" />
<text transform="translate(191, 148.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">G3</text>
</g>
<g>
<use xlink:href="#pod" transform="translate(238, 94.5)" fill="transparent" />
<text transform="translate(238, 94.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">I1</text>
</g>
<g>
<use xlink:href="#pod" transform="translate(238, 121.5)" fill="transparent" />
<text transform="translate(238, 121.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">I2</text>
</g>
<g>
<use xlink:href="#pod" transform="translate(238, 148.5)" fill="transparent" />
<text transform="translate(238, 148.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">I3</text>
</g>
<!--EVEN COLUMNS-->
<g>
<use xlink:href="#pod" transform="translate(73.52, 54.5)" fill="transparent" />
<text transform="translate(73.52, 54.54)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">B1</text>
</g>
<g>
<use xlink:href="#pod" transform="translate(73.52, 81.5)" fill="transparent" />
<text transform="translate(73.52, 81.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">B2</text>
</g>
<g>
<use xlink:href="#pod" transform="translate(73.52, 108.5)" fill="transparent" />
<text transform="translate(73.52, 108.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">B3</text>
</g>
<g>
<use xlink:href="#pod" transform="translate(73.52, 135.5)" fill="transparent" />
<text transform="translate(73.52, 135.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">B4</text>
</g>
<g>
<use xlink:href="#pod" transform="translate(120.5, 54.5)" fill="transparent" />
<text transform="translate(120.5, 54.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">D1</text>
</g>
<g>
<use xlink:href="#pod" transform="translate(120.5, 81.5)" fill="transparent" />
<text transform="translate(120.5, 81.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">D2</text>
</g>
<g>
<use xlink:href="#pod" transform="translate(120.5, 108.5)" fill="transparent" />
<text transform="translate(120.5, 108.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">D3</text>
</g>
<g>
<use xlink:href="#pod" transform="translate(120.5, 135.5)" fill="transparent" />
<text transform="translate(120.5, 135.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">D4</text>
</g>
<g>
<use xlink:href="#pod" transform="translate(167.5, 54.5)" fill="transparent" />
<text transform="translate(167.5, 54.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">F1</text>
</g>
<g>
<use xlink:href="#pod" transform="translate(167.5, 81.5)" fill="transparent" />
<text transform="translate(167.5, 81.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">F2</text>
</g>
<g>
<use xlink:href="#pod" transform="translate(167.5, 108.5)" fill="transparent" />
<text transform="translate(167.5, 108.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">F3</text>
</g>
<g>
<use xlink:href="#pod" transform="translate(167.5, 135.5)" fill="transparent" />
<text transform="translate(167.5, 135.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">F4</text>
</g>
<g>
<use xlink:href="#pod" transform="translate(214.5, 54.5)" fill="transparent" />
<text transform="translate(214.5, 54.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">H1</text>
</g>
<g>
<use xlink:href="#pod" transform="translate(214.5, 81.5)" fill="transparent" />
<text transform="translate(214.5, 81.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">H2</text>
</g>
<g>
<use xlink:href="#pod" transform="translate(214.5, 108.5)" fill="transparent" />
<text transform="translate(214.5, 108.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">H3</text>
</g>
<g>
<use xlink:href="#pod" transform="translate(214.5, 135.5)" fill="transparent" />
<text transform="translate(214.5, 135.5)" text-anchor="middle" alignment-baseline="middle"
font-weight="bold" font-size="8" fill="white">H4</text>
</g>
</svg>
I have a bar chart (svg) and need to color bars(rects) with two colors. Is there any way to do it in CSS with odd even?
.chart rect {
fill: steelblue;
}
.chart text {
fill: white;
font: 10px sans-serif;
text-anchor: end;
}
<!DOCTYPE html>
<svg class="chart" width="420" height="120">
<g transform="translate(0,0)">
<rect width="40" height="19"></rect>
<text x="37" y="9.5" dy=".35em">4</text>
</g>
<g transform="translate(0,20)">
<rect width="80" height="19"></rect>
<text x="77" y="9.5" dy=".35em">8</text>
</g>
<g transform="translate(0,40)">
<rect width="150" height="19"></rect>
<text x="147" y="9.5" dy=".35em">15</text>
</g>
<g transform="translate(0,60)">
<rect width="160" height="19"></rect>
<text x="157" y="9.5" dy=".35em">16</text>
</g>
<g transform="translate(0,80)">
<rect width="230" height="19"></rect>
<text x="227" y="9.5" dy=".35em">23</text>
</g>
<g transform="translate(0,100)">
<rect width="420" height="19"></rect>
<text x="417" y="9.5" dy=".35em">42</text>
</g>
</svg>
Any help will be appreciated!
You can use the nth-child selectors like below to apply the styles. Since the rect element is not a direct child of your svg element you cannot apply the nth-child selector directly on it. Instead, we have to select the odd/even g element and then style the rect element inside it accordingly.
.chart g:nth-child(even) rect {
fill: steelblue;
}
.chart g:nth-child(odd) rect {
fill: red;
}
.chart text {
fill: white;
font: 10px sans-serif;
text-anchor: end;
}
<!DOCTYPE html>
<svg class="chart" width="420" height="120">
<g transform="translate(0,0)">
<rect width="40" height="19"></rect>
<text x="37" y="9.5" dy=".35em">4</text>
</g>
<g transform="translate(0,20)">
<rect width="80" height="19"></rect>
<text x="77" y="9.5" dy=".35em">8</text>
</g>
<g transform="translate(0,40)">
<rect width="150" height="19"></rect>
<text x="147" y="9.5" dy=".35em">15</text>
</g>
<g transform="translate(0,60)">
<rect width="160" height="19"></rect>
<text x="157" y="9.5" dy=".35em">16</text>
</g>
<g transform="translate(0,80)">
<rect width="230" height="19"></rect>
<text x="227" y="9.5" dy=".35em">23</text>
</g>
<g transform="translate(0,100)">
<rect width="420" height="19"></rect>
<text x="417" y="9.5" dy=".35em">42</text>
</g>
</svg>