Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stretch background image of a table cell with CSS?

How can I stretch background image of a table cell with CSS? I have Googled it with no results.

When you set background image of a cell and the background size is smaller than the cell, then it will be repeated.

How can I force this background to stretch the entire table cell instead?

like image 812
SilverLight Avatar asked Aug 02 '10 13:08

SilverLight


People also ask

How do I stretch a background image in CSS?

You can use the CSS background-size: cover; to stretch and scale an image in the background with CSS only. This scales the image as large as possible in such a way that the background area is completely covered by the background image, while preserving its intrinsic aspect ratio.

How can we change background image in a table cell?

In the table cell tag <td> where you would like to place the background image, paste the image attributes and values. Change the src= attribute name to the background= attribute.

How do I resize an image in HTML table?

One of the simplest ways to resize an image in the HTML is using the height and width attributes on the img tag. These values specify the height and width of the image element. The values are set in px i.e. CSS pixels.


1 Answers

It is possible to stretch a background image using the background-size CSS property.

Examples:

body { background-size: 100% auto } /* Stretches image to full horiz. width */
body { background-size: 50% 50% } /* Stretches image to half of available
width and height - WARNING: aspect ratio not maintained */

body { background-size: cover } /* Ensures that image covers full area */
body { background-size: contain } /* Ensures that image is completely visible */

All major current browsers support the feature:

  • IE since 9.0
  • Chrome since 3.0
  • Firefox since 4.0
  • Opera since 10.0
  • Safari since 4.1
like image 123
Pekka Avatar answered Sep 19 '22 15:09

Pekka