Check out my "hamburger button" made in SVG, shown below.
body {
margin: 0;
text-align: center;
}
svg#ham-btn {
margin: 2rem;
border: 1px solid black;
fill: #383838;
}
<svg id="ham-btn" width="107.5px" height="80px" viewBox="0 0 80 80" preserveAspectRatio="xMidYMid meet" xmlns="http://www.w3.org/2000/svg">
<style>
#ham-btn {
cursor: pointer;
}
#ham-btn:hover #r1 {
outline: 1px solid transparent;
transition: transform 0.2s;
transform-origin: 50% 50%;
transform: rotate(37deg) translate(0%, 28.75%) scaleX(1.1);
}
#ham-btn:hover #r2 {
transition: transform 0.2s;
transform: translate(120%, 0);
}
#ham-btn:hover #r3 {
outline: 1px solid transparent;
transition: transform 0.2s;
transform-origin: 50% 50%;
transform: rotate(-37deg) translate(0%, -28.75%) scaleX(1.1);
}
</style>
<rect id="r3" x="0" y="71.25%" width="100%" height="15%" rx="5%" />
<rect id="r2" x="0" y="42.5%" width="100%" height="15%" rx="5%"/>
<rect id="r1" x="0" y="13.75%" width="100%" height="15%" rx="5%" />
</svg>
I have a question:
Currently, if I want to resize the button, I need to adjust both the dimensions of the viewport and viewBox
manually by the same amount of pixels.
Is there a way to make this more adaptable? Perhaps by setting viewBox
as a percentage of the viewport?
Based on the specification, it appears that viewBox
can only be a numeric value and cannot be expressed as a percentage.
Any ideas or suggestions are welcome!
Thank you for your input.
Note:
Accessibility and additional features have not been implemented in this button yet. The focus is solely on making it responsive.
Additional Information:
I am aware that I can set the SVG viewport size relative to its container or the browser window.
However, this approach does not resolve my issue since maintaining a fixed ratio between the viewBox and viewport dimensions is crucial for the button's shape retention.
Therefore, my goal was to define the viewBox
as a percentage of the viewport.
In case you are curious, here are some key aspects of my current solution:
- The
viewBox
is wider than the viewport but shares the same height - To horizontally center the
rect
s, thepreserveAspectRatio
property is utilized - Dimensions of the
rect
s are defined in relation to theviewBox
An outline
has been added to address jagged lines observed in Firefox after applying transformations.
Edit:
If future visitors find it helpful, the finalized version of the button, including comprehensive accessibility features, can be accessed through this link: https://codepen.io/magnusriga/pen/KrrJKa