⌨️
Web Design
  • Index
  • Front End
    • Front End Index
  • HTML/CSS/JS
    • HTML Index
      • Page layout
      • Form
      • Image Optimization
    • CSS Index
      • CSS Selectors
        • Selectors
        • Combinators
        • Attribute Selectors
        • Pseudo classes
      • CSS Cascade and Inheritance
        • Cascade
        • Inheritance
      • Specificity
      • Box Model
      • CSS Clearfix
      • Responsive Meta Tag
      • Flexbox Layout
      • Grid Layout
      • CSS Naming Rules
        • Golden Guidelines for Writing Clean CSS
        • BEM
        • Rules
      • Frameworks & Libraries
    • JS Index
      • JS loading
      • Modules
      • Js Array Common Method 1
      • Js Array Common Method 2
      • Custom attributes on the element
      • Operator
      • Parse a JSON Date in JavaScript
      • Importmap
  • Git
    • Git Index
    • Initialize a Repository
    • Top 20 Git Commands With Examples
  • UML
    • UML Class
  • React
    • Index
      • setState async issue
      • Redux
      • Axios
      • useState update in A Timeout
  • WebGL
    • WebGL Index
  • Back End
    • Back-End Index
      • Knex
      • Bookshelf
        • bookshelf install
        • Bookshelf only returns one row
      • Server side init flow
      • Install Wordpress on Ubuntu 22.04 with a LAMP Stack
  • React Native
    • React Native Index
      • Dynamically changing Image URLs in React Native
  • Expo
    • EAS build config
Powered by GitBook
On this page
  • SRCSET
  • Picture Tag

Was this helpful?

  1. HTML/CSS/JS
  2. HTML Index

Image Optimization

SRCSET

  • The srcset attribute defined a list of images

  • Each image should include the related size or the pixel density

  • The browser will determine which image to use based on the viewport size and screen resolution

<img
 srcset="img/books-400.jpg 400w,
 img/books-800.jpg 800w,
 img/books-1200.jpg 1200w"
 sizes="(min-width: 1024px) 25vw,
 (min-width: 800px) 50vw,
 100vw"
 src="img/books-800.jpg"
 alt="Books on a shelf">

Picture Tag

<picture>
 <source srcset="img/books-400.jpg 400w,
                 img/books-800.jpg 800w,
                 img/books-1200.jpg 1200w"
         media="(max-width: 960px)">
         
 <source srcset="img/books-square-400.jpg 400w,
                 img/books-square-600.jpg 600w,
                 img/books-square-800.jpg 800w,"
         sizes="50vw"
         media="(min-width: 961px)">
 <img src="img/books-800.jpg" alt="Books on a shelf">
</picture>

PreviousFormNextCSS Index

Last updated 3 years ago

Was this helpful?