Hover is not working on navbar items and the cursor is changing to hand when we hover beside text and not when we hover on text

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

Hover is not working on navbar items and the cursor is changing to hand when we hover beside text and not when we hover on text

问题

代码中的悬停效果在我的代码中不起作用。有人可以帮忙吗?当我运行这段代码时,导航栏存在,但不能点击,而它左侧的空白区域可以点击,也没有我的CSS悬停效果起作用。

* {
    padding: 0;
    margin: 0;
    box-sizing: border-box;
    scroll-behavior: smooth;
    font-family: 'Poppins', sans-serif;
    list-style: none;
    text-decoration: none;
}

:root {
    /* 全局变量 */
    --main-color: #ff702a;
    --text-color: #fff;
    --background-color: #1e1c2a;
    --big-font: 5rem;
    --h2-font: 2.25rem;
    --p-font: 0.9rem;
}

*::selection {
    background: var(--main-color);
    color: #fff;
}

body {
    color: var(--text-color);
    background: var(--background-color);
}

header {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 30px 170px;
    background: var(--background-color);
}

.logo {
    color: var(--main-color);
    font-weight: 600;
    font-size: 2.4rem;
}

.navbar {
    display: flex;
}

.navbar li a {
    color: var(--text-color);
    font-size: 1.1rem;
    padding: 10px 20px;
    font-weight: 500;
}

.navbar li a:hover {
    color: var(--main-color);
    transition: .4s;
}

在上述代码中,.navbar li a:hover 部分是用于实现悬停效果的CSS代码。当你悬停在导航链接上时,它会将文本颜色更改为 var(--main-color),并带有0.4秒的过渡效果。如果这个效果没有生效,你可能需要检查其他可能影响悬停效果的CSS规则或JavaScript代码。

英文:

The hover effect is not working in my code. Can someone help?When I run this code there is navbar present but is not clickable whereas the empty space on it's left side is clickable nor are my css hover effect working on it.

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

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

* {
padding: 0;
margin: 0;
box-sizing: border-box;
scroll-behavior: smooth;
font-family: &#39;Poppins&#39;, sans-serif;
list-style: none;
text-decoration: none;
}
:root {
/* global variables */
--main-color: #ff702a;
--text-color: #fff;
--background-color: #1e1c2a;
--big-font: 5rem;
--h2-font: 2.25rem;
--p-font: 0.9rem;
}
*::selection {
background: var(--main-color);
color: #fff;
}
body {
color: var(--text-color);
background: var(--background-color);
}
header {
position: fixed;
top: 0;
left: 0;
width: 100%;
z-index: 1000;
/*z-index defines stack order of element*/
display: flex;
align-items: center;
/*controls space around cross axis*/
justify-content: space-between;
/*controls space around main axis*/
padding: 30px 170px;
background: var(--background-color);
}
.logo {
color: var(--main-color);
font-weight: 600;
font-size: 2.4rem;
}
.navbar {
display: flex;
}
.navbar li a {
color: var(--text-color);
font-size: 1.1rem;
padding: 10px 20px;
font-weight: 500;
}
.navbar li a:hover {
color: var(--main-color);
transition: .4s;
}

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

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset=&#39;utf-8&#39;&gt;
&lt;meta http-equiv=&#39;X-UA-Compatible&#39; content=&#39;IE=edge&#39;&gt;
&lt;title&gt;Website for Foodies!&lt;/title&gt;
&lt;meta name=&#39;viewport&#39; content=&#39;width=device-width, initial-scale=1&#39;&gt;
&lt;link rel=&#39;stylesheet&#39; type=&#39;text/css&#39; media=&#39;screen&#39; href=&#39;main.css&#39;&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/boxicons@latest/css/boxicons.min.css&quot;&gt;
&lt;link rel=&quot;preconnect&quot; href=&quot;https://fonts.googleapis.com&quot;&gt;
&lt;link rel=&quot;preconnect&quot; href=&quot;https://fonts.gstatic.com&quot; crossorigin&gt;
&lt;link
href=&quot;https://fonts.googleapis.com/css2?family=Nunito+Sans:wght@300;800&amp;family=Poppins:wght@100;200;300;400;500;600;700;800;900&amp;display=swap&quot;
rel=&quot;stylesheet&quot;&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;header&gt;
&lt;a href=&quot;#&quot; class=&quot;logo&quot;&gt;Foods&lt;/a&gt;
&lt;div class=&quot;bx bx-menu&quot; id=&quot;menu-icon&quot;&gt;&lt;/div&gt;
&lt;!--class=&quot; bx bx-menu&quot; is responsible for icon from boxicon--&gt;
&lt;ul class=&quot;navbar&quot;&gt;
&lt;li&gt;&lt;a href=&quot;#Home&quot;&gt;&lt;/a&gt;Home&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#About&quot;&gt;&lt;/a&gt;About&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#Menu&quot;&gt;&lt;/a&gt;Menu&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#Service&quot;&gt;&lt;/a&gt;Service&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#Contact&quot;&gt;&lt;/a&gt;Contact&lt;/li&gt;
&lt;/ul&gt;
&lt;/header&gt;
&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

I was trying to make a responsive website and I was expecting the text in my navbar to change color when i hover on it

答案1

得分: 1

根据您的代码:

.navbar li a:hover {
    color: var(--main-color);
    transition: .4s;
}

您试图在锚点标签上创建悬停效果,类似于 class navbar &gt; li &gt; a

现在看看您的 HTML 代码:

&lt;ul class=&quot;navbar&quot;&gt;
    &lt;li&gt;&lt;a href=&quot;#Home&quot;&gt;&lt;/a&gt;Home&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;#About&quot;&gt;&lt;/a&gt;About&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;#Menu&quot;&gt;&lt;/a&gt;Menu&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;#Service&quot;&gt;&lt;/a&gt;Service&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;#Contact&quot;&gt;&lt;/a&gt;Contact&lt;/li&gt;
&lt;/ul&gt;
&lt;a href=&quot;#Home&quot;&gt;&lt;/a&gt;

在锚点标签内没有内容显示。希望您理解这个问题。将所有菜单文本 Home about menu service contact 放入锚点标签中,就像这样:

&lt;a href=&quot;#Home&quot;&gt;Home&lt;/a&gt;
英文:

According to your code :

.navbar li a:hover {
    color: var(--main-color);
    transition: .4s;
}

you tried to give hover effect on anchor tag as class navbar &gt; li &gt; a.

Look at this your html code now:

&lt;ul class=&quot;navbar&quot;&gt;
            &lt;li&gt;&lt;a href=&quot;#Home&quot;&gt;&lt;/a&gt;Home&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;#About&quot;&gt;&lt;/a&gt;About&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;#Menu&quot;&gt;&lt;/a&gt;Menu&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;#Service&quot;&gt;&lt;/a&gt;Service&lt;/li&gt;
            &lt;li&gt;&lt;a href=&quot;#Contact&quot;&gt;&lt;/a&gt;Contact&lt;/li&gt;
        &lt;/ul&gt;
&lt;a href=&quot;#Home&quot;&gt;&lt;/a&gt;

There is nothing to show inside anchor tag. Hope you understand this problem. Put all the menu texts Home about menu service contact inside anchor tag. like this:<br>

&lt;a href=&quot;#Home&quot;&gt;Home&lt;/a&gt;

huangapple
  • 本文由 发表于 2023年6月2日 00:12:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76383838.html
匿名

发表评论

匿名网友

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

确定