Skip to content

Instantly share code, notes, and snippets.

@av1d
Created April 6, 2024 21:06
Show Gist options
  • Save av1d/331b36237c570e3e8da19cec0a9bdf4b to your computer and use it in GitHub Desktop.
Save av1d/331b36237c570e3e8da19cec0a9bdf4b to your computer and use it in GitHub Desktop.
perfectly centered div
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Centered Div</title>
<style>
/* Styles to center the div */
.centered-div {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: lightgray;
padding: 20px;
border-radius: 5px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
/* Optional styles for the body to ensure full div height */
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
</style>
</head>
<body>
<div class="centered-div">
<h1>Centered Div</h1>
<p>This div is centered both horizontally and vertically on the page.</p>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment