/ Gists / Anchor positioning - popover (Coding2Go)
On gists

Anchor positioning - popover (Coding2Go)

CSS CSS trick

index.html Raw #

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
	
	
	<button popovertarget="menu">Menu button</button>
	
	<div id="menu" popover>
		<a href="">Item 1</a>
		<a href="">Another Item 2</a>
		<a href="">Very long long long Item 3</a>
	</div>

</body>
</html>

index.css Raw #

body {
	min-height: 100vh;
	min-height: 100dvh;
	padding: 1em;
	display: flex;
	align-items: start; /* end for fallbacks ..*/
	justify-content: start; /* end for fallbacks ..*/
	font-family: Poppins, 'SegoeUI', Tahoma, Geneva, sans-serif;
}

button {
	background: plum;
	padding: 0.7em 1.5em;
	border: none;
	font-weight: 500;
	border-radius: .3em;
	anchor-name: --test;
}

#menu {
	unset: all;
	width: max-content;
	border: 1px solid #ccc;
	padding: 1rem;
	background: #fff;
	border-radius: .3em;
	position: absolute;
	
	margin: 0; /* important !!! */
	position-anchor: --test;
	top: anchor(bottom);
	left: anchor(left);
	position-try-fallbacks: --bottom-right, --bottom-left, --up-left, --up-right;
	

}

#menu:popover-open {
	display: grid;
}

@position-try --bottom-right {
	inset: unset;
	top: anchor(bottom);
	left: anchor(left);
}

@position-try --bottom-left {
	inset: unset;
	top: anchor(bottom);
	right: anchor(right);
}

@position-try --up-left {
	inset: unset;
	bottom: anchor(top);
	right: anchor(right);
	
}

@position-try --up-right {
	inset: unset;
	bottom: anchor(top);
	left: anchor(left);
	
}

demo.txt Raw #

https://jsbin.com/hegesunoyu/3/edit?html,css,output