Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
146 views
in Technique[技术] by (71.8m points)

html - SVG Doesn't show up in button element

I'm using inline SVG ( as JSX ) on my website, I'm trying to display a hamburger menu button but it isn't showing up. Here is my code:

import Link from "next/link"

function Nav() {
    return (
        <nav>
            <button > /// SVG is here
                <svg xmlns="http://www.w3.org/2000/svg" viewBox="-172 -12 210 50" stroke="#000000">
                    <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M4 6h16M4 12h16m-7 6h7" />
                </svg>
            </button>
            <ul className="">
                <li>
                        <Link href="/">
                                <a>Main</a>
                        </Link>
                </li>
                <li>
                        <Link href="/resume">
                                <a>Resume</a>
                        </Link>
                </li>
                <li>
                        <Link href="/projects">
                                <a>Projects</a>
                        </Link>
                </li>

                <li>
                        <Link href="/blog">
                                <a>Blog</a>
                        </Link>
                </li>
                <li>
                        <Link href="/contact">
                                <a>Get in touch</a>
                        </Link>
                </li>
            </ul>
        </nav>
    );
}

export default Nav;

The Icon is only visible when it's outside the button tag, how do I view it while preserving the viewBox attribute?

question from:https://stackoverflow.com/questions/65926794/svg-doesnt-show-up-in-button-element

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In your case you need to adjust the viewBox for your svg element, that way when you set your svg's height and width, your svg scales properly and you get the desired result. Here's your code which I tweaked:

<button> 
  <svg xmlns="http://www.w3.org/2000/svg" viewBox="2 1 20 20" stroke="#000000" height="50px" width="50px">
       <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M4 6h16M4 12h16m-7 6h7" />
     </svg>
</button>

See it work on codepen here.

You can learn about how viewBox works at CSS tricks and MDN.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...