CSS Reference: background-size
can stretch a background image proportionally using cover
or contain
. Check out this Demo.
HTML
<div class="square"></div>
<div class="rec"></div>
CSS
.rec, .square {
border: solid red 1px;
background-color:black;
background: url("https://placeholdit.imgix.net/~text?txtsize=23&txt=120×120&w=120&h=120");
background-size:contain;
background-repeat: no-repeat;
background-position: center;
}
.rec {
width:100px;
height:60px;
}
.square {
width:100px;
height:100px;
}
The remaining space with contain
will be transparent so just put it in a container with a background color set.
Related Posts:
- Limit image resolution on upload
- Setting JPEG compression for custom image sizes doesn’t work in specific cases
- get_the_post_thumbnail_url with an unregistered size
- Resizing only featured images while uploading
- Adding upload button in metabox
- How can I serve different images depending on screen size with wordpress
- Setting up a gallery with images and a zip download
- When inserting images into posts auto add a wrapping div
- Issues renaming images during upload
- how to base64 encode images in wordpress template
- get_children – wp_get_attachment_image
- Generate fixed image sizes for different pages, eliminate sizes during upload
- Setup A Default Featured Image
- Find posts without featured image? [duplicate]
- How do I add a featured image to a single post page?
- Custom image size not regenerating when image editted
- How can I autopopulate titles in the media library?
- How to disable the suffix “-scaled” which is being added at the end of each uploaded image?
- Image still linked as attachment to page even though it has been deleted
- Disable wordpress image sizes generation
- In media library images are not displaying run time [closed]
- Best option for photo/gallery handling?
- Frontend Post – Allow Only Image File Upload
- Add a new data attribute in tag
- File format of post image attachment
- SEO images in Nextgen Galleries
- Remove Size images without use
- Woocommerce featured image of page – not product
- Watermark images only for some users
- Can’t seem to attach uploaded image to post and set it as thumbnail
- Add an attribute to attachments
- Include caption underneath post images
- Domain Change with New Image Sizes
- Why no images on a fresh WordPress installation?
- How to prevent WordPress scaling large images down
- How to stop thumbnail generation from some images and different size thumbnail generate
- wordpress (woocommerce) add image to media library programmatically python
- How can I filter image HTML to add schema.org attributes?
- How to translate get_intermediate_image_sizes?
- Change media URL (images)
- Problem with images when no dimension in the URL [closed]
- some images not appearing on the website after migration [closed]
- Why does WordPress convert my jpg’s to png?
- How do I get my main image to stretch the full length of the screen?
- How to make my site use HTTPS for images or how to insert images as relative paths?
- HowTo: develop WP template with dynamic background image
- How can I use wp_save_image_editor_file filter?
- Featured Image or Post Thumbnail Displaying on index.php?
- How to set featured image from external url programmatically
- What did I do WRONG?
- Add retina “2x” in attribute “srcset”
- Capitilize Alt/Title Tag in Image HTML, Reorder HTML Output
- Change logo image condition based on url
- How to display a sized image from Simple Fields repeatable field
- Hiding Media Source Links
- Random Characters in the end of every image url in post
- resize and crop thumbnail photos
- Broken thumbnails (sometimes) with TimThumb
- Can i have 2 separate Galleries on 2 separate pages each go to a DIFFERENT Attachment.php file?
- Why is my table background image not showing in wordpress?
- Is it possible to save crop to a different size of the image when in image editor?
- How do you change the fetched image of a PDF uploaded into WordPress Media Library?
- Apply manual image cropping not to thumbnail but medium_large size
- Hide or mask the url to an image
- Trying to change the URL of a product image once I upload it, but there seems to be no way to access the URL
- WordPress Multisite: Images moved to /sites/1?
- How can I insert and display copyright owner field on featured-images, pictures and galleries in articles?
- Site only working in browser where I created it
- Image alt attribute
- wp_download_url() for dynamically generated image
- How to change img src in home-made theme, using WordPress` live customizer?
- Full size image from srcset ALWAYS used on mobile DESPITE setting size of image in editor
- Run function before uploaded image is saved
- Are WP images responsive by default?
- How to get attachements URL from author meta and display them?
- wordpress images not showing properly
- all images of past post messed up after changing theme
- image_send_to_editor filter – Unwanted paragraphs and line breaks
- Edit image itself
- Url link to feature image in the portfolio [closed]
- Where is this folder [closed]
- Different image heights for different resolutions without distortion
- Get the name of an image [duplicate]
- Get an array containing ONLY post IDs of attached images
- Exporting a portfolio with pictures
- Lots of attachments is causing slow load
- Image editor not working correctly
- Protocol relative url not pulling up images on IE, FF, and OPERA
- Making a thumbnail if there exist a link with. jpg or .png extension in WordPress
- Transfered live site to new server and images are not showing
- Limit files uploaded per folder
- Aviary + WordPress Media
- Issues with the excerpt (wordcount/HTML/images)
- Display image title and caption inside a single post
- WP is showing wrong image dimensions
- Bug when uploading pictures with Arabic name characters?
- Adding social share buttons for individual images on a web page
- How to get the attribues (alt and title) of an image import with ACF [closed]
- WordPress can’t get image url but both the attachment and the actual file are there
- How can I use an image from an external source without uploading it to the media library