이미지가 추가될 때 div 높이가 과도하게 늘어남 (div height overstretching when image is appended)


문제 설명

이미지가 추가될 때 div 높이가 과도하게 늘어남 (div height overstretching when image is appended)

I'm having a problem with nested divs and their height.

What i want to accomplish is that the an image's parent's height stretch to fit the image. What is happening is that the parent is overstretching and the height is bigger than the image.

you can check the code working here: http://jsfiddle.net/83Ke6/

HTML

<div class="select_box">
    <div class="selected">
        <img src="http://awardwallet.com/images/fbSmallLogo.png" />
    </div>
</div>

CSS

.select_box {
    display:inline‑block;
    height:50px;
    border:1px solid blue;
}
.selected {
    border:1px solid gray;
}
img {
    border:1px solid red;
}

So on this case, .selected should equal img height, however it is 5px heigher; Thanks for the help


참조 솔루션

방법 1:

By default img is a inline element and therefore reserves a space under just like lines in a paragraph. Add display: block; to the image to remove the spacing.

Fiddle: http://jsfiddle.net/83Ke6/3/

방법 2:

.select_box {
    display:inline‑block;
    height:auto;
    border:1px solid blue;
    line‑height:0px;
}
.selected {
    border:1px solid gray;
    height:auto;
}
img {
    border:1px solid red;
}

By setting an auto; height and an line‑height of 0px;, your div will adjust to it's inner content. Resulting like this: http://jsfiddle.net/wz45k/

(by Dan SternmonokhR Lacorne)

참조 문서

  1. div height overstretching when image is appended (CC BY‑SA 3.0/4.0)

#image #css #height






관련 질문

이미지가 추가될 때 div 높이가 과도하게 늘어남 (div height overstretching when image is appended)

MS-Outlook 임베디드 이미지를 가져오고 MS-Access VBA를 사용하여 파일 시스템에 저장 (Getting MS-Outlook embedded images and saving them within the file system using MS-Access VBA)

Android 이미지 갤러리 이미지 자르기 및 ImageView 설정 (Android Image Gallery Crop Image and set ImageView)

Facebook은 드래그하여 이미지를 닫는 데 어떤 라이브러리를 사용합니까? (What library does Facebook use for dismiss image by dragging?)

이미지를 Picturebox(VB6의 이미지)에서 문자열(Base64)로 변환하는 방법은 무엇입니까? (How to convert Image from a Picturebox(Image in VB6) to String(Base64)?)

HTML, Canvas 및 WebGL 간의 품질 차이 (Quality differences between HTML, Canvas and WebGL)

TBitmapImage는 Inno Setup 6에서 크기 조정된 디스플레이의 크기보다 크게 렌더링됩니다. (TBitmapImage is rendered larger than its size on scaled display in Inno Setup 6)

이미지 URL이 실제 이미지를 가리키는지 확인합니다. 문제를 일으키는 URL의 공백 (Check if an image URL points to an actual image. Space in URL in causing issue)

android studio에서 인텐트를 통해 여러 콘텐츠를 보내는 방법 (how can i send multiple contents through intent in android studio)

Squarespace 사이트용 CSS로 이미지의 위치를 완벽하게 제어 (Fully Control The Location of an Image with CSS for Squarespace site)

iText7을 사용하여 PDFButtonFormField에 이미지를 추가할 때 종횡비를 유지하는 방법 (How to Maintain Aspect Ratio when Adding an Image to a PDFButtonFormField using iText7)

png 이미지에서 픽셀 색상 추출 (extracting pixel color from png image)







코멘트