html - How do you make a div box fit all of it's contents, even if the content's height is larger than the viewport height? - TagMerge
3How do you make a div box fit all of it's contents, even if the content's height is larger than the viewport height?How do you make a div box fit all of it's contents, even if the content's height is larger than the viewport height?

How do you make a div box fit all of it's contents, even if the content's height is larger than the viewport height?

Asked 1 years ago
0
3 answers

Removing position: absolute; fixes this issue.

Source: link

0

<body>
  <div class="box"> 
    <img src="https://pp.userapi.com/c622225/v622225117/10f33/47AAEI48pJU.jpg?ava=1"  alt="Example image"/> 
  </div>
</body>
.box {
  width: 30%;
  height: 200px;
  border: 5px dashed #f7a239;
}

img {
  width: 100%;
  height: 100%;
}
<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      .box {
        width: 30%;
        height: 200px;
        border: 5px dashed #f7a239;
      }
      img {
        width: 100%;
        height: 100%;
      }
    </style>
  </head>
  <body>
    <div class="box">
      <img src="https://pp.userapi.com/c622225/v622225117/10f33/47AAEI48pJU.jpg?ava=1" alt="Example image"/>
    </div>
  </body>
</html>
<!DOCTYPE html>
<html>
  <head>
    <style>
      .container {
        width: 60%;
        height: 300px;
      }
      img {
        width: 100%;
        height: 100%;
        object-fit: cover;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <img src="https://images.unsplash.com/photo-1581974267369-3f2fe3b4545c?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60" alt="Image" />
    </div>
  </body>
</html>
<!DOCTYPE html>
<html>
  <head>
    <style>
      body {
        text-align: center;
      }
      img {
        width: 400px;
        height: 200px;
        object-fit: cover;
      }
    </style>
  </head>
  <body>
    <img src="https://images.unsplash.com/photo-1581974267369-3f2fe3b4545c?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60" alt="Image">
  </body>
</html>

Source: link

0

I have a simple div element with a class name box.
<div class="box">
</div>
Then, clear any default margin or padding from the html and body tags.
html, body {
  margin:0px;
}
This is because when you set the height to 100% to an element it will try to stretch to its parent element height.
html, body {
  margin:0px;
  height:100%;
}

.box {
  background:red;
  height:100%;
}
I do not have to add one for horizontal as div is a block-level element that will take the full width horizontally by default.
.box {
  background:red;
  height:100vh;
}
You can also use position absolute as well as setting all the viewport sides (top, right, bottom, left) to 0px will make the div takes the full screen.
.box {
  background:red;
  position:absolute;
  top:0px;
  right:0px;
  bottom:0px;
  left:0px;
}

Source: link

Recent Questions on html

    Programming Languages