I tried all the solutions on here, but none of them worked. I want to center this horizontally and vertically, regardless of the window size.
Note: I have my container
div just the way I want it. It wraps around several other divs. If I adapt the changes suggested by this link, my container div gets messed up. I'm not trying have this be responsive. It's a fixed size (think an image), and I just want it to always be in the center of the window, regardless of window size.
Here's what I have:
* {
margin: 0;
padding: 0;
}
#container {
background-color: black;
border-radius: 10px;
padding: 5px;
display: block;
margin: auto;
/* changed to auto, didn't make a difference*/
border-width: 1px;
border-color: black;
border-style: solid;
position: absolute;
}
.light {
height: 100px;
width: 100px;
display: block;
border-radius: 50%;
margin: 10px;
border-width: 5px;
background-color: grey;
}
<body>
<div id="container" onclick="changeColor()">
<div id="green" class="light"></div>
<div id="yellow" class="light"></div>
<div id="red" class="light"></div>
</div>
</body>
Maybe it is not working for you as container
is absolute and hence the body
has zero height.
Add height: 100%
to html
and body
first.
Use centering method for absolute
ly positioned element using transform
to container
:
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
Let me know your feedback on this. Thanks!
html,
body {
height: 100%;
}
* {
margin: 0;
padding: 0;
}
#container {
background-color: black;
border-radius: 10px;
padding: 5px;
display: block;
margin: 0 auto;
border-width: 1px;
border-color: black;
border-style: solid;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.light {
height: 100px;
width: 100px;
display: block;
border-radius: 50%;
margin: 10px;
border-width: 5px;
background-color: grey;
}
<body>
<div id="container" onclick="changeColor()">
<div id="green" class="light"></div>
<div id="yellow" class="light"></div>
<div id="red" class="light"></div>
</div>
</body>
You can also do this with Flexbox (I realize that in a comment you said you didn't need this 'responsive' or 'flex'). Flexbox will get this "smack-dab in the middle". The element that needs to be centered needs to have a parent element that takes the following css:
.whatever-container {
display: flex;
align-items: center;
justify-content: center;
}
With your example I've wrapped it in a div
with a class of light-wrap
. I also gave body
and html
100% height so that .light-wrap
could use a percentage value for height. If you Run Code Snippet below, make sure to try it on full screen for the full effect.
* {
margin: 0;
padding: 0;
}
body, html {
height:100%;
}
.light-wrap {
display: flex;
align-items: center;
justify-content: center;
height: 100%; /* height is just to demonstrate */
background:#eee;
}
#container {
background-color: black;
border-radius: 10px;
padding: 5px;
display:inline-block;
border: 1px solid black;
}
.light {
height: 100px;
width: 100px;
display: block;
border-radius: 50%;
margin: 10px;
border-width: 5px;
background-color: grey;
}
<div class="light-wrap">
<div id="container" onclick="changeColor()">
<div id="green" class="light"></div>
<div id="yellow" class="light"></div>
<div id="red" class="light"></div>
</div>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With