我的一个元素与另一个元素重叠
My one element is overlaping other element
我是网络开发的初学者。
我不知道如何使用 CSS 将我的徽标放在前面。我已经尝试将 z-index 设置为 1000 并将位置设置为 relative,但仍然失败。
我在这里提供了完整的代码,因为我无法弄清楚我哪里出错了!
如何将元素置于最前面?
¯\_(ツ)_/¯
*{
margin: 0;
padding: 0;
}
body{
font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
background: linear-gradient(to right, #9c27b0, #8ecdff);
}
.logo{
display: inline-block;
position: relative;
flex-basis: 20%;
z-index: 1;
}
.logo img{
width: 13vw;
margin-left: 1vw;
margin-top: 1vw;
}
nav{
position: sticky;
}
.navbar{
float: right;
}
.navbar ul{
display: flex;
list-style: none;
}
.navbar ul li{
margin-top: 1.8vw;
margin-right: 3vw;
}
.navbar ul li a{
text-decoration: none;
color: #fff;
}
.info{
z-index: 0;
width: 60vw;
height: 140vh;
position: absolute;
top: 50%;
left: -10%;
transform: translateY(-50%);
}
#circle{
width: 60vw;
height: 140vh;
position: absolute;
top: 50%;
transform: translateY(-50%);
border-radius: 50%;
background-color: #ccc ;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Real Phone</title>
</head>
<body>
<div class="wrapper">
<nav>
<div class="logo">
<!-- I've commented the actual logo -->
<!-- <img src="images/resourses/logo.png" alt=""> -->
<h1>LOGO</h1>
</div>
<div class="navbar">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Phones</a></li>
<li><a href="#">Accessories</a></li>
<li><a href="#">Cart</a></li>
</ul>
</div>
</nav>
<div class="info">
<div id="circle">
<div class="feature" id="f1"></div>
<div class="feature" id="f2"></div>
<div class="feature" id="f3"></div>
<div class="feature" id="f4"></div>
</div>
</div>
</div>
</body>
</html>
试试这个:
<body>
<div class="wrapper">
<div class="info">
<div id="circle">
<div class="feature" id="f1"></div>
<div class="feature" id="f2"></div>
<div class="feature" id="f3"></div>
<div class="feature" id="f4"></div>
</div>
</div>
<nav>
<div class="logo">
<!-- I've commented the actual logo -->
<!-- <img src="images/resourses/logo.png" alt=""> -->
<h1>LOGO</h1>
</div>
<div class="navbar">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Phones</a></li>
<li><a href="#">Accessories</a></li>
<li><a href="#">Cart</a></li>
</ul>
</div>
</nav>
</div>
</body>
基本上,通过将带有信息 class 的 dev 元素放在导航之前,浏览器将优先显示导航而不是信息。
您的解决方案无效,因为您将 z-index
设置在已经位于圆圈后面的导航元素内部。 z-index
仅适用于容器内的所有元素,因此仅适用于 nav
元素内。
要解决此问题,请将 .info
的 z-index
设置在其他所有内容的后面,因此 .info
中的 #circle
也会在其他所有内容的后面。
*{
margin: 0;
padding: 0;
}
body{
font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
background: linear-gradient(to right, #9c27b0, #8ecdff);
}
.logo{
display: inline-block;
position: relative;
flex-basis: 20%;
z-index: 1;
}
.logo img{
width: 13vw;
margin-left: 1vw;
margin-top: 1vw;
}
nav{
position: sticky;
}
.navbar{
float: right;
}
.navbar ul{
display: flex;
list-style: none;
}
.navbar ul li{
margin-top: 1.8vw;
margin-right: 3vw;
}
.navbar ul li a{
text-decoration: none;
color: #fff;
}
.info{
/* set the z-index of the entire info to be behind everything else */
z-index: -1;
width: 60vw;
height: 140vh;
position: absolute;
top: 50%;
left: -10%;
transform: translateY(-50%);
}
#circle{
width: 60vw;
height: 140vh;
position: absolute;
top: 50%;
transform: translateY(-50%);
border-radius: 50%;
background-color: #ccc ;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Real Phone</title>
</head>
<body>
<div class="wrapper">
<nav>
<div class="logo">
<!-- I've commented the actual logo -->
<img src="images/resourses/logo.png" alt="">
<h1>LOGO</h1>
</div>
<div class="navbar">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Phones</a></li>
<li><a href="#">Accessories</a></li>
<li><a href="#">Cart</a></li>
</ul>
</div>
</nav>
<div class="info">
<div id="circle">
<div class="feature" id="f1"></div>
<div class="feature" id="f2"></div>
<div class="feature" id="f3"></div>
<div class="feature" id="f4"></div>
</div>
</div>
</div>
</body>
</html>
我是网络开发的初学者。
我不知道如何使用 CSS 将我的徽标放在前面。我已经尝试将 z-index 设置为 1000 并将位置设置为 relative,但仍然失败。
我在这里提供了完整的代码,因为我无法弄清楚我哪里出错了!
如何将元素置于最前面?
¯\_(ツ)_/¯
*{
margin: 0;
padding: 0;
}
body{
font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
background: linear-gradient(to right, #9c27b0, #8ecdff);
}
.logo{
display: inline-block;
position: relative;
flex-basis: 20%;
z-index: 1;
}
.logo img{
width: 13vw;
margin-left: 1vw;
margin-top: 1vw;
}
nav{
position: sticky;
}
.navbar{
float: right;
}
.navbar ul{
display: flex;
list-style: none;
}
.navbar ul li{
margin-top: 1.8vw;
margin-right: 3vw;
}
.navbar ul li a{
text-decoration: none;
color: #fff;
}
.info{
z-index: 0;
width: 60vw;
height: 140vh;
position: absolute;
top: 50%;
left: -10%;
transform: translateY(-50%);
}
#circle{
width: 60vw;
height: 140vh;
position: absolute;
top: 50%;
transform: translateY(-50%);
border-radius: 50%;
background-color: #ccc ;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Real Phone</title>
</head>
<body>
<div class="wrapper">
<nav>
<div class="logo">
<!-- I've commented the actual logo -->
<!-- <img src="images/resourses/logo.png" alt=""> -->
<h1>LOGO</h1>
</div>
<div class="navbar">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Phones</a></li>
<li><a href="#">Accessories</a></li>
<li><a href="#">Cart</a></li>
</ul>
</div>
</nav>
<div class="info">
<div id="circle">
<div class="feature" id="f1"></div>
<div class="feature" id="f2"></div>
<div class="feature" id="f3"></div>
<div class="feature" id="f4"></div>
</div>
</div>
</div>
</body>
</html>
试试这个:
<body>
<div class="wrapper">
<div class="info">
<div id="circle">
<div class="feature" id="f1"></div>
<div class="feature" id="f2"></div>
<div class="feature" id="f3"></div>
<div class="feature" id="f4"></div>
</div>
</div>
<nav>
<div class="logo">
<!-- I've commented the actual logo -->
<!-- <img src="images/resourses/logo.png" alt=""> -->
<h1>LOGO</h1>
</div>
<div class="navbar">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Phones</a></li>
<li><a href="#">Accessories</a></li>
<li><a href="#">Cart</a></li>
</ul>
</div>
</nav>
</div>
</body>
基本上,通过将带有信息 class 的 dev 元素放在导航之前,浏览器将优先显示导航而不是信息。
您的解决方案无效,因为您将 z-index
设置在已经位于圆圈后面的导航元素内部。 z-index
仅适用于容器内的所有元素,因此仅适用于 nav
元素内。
要解决此问题,请将 .info
的 z-index
设置在其他所有内容的后面,因此 .info
中的 #circle
也会在其他所有内容的后面。
*{
margin: 0;
padding: 0;
}
body{
font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
background: linear-gradient(to right, #9c27b0, #8ecdff);
}
.logo{
display: inline-block;
position: relative;
flex-basis: 20%;
z-index: 1;
}
.logo img{
width: 13vw;
margin-left: 1vw;
margin-top: 1vw;
}
nav{
position: sticky;
}
.navbar{
float: right;
}
.navbar ul{
display: flex;
list-style: none;
}
.navbar ul li{
margin-top: 1.8vw;
margin-right: 3vw;
}
.navbar ul li a{
text-decoration: none;
color: #fff;
}
.info{
/* set the z-index of the entire info to be behind everything else */
z-index: -1;
width: 60vw;
height: 140vh;
position: absolute;
top: 50%;
left: -10%;
transform: translateY(-50%);
}
#circle{
width: 60vw;
height: 140vh;
position: absolute;
top: 50%;
transform: translateY(-50%);
border-radius: 50%;
background-color: #ccc ;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Real Phone</title>
</head>
<body>
<div class="wrapper">
<nav>
<div class="logo">
<!-- I've commented the actual logo -->
<img src="images/resourses/logo.png" alt="">
<h1>LOGO</h1>
</div>
<div class="navbar">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Phones</a></li>
<li><a href="#">Accessories</a></li>
<li><a href="#">Cart</a></li>
</ul>
</div>
</nav>
<div class="info">
<div id="circle">
<div class="feature" id="f1"></div>
<div class="feature" id="f2"></div>
<div class="feature" id="f3"></div>
<div class="feature" id="f4"></div>
</div>
</div>
</div>
</body>
</html>