英文:
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: 'Poppins', 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 -->
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<title>Website for Foodies!</title>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<link rel='stylesheet' type='text/css' media='screen' href='main.css'>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/boxicons@latest/css/boxicons.min.css">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link
href="https://fonts.googleapis.com/css2?family=Nunito+Sans:wght@300;800&family=Poppins:wght@100;200;300;400;500;600;700;800;900&display=swap"
rel="stylesheet">
</head>
<body>
<header>
<a href="#" class="logo">Foods</a>
<div class="bx bx-menu" id="menu-icon"></div>
<!--class=" bx bx-menu" is responsible for icon from boxicon-->
<ul class="navbar">
<li><a href="#Home"></a>Home</li>
<li><a href="#About"></a>About</li>
<li><a href="#Menu"></a>Menu</li>
<li><a href="#Service"></a>Service</li>
<li><a href="#Contact"></a>Contact</li>
</ul>
</header>
</body>
</html>
<!-- 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 > li > a
。
现在看看您的 HTML 代码:
<ul class="navbar">
<li><a href="#Home"></a>Home</li>
<li><a href="#About"></a>About</li>
<li><a href="#Menu"></a>Menu</li>
<li><a href="#Service"></a>Service</li>
<li><a href="#Contact"></a>Contact</li>
</ul>
<a href="#Home"></a>
在锚点标签内没有内容显示。希望您理解这个问题。将所有菜单文本 Home
about
menu
service
contact
放入锚点标签中,就像这样:
<a href="#Home">Home</a>
英文:
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 > li > a
.
Look at this your html code now:
<ul class="navbar">
<li><a href="#Home"></a>Home</li>
<li><a href="#About"></a>About</li>
<li><a href="#Menu"></a>Menu</li>
<li><a href="#Service"></a>Service</li>
<li><a href="#Contact"></a>Contact</li>
</ul>
<a href="#Home"></a>
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>
<a href="#Home">Home</a>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论