What css attribute and value do I use to set image width to whatever the user's window width happens to be?

Progz

Dani AI

Generated

Good, concise direction from — scaling an image to its container is the right idea. For : if the goal is “make the image as wide as the user’s window” there are two common approaches. One sizes the image against the viewport itself (full-bleed). The other sizes it against a wrapper that stretches to the viewport (often safer in responsive layouts).

Viewport-based (full-bleed): use viewport units so the image fills the visible window. Note that viewport units can include scrollbar width in some browsers and may cause horizontal scrolling on certain layouts. When only width is set, the browser preserves the image’s aspect ratio automatically.

img.full-viewport {
  width: 100vw;
  display: block;
}

Container-based (preferred for most responsive pages): make a full-width wrapper and let the image fill that wrapper. This avoids the scrollbar issue and integrates better with grid/layout constraints. Combine this with responsive images so the browser downloads an appropriate file for the device:

<img
  src="large.jpg"
  srcset="small.jpg 640w, medium.jpg 1280w, large.jpg 1920w"
  sizes="100vw"
  alt="Descriptive text">

Practical tips: set images to display: block to remove inline-gap artifacts; avoid upscaling small source files (use larger assets or srcset to serve the right resolution); for decorative hero graphics consider background-image with background-size: cover; and test across devices and scrollbars. For in-depth guidance on responsive delivery and srcset/sizes, see the MDN Responsive images guide: Responsive images.

Recommended Answers

All 2 Replies

max-width: 100%;
height: auto;

Thanks. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.