EDIT:
To say it simply, you can't.
Removing/hiding an image element must be done in CSS with media queries, or with Javascript.
The srcset
and sizes
tags are useful for choosing the content source of an image element, but it cannot control the existence or visibility of the element.
The srcset
and sizes
tags are intended to augment responsive CSS. Their values should follow whatever breakpoints are defined in CSS.
srcset
srcset
is a list of available images the browser can choose from with their respective widths.
According to the latest spec, it will choose only from that list when populated:
For backwards compatibility, one of the URLs is specified in the img
element's src
attribute. In new user agents, the src
attribute is ignored when the srcset
attribute uses w
descriptors.
Therefore, it sees 1024.jpg
as the only choice and ignores default.jpg
.
Add the default image to srcset
(with the correct w
descriptor - here I assume default.jpg is 768px wide):
<img
src="default.jpg"
srcset="default.jpg 768w, img/1024.jpg 1024w"
sizes="(min-width: 768px) 768px, 100vw"
/>
sizes
sizes
tells the browser how wide the image will be when a given media query is true. This helps the browser calculate which image to pick from srcset
.
Currently, sizes="(min-width: 768px) 768px, 100vw"
is telling the browser:
"The image will be 768px wide if the viewport is larger than 768px, otherwise the image will be full width when the viewport is less than 768px."
I assume you don't want to use a 1024px image when the viewport is less than 768px.
To hint at a small image when the viewport is less than 768px, use max-width: 768px
instead:
<img
src="default.jpg"
srcset="default.jpg 768w, img/1024.jpg 1024w"
sizes="(max-width: 768px) 768px, 100vw"
/>