Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I prevent images from being squished?

Tags:

html

css

I am coding a simple hero section with automatic changing of images in the background every 2 seconds. However, since the images has different dimensions they look really squished in larger screens. How do I fix this and make it responsive and fit on every screen size?

$hero: 100vh;
$white: #fff;
$black: #000;
$blue: #6db8d7;
$grey: #808080;

* {
  box-sizing: border-box;
}
body {
  padding: 0;
  margin: 0;
}
.hero-body {
  height: 100vh;
  width: 100vw;
  position: relative;
  overflow: hidden;
  text-align: center;
}
.hero-text {
  position: absolute;
  bottom: 40%;
  left: 20px;
}
img {
  width: 100%;
  height: $hero;
}
.title,
h4 {
  color: $white;
  font-size: 1em;
  text-shadow: 1px 1px 2px $black;
  text-align: center;
}
button {
  background-color: $blue;
  padding: 10px;
  text-transform: uppercase;
  font-weight: 500;
  color: $white;
  text-align: center;
  border: 1px solid transparent;
  border-radius: 6px;
  box-shadow: 0 3px 6px 0 $grey;
  cursor: pointer;
  &:hover,
  &:focus {
    transform: scale(1.05);
    transition: all 0.5s ease-in-out;
  }
}
<section class="hero">
  <div class="hero-body">
    <img class="bg-img" src="https://source.unsplash.com/alEZLDPPRBU">
    <img class="bg-img" src="https://source.unsplash.com/Ov0u44CyGdM">
    <img class="bg-img" src="https://source.unsplash.com/8beTH4VkhLI">
    <div class="hero-text">
      <h1 class="title">
        Delight, Fresh, Fruity
      </h1>
      <h4>Come join us for our organic cones!</h4>
      <button>View flavors</button>
    </div>
  </div>
</section>
like image 875
ChuChu Avatar asked Oct 22 '25 11:10

ChuChu


1 Answers

Set object-fit: cover to <img>:

:root {
  --hero: 100vh;
  --white: #fff;
  --black: #000;
  --blue: #6db8d7;
  --grey: #808080;
}

* {
  box-sizing: border-box;
}

body {
  padding: 0;
  margin: 0;
}

.hero-body {
  height: 100vh;
  width: 100vw;
  position: relative;
  overflow: hidden;
  text-align: center;
}

.hero-text {
  position: absolute;
  bottom: 40%;
  left: 20px;
}

img {
  width: 100%;
  height: var(--hero);
  object-fit: cover;
}

.title,
h4 {
  color: var(--white);
  font-size: 1em;
  text-shadow: 1px 1px 2px var(--black);
  text-align: center;
}

button {
  background-color: var(--blue);
  padding: 10px;
  text-transform: uppercase;
  font-weight: 500;
  color: var(--white);
  text-align: center;
  border: 1px solid transparent;
  border-radius: 6px;
  box-shadow: 0 3px 6px 0 var(--grey);
  cursor: pointer;
}

button:hover,
button:focus {
  transform: scale(1.05);
  transition: all 0.5s ease-in-out;
}
<section class="hero">
  <div class="hero-body">
    <img class="bg-img" src="https://source.unsplash.com/alEZLDPPRBU">
    <img class="bg-img" src="https://source.unsplash.com/Ov0u44CyGdM">
    <img class="bg-img" src="https://source.unsplash.com/8beTH4VkhLI">
    <div class="hero-text">
      <h1 class="title">
        Delight, Fresh, Fruity
      </h1>
      <h4>Come join us for our organic cones!</h4>
      <button>View flavors</button>
    </div>
  </div>
</section>
like image 94
Kosh Avatar answered Oct 24 '25 01:10

Kosh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!