Programing

절대적으로 위치한 부모 div 안에 div를 세로로 가운데에 배치하는 방법

lottogame 2020. 7. 19. 09:47
반응형

절대적으로 위치한 부모 div 안에 div를 세로로 가운데에 배치하는 방법


분홍색 컨테이너 중간에 파란색 컨테이너를 얻으려고하지만 vertical-align: middle;그 경우에는 효과가없는 것 같습니다 .

<div style="display: block; position: absolute; left: 50px; top: 50px;">
    <div style="text-align: left; position: absolute;height: 56px;vertical-align: middle;background-color: pink;">
        <div style="background-color: lightblue;">test</div>
    </div>
</div>

결과:

여기에 이미지 설명을 입력하십시오

기대:

여기에 이미지 설명을 입력하십시오

어떻게 달성 할 수 있는지 제안하십시오.

JS 피들


우선 vertical-align테이블 셀 및 인라인 레벨 요소에만 적용 할 수 있습니다.

귀하의 요구를 충족 시키거나 충족시키지 못할 수있는 수직 정렬을 달성하는 몇 가지 방법이 있습니다. 그러나 나는 내가 좋아하는 두 가지 방법보여줄 것이다 .

1. 사용 transformtop

.valign {
    position: relative;
    top: 50%;
    transform: translateY(-50%);
    /* vendor prefixes omitted due to brevity */
}
<div style="position: absolute; left: 50px; top: 50px;">
    <div style="text-align: left; position: absolute;height: 56px;background-color: pink;">
        <div class="valign" style="background-color: lightblue;">test</div>
    </div>
</div>

요점은 백분율 값 이 포함 블록 의 비율 값과 top관련이 있다는 것 height입니다. transforms 의 백분율 값 은 상자 자체의 크기 (경계 상자)와 관련이 있습니다.

당신이 발생하는 경우 폰트 렌더링 문제 (흐릿한 글꼴을), 수정은 추가하는 것입니다 perspective(1px)받는 transform이가되도록 선언 :

transform: perspective(1px) translateY(-50%);

CSS transform 가 IE9 +에서 지원 된다는 점에 주목할 가치가 있습니다.

2. inline-block(의사) 요소 사용

이 방법에는 선언에 inline-block의해 가운데에 세로로 정렬되는 두 개의 형제 요소가 있습니다 vertical-align: middle.

그 중 하나는이 height100%부모와 다른 하나는 우리의 원하는 요소 누구의 우리가 중앙에 맞 춥니 다 싶어합니다.

.parent {
    text-align: left;
    position: absolute;
    height: 56px;
    background-color: pink;
    white-space: nowrap;
    font-size: 0; /* remove the gap between inline level elements */
}

.dummy-child { height: 100%; }

.valign {
    font-size: 16px; /* re-set the font-size */
}

.dummy-child, .valign {
    display: inline-block;
    vertical-align: middle;
}
<div style="position: absolute; left: 50px; top: 50px;">
    <div class="parent">
        <div class="dummy-child"></div>
        <div class="valign" style="background-color: lightblue;">test</div>
    </div>
</div>

마지막으로 사용 가능한 방법 중 하나를 사용 하여 인라인 수준 요소 사이의 간격을 제거해야합니다 .


이것을 사용하십시오 :

.Absolute-Center {
    margin: auto;
    position: absolute;
    top: 0; left: 0; bottom: 0; right: 0;
}

이 링크를 참조하십시오 : https://www.smashingmagazine.com/2013/08/absolute-horizontal-vertical-centering-css/


Use flex blox in your absoutely positioned div to center its content.

See example https://plnkr.co/edit/wJIX2NpbNhO34X68ZyoY?p=preview

.some-absolute-div {    
  display: -webkit-box;
  display: -webkit-flex;
  display: -moz-box;
  display: -moz-flex;
  display: -ms-flexbox;
  display: flex;

  -webkit-box-pack: center;
  -ms-flex-pack: center;
  -webkit-justify-content: center;
  -moz-justify-content: center;
  justify-content: center;

  -webkit-box-align: center;
  -ms-flex-align: center;
  -webkit-align-items: center;
  -moz-align-items: center;
  align-items: center;
}

Center vertically and horizontally:

.parent{
  height: 100%;
  position: absolute;
  width: 100%;
  top: 0;
  left: 0;
}
.c{
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  transform: translateY(-50%);
}

You may use display:table/table-cell;

.a{
   position: absolute; 
  left: 50px; 
  top: 50px;
  display:table;
}
.b{
  text-align: left; 
  display:table-cell;
  height: 56px;
  vertical-align: middle;
  background-color: pink;
}
.c {
  background-color: lightblue;
}
<div class="a">
  <div  class="b">
    <div class="c" >test</div>
  </div>
</div>


Please check this answer to a similar issue.

This is by far the easiest way I have found.

https://stackoverflow.com/a/26079837/4593442

NOTE. I used display: -webkit-flex; instead of display: flex; for testing inside safari.

FOOTNOTE. I am a novice only, sharing help from someone who is clearly experienced.


Here is simple way using Top object.

eg: If absolute element size is 60px.

.absolute-element { 
    position:absolute; 
    height:60px; 
    top: calc(50% - 60px);
}

An additional simple solution

HTML:

<div id="d1">
    <div id="d2">
        Text
    </div>
</div>

CSS:

#d1{
    position:absolute;
    top:100px;left:100px;
}

#d2{
    border:1px solid black;
    height:50px; width:50px;
    display:table-cell;
    vertical-align:middle;
    text-align:center;  
}

You can do it by using display:table; in parent div and display: table-cell; vertical-align: middle; in child div

<div style="display:table;">
      <div style="text-align: left;  height: 56px;  background-color: pink; display: table-cell; vertical-align: middle;">
          <div style="background-color: lightblue; ">test</div>
      </div>
  </div>

참고 URL : https://stackoverflow.com/questions/28455100/how-to-center-div-vertically-inside-of-absolutely-positioned-parent-div

반응형