0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[HTML/CSS] スプシの右クリックメニュー的なサムシングを作る備忘録

Last updated at Posted at 2022-01-31

スプシの右クリックメニューに限らず、よくある階層的なヘッダーのサイトマップにも。

mojikyo45_640-2.gif
index.html
<div>
	<ul>
		<li>
			<div class="li-txt">1層目 - A</div>
		</li>
		<li>
			<div class="li-txt">1層目 - B</div>
			<ul>
				<li>
					<div class="li-txt">2層目 - A</div>
				</li>
				<li>
					<div class="li-txt">2層目 - B</div>
					<ul>
						<li>
							<div class="li-txt">3層目 - A</div>
						</li>
						<li>
							<div class="li-txt">3層目 - B</div>
						</li>
					</ul>
				</li>
			</ul>
		</li>
		<li>
			<div class="li-txt">1層目 - C</div>
		</li>
	</ul>
</div>
index.css
ul {
  list-style:none;
  width: fit-content;
  padding: 0;
  margin: 0;
}
/* ul内の深い階層も含む全てulを非表示 */
ul ul {
  display: none;
}
/* liホバー時にそのli直下のul要素だけを表示 */
ul li:hover > ul {
  display: block;
  width: fit-content;
}
ul li {
  position: relative;
}
ul li:hover {
  background-color: #ededed;
}
/* ホバーしているliに対して、その下の階層のulが横並びになるようにする */
ul ul {
  position: absolute;
  left: 100%;
  top: 0;
}
.li-txt {
  padding: 5px 10px;
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?