英文:
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.
答案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 -->
<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>
<!-- 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论