div 안에 이미지를 세로로 정렬하는 방법 height */ line-height: 25px;

이미지를 포함하는 이미지 안에 어떻게 정렬 할 수 div있습니까?

내 예제에서는 수직 가운데 놓아야 <img>에서을 <div>class ="frame

<div class="frame" style="height: 25px;">
    <img src="http://jsfiddle.net/img/logo.png" />
</div>

.frame의 높이는 고정되어 있으며 이미지의 높이를 알 수 없습니다. 그것이 .frame유일한 해결책이라면 새로운 요소를 추가 할 수 있습니다 . Internet Explorer 7 이상, WebKit, Gecko 에서이 작업을 수행하려고합니다.

여기 jsfiddle을 참조 하십시오 .

.frame {
    height: 25px;      /* Equals maximum image height */
    line-height: 25px;
    width: 160px;
    border: 1px solid red;

    text-align: center;
    margin: 1em 0;
}
img {
    background: #3A6F9A;
    vertical-align: middle;
    max-height: 25px;
    max-width: 160px;
}
<div class=frame>
   <img src="http://jsfiddle.net/img/logo.png" height=250 />
</div>
<div class=frame>
   <img src="http://jsfiddle.net/img/logo.png" height=25 />
</div>
<div class=frame>
   <img src="http://jsfiddle.net/img/logo.png" height=23 />
</div>
<div class=frame>
   <img src="http://jsfiddle.net/img/logo.png" height=21 />
</div>
<div class=frame>
   <img src="http://jsfiddle.net/img/logo.png" height=19 />
</div>
<div class=frame>
    <img src="http://jsfiddle.net/img/logo.png" height=17 />
</div>
<div class=frame>
    <img src="http://jsfiddle.net/img/logo.png" height=15 />
 </div>
<div class=frame>
    <img src="http://jsfiddle.net/img/logo.png" height=13 />
 </div>
<div class=frame>
    <img src="http://jsfiddle.net/img/logo.png" height=11 />
 </div>
<div class=frame>
    <img src="http://jsfiddle.net/img/logo.png" height=9 />
 </div>
<div class=frame>
    <img src="http://jsfiddle.net/img/logo.png" height=7 />
 </div>
<div class=frame>
    <img src="http://jsfiddle.net/img/logo.png" height=5 />
 </div>
<div class=frame>
    <img src="http://jsfiddle.net/img/logo.png" height=3 />
 </div>



답변

유일한 (최고의 크로스 브라우저) 방법 내가 알기로는 사용하는 것입니다 inline-block와 도우미를 height: 100%하고 vertical-align: middle두 요소.

따라서 해결책이 있습니다 : http://jsfiddle.net/kizu/4RPFa/4570/

또는 최신 브라우저에서 추가 요소를 원하지 않고 Internet Explorer 표현식을 사용하지 않으려는 경우 의사 요소를 사용하고 편리한 Expression을 사용하여 Internet Explorer에 추가 할 수 있습니다. 성능 문제가 없습니다.

와 솔루션 :beforeexpression()Internet Explorer 용 : http://jsfiddle.net/kizu/4RPFa/4571/


작동 방식 :

  1. inline-block서로 가까이에 두 개의 요소 가 있으면 서로의 측면에 맞출 수 있으므로 vertical-align: middle다음과 같이 얻을 수 있습니다.

    두 개의 정렬 된 블록

  2. 는 (고정 된 높이를 갖는 블록이있을 때 px, em또는 다른 절대 부)는 내부에 블록의 높이를 설정할 수있다 %.

  3. 따라서 고정 높이의 블록에 하나 inline-block를 추가하면 height: 100%다른 inline-block요소 ( <img/>귀하의 경우) 근처에 수직으로 정렬 됩니다.

답변

이것은 유용 할 수 있습니다 :

div {
    position: relative;
    width: 200px;
    height: 200px;
}
img {
    position: absolute;
    top: 0;
    bottom: 0;
    margin: auto;
}
.image {
    min-height: 50px
}


답변

matejkramny의 솔루션은 좋은 시작이지만 크기가 큰 이미지의 비율이 잘못되었습니다.

여기 내 포크가 있습니다 :

데모 : https://jsbin.com/lidebapomi/edit?html,css,output

시사


HTML :

<div class="frame">
  <img src="foo"/>
</div>

CSS :

.frame {
    height: 160px; /* Can be anything */
    width: 160px; /* Can be anything */
    position: relative;
}
img {
    max-height: 100%;
    max-width: 100%;
    width: auto;
    height: auto;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    margin: auto;
}


답변

3 줄 솔루션 :

position: relative;
top: 50%;
transform: translateY(-50%);

이것은 모든 것에 적용됩니다.

에서 여기 .


답변

순수 CSS의 솔루션 :

.frame {
  margin: 1em 0;
  height: 35px;
  width: 160px;
  border: 1px solid red;
  position: relative;
}

img {
  max-height: 25px;
  max-width: 160px;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  margin: auto;
  background: #3A6F9A;
}
<div class=frame>
  <img src="http://jsfiddle.net/img/logo.png" height=250 />
</div>
<div class=frame>
  <img src="http://jsfiddle.net/img/logo.png" height=25 />
</div>
<div class=frame>
  <img src="http://jsfiddle.net/img/logo.png" height=23 />
</div>
<div class=frame>
  <img src="http://jsfiddle.net/img/logo.png" height=21 />
</div>
<div class=frame>
  <img src="http://jsfiddle.net/img/logo.png" height=19 />
</div>
<div class=frame>
  <img src="http://jsfiddle.net/img/logo.png" height=17 />
</div>
<div class=frame>
  <img src="http://jsfiddle.net/img/logo.png" height=15 />
</div>
<div class=frame>
  <img src="http://jsfiddle.net/img/logo.png" height=13 />
</div>
<div class=frame>
  <img src="http://jsfiddle.net/img/logo.png" height=11 />
</div>
<div class=frame>
  <img src="http://jsfiddle.net/img/logo.png" height=9 />
</div>
<div class=frame>
  <img src="http://jsfiddle.net/img/logo.png" height=7 />
</div>
<div class=frame>
  <img src="http://jsfiddle.net/img/logo.png" height=5 />
</div>
<div class=frame>
  <img src="http://jsfiddle.net/img/logo.png" height=3 />
</div>

중요한 것들

// position: relative; - in .frame holds the absolute element within the frame
// top: 0; bottom: 0; left: 0; right: 0; - this is key for centering a component
// margin: auto; - centers the image horizontally & vertically


답변

보다 현대적인 솔루션을 원하고 레거시 브라우저를 지원할 필요가없는 경우 다음을 수행 할 수 있습니다.

.frame {
    display: flex;
    /**
    Uncomment 'justify-content' below to center horizontally.
    ✪ Read below for a better way to center vertically and horizontally.
    **/

    /* justify-content: center; */
    align-items: center;
}

img {
    height: auto;

    /**
    ✪ To center this image both vertically and horizontally,
    in the .frame rule above comment the 'justify-content'
    and 'align-items' declarations,
    then  uncomment 'margin: auto;' below.
    **/

    /* margin: auto; */
}

/* Styling stuff not needed for demo */
.frame {
    max-width: 900px;
    height: 200px;
    margin: auto;
    background: #222;
}
p {
    max-width: 900px;
    margin: 20px auto 0;
}
img {
    width: 150px;
}
<div class="frame">
    <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/9988/hand-pointing.png">
</div>

여기 펜이 있습니다 : http://codepen.io/ricardozea/pen/aa0ee8e6021087b6e2460664a0fa3f3e


답변

이렇게하면 이미지를 세로로 가운데에 맞출 수 있습니다 ( demo ).

div{
  height: 150px; // Internet Explorer 7 fix
  line-height: 150px;
}
img{
  vertical-align: middle;
  margin-bottom: 0.25em;
}