简体   繁体   English

如何使导航栏中的图像可点击?

[英]How do I make an image clickable in a navigation bar?

I've currently created a navigation bar for my website for university assignment.我目前为我的大学作业网站创建了一个导航栏。 I've implemented an image on their which is the universities logo which I'd like to link to the university homepage.我已经在他们的图像上实现了一个图像,这是我想链接到大学主页的大学标志。 However, when I try to use the anchor tag '' to make the image clickable to link to the homepage, it messes up for the style of my navigation bar and would like to know if there's a workaround.但是,当我尝试使用锚标记 '' 使图像可单击以链接到主页时,它会弄乱我的导航栏的样式,并且想知道是否有解决方法。 I know the issue is that the image will take on the styles of the anchor tags I have declared for the navigation bar.我知道问题是图像将采用我为导航栏声明的锚标签的 styles。 I'll include images of before and after creating the link and show the HTML and CSS of that section.我将包含创建链接之前和之后的图像,并显示该部分的 HTML 和 CSS。

这是我更改链接之前的图像链接

This is the navbar before making the image clickable.这是使图像可点击之前的导航栏。

This is the HTML for it:这是它的 HTML:

<div class="top_nav"> 
    <img class="logo" src="images/NTU_badge.png" alt="NTU Badge">
    <a class="active" href="#Home"> Home </a>
    <a href="#Hackathon">Hackathon</a>
    <a href="#Choose_a_Challenge">Choose a Challenge</a>
    <a href="#Digital_Horizons"> Digital Horizons </a>
</div>

This is the CSS for it:这是它的 CSS:

.top_nav {
    overflow: hidden;
    background-color: #2c3e50;
    border-bottom: 20px solid #ed0162;
    position: fixed;
    top: 0;
    width: 100%;
}

.top_nav a {
    float: left;
    color: white;
    text-align: center;
    padding: 30px 20px;
    text-decoration: none;
    font-size: 17px;
    margin-bottom: 0;
    font-family: monospace;

}

.top_nav a:hover {
    background-color: #ed0162;
    color: white;
}


.logo {
    float: left;
    margin: 15px;
}

This is the HTML and webpage after I try to make the image clickable:这是我尝试使图像可点击后的 HTML 和网页:

]破碎的导航栏

There is now big spacing inbetween and the hover style now affects the image when I don't want it too.现在之间有很大的间距,当我不想要它时,hover 样式现在会影响图像。

The HTML code after: HTML 代码后:

<header>

    <div class="top_nav"> 
        <a href="https://www.ntu.ac.uk/">
        <img class="logo" src="images/NTU_badge.png" alt="NTU Badge">
        </a>
        <a class="active" href="#Home"> Home </a>
        <a href="#Hackathon">Hackathon</a>
        <a href="#Choose_a_Challenge">Choose a Challenge</a>
        <a href="#Digital_Horizons"> Digital Horizons </a>


    </div>

</header>

I've tried removing the 'logo' class from the image style but it doesn't really change it that much.我尝试从图像样式中删除“徽标”class,但它并没有真正改变它。

There are lots of ways to do this but flexbox makes it really easy.有很多方法可以做到这一点,但 flexbox 让它变得非常容易。 also will simplify your css.还将简化您的 css。 Just wrap the image in a anchor tag to make it clickable.只需将图像包装在锚标记中以使其可点击。

 .top_nav { display:flex; justify-content:space-around; align-items:center; overflow: hidden; background-color: #2c3e50; font-size:2vw; width: 100%; }.top_nav a { width:10%; color: white; text-decoration: none; font-family: monospace; }.top_nav a:hover { background-color: #ed0162; color: white; } img{ width:100%; } #short{ width:2.5%; }
 <div class="top_nav"> <a id='short' href='https:\\www.google.com'><img class="fa facebook" src="https://www.sustainablewestonma.org/wp-content/uploads/2019/09/facebook-square-brands-blue.png" scale="0"></a> <a class="active" href="#Home"> Home </a> <a href="#Hackathon">Hackathon</a> <a href="#Choose_a_Challenge">Choose a Challenge</a> <a href="#Digital_Horizons"> Digital Horizons </a> </div>

One way to solve this is to use the:first-of-type pseudo css selector.解决此问题的一种方法是使用:first-of-type伪 css 选择器。 Something like that would be the correct way to handle it:这样的事情将是处理它的正确方法:

.top_nav a:first-of-type {padding: 0;}

EDIT编辑

I'm sorry, on the original answer i miss the part to advice you wrapping your image with a element.对不起,在最初的答案中,我错过了建议您用元素包装图像a部分。 So change this:所以改变这个:

<img class="logo" src="images/NTU_badge.png" alt="NTU Badge">

To:至:

<a href="URL" target="_blank">  <img class="logo" src="images/NTU_badge.png" alt="NTU Badge" /></a>

I find that it's generally pretty poor practice to use element names (such as a and div ) in CSS selectors at all, for this exact reason.由于这个确切的原因,我发现在 CSS 选择器中使用元素名称(例如adiv )通常是非常糟糕的做法。

Consider adding something like class="navigation" to each link in your navbar, and then change the .top_nav a selector to .top_nav.navigation .考虑为导航栏中的每个链接添加类似class="navigation"的内容,然后将.top_nav a选择器更改为.top_nav.navigation Then you could add a similar class to the logo <a> .然后,您可以将类似的 class 添加到徽标<a>中。

Not only does this make the CSS more specific, but much more readable when someone else (or you in six months' time) has a look at this without any other context of the rest of the page.这不仅使 CSS 更具体,而且当其他人(或六个月后的您)在没有页面 rest 的任何其他上下文的情况下查看此内容时,更具可读性。

I've now changed my css to:我现在将我的 css 更改为:

.top_nav {
    overflow: hidden;
    background-color: #2c3e50;
    border-bottom: 20px solid #ed0162;
    position: fixed;
    top: 0;
    width: 100%;

}

.top_nav .navigation {
    float: left;
    color: white;
    text-align: center;
    padding: 30px 20px;
    text-decoration: none;
    font-size: 17px;
    margin-bottom: 0;
    font-family: monospace;


}

.top_nav .navigation:hover {
    background-color: #ed0162;
    color: white;
}

.top_nav .navigation:active {
  background-color: #ed0162;
  color: white;

I then added each class to each link in the nav bar like snazzy said, however the active css does not apply anymore when on that page, the 'active' page name should appear pink in the nav bar but it doesn't, any ideas?然后我将每个 class 添加到导航栏中的每个链接,就像时髦所说的那样,但是活动的 css 在该页面上不再适用,“活动”页面名称应该在导航栏中显示为粉红色,但它没有,任何想法?

Removing the margin: 15px;去除margin: 15px; from the.logo css properties seems to help tighten it up.从.logo css 属性似乎有助于收紧它。

You can Nest your image inside and design appropriate css for that link separately.您可以将您的图像嵌套在内部,并为该链接单独设计适当的 css。

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

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