4

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>
kukkuz
  • 41,512
  • 6
  • 59
  • 95
TheRealFakeNews
  • 7,512
  • 16
  • 73
  • 114

3 Answers3

5

Maybe it is not working for you as container is absolute and hence the body has zero height.

  1. Add height: 100% to html and body first.

  2. Use centering method for absolutely 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>
kukkuz
  • 41,512
  • 6
  • 59
  • 95
2

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>
alexwc_
  • 1,595
  • 2
  • 24
  • 35
1
<div class="shell">
  <div class="container">
    <div class="red">Red</div>
    <div class="green">Green</div>
    <div class="blue">Blue</div>
  </div>
</div>

css:

html, body {
  height: 100%;
}
.shell {
  height: 100%;
  margin: 0;
 text-align: center;
  width: 100%;
}

.shell:before {
  content: ' ';
  display: inline-block;
  height: 100%;
  vertical-align: middle;
  margin-right: -0.25em;
}

.container {
  width: 100px;
  color: #fff;
  display: inline-block;
  margin: auto;
  vertical-align: middle;
}

.red {
  background: red;
  height: 100px;
  line-height: 100px;
  width: 100px;
  color: #fff;
  border-radius: 50%;
  margin-bottom: 10px;
}

.green {
  background: green;
  height: 100px;
  line-height: 100px;
  width: 100px;
  color: #fff;
  border-radius: 50%;
  margin-bottom: 10px;
}

.blue {
  background: blue;
  height: 100px;
  line-height: 100px;
  width: 100px;
  color: #fff;
  border-radius: 50%;
}

See fiddle: https://jsfiddle.net/wLpv9x2o/3/

Edit: this is just a quick approach, style it to your needs

Wim Mertens
  • 1,780
  • 3
  • 20
  • 31