IT박스

div를 세로로 가운데에 배치하는 방법은 무엇입니까?

itboxs 2020. 7. 22. 08:21
반응형

div를 세로로 가운데에 배치하는 방법은 무엇입니까? [복제]


이 질문에는 이미 답변이 있습니다.

작은 사용자 이름과 비밀번호 입력 상자를 만들려고합니다.

div를 어떻게 수직으로 정렬합니까?

내가 가진 것은 :

<div id="Login" class="BlackStrip floatright">
   <div id="Username" class="floatleft">Username<br>Password</div>
   <div id="Form" class="floatleft">
   <form action="" method="post">
      <input type="text" border="0"><br>
      <input type="password" border="0">
   </form>
   </div>
</div>

id 사용자 이름 및 양식으로 div를 세로로 가운데에 맞추려면 어떻게해야합니까? 나는 넣어 보았습니다 :

vertical-align: middle;

ID가 로그인 된 div의 CSS에서는 작동하지 않는 것 같습니다. 도움을 주시면 감사하겠습니다.


최신 브라우저에서 가장 좋은 방법은 flexbox를 사용하는 것입니다.

#Login {
    display: flex;
    align-items: center;
}

일부 브라우저에는 공급 업체 접두사가 필요합니다. flexbox를 지원하지 않는 구형 브라우저 (예 : IE 9 이하)의 경우 이전 방법 중 하나를 사용하여 대체 솔루션을 구현해야 합니다 .

추천 독서


이것은 3 줄의 CSS로 수행 할 수 있으며 IE9와 호환되며 다음을 포함합니다.

.element {
  position: relative;
  top: 50%;
  transform: translateY(-50%);
}

예 : http://jsfiddle.net/cas07zq8/

신용


다른 div에서 div를 세로로 정렬 할 수 있습니다. 참조 JSFiddle에이 예제를 하거나 아래의 예를 고려한다.

HTML

<div class="outerDiv">
    <div class="innerDiv"> My Vertical Div </div>
</div>

CSS

.outerDiv {
    display: inline-flex;  // <-- This is responsible for vertical alignment
    height: 400px;
    background-color: red;
    color: white;
}

.innerDiv {
    margin: auto 5px;   // <-- This is responsible for vertical alignment
    background-color: green;   
}

.innerDiv의 마진이 형식이어야합니다 :margin: auto *px;

[어디서 *원하는 값입니까?]

display: inline-flex HTML5를 지원하는 최신 (업데이트 된 / 현재 버전) 브라우저에서 지원됩니다.

Internet Explorer에서는 작동하지 않을 수 있습니다 : P :)

호환성 문제를 해결하기 위해 항상 세로로 정렬 된 div (즉, innerDiv)의 높이를 정의하십시오.


나는 파티에 꽤 늦었지만, 나는 이것을 스스로 생각해 냈고 그것은 수직 정렬을 위해 내가 가장 좋아하는 빠른 해킹 중 하나입니다. 정말 간단하고 진행 상황을 이해하기 쉽습니다.

당신은 사용 :before, 부모 DIV의 시작 부분에 사업부를 삽입을 제공하는 CSS 속성을 display:inline-block하고 vertical-align:middle다음 자식 DIV에 그 같은 속성을 제공합니다. vertical-align선을 따라 정렬 하기 때문에 인라인 div는 선으로 간주됩니다.

:beforediv를 만들면 height:100%하위 div가 자동으로 매우 높은 "줄"의 중간을 따라 정렬됩니다.

.parent:before, .child {
    display:inline-block;
    vertical-align:middle;
}
.parent:before {
    content:""; // so that it shows up
    height:100%; // so it takes up the full height
}

여기 내가 말하는 것을 보여주는 바이올린 이 있습니다. 자식 div의 높이는 제한이 없으며 여백 / 패딩을 수정할 필요가 없습니다.
그리고 여기 더 설명적인 바이올린이 있습니다.

을 좋아하지 않는 경우 :before언제든지 수동으로 div를 넣을 수 있습니다.

<div class="parent">
    <div class="before"></div>
    <div class="child">
        content
    </div>
</div>

그리고 단지 재 할당 .parent:before.parent .before


높이를 알고 있다면 다음과 같이 음의 절대 위치를 사용할 수 있습니다 margin-top.

#Login {
    width:400px;
    height:400px;
    position:absolute;
    top:50%;
    left:50%;
    margin-left:-200px; /* width / -2 */
    margin-top:-200px; /* height / -2 */
}

그렇지 않으면 CSS만으로 div를 세로로 가운데에 배치하는 실제 방법이 없습니다


내 파이어 폭스와 크롬에서 다음을 수행하십시오.

CSS :

display: flex;
justify-content: center;     // vertical align
align-items: center;         // horizontal align

이 사이트가 유용하다는 것을 알았습니다 : http://www.vanseodesign.com/css/vertical-centering/ 이것은 나를 위해 일했습니다 :

HTML

<div id="parent">
    <div id="child">Content here</div>
</div>

CSS

#parent {
    padding: 5% 0;
}

#child {
    padding: 10% 0;
}

@ GáborNagy의 다른 게시물 에 대한 의견 은 jsfiddle을 가져 와서 작은 추가 내용으로 여기에 복사하고 있기 때문에 내가 찾을 수있는 가장 간단한 해결책이었습니다.

CSS :

#wrapper {
    display: table;
    height: 150px;
    width: 800px;
    border: 1px solid red;
}

#cell {
    display: table-cell;
    vertical-align: middle;
}

HTML :

<div id="wrapper">
    <div id="cell">
        <div class="content">
            Content goes here
        </div>
    </div>
</div>

수평으로 정렬하려면 "cell"div 안에 다른 div "inner-cell"을 추가하고 다음 스타일을 지정해야합니다.

#inner-cell{
      width: 250px;
      display: block;
      margin: 0 auto;
}

수직 정렬은 항상 까다로 웠습니다.

여기에서는 div를 세로로 정렬하는 방법을 설명했습니다.

http://jsfiddle.net/3puHE/

HTML:

<div style="display:flex;">

<div class="container table">
<div class="tableCell">
<div class="content"><em>Table</em> method</div>
</div>
</div>

<div class="container flex">
<div class="content new"><em>Flex</em> method<br></div>
</div>

<div class="container relative">
<div class="content"><em>Position</em> method</div>
</div>

<div class="container margin">
<div class="content"><em>Margin</em> method</div>
</div>

</div>

CSS:

em{font-style: normal;font-weight: bold;}
.container {
    width:200px;height:200px;background:#ccc;
    margin: 5px; text-align: center;
}
.content{
    width:100px; height: 100px;background:#37a;margin:auto;color: #fff;
}
.table{display: table;}
.table > div{display: table-cell;height: 100%;width: 100%;vertical-align: middle;}
.flex{display: flex;}
.relative{position: relative;}
.relative > div {position: absolute;top: 0;left: 0;right: 0;bottom: 0;}
.margin > div {position:relative; margin-top: 50%;top: -50px;}

http://jsfiddle.net/dvL512e7/

Unless the aligned div has fixed height, try using the following CSS to the aligned div:

{
  margin: auto;
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  display: table;
}

I needed to specify min-height

#login
    display: flex
    align-items: center
    justify-content: center
    min-height: 16em

if you are using fix height div than you can use padding-top according your design need. or you can use top:50%. if we are using div than vertical align does not work so we use padding top or position according need.


I found a way that works great for me. The next script inserts an invisible image (same as bgcolor or a transparant gif) with height equal to half the size of the white-space on the screen. The effect is a perfect vertical-alignment.

Say you have a header div (height=100) and a footer div (height=50) and the content in the main div that you would like to align has a height of 300:

<script type="text/javascript" charset="utf-8">
var screen = window.innerHeight;
var content = 150 + 300;
var imgheight = ( screen - content) / 2 ;
document.write("<img src='empty.jpg' height='" + imgheight + "'>"); 
</script>   

You place the script just before the content that you want to align!

In my case the content I liked to align was an image (width=95%) with an aspect ratio of 100:85 (width:height).Meaning the height of the image is 85% of it's width. And the Width being 95% of the screenwidth.

I therefore used:

var content = 150 + ( 0.85 * ( 0.95 * window.innerWidth ));

Combine this script with

<body onResize="window.location.href = window.location.href;">

and you have a smooth vertical alignment.

Hope this works for you too!


Centering the child elements in a div. It works for all screen sizes

#parent {
    padding: 5% 0;
}

#child {
    padding: 10% 0;
}

<div id="parent">
    <div id="child">Content here</div>
</div>

for more details, you can visit to this link


simplest way to center your div element is to use this class with following properties.

.light {
    margin: auto;
    width: 50%;
    border: 3px solid green;
    padding: 10px;
}

have you try this one?

.parentdiv {
 margin: auto;
 position: absolute;
 top: 0; left: 0; bottom: 0; right: 0;
 height: 300px; // at least you have to specify height
}

hope this helps


divs can't be vertically aligned that way, you can however use margins or position:relative to modify its location.

참고URL : https://stackoverflow.com/questions/2743989/how-to-vertically-center-divs

반응형