Home > Blockchain >  How do to center the image even though the width of the div is smaller then the image width
How do to center the image even though the width of the div is smaller then the image width

Time:11-04

Currently, my image is center within it's div, but when i make the browser smaller, the image won't move further left to keep center within it's div.

enter image description here

HTML

<!doctype html>
<html  lang="">

<head>
  <meta charset="utf-8">
  <title></title>
  <meta name="description" content="">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <meta property="og:title" content="">
  <meta property="og:type" content="">
  <meta property="og:url" content="">
  <meta property="og:image" content="">

  <link rel="manifest" href="site.webmanifest">
  <link rel="apple-touch-icon" href="icon.png">

  <link rel="stylesheet" href="https://development.beeldenfabriek.nl/woningzoeker/stylesheet.css">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">
  <!-- Place favicon.ico in the root directory -->

  <link rel="stylesheet" href="css/normalize.css">
  <link rel="stylesheet" href="css/main.css?version=3">
</head>

<body>
  <section  id="Scene Section">
    <div >
      <div >
        <div >
          <img src="images/building_rotation/building_rotation_0014.png" >
        </div>
      </div>
    </div>
    <div >
      <div >
        <div >
          
        </div>
        <div >
          
        </div>
      </div>
    </div>

  </section>
</body>

</html>



CSS

/* COLUMN STRUCTURE */

[class*="col-"] {
  width: 100%;
  float: left;
  text-align: center;
}

@media only screen and (min-width: 600px) {
  .col-s-1 {width: 8.33%;}
  .col-s-2 {width: 16.66%;}
  .col-s-3 {width: 25%;}
  .col-s-4 {width: 33.33%;}
  .col-s-5 {width: 41.66%;}
  .col-s-6 {width: 50%;}
  .col-s-7 {width: 58.33%;}
  .col-s-8 {width: 66.66%;}
  .col-s-9 {width: 75%;}
  .col-s-10 {width: 83.33%;}
  .col-s-11 {width: 91.66%;}
  .col-s-12 {width: 100%;}
}

@media only screen and (min-width: 768px) {
  .col-1 {width: 8.33%;}
  .col-2 {width: 16.66%;}
  .col-3 {width: 25%;}
  .col-4 {width: 33.33%;}
  .col-5 {width: 41.66%;}
  .col-6 {width: 50%;}
  .col-7 {width: 58.33%;}
  .col-8 {width: 66.66%;}
  .col-9 {width: 75%;}
  .col-10 {width: 83.33%;}
  .col-11 {width: 91.66%;}
  .col-12 {width: 100%;}
}

* {
  box-sizing: border-box;
  margin: 0px;
  padding: 0px;
}

html {
  font-family: 'Arial';
}



.deel1 {
  display: inline-block;
  height: 100vh;
  width: 100%;
}
.deel1 .imgbox {
  height: 100vh;
  width: 100%;
  background-color: rgba(200, 200, 200, 1.0);
}
.deel1 .imgbox .img {
  justify-content: center;
  height: 100%;
}




.deel2 {
  display: block;
  height: 100vh;
}
.deel2 .infobox {
  width: 100%;
  height: 50vh;
  background-color: rgba(230, 230, 230, 1.0);
}
.deel2 .planbox {
  width: 100%;
  height: 50vh;
  background-color: rgba(240, 240, 240, 1.0);
}

I've tried to take the display: flex; approach and justify-content: center; but it still doesn't keep the image centered when the div gets smaller then the image width.

CodePudding user response:

Set the image width to 100%, it will take up the whole width of parent div. Then give the image object-fit:contain or object-fit:cover (if you want it cropped). The image doesn't need to has a height specified. height:auto would be ok.

CodePudding user response:

change the below code in the CSS

.deel1 .imgbox {
  height: 100vh;
  width: 100%;
  background-color: rgba(200, 200, 200, 1.0);

  position: relative;
  overflow: hidden; /* if needed */
}
.deel1 .imgbox .img {
  /* justify-content: center; */
  height: 100%;

  position: absolute;
  top: 0;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
}
  • Related