如何在另一个 div 成为目标时受到影响
How to get another div affected when another one is targeted
/* Header styles */
* {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
}
header {
background-color: aqua;
text-align: center;
padding: 20px 0px;
}
header a {
font-size: xx-large;
margin: 10px;
text-decoration: none;
color: black;
background-color: cyan;
font-weight: bolder;
padding: 0px 15px;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
}
header a:hover {
background-color: darkcyan;
color: white;
}
/* Header Styles End */
/* --- */
/* Sections Style */
#gallery,
#about,
#contact {
display: none;
}
main {
padding: 25px;
background-color: aliceblue;
margin: 10px;
}
#img {
width: 100px;
height: 100px;
}
#gallery:target {
display: block !important;
}
#gallery:target~#home {
display: none !important;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<header>
<a href="#home">Home</a>
<a href="#gallery">Gallery</a>
<a href="#about">About</a>
<a href="#contact">Contact</a>
</header>
<main>
<div id="home">
<h1>
Welcome To the Web Page where there at 4 sections! -
<font size="10" color="grey">Home</font>
</h1>
<h2>Click them to see their relevant sections!</h2>
<p>
Here's a bunch of Lorem to make it seems a bit big. Better know Latin! Here it comes! Lorem ipsum, dolor sit amet consectetur adipisicing elit. Sint blanditiis nulla mollitia quam, sequi consectetur, asperiores itaque minus ut, soluta obcaecati inventore
hic natus dolorem. Repellendus vero optio temporibus esse. A few more. Lorem ipsum dolor sit amet consectetur adipisicing elit. Adipisci dolores amet saepe dignissimos, eligendi non sint nobis! Dignissimos, eligendi non sint nobis! Lorem Ipsum
Dolor sit amet consectetur
</p>
<h2>And that's it, Here's a soothing gif</h2>
<img src="https://i.pinimg.com/originals/4c/ba/93/4cba9388025117cb8c60a4f8610f90f5.gif" width="300">
</div>
<div id="gallery">
<h1>Here's the gallery! Sorry...The images aren't available at the moment</h1>
</div>
<div id="#about"></div>
<div id="#contact"></div>
</main>
</body>
</html>
您好。我有 2 个 ID 为#home 和#gallery 的 DIV。当我单击画廊时,我希望隐藏主页部分并显示画廊。我将 id gallery 设置为 display:none;
,然后使用伪选择器 :target
将其设置为 display:block
...像这样
#gallery {
display: none;
<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre><code>/* Header styles */
* {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
}
header {
background-color: aqua;
text-align: center;
padding: 20px 0px;
}
header a{
font-size: xx-large;
margin: 10px;
text-decoration: none;
color: black;
background-color:cyan;
font-weight: bolder;
padding: 0px 15px;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
}
header a:hover {
background-color: darkcyan;
color: white;
}
/* Header Styles End */
/* --- */
/* Sections Style */
#gallery, #about, #contact {
display: none;
}
main {
padding: 25px;
background-color: aliceblue;
margin: 10px;
}
#img {
width: 100px;
height: 100px;
}
#gallery:target {
display: block !important;
}
#gallery:target ~ #home {
display: none !important;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<header>
<a href="#home">Home</a>
<a href="#gallery">Gallery</a>
<a href="#about">About</a>
<a href="#contact">Contact</a>
</header>
<main>
<div id="home">
<h1>
Welcome To the Web Page where there at 4 sections! -
<font size="10" color="grey">Home</font>
</h1>
<h2>Click them to see their relevant sections!</h2>
<p>
Here's a bunch of Lorem to make it seems a bit big. Better know Latin! Here it comes! Lorem ipsum, dolor sit amet consectetur adipisicing elit. Sint blanditiis nulla mollitia quam, sequi consectetur, asperiores itaque minus ut, soluta obcaecati inventore hic natus dolorem. Repellendus vero optio temporibus esse. A few more. Lorem ipsum dolor sit amet consectetur adipisicing elit. Adipisci dolores amet saepe dignissimos, eligendi non sint nobis! Dignissimos, eligendi non sint nobis! Lorem Ipsum Dolor sit amet consectetur
</p>
<h2>And that's it, Here's a soothing gif</h2>
<img src="https://i.pinimg.com/originals/4c/ba/93/4cba9388025117cb8c60a4f8610f90f5.gif" width="300">
</div>
<div id="gallery">
<h1>Here's the gallery! Sorry...The images aren't available at the moment</h1>
</div>
<div id="#about"></div>
<div id="#contact"></div>
</main>
</body>
</html>
/* Header styles */
* {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
}
header {
background-color: aqua;
text-align: center;
padding: 20px 0px;
}
header a {
font-size: xx-large;
margin: 10px;
text-decoration: none;
color: black;
background-color: cyan;
font-weight: bolder;
padding: 0px 15px;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
}
header a:hover {
background-color: darkcyan;
color: white;
}
/* Header Styles End */
/* --- */
/* Sections Style */
#gallery,
#about,
#contact {
display: none;
}
main {
padding: 25px;
background-color: aliceblue;
margin: 10px;
}
#img {
width: 100px;
height: 100px;
}
#gallery:target {
display: block !important;
}
#gallery:target~#home {
display: none !important;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<header>
<a href="#home">Home</a>
<a href="#gallery">Gallery</a>
<a href="#about">About</a>
<a href="#contact">Contact</a>
</header>
<main>
<div id="home">
<h1>
Welcome To the Web Page where there at 4 sections! -
<font size="10" color="grey">Home</font>
</h1>
<h2>Click them to see their relevant sections!</h2>
<p>
Here's a bunch of Lorem to make it seems a bit big. Better know Latin! Here it comes! Lorem ipsum, dolor sit amet consectetur adipisicing elit. Sint blanditiis nulla mollitia quam, sequi consectetur, asperiores itaque minus ut, soluta obcaecati inventore
hic natus dolorem. Repellendus vero optio temporibus esse. A few more. Lorem ipsum dolor sit amet consectetur adipisicing elit. Adipisci dolores amet saepe dignissimos, eligendi non sint nobis! Dignissimos, eligendi non sint nobis! Lorem Ipsum
Dolor sit amet consectetur
</p>
<h2>And that's it, Here's a soothing gif</h2>
<img src="https://i.pinimg.com/originals/4c/ba/93/4cba9388025117cb8c60a4f8610f90f5.gif" width="300">
</div>
<div id="gallery">
<h1>Here's the gallery! Sorry...The images aren't available at the moment</h1>
</div>
<div id="#about"></div>
<div id="#contact"></div>
</main>
</body>
</html>
您好。我有 2 个 ID 为#home 和#gallery 的 DIV。当我单击画廊时,我希望隐藏主页部分并显示画廊。我将 id gallery 设置为 display:none;
,然后使用伪选择器 :target
将其设置为 display:block
...像这样
#gallery {
display: none;
<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre><code>/* Header styles */
* {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
}
header {
background-color: aqua;
text-align: center;
padding: 20px 0px;
}
header a{
font-size: xx-large;
margin: 10px;
text-decoration: none;
color: black;
background-color:cyan;
font-weight: bolder;
padding: 0px 15px;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
}
header a:hover {
background-color: darkcyan;
color: white;
}
/* Header Styles End */
/* --- */
/* Sections Style */
#gallery, #about, #contact {
display: none;
}
main {
padding: 25px;
background-color: aliceblue;
margin: 10px;
}
#img {
width: 100px;
height: 100px;
}
#gallery:target {
display: block !important;
}
#gallery:target ~ #home {
display: none !important;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<header>
<a href="#home">Home</a>
<a href="#gallery">Gallery</a>
<a href="#about">About</a>
<a href="#contact">Contact</a>
</header>
<main>
<div id="home">
<h1>
Welcome To the Web Page where there at 4 sections! -
<font size="10" color="grey">Home</font>
</h1>
<h2>Click them to see their relevant sections!</h2>
<p>
Here's a bunch of Lorem to make it seems a bit big. Better know Latin! Here it comes! Lorem ipsum, dolor sit amet consectetur adipisicing elit. Sint blanditiis nulla mollitia quam, sequi consectetur, asperiores itaque minus ut, soluta obcaecati inventore hic natus dolorem. Repellendus vero optio temporibus esse. A few more. Lorem ipsum dolor sit amet consectetur adipisicing elit. Adipisci dolores amet saepe dignissimos, eligendi non sint nobis! Dignissimos, eligendi non sint nobis! Lorem Ipsum Dolor sit amet consectetur
</p>
<h2>And that's it, Here's a soothing gif</h2>
<img src="https://i.pinimg.com/originals/4c/ba/93/4cba9388025117cb8c60a4f8610f90f5.gif" width="300">
</div>
<div id="gallery">
<h1>Here's the gallery! Sorry...The images aren't available at the moment</h1>
</div>
<div id="#about"></div>
<div id="#contact"></div>
</main>
</body>
</html>