Reputation: 359
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>
Upvotes: 4
Views: 7549
Reputation: 28553
I would recommend setting the height to auto where width is 100% to keep images in proportion. You could add media queries for mobile devices to set the width to e.g. 50% (depending on the size of the device - you decide) and also set the height to auto. You could also use the new picture tag to accommodate various screen sizes.
Sample picture tag usage:
<picture>
<source media="(min-width: 650px)" srcset="img_pink_flowers.jpg">
<source media="(min-width: 465px)" srcset="img_white_flower.jpg">
<img src="img_orange_flowers.jpg" alt="Flowers" style="width:auto;">
</picture>
Hope this helps
Upvotes: 3
Reputation: 18378
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>
Upvotes: 6