SVG 在自定义导航栏中完全不可见。

huangapple go评论58阅读模式
英文:

svg is completely invisible in custom navigation bar

问题

I'm here to assist with the translation. Here's the translated content:

我试图复制Razer的网站我下载了他们的razer图标作为svg
这是我的当前网站的样子SVG应该位于蓝色圈圈的区域内如何显示它是背景颜色覆盖了SVG吗我还将SVG显示在导航栏下方它正常工作所以问题不是出在SVG上

If you have any more code or specific questions related to this, feel free to ask.

英文:

I'm trying to replicate Razer's website and I downloaded their razer icon as an svg.

import "./NavigationBar.css";
import { ReactComponent as Logo } from "./razer-ths-logo.svg";

const NavigationBar = () => {
    return (
        <div>
            <ul className="nav">
                <li>
                    <Logo />
                </li>
                <li>
                    <a href="a">Store</a>
                </li>
                <li>
                    <a href="a">PC</a>
                </li>
                <li>
                    <a href="a">Console</a>
                </li>
                <li>
                    <a href="a">Mobile</a>
                </li>
                <li>
                    <a href="a">Lifestyle</a>
                </li>
                <li>
                    <a href="a">Services</a>
                </li>
                <li>
                    <a href="a">Community</a>
                </li>
                <li>
                    <a href="a">Support</a>
                </li>
            </ul>
            <Logo />
        </div>
    );
}

export default NavigationBar;
.nav {
    background-color: black;
    width: 100%;
    list-style: none;
    margin-top: 0px;
    float: left;
    padding-left: 0px;
    padding-right: 0px;
    text-align: center;
}

.nav li {
    display: inline-block;
    margin-left: 40px;
    margin-right: 40px;
    margin-top: 20px;
    margin-bottom: 20px;
}

.nav a {
    font-size: 16px;
    color: gray;
    cursor: pointer;
    text-decoration: none;
}

.nav a:hover {
    color: white;
}

This is what my current website looks like. The SVG should be within the circled blue area. How do I display it? Is the background color covering up the SVG? I've also displayed the SVG below my navigation bar and it works, so the issue is not with the SVG.
SVG 在自定义导航栏中完全不可见。

答案1

得分: 0

这似乎是因为您没有为SVG图像指定尺寸。当li具有display: inline-block时,SVG图像会以0x0的大小呈现(根据开发者工具)。否则,图像将以其完整的内在尺寸呈现。

在这个示例中,我给图像设置了width: 1em

.nav img {
  width: 1em;
}
.nav {
    background-color: black;
    width: 100%;
    list-style: none;
    margin-top: 0px;
    float: left;
    padding-left: 0px;
    padding-right: 0px;
    text-align: center;
}

.nav li {
    display: inline-block;
    margin-left: 40px;
    margin-right: 40px;
    margin-top: 20px;
    margin-bottom: 20px;
}

.nav a {
    font-size: 16px;
    color: gray;
    cursor: pointer;
    text-decoration: none;
}

.nav a:hover {
    color: white;
}

.nav img {
  width: 1em;
}
<ul class="nav">
  <li>
      <img src="https://assets2.razerzone.com/images/phoenix/razer-ths-logo.svg">
  </li>
  <li>
      <a href="#">Store</a>
  </li>
  <li>
      <a href="#">PC</a>
  </li>
  <li>
      <a href="#">Console</a>
  </li>
  <li>
      <a href="#">Mobile</a>
  </li>
  <li>
      <a href="#">Lifestyle</a>
  </li>
  <li>
      <a href="#">Services</a>
  </li>
  <li>
      <a href="#">Community</a>
  </li>
  <li>
      <a href="#">Support</a>
  </li>
</ul>
英文:

This seems to be because you did not give dimensions for the SVG image. When li has display: inline-block, the SVG image is rendered at 0x0 (according to Developer Tools.) Otherwise, the image is rendered at its full intrinsic size.

For this example, I gave the image width: 1em

.nav img {
width: 1em;
}

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.nav {
background-color: black;
width: 100%;
list-style: none;
margin-top: 0px;
float: left;
padding-left: 0px;
padding-right: 0px;
text-align: center;
}
.nav li {
display: inline-block;
margin-left: 40px;
margin-right: 40px;
margin-top: 20px;
margin-bottom: 20px;
}
.nav a {
font-size: 16px;
color: gray;
cursor: pointer;
text-decoration: none;
}
.nav a:hover {
color: white;
}
.nav img {
width: 1em;
}

<!-- language: lang-html -->

&lt;ul class=&quot;nav&quot;&gt;
&lt;li&gt;
&lt;img src=&quot;https://assets2.razerzone.com/images/phoenix/razer-ths-logo.svg&quot;&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&quot;#&quot;&gt;Store&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&quot;#&quot;&gt;PC&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&quot;#&quot;&gt;Console&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&quot;#&quot;&gt;Mobile&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&quot;#&quot;&gt;Lifestyle&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&quot;#&quot;&gt;Services&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&quot;#&quot;&gt;Community&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&quot;#&quot;&gt;Support&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

<!-- end snippet -->

答案2

得分: 0

A) SVG 图像是否已渲染:您可以使用“检查元素”来查看是否已经渲染。如果没有渲染,请确保使用正确的引用和 URL 链接来导入它。

B) SVG 已经渲染 - 然后,修改 svg 文件内部的填充颜色和边框颜色,您可以使用任何 SVG 文件 - 甚至在记事本中打开。

C) 如果仍然存在与背景的冲突问题,使用 CSS 属性 - 为 SVG 图像提供白色边框,一旦确认图像已经渲染,您将能够解决问题。

英文:

To solve your issue there could be a couple of things:

A) The SVG Images is rendered or not: You could use 'inspect element' to see if it is either rendered or not. If it is not rendered, kindly use correct reference and URL link to import so that it is imported.

B) The SVG is rendered - then, the change the inside fill-color and color-of-border in svg file, you could open any SVG file -- in even notepad.

C) If still there is issue with collision with background, use CSS properties - to provide a white border to SVG Image, you'll be able to fix issue, once you confirm the image has been rendered.

答案3

得分: 0

我通过添加以下内容来解决了这个问题:

import logo from './razer-ths-logo.svg';

至顶部。
然后我设置了 src={logo}
我不确定为什么这是解决方法,但至少它有效。我只是从文件中复制了这些内容,就在你运行 npm-create 之后。

英文:

I fixed this issue by adding the following

import logo from './razer-ths-logo.svg';

to the top.
I then set src={logo}
I'm not sure why this is the fix, but at least it works. I just copied this from the files right after you do npm-create.

huangapple
  • 本文由 发表于 2023年7月18日 07:27:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76708676.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定