简体   繁体   English

SVG 不显示在按钮元素中

[英]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.我在我的网站上使用内联 SVG (作为 JSX ),我试图显示一个汉堡菜单按钮,但它没有出现。 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?图标仅在按钮标签之外时可见,如何在保留 viewBox 属性的同时查看它?

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.在您的情况下,您需要调整 svg 元素的viewBox ,这样当您设置 svg 的heightwidth时,您的 svg 会正确缩放并获得所需的结果。 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 .这里查看它在 codepen 上的工作。

You can learn about how viewBox works at CSS tricks and MDN .您可以在CSS 技巧MDN了解viewBox的工作原理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM