Централизовать блок div по центру и внутреннему тексту, независимо от размера экрана - PullRequest
0 голосов
/ 26 февраля 2020

Я пытаюсь сделать так, чтобы поле всегда оставалось в центре (по горизонтали и вертикали) страницы независимо от размера окна браузера. Текст внутри поля должен быть также в центре поля, в котором он находится.

body{
  background-color: black;
}

.box{
  border-style: solid;
  width: 240px;
  height: 240px;
  margin: auto;
}

.boxInside{
  border-style: solid;
  width: 90%;
  height: 90%;
  margin: auto;
  padding: 0px;

/*   align a div vertically within its parent element */
  position: relative;
  top: 50%;
  transform: translateY(-50%);
}

p{
  text-align: center;
}
<div class="box" style="background-color: white;">  
  <div class="boxInside" style="background-color: gray;">
    <div class="boxInside" style="background-color: white;">
      <div class="boxInside" style="background-color: gray;">
        <div class="boxInside" style="background-color: white;">
          <div class="boxInside" style="background-color: gray;">
            <div class="boxInside" style="background-color: white;">
              <div class="boxInside" style="background-color: gray;">
                <div class="boxInside" style="background-color: white;">
                  <div class="boxInside" style="background-color: gray;">
                    <div style="">
                      <p>Testing Display</p>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

1 Ответ

1 голос
/ 26 февраля 2020

Использование flexbox для каждого div и выравнивание по центру по горизонтали и вертикали.

body {
  background-color: black;
}

div {
  display: flex;
  justify-content: center;
  align-items: center;
}

.box {
  border-style: solid;
  width: 240px;
  height: 240px;
  margin: auto;
}

.boxInside {
  border-style: solid;
  width: 90%;
  height: 90%;
  margin: auto;
  padding: 0px;
  /*   align a div vertically within its parent element 
  position: relative;
  top: 50%;
  transform: translateY(-50%);
  */
}

p {
  text-align: center;
}
<div class="box" style="background-color: white;">
  <div class="boxInside" style="background-color: gray;">
    <div class="boxInside" style="background-color: white;">
      <div class="boxInside" style="background-color: gray;">
        <div class="boxInside" style="background-color: white;">
          <div class="boxInside" style="background-color: gray;">
            <div class="boxInside" style="background-color: white;">
              <div class="boxInside" style="background-color: gray;">
                <div class="boxInside" style="background-color: white;">
                  <div class="boxInside" style="background-color: gray;">
                    <div style="">
                      <p>Testing Display</p>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
...