Search code examples
htmlcssimageviewport

Keep fixed position image always in center of viewport


I'm looking for a way to keep my fixed position images, which are placed in the background of the page via z-index, always in the center.

Right now they are in the center, but when I start scrolling, they don't move down the page and are therefore no longer centered.

I've tried setting the image container to fixed positioning with top and left 50%. I've also tried using 50vh and 50vw, and have tested other vh and vw values.

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

body {
  padding: 30px;
}

li.track {
  display: block;
  font-size: 5rem;
  margin-bottom: 15px;
}

li.track:hover {
  color: transparent;
  -webkit-text-stroke-color: #18181b;
  -webkit-text-stroke-width: 3px;
}

li.track:hover .album-art {
  display: block;
}

.album-art {
  z-index: -10;
  position: fixed;
  left: 47vw;
  top: 12vh;
  width: 90%;
  height: 50%;
  max-height: 450px;
  display: none;
  opacity: 0.5;
  overflow: hidden;
  transform: translateX(-50%);
}

.album-art img {
  width: 100%;
}

a {
  text-decoration: none;
  color: inherit;
}

<div class="tracks">

  <ol>
    <li class="track">
      <a href="#" class="nav">
        <div class="album-art"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/1774776/michael-nau.jpg"></div>
        <p class="track-name">King Princess - 'Cheap Queen'</p>
      </a>
    </li>

    <li class="track">
      <a href="#" class="nav">
        <div class="album-art"><img src="hhttps://s3-us-west-2.amazonaws.com/s.cdpn.io/1774776/louie-short.jpg"></div>
        <p class="track-name">Charly Bliss - 'Capacity'</p>
      </a>
    </li>

    <li class="track">
      <a href="#" class="nav">
        <div class="album-art"><img src="/https://s3-us-west-2.amazonaws.com/s.cdpn.io/1774776/P4K-Tom-Waits-Jim-Jarmusch_PitchHeader.jpg"></div>
        <p class="track-name">Julia Jacklin - 'Good Guy'</p>
      </a>
    </li>

    <li class="track">
      <a href="#" class="nav">
        <div class="album-art"><img src="/img/kevin-abstract.png"></div>
        <p class="track-name">Kevin Abstract - 'Georgia'</p>
      </a>
    </li>

    <li class="track">
      <a href="#" class="nav">
        <div class="album-art"><img src="/img/okey-dokey.jpg"></div>
        <p class="track-name">Okey Dokey - 'Wag Your Tail'</p>
      </a>
    </li>

    <li class="track">
      <a href="#" class="nav">
        <div class="album-art"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/1774776/fruit-bats.jpg"></div>
        <p class="track-name">Devendra Banhart - 'Kantori Ongaku'</p>
      </a>
    </li>

    <li class="track">
      <a href="#" class="nav">
        <div class="album-art"><img src="/img/michael-nau.jpg"></div>
        <p class="track-name">Michael Nau - 'Rides Through The Morning'</p>
      </a>
    </li>

    <li class="track">
      <a href="#" class="nav">
        <div class="album-art"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/1774776/fruit-bats.jpg"></div>
        <p class="track-name">Hop Along - 'Prior Things'</p>
      </a>
    </li>

    <ol>
</div>

I want the images that appear when hovered to always be in the center of the page (scrolled or not).


Solution

  • try this css:

    body {
      padding: 30px;
    }
    
    li.track {
      display: block;
      font-size: 5rem;
      margin-bottom: 15px;
    }
    
    li.track:hover {
      color: transparent;
      -webkit-text-stroke-color: #18181b;
      -webkit-text-stroke-width: 3px;
    }
    
    li.track:hover .album-art {
      display: block;
    }
    
    .album-art {
      z-index: -10;
      position: fixed;
      left: 50%;
      top: 50%;
      width: 90%;
      height: 50%;
      max-height: 450px;
      display: none;
      opacity: 0.5;
      overflow: hidden;
      transform: translate(-50%,-50%);
    }
    
    .album-art img {
      width: 100%;
    }
    
    a {
      text-decoration: none;
      color: inherit;
    }
    

    thank you