수평 및 수직 요소를 중심에 배치하는 방법 수 있습니까? * { box-sizing:

탭 내용을 세로로 가운데에 맞추려고하지만 CSS 스타일을 추가 display:inline-flex하면 가로 텍스트 정렬이 사라집니다.

각 탭에 대해 텍스트 맞춤 x 및 y를 어떻게 만들 수 있습니까?

* { box-sizing: border-box; }
#leftFrame {
  background-color: green;
  position: absolute;
  left: 0;
  right: 60%;
  top: 0;
  bottom: 0;
}
#leftFrame #tabs {
  background-color: red;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 25%;
}
#leftFrame #tabs div {
  border: 2px solid black;
  position: static;
  float: left;
  width: 50%;
  height: 100%;
  text-align: center;
  display: inline-flex;
  align-items: center;
}
<div id=leftFrame>
  <div id=tabs>
    <div>first</div>
    <div>second</div>
  </div>
</div>



답변

  • 접근법 1- transform translateX/ translateY:

    여기 예 / 전체 화면 예

    에서 지원되는 브라우저 (대부분), 당신은 사용할 수 있습니다 top: 50%/ left: 50%와 함께 translateX(-50%) translateY(-50%)수평 요소를 중심 / 동적 수직으로.

.container {
    position: absolute;
    top: 50%;
    left: 50%;
    -moz-transform: translateX(-50%) translateY(-50%);
    -webkit-transform: translateX(-50%) translateY(-50%);
    transform: translateX(-50%) translateY(-50%);
}
<div class="container">
    <span>I'm vertically/horizontally centered!</span>
</div>


  • 접근법 2-Flexbox 방법 :

    여기 예 / 전체 화면 예

    에서 지원되는 브라우저 의 설정 display대상에 요소의 flex사용을 align-items: center수직 중심과 justify-content: center수평 중심에 대해. 추가 브라우저 지원을 위해 공급 업체 접두사를 추가하는 것을 잊지 마십시오 ( 예 참조 ).

html, body, .container {
    height: 100%;
}

.container {
    display: -webkit-flexbox;
    display: -ms-flexbox;
    display: -webkit-flex;
    display: flex;
    -webkit-flex-align: center;
    -ms-flex-align: center;
    -webkit-align-items: center;
    align-items: center;
    justify-content: center;
}
<div class="container">
  <span>I'm vertically/horizontally centered!</span>
</div>


  • 접근법 3- table-cell/ vertical-align: middle:

    여기 예 / 전체 화면 예

    경우에 따라 html/ body요소의 높이가로 설정되어 있는지 확인해야합니다 100%.

    수직 정렬의 경우 부모 요소의 width/ height를 설정 100%하고 추가하십시오 display: table. 그런 다음 자식 요소를 들어, 변경 displaytable-cell추가합니다 vertical-align: middle.

    가로 text-align: center로 가운데를 맞추려면 텍스트와 다른 inline자식 요소 를 가운데에 추가 할 수 있습니다 . 또는 margin: 0 auto요소가 block수평 이라고 가정 하면을 사용할 수 있습니다 .

html, body {
    height: 100%;
}
.parent {
    width: 100%;
    height: 100%;
    display: table;
    text-align: center;
}
.parent > .child {
    display: table-cell;
    vertical-align: middle;
}
<section class="parent">
    <div class="child">I'm vertically/horizontally centered!</div>
</section>


  • 접근법 4- 50%변위를 통해 상단에서 절대 위치 :

    여기 예 / 전체 화면 예

    이 방법에서는 텍스트의 높이가 알려진 것으로 가정합니다 18px. 50%부모 요소를 기준으로 요소 를 맨 위에서 절대적으로 배치하십시오 . margin-top알려진 경우 높이의 절반 인 음수 값을 사용하십시오 ( 이 경우-) -9px.

html, body, .container {
    height: 100%;
}

.container {
    position: relative;
    text-align: center;
}

.container > p {
    position: absolute;
    top: 50%;
    left: 0;
    right: 0;
    margin-top: -9px;
}
<div class="container">
    <p>I'm vertically/horizontally centered!</p>
</div>


  • 접근법 5- line-height방법 (최소 유연성 – 제안되지 않음) :

    여기 예

    경우에 따라 상위 요소의 높이가 고정됩니다. 수직 센터링의 line-height경우 자식 요소의 값을 부모 요소의 고정 높이와 동일하게 설정하면 됩니다.

    -이 솔루션은 어떤 경우에 작동하지만, 그것의 가치는 여러 줄의 텍스트가있는 경우는 작동하지 않습니다 지적 과 같습니다 .

.parent {
    height: 200px;
    width: 400px;
    background: lightgray;
    text-align: center;
}

.parent > .child {
    line-height: 200px;
}
<div class="parent">
    <span class="child">I'm vertically/horizontally centered!</span>
</div>


답변

CSS3이 옵션이거나 폴 백인 경우 transform을 사용할 수 있습니다.

.center {
    right: 50%;
    bottom: 50%;
    transform: translate(50%,50%);
    position: absolute;
}

위의 첫 번째 접근 방식과 달리 IE9 +에는 오버플로 버그가 있기 때문에 음수 변환에 left : 50 %를 사용하고 싶지 않습니다. 양의 올바른 값을 사용하면 가로 스크롤 막대가 표시되지 않습니다.


답변

상자를 세로 및 가로로 가운데 정렬하는 가장 좋은 방법은 두 개의 컨테이너를 사용하는 것입니다.

외부 컨테이너 :

  • 있어야한다 display: table;

내부 컨테이너 :

  • 있어야한다 display: table-cell;
  • 있어야한다 vertical-align: middle;
  • 있어야한다 text-align: center;

내용 상자 :

  • 있어야한다 display: inline-block;
  • 텍스트를 중앙에 배치하지 않으려면 가로 텍스트 정렬을 조정해야합니다.

데모 :

body {
    margin : 0;
}

.outer-container {
    display: table;
    width: 80%;
    height: 120px;
    background: #ccc;
}

.inner-container {
    display: table-cell;
    vertical-align: middle;
    text-align: center;
}

.centered-content {
    display: inline-block;
    text-align: left;
    background: #fff;
    padding : 20px;
    border : 1px solid #000;
}
<div class="outer-container">
   <div class="inner-container">
     <div class="centered-content">
        Center this!
     </div>
   </div>
</div>

이 바이올린을 참조하십시오 !

페이지 중앙을 중심으로 :

콘텐츠를 페이지 중앙에 배치하려면 외부 컨테이너에 다음을 추가하십시오.

  • position : absolute;
  • width: 100%;
  • height: 100%;

이에 대한 데모는 다음과 같습니다.

body {
    margin : 0;
}

.outer-container {
    position : absolute;
    display: table;
    width: 100%;
    height: 100%;
    background: #ccc;
}

.inner-container {
    display: table-cell;
    vertical-align: middle;
    text-align: center;
}

.centered-content {
    display: inline-block;
    text-align: left;
    background: #fff;
    padding : 20px;
    border : 1px solid #000;
}
<div class="outer-container">
   <div class="inner-container">
     <div class="centered-content">
        Center this!
     </div>
   </div>
</div>

이 바이올린을 참조하십시오 !


답변

다음은 2 개의 간단한 flex 속성을 사용 하여 2 개의 축에서 n div 를 가운데 에 배치하는 방법입니다.

  • 컨테이너의 높이를 설정하십시오. 여기서 본체는 100vh 이상으로 설정되어 있습니다.
  • align-items: center 수직으로 블록을 중앙에 배치합니다
  • justify-content: space-around div 주위에 빈 공간을 분배합니다
body {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: space-around;
}
<div>foo</div>
<div>bar</div>


답변

이 코드 스 니펫을 실행하면 세로 및 가로로 정렬 된 div가 표시됩니다.

html,
body,
.container {
  height: 100%;
  width: 100%;
}
.container {
  display: flex;
  align-items: center;
  justify-content: center;
}
.mydiv {
  width: 80px;
}
<div class="container">
  <div class="mydiv">h & v aligned</div>
</div>


답변

가장 간단하고 깨끗한 솔루션은 CSS3 속성 “transform”을 사용하는 것입니다.

.container {
  position: relative;
}

.container a {
  position: absolute;
  top: 50%;
  transform: translate(0,-50%);
}
<div class="container">
  <a href="#">Hello world!</a>
</div>


답변

    .align {
        display: flex;
        width: 400px;
        height: 400px;
        border: solid 1px black;
        align-items: center;
        justify-content: space-around;
    }
    .align div:first-child {
        width: 20px;
        height: 20px;
        background-color: red;
        position: absolute;
    }
    .align div {
        width: 20px;
        height: 20px;
        background-color: blue;
    }
    <div class='align'>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
    </div>

첫 번째 아이는 중앙에서 수직 및 수평으로 정렬됩니다