Custom figures inside a button - html

I'm trying to make a simple board game, where I have 2 dices, which is just a paragraph with borders and a coloured circle inside. You roll both dices at the same time, so I wanted to have them inside a button, so that when you click on the button it calls the RollTheDice function (I'm using Blazor Server Side).
This is what I have so far, which is actually working (ignoring the fact that everything gets a grey background because of the button):
<button #onclick="RollTheDice" style="margin: 0 auto; display: block">
<span class="d-flex">
<p style="border: 2px solid gray; border-radius: 10px; height: 100px; width: 100px; margin: 10px">
<svg height="90" width="90" style="margin: 5px">
<circle cx="45" cy="45" r="40" stroke="black" stroke-width="2" fill="#dices[0]" />
</svg>
</p>
<p style="border: 2px solid gray; border-radius: 10px; height: 100px; width: 100px; margin: 10px">
<svg height="90" width="90" style="margin: 5px">
<circle cx="45" cy="45" r="40" stroke="black" stroke-width="2" fill="#dices[1]" />
</svg>
</p>
</span>
</button>
However, it appears that putting paragraphs (<p>) inside a button is bad practice (as mentioned in this post).
So how do you make a button with some custom stuff inside, without violatin all these rules (it's not only <p> that isn't allowed)?
Note: I have also tried making it a <div> instead of a <button>, but then if you click the div "button" too fast repeatedly, you start marking other elements (like when you double click somewhere in a text).
Update: I have also tried making each dice an individual button, but then I get this weird glitch with a double when I click the dice:

Related

How do I use responsive svgs in this context?

.contained {
width: 80%;
max-width: 2500px;
margin: 0 auto;
}
#media all and (max-width: 780px) {
.icon-text-size {
display: inline
}
}
.svg-align {
vertical-align: middle;
}
.icon-fix {
display: none;
}
<div class="contained">
<h2 class="title"><span>Contact</span> Us</h2>
<h1 class="icon-text-size">
<svg class="svg-align" width="144" height="144" viewBox="0 0 144 144" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M91.4849 96.6599L101.565 86.5799C102.923 85.2391 104.64 84.3213 106.509 83.9379C108.379 83.5544 110.319 83.7219 112.095 84.4199L124.38 89.3249C126.174 90.0534 127.713 91.2968 128.802 92.8984C129.892 94.5 130.482 96.3882 130.5 98.3249V120.825C130.489 122.142 130.212 123.444 129.686 124.652C129.159 125.86 128.393 126.948 127.435 127.852C126.476 128.756 125.344 129.456 124.108 129.912C122.871 130.367 121.556 130.567 120.24 130.5C34.1549 125.145 16.7849 52.2449 13.4999 24.3449C13.3474 22.9749 13.4867 21.588 13.9087 20.2756C14.3307 18.9633 15.0257 17.7551 15.9482 16.7307C16.8706 15.7062 17.9995 14.8887 19.2606 14.3318C20.5217 13.775 21.8864 13.4915 23.2649 13.4999H44.9999C46.9394 13.5057 48.8329 14.0915 50.4367 15.1822C52.0406 16.2728 53.2815 17.8183 53.9999 19.6199L58.9049 31.9049C59.626 33.6737 59.81 35.6158 59.4338 37.4886C59.0577 39.3614 58.1381 41.0818 56.7899 42.4349L46.7099 52.5149C46.7099 52.5149 52.5149 91.7999 91.4849 96.6599Z" fill="#F62AA0"/>
</svg>
<br class="icon-fix"> 01328 633037</h1>
<h1 class="icon-text-size">
<svg class="svg-align" width="144" height="125" viewBox="0 0 144 125" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M129.32 20.8333C128.775 20.7846 128.225 20.7846 127.68 20.8333H15.6801C14.9622 20.8429 14.2492 20.9364 13.5601 21.1111L71.3601 71.0764L129.32 20.8333Z" fill="#F62AA0"/>
<path d="M135.24 22.4606L77.0002 72.8078C75.5013 74.1012 73.4737 74.8272 71.3602 74.8272C69.2467 74.8272 67.2191 74.1012 65.7202 72.8078L8.00022 22.8425C7.82277 23.4086 7.72869 23.992 7.72021 24.5786V94.0231C7.72021 95.8649 8.56307 97.6312 10.0634 98.9335C11.5637 100.236 13.5985 100.968 15.7202 100.968H127.72C129.842 100.968 131.877 100.236 133.377 98.9335C134.877 97.6312 135.72 95.8649 135.72 94.0231V24.5786C135.688 23.8552 135.526 23.1406 135.24 22.4606ZM21.2002 94.0231H15.6402V89.0578L44.7202 64.0231L50.3602 68.9189L21.2002 94.0231ZM127.64 94.0231H122.04L92.8802 68.9189L98.5202 64.0231L127.6 89.0578L127.64 94.0231Z" fill="#F62AA0"/>
</svg>
<br class="icon-fix"> info#verusamor.salon
</h1>
</div>
I want the svg and the text on the same line on larger screens and for the svg to get smaller as the screen gets smaller in order to keep it on one line, when the svg becomes too small I want the order to change from
svg text
new line
svg text
and change it to
svg
new line
text
new line
svg
new line text.
Thanks for any help.
set svg width & height in percentages and also use max-width properties
<svg width="15%" max-width="70%" height=""></svg>
<svg width="25%" max-width="75%" height=""></svg>

Cropped SVG with Clip-path changes Height on zooming in and out [duplicate]

I wanted a cylindrical container containing liquid and this liquid changes color and its amount in that container, So I used SVG for this purpose (SVG is used for liquid in a cylindrical container).
Here is the source code along with SVG
function changeCol(col) {
document.querySelector('path').style.setProperty('fill', col, '!important');
document.querySelector('ellipse').style.setProperty('fill', col, '!important');
//Its not working I dont know why.
}
function changeHeight(vol) {
//Some Code to change its height.
}
.container {
width: fit-content;
border: solid red;
}
.liquid {
width: 200px;
}
.liquid svg * {
fill: red !important;
}
<div class="container">
<div class="liquid">
<svg id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 216.66 325.25">
<title>liquid_RBt</title>
<path d="M216.66,28V297.25c0,15.47-48.5,28-108.33,28S0,312.72,0,297.25V28C0,43.46,48.5,56,108.33,56S216.66,43.46,216.66,28Z" style="fill:#fff;stroke:#fff;stroke-miterlimit:10;opacity:0.7000000000000001"/>
<ellipse cx="108.33" cy="28" rx="108.33" ry="28" style="fill:#fff;stroke:#fff;stroke-miterlimit:10;opacity:0.8"/>
</svg>
</div>
</div>
<div class="controller">
<div class="color-change">
<button class="col-btn" onclick="changeCol('red');">Red</button>
<button class="col-btn" onclick="changeCol('blue');">Blue</button>
<button class="col-btn" onclick="changeCol('green');">green</button>
</div>
<div class="change-amount">
<input type="number" id="amountInp" onchange='changeHeight(this.value)' placeholder="(in ml)">
</div>
</div>
I've clipped the path with a clipPath that I then move about to hide the parts of the polygon that need to disappear.
I don't know the volume of the cylinder either so you might want to scale the number.
I also fixed the colouring.
function changeCol(col) {
document.querySelector('path').style.setProperty('fill', col);
document.querySelector('ellipse').style.setProperty('fill', col);
}
function changeHeight(vol) {
// not sure how much 100ml is supposed to fill up
document.querySelector('ellipse').cy.baseVal.value = 300 - vol;
document.querySelector('rect').y.baseVal.value = 300-vol;
}
.container {
width: fit-content;
border: solid red;
}
.liquid {
width: 200px;
}
<div class="container">
<div class="liquid">
<svg id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 216.66 325.25">
<title>liquid_RBt</title>
<defs>
<clipPath id="cp">
<rect x="0" y="0" width="230" height="400"/>
</clipPath>
</defs>
<path d="M216.66,28V297.25c0,15.47-48.5,28-108.33,28S0,312.72,0,297.25V28C0,43.46,48.5,56,108.33,56S216.66,43.46,216.66,28Z" style="fill:red;stroke:#fff;stroke-miterlimit:10;opacity:0.7000000000000001;clip-path: url(#cp)"/>
<ellipse cx="108.33" cy="28" rx="108.33" ry="28" style="fill:red;stroke:#fff;stroke-miterlimit:10"/>
</svg>
</div>
</div>
<div class="controller">
<div class="color-change">
<button class="col-btn" onclick="changeCol('red');">Red</button>
<button class="col-btn" onclick="changeCol('blue');">Blue</button>
<button class="col-btn" onclick="changeCol('green');">green</button>
</div>
<div class="change-amount">
<input type="number" id="amountInp" onchange='changeHeight(this.value)' placeholder="(in ml)">
</div>
</div>
Set the preserve aspect ratio to none.
<svg preserveAspectRatio="none" /* here are your other attributes */>

Use SVG image on website with rounded corners - showing white for rounded area

I am creating a very simple React page with an SVG image on it. That SVG image has round corners. I can open it up in GIMP and see the rounded corners shown.
Now I add it to my website like this:
import cardBackR from '../img/cards/2B.svg'
...
<img className='card' src={cardBackR} />
And then when I view the page with the card over a background image, the rounded corners show as white:
Here is the full SVG code of that image.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg xmlns="http://www.w3.org/2000/svg" class="card" face="2B" height="3.5in" preserveAspectRatio="none" viewBox="-120 -168 240 336" width="2.5in">
<defs>
<pattern id="B2" width="6" height="6" patternUnits="userSpaceOnUse">
<path d="M3 0L6 3L3 6L0 3Z" fill="red"></path>
</pattern>
</defs>
<rect width="239" height="335" x="-119.5" y="-167.5" rx="12" ry="12" fill="white" stroke="black"></rect>
<rect fill="url(#B2)" width="216" height="312" x="-108" y="-156" rx="12" ry="12"></rect>
</svg>
I've tested this in both Firefox and Chrome. Any ideas what is causing the rounded corners to fail?
SVG in an IMG src should work fine, provided you escape the # with %23
<style>
body{
background:green;
}
</style>
<img src="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg'
class='card' face='2B' height='3.5in' preserveAspectRatio='none' viewBox='-120 -168 240 336' width='2.5in'>
<defs><pattern id='B2' width='6' height='6' patternUnits='userSpaceOnUse'>
<path d='M3 0L6 3L3 6L0 3Z' fill='red'></path></pattern></defs>
<rect width='239' height='335' x='-119.5' y='-167.5' rx='12' ry='12' fill='white' stroke='black'></rect>
<rect fill='url(%23B2)' width='216' height='312' x='-108' y='-156' rx='12' ry='12'></rect>
</svg>">
Figured out the issue. I unfortunately picked a poor name for my class. That is a special class in Bootstrap and that was adding the following CSS:
.card {
position: relative;
display: flex;
flex-direction: column;
min-width: 0;
word-wrap: break-word;
background-color: #fff;
background-clip: border-box;
border: 1px solid rgba(0, 0, 0, 0.125);
border-radius: 0.25rem;
}

How can i make Internet Explorer understand my SVG css

Im in the process of generating a map through html and css, however i seem to have an issue when it comes to Internet Explorer as the map is not shown at all i just get a blank page.
the following code is the html part of my map.
<div class="svg">
<svg class="box" width="65%" height="auto" viewbox="0 0 800 800"
xmlns="http://www.w3.org/2000/svg">
<a href="https://www.google.dk"><polygon class="haderslev"
points="519,5, 538,46, 556,65, 576,61, 591,39, 609,43, 615,60, 622,73,
635,77, 661,70, 730,32, 744,49, 741,69, 731,90, 742,104, 781,128, 787,142,
784,154, 772,179, 769,197, 750,207, 683,212, 667,236, 660,276, 658,281,
626,270, 607,254, 563,247, 520,233, 513,244, 509,265, 371,364, 352,252,
456,227, 437,218, 426,221, 409,242, 389,238, 384,229, 391,216, 400,201,
393,185, 373,170, 349,169, 318,182, 315,197, 302,202, 297,196, 294,171,
290,157, 295,124, 279,99, 265,60, 271,56, 296,63, 311,69, 316,58, 319,49,
351,52, 381,70, 398,50, 406,60, 409,94, 433,98, 451,89, 448,65, 452,50,
463,46, 479,42, 488,12, 507,2" />
<foreignobject x="490" y="125" width="140px" height="30px"
class="haderslevnode">
<div class="htext">Haderslev</div>
</foreignobject></a>
</svg>
</div>
And this is the css to make it work. which functions perfectly on Chrome and Firefox respectively.
.svg{
height: 100vmin;
}
.haderslev{
fill:#7c7c7c;
}
.haderslev:hover{
fill:#eab616;
}
.htext{
font-family:"Helvetica Neue",Helvetica,Arial,sans-serif,MagistralBold;
font-weight:bold;
font-size:24px;
color:#ffffff;
}
any advice is apreciated as i would like this to be as accessible as posible.
*Edit
Internet explorer 10 and 11 are the versions i would like this to funktion in
This is working for me in IE:
I've removed the foreign object and I'm using a text element instead. The class .htext has a fill instead of a color.
I've removed width="65%" height="auto" attributes of the SVG element and I've added this in css:
.svg{
height: 100vmin;
width: 100vmin;
}
svg{border:1px black}
.svg{
height: 100vmin;
width: 100vmin;
border:1px solid;
}
.haderslev{
fill:#7c7c7c;
}
.haderslev:hover{
fill:#eab616;
}
.htext{
font-family:"Helvetica Neue",Helvetica,Arial,sans-serif,MagistralBold;
font-weight:bold;
font-size:30px;
fill:#ffffff;
}
<div class="svg">
<svg class="box" viewbox="0 0 800 800"
xmlns="http://www.w3.org/2000/svg">
<a href="https://www.google.dk"><polygon class="haderslev"
points="519,5, 538,46, 556,65, 576,61, 591,39, 609,43, 615,60, 622,73,
635,77, 661,70, 730,32, 744,49, 741,69, 731,90, 742,104, 781,128, 787,142,
784,154, 772,179, 769,197, 750,207, 683,212, 667,236, 660,276, 658,281,
626,270, 607,254, 563,247, 520,233, 513,244, 509,265, 371,364, 352,252,
456,227, 437,218, 426,221, 409,242, 389,238, 384,229, 391,216, 400,201,
393,185, 373,170, 349,169, 318,182, 315,197, 302,202, 297,196, 294,171,
290,157, 295,124, 279,99, 265,60, 271,56, 296,63, 311,69, 316,58, 319,49,
351,52, 381,70, 398,50, 406,60, 409,94, 433,98, 451,89, 448,65, 452,50,
463,46, 479,42, 488,12, 507,2" />
<text class="htext" x="490" y="150">Haderslev</text>
</a>
</svg>
</div>
I hope it helps.

How to create a clickable grid of triangles using html, svg?

I have already created a grid of triangles like so:
svg {
margin-left: 0px;
margin-right: -60px;
padding: 0;
}
<div data-bind="foreach: Grid">
<div data-bind="foreach: $data.rowData">
<!-- ko if: $data.owner() === 0 && ($data.pos[0] + $data.pos[1])%2 === 0-->
<svg height="103.92" width="120">
<polygon class="" points="60,0 0,103.92 120,103.92" style="fill:grey;" data-bind="click: $root.test.bind($data, $data)" />
</svg>
<!-- /ko -->
<!-- ko if: $data.owner() === 0 && ($data.pos[0] + $data.pos[1])%2 === 1-->
<svg height="103.92" width="120">
<polygon class="" points="0,0 120,0 60,103.92" style="fill:grey;" data-bind="click: $root.test.bind($data, $data)" />
</svg>
<!-- /ko -->
</div>
</div>
My problem is that only the left half of the triangles is clickable. I think this is due to the (still rectangular) shape of the svg-element. But I have no idea how to fix this. Is there any way to make every triangle clickable in its whole area?
At the moment, all your individual SVGs are overlapping one another and any click that misses a triangle will be swallowed by the parent <svg> element.
The cleanest solution would be to put all your polygons in one big SVG. However there is another way around your problem using the pointer-events property.
Set pointer-events="none" on your <svg> elements so that clicks will pass through them. But you'll also need to set an explicit pointer-events="fill" on your polygons, since otherwise they'll inherit the "none" from their parent SVGs.
var output = document.getElementById("output");
document.getElementById("red").addEventListener("click", function(e) {
output.textContent = "red";
});
document.getElementById("green").addEventListener("click", function(e) {
output.textContent = "green";
});
svg {
position: absolute;
top: 0;
left: 0;
pointer-events: none;
}
polygon {
pointer-events: fill;
}
#output {
margin-top: 120px;
}
<svg width="100px" height="100px">
<polygon points="0,0,100,0,100,100" fill="red" id="red"/>
</svg>
<svg width="100px" height="100px">
<polygon points="0,0,100,100,0,100" fill="green" id="green"/>
</svg>
<div id="output"></div>
You should use one svg tag with both polygons inside it. This way the Square svg elements won't overlap each other:
polygon {
fill: grey;
}
polygon:hover {
fill: #000;
}
<svg height="103.92" width="185">
<polygon points="60,0 0,103.92 120,103.92" />
<polygon points="65,0 185,0 125,103.92" />
</svg>