Amini
Amini

Reputation: 1792

Scale images in css grid but the width and height increases size

I wanted to scale the image and so it worked there isn't any problem with that, but the only problem I have is that I want to apply overflow but it also doesn't work.

* {
  padding: 0;
  margin: 0;
}

a {
  text-decoration: none;
  color: black;
  font-family: sans-serif;
  font-weight: 600;
  transition: color 125ms;
}

a:hover {
  color: #f8b500;
}

ul {
  list-style: none;
}
header {
  padding: 4vh 7.5vw;
}
header button {
  display: none;
}
header img {
  padding-left: 1vw;
}
header nav {
  display: inline;
}
header nav ul {
  display: inline;
}
header nav ul li {
  display: inline;
  float: right;
  padding: 0 1.5vw;
  padding-top: 5px;
}
main {
  width: 85vw;
  height: 80vh;
  margin: auto auto;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  grid-column-gap: 1vw;
  grid-row-gap: 1vw;
}
main .bg-1 {
  background: /*url("images/slider-1-1200x900.jpg")*/black no-repeat center;
  grid-column: 1 / 3;
  grid-row: 1 / 3;
}
main .bg-2 {
  background: /*url("images/slider-2-450x600.jpg")*/green no-repeat center;
  grid-column: 3 / 4;
  grid-row: 1 / 1;
}
main .bg-3 {
  background: /*url("images/slider-3-450x600.jpg")*/yellow no-repeat center;
}
main .bg-4 {
  background: /*url("images/slider-4-450x600.jpg")*/blue no-repeat center;
}
main .bg-5 {
  background: /*url("images/slider-5-450x600.jpg")*/gray no-repeat center;
}
main .bg-6 {
  background: /*url("images/slider-6-450x600.jpg")*/red no-repeat center;
}
main .bg-1,
main .bg-2,
main .bg-3,
main .bg-4,
main .bg-5,
main .bg-6 {
  overflow: hidden;
  background-size: cover;
  transition: 350ms;
}
main .bg-1:hover,
main .bg-2:hover,
main .bg-3:hover,
main .bg-4:hover,
main .bg-5:hover,
main .bg-6:hover {
  transform: scale(1.1, 1.1);
}
<header>
    <a href="#"><img src="images/logo-black.png" alt="NewsBit" /></a>
    <button type="button"><i class="fas fa-bars"></i></button>
    <nav>
        <ul class="hidden">
            <li><a href="#">ICON CALENDAR</a></li>
            <li><a href="#">EXPLAINED</a></li>
            <li><a href="#">EVENTS</a></li>
            <li><a href="#">GUIDS & ANALYTICS</a></li>
            <li><a href="#">NEWS</a></li>
        </ul>
    </nav>
</header>
<main>
    <section class="bg-1"></section>
    <section class="bg-2"></section>
    <section class="bg-3"></section>
    <section class="bg-4"></section>
    <section class="bg-5"></section>
    <section class="bg-6"></section>
</main>

Upvotes: 0

Views: 51

Answers (1)

Temani Afif
Temani Afif

Reputation: 272901

Use the image as pseudo element and overflow will work:

* {
  padding: 0;
  margin: 0;
}

a {
  text-decoration: none;
  color: black;
  font-family: sans-serif;
  font-weight: 600;
  transition: color 125ms;
}

a:hover {
  color: #f8b500;
}

ul {
  list-style: none;
}
header {
  padding: 4vh 7.5vw;
}
header button {
  display: none;
}
header img {
  padding-left: 1vw;
}
header nav {
  display: inline;
}
header nav ul {
  display: inline;
}
header nav ul li {
  display: inline;
  float: right;
  padding: 0 1.5vw;
  padding-top: 5px;
}
main {
  width: 85vw;
  height: 80vh;
  margin: auto auto;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  grid-column-gap: 1vw;
  grid-row-gap: 1vw;
}
main section {
  position:relative;
  z-index:0;
}
main section::before {
  content:"";
  position:absolute;
  top:0;
  left:0;
  right:0;
  bottom:0;
  z-index:-1;
  background:var(--i);
}
main .bg-1 {
  --i: url(https://picsum.photos/id/1060/800/800) no-repeat center;
  grid-column: 1 / 3;
  grid-row: 1 / 3;
}
main .bg-2 {
  --i: url(https://picsum.photos/id/1024/800/800) no-repeat center;
  grid-column: 3 / 4;
  grid-row: 1 / 1;
}
main .bg-3 {
  --i: url(https://picsum.photos/id/1018/800/800) no-repeat center;
}
main .bg-4 {
  --i:url(https://picsum.photos/id/1024/800/800) no-repeat center;
}
main .bg-5 {
  --i: url(https://picsum.photos/id/101/800/800) no-repeat center;
}
main .bg-6 {
  --i: url(https://picsum.photos/id/107/800/800) no-repeat center;
}
main section {
  overflow: hidden;
}

main section::before {
  background-size: cover;
  transition: 350ms;
}
main section:hover::before{
  transform: scale(1.1, 1.1);
}
<header>
    <a href="#"><img src="images/logo-black.png" alt="NewsBit" /></a>
    <button type="button"><i class="fas fa-bars"></i></button>
    <nav>
        <ul class="hidden">
            <li><a href="#">ICON CALENDAR</a></li>
            <li><a href="#">EXPLAINED</a></li>
            <li><a href="#">EVENTS</a></li>
            <li><a href="#">GUIDS & ANALYTICS</a></li>
            <li><a href="#">NEWS</a></li>
        </ul>
    </nav>
</header>
<main>
    <section class="bg-1"></section>
    <section class="bg-2"></section>
    <section class="bg-3"></section>
    <section class="bg-4"></section>
    <section class="bg-5"></section>
    <section class="bg-6"></section>
</main>

Upvotes: 1

Related Questions