I was wondering if this approach is okay. In the past, I used to create an empty div with ::before and ::after pseudo-classes to build the hamburger icon, but I think using a button is better for accessibility. Do you see any issues with my method?
There are instances where using <defs> and <use> SVG elements can be useful, particularly when removing repetition from your code when SVG is in-lined in the HTML.
The first question to ask is: Do I need to in-line the SVG content?
It may be preferable to reference SVG from an external file via img and src, or as CSS background, in order to reduce code clutter and maintain separation of concerns. But if you plan on using CSS to manipulate the SVG in an interactive way, such as hover or click effects, the SVG needs to be in-line.
The next consideration for <defs> and <use> is repetition. If an SVG image is used multiple times in a page, such as a icon which reoccurres, you can reduce code repetition by defining the geometry only once (in <defs>), then use multiple times in a less verbose way with <use>.
The <defs> and <use> can also be utilised within an SVG image, for example a āhamburgerā icon may have three identical horizontal bars. You could define the bar shape once, then use three times, altering only the position.
In your use case, I donāt see CSS being applied to the SVG in a way that would require in-lining. Also I donāt the SVG geometry being reused.
Though on that second point it could be argued that <defs> and <use> does aid separation of concerns by removing the untidy bulk of geometry definition from the main content mark-up and putting it to one side, while only the more minimal <use> appears among the content.
So in summary, this can be a useful method to trim down your code, but it needs to be considered on a case by case basis to asess whether it is the best method to achieve what you need.
In your case, I would probably just use external SVG files, unless I intended to add things like CSS hover effects to the icons.
Then just style it with css and not have any extra html or links to images or svgs,
Or:
Obviously the svg may look a little nicer but with a little more effort the css version could be embellished a bit and has a transition from one state to the other baked in also.
Yes thatās fine if your button has no logical content such as Menu (as in my second example) or Open/close menu etcā¦
I would actually prefer to have text in there that says toggle menu or changes from open menu to close menu or something similar and then thereās no need for aria labels even if you hide the text off screen.
In some ways you can consider aria labels as a failure of your html in that you have to explain to someone what your html is doing.
for the text to be centered and pushed a bit to the right, to have a little space between the hamburger icon and the text. Instead of the <b> for the text, is it also possible to use just an empty span element?
Yes that is perfectly clear what the button is for without aria labels etc. A button element means that an action will take place when clicked and the text tells you that a menu will result from that action.
Yes a span is perfectly fine and probably the correct element to use there.
I used the b element because its less characters and these days basically means that the text is brought to the readers attention.
Sometimes semantics can be argued back and forth so you make a choice based on what you think best as its not always so clear cut.
I was wondering if itās possible to add 5px padding on each side of the hamburger icon, making that padding clickable as well? This would help in cases where users might not click precisely on the hamburger icon. In your first example of codepen.
Thank you! How do you edit these svgs? Can they be imported into Inkscape? I was wondering if itās possible to make the lines just a bit longer, about 3-4px.
I do not have Inkscape but it seems it can import .svg files.
I have now changed the SVGās viewbox in the Codepen above so it is now 12 units wide by 10 units high. The coordinates of the lines have been changed accordingly. In the CSS I have increased the width accordingly so the button is now rectangular.
EDIT: I do not understand why the SVG is not central vertically within the button. Anyway we can adjust the position by adjusting padding,
Iām not a fan of nesting as it its just something else that can go wrong. You canāt look at a long css file and see where the nested rule applies unless you track all the way back up the stylesheet to see where the nesting started. It also encourages longer selectors than could be achieved with a single class.
However Iām in the minority as others love it
In the end these types of questions are down to what suits your way of working the best.