<p>
    เปลี่ยนขนาด หน้าต่าง เพื่อดูการเปลี่ยนแปลง.<br>
    Resize the window to see changes.
</p>
<div id="debug"></div>
<div id="resizable-container" class="img-container">
    <picture>
        <source media="(min-width: 1265px)" srcset="https://dummyimage.com/1200x600/eeeeee/999999">
        <source media="(min-width: 1060px)" srcset="https://dummyimage.com/1000x500/eeeeee/999999">
        <source media="(min-width: 860px)" srcset="https://dummyimage.com/800x400/eeeeee/999999">
        <source media="(min-width: 650px)" srcset="https://dummyimage.com/600x300/eeeeee/999999">
        <source media="(min-width: 350px)" srcset="https://dummyimage.com/300x150/eeeeee/999999">
        <img src="https://dummyimage.com/200x100/eeeeee/999999" alt="dummy image">
    </picture>
</div>
<button id="reset-img-container">คืนค่าเดิมของขนาด container. Reset container size.</button>
<p>
    Thanks dummy image to <a href="https://dummyimage.com/" target="_blank">dummyimage.com</a>
</p>
<p>
    ดูในแบบที่ใช้แท็ก <a href="https://codepen.io/ve3/full/XWVYeLE" target="_blank"><code>img</code></a>.<br>
    View responsive image that use <a href="https://codepen.io/ve3/full/XWVYeLE" target="_blank"><code>img</code></a> tag.
</p>

/* make <img> responsive size but max width 100% not larger. */
img {
    height: auto;
    max-width: 100%;
}

#debug {
    margin: 0 0 20px;
}

.img-container {
    background-color: rgba(255, 255, 0, .1);
    border: 3px dashed #ccc;
    margin: 0 0 20px;
    overflow: auto;
    padding: 10px;
    resize: both;
    text-align: center;
}
@media (min-width: 800px) {
    .img-container {
        background-color: rgba(255, 0, 0, .1);
    }
}
function debugSize() {
    let debugE = document.getElementById('debug');
    let imgContainerE = document.querySelector('.img-container');
    if (imgContainerE && debugE) {
        debugE.innerHTML = 'window size: ' + window.innerWidth + 'x' + window.innerHeight + ' pixels.<br>' +
            'container size: ' + imgContainerE.offsetWidth + 'x' + imgContainerE.offsetHeight + ' pixels.<br>';
    }
}


window.onload = () => {
    debugSize();

    // reset container.
    let resetContainerBtn = document.getElementById('reset-img-container');
    if (resetContainerBtn) {
        resetContainerBtn.addEventListener('click', (e) => {
            e.preventDefault();
            let imgContainerE = document.querySelector('.img-container');
            imgContainerE.style = '';
            debugSize();
        });
    }
    // end reset container.

    setInterval(function() {
        debugSize();
    }, 2000);
}

window.onresize = (e) => {
    debugSize();
}

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.