search

Home  >  Q&A  >  body text

Full-size images that automatically adjust resolution based on browser size

<p>I have a shipping website. </p> <p>How can I make my hover image fill the entire screen and be independent of my computer screen resolution. When I shrink the browser window, they should also crop and shrink. Thanks. </p> <pre class="brush:php;toolbar:false;">.hover-image { display: flex; position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); z-index: -1; pointer-events: none; flex-direction: column; align-items: center; justify-content: center; /* Change width and height to scaled image */ width: 100vw; height: auto; } .hover-image img { max-width: 100% !important; max-height: 100% !important; width: auto !important; height: auto !important; margin-bottom: 0; }</pre> Image Hover One {image 1} Image Hover Two {image 2} <p>I've tried using pixels, but that's resolution specific. </p>
P粉302160436P粉302160436471 days ago479

reply all(1)I'll reply

  • P粉322106755

    P粉3221067552023-08-17 00:57:17

    This CSS code causes the image on mouseover to cover the entire screen while maintaining its aspect ratio. The image will be cropped as needed and adjusted when the browser window is resized. The container uses fixed positioning and flex alignment for centered display, while object-fit: cover ensures cropping.

    /* 将此CSS应用于您的鼠标悬停图像 */
    .hover-image {
       position: fixed;
       top: 0;
       left: 0;
       width: 100%;
       height: 100%;
       z-index: -1;
       pointer-events: none;
       display: flex;
       align-items: center;
       justify-content: center;
       overflow: hidden; /* 当图像大于屏幕时,这将隐藏任何溢出部分 */
    }
    
    .hover-image img {
        width: 100%; /* 使图像占据容器的整个宽度 */
        height: auto; /* 保持宽高比 */
        object-fit: cover; /* 裁剪图像以覆盖容器并保持宽高比 */
    }
    <img src="https://picsum.photos/200/300" class="hover-image">

    reply
    0
  • Cancelreply