잡동사니

반응형

질문

 

가변 내용을 포함하는 component를 만들고 있습니다. 그래서 나는 img가 bg img처럼 반응하고 높이를 정의하는 대신 컨테이너의 높이를 취하고 싶습니다. add object-fit : cover 를 정의하더라도 img는 여전히 부모의 높이에 영향을 미칩니다. 텍스트가 img가 아닌 부모의 높이를 정의하기를 원합니다. 이미지가 wysiwyg 편집기를 통해 채워지고 업로드 된 이미지가로드되는 위치를 제어 할 수 없기 때문에 배경 이미지를 사용하지 않는 것이 좋습니다 (즉, img 여야 함). 당신의 도움을 주셔서 감사합니다.

Codesandbox

CODE:

 

.container {
    display: flex;
    flex-direction: row;
    padding: 20px;
    background: lightblue;
    width: 80%;
}

.img-item {
    align-self: stretch;
    flex-shrink: 2;
}

.img-item img {
    object-fit: cover;
    height: 100%;
    width: 100%;
}

.text-item {
    flex-shrink: 3;
    padding: 20px;
}
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <meta http-equiv="X-UA-Compatible" content="ie=edge" />
        <title>Static Template</title>
        <link rel="stylesheet" href="style.css" />
    </head>
    <body>
        <div class="container">
            <div class="img-item">
                <img src="https://i.imgur.com/2bvab7y.jpg" alt="" />
            </div>
            <div class="text-item">
                <h1>
                    Heading one
                </h1>
                <p>Hello world. You're the best.</p>
            </div>
        </div>
    </body>
</html>

 

답변1

 

flex-basis CSS 속성 이 누락되었습니다.

 

.container {
  display: flex;
  flex-direction: row;
  padding: 20px;
  background: lightblue;
  width: 80%;
}
.img-item {
  align-self: stretch;
  flex-shrink: 2;
}
.img-item img {
  object-fit: cover;
  height: 100%;
  width: 100%;
}
.text-item {
  flex-shrink: 3;
  padding: 20px;
}
[class$=item]{flex-basis: 120px;}
<div class="container">
  <div class="img-item">
    <img src="https://i.imgur.com/2bvab7y.jpg" alt="" />
  </div>
  <div class="text-item">
    <h1>
      Heading one
    </h1>
    <p>Hello world. You're the best.</p>
  </div>
</div>

 Run code snippetHide results

Full page

 

 

 

 

 

 

출처 : https://stackoverflow.com/questions/63176128/img-using-object-fit-cover-still-increases-height-of-parent

반응형

이 글을 공유합시다

facebook twitter googleplus kakaoTalk kakaostory naver band