Expand a section of the background picture

I am working with an image that contains multiple flag images:

https://i.stack.imgur.com/xg0iM.jpg

I have a div element with the dimensions: width:100px; height:50px. My goal is to resize each flag to fit inside it:

.flag{
      width:100px;
      height:50px;
      background-image: url('http://icons.iconarchive.com/icons/famfamfam/flag/icons-390.jpg');
      background-position: 30px 25px;
      background-size: 390px 260px; // actual size of image
    }
<div class="flag"></div>

Answer №1

To resize your flag from 16x11px to 100x50px, you will need to scale the image 6.25 times on the x axis and 4.54 times on the y axis. Adjust the background-size property accordingly:

background-size: calc( 390px * 6.25 ) calc ( 260px * 4.54 );

Don't forget to update the background-position as well. Here's an example for the first flag:

.flag {
  width: 100px;
  height: 50px;
  background-image: url('http://icons.iconarchive.com/icons/famfamfam/flag/icons-390.jpg');
  background-position: -38px -36px;
  background-size: calc(390px * 6.25) calc(260px * 4.5); // original image size
}
<div class="flag"></div>

Remember that stretched images may not look great. To improve rendering in FireFox and Chrome, consider using the image-rendering property:

.flag {
  width: 100px;
  height: 50px;
  background-image: url('http://icons.iconarchive.com/icons/famfamfam/flag/icons-390.jpg');
  background-position: -38px -36px;
  background-size: calc(390px * 6.25) calc(260px * 4.5);
  image-rendering: -moz-crisp-edges;
  image-rendering: pixelated;
}
<div class="flag"></div>

Answer №2

avoid repetition

<div class="flag"></div>


//Custom Styling
.flag{
   width:100px;
   height:50px;
   background-image: url('http://icons.iconarchive.com/icons/famfamfam/flag/icons-390.jpg');
   background-position-x: 30px;
   background-position-y: 25px;
   background-size: 390px 260px; // original image dimensions
   background-repeat: no-repeat;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Expansive picture feature within jQuery Mobile

I need help with displaying a large image (685 × 900, only 25kb in PNG) in the 'content' section so that it adjusts automatically for different screen sizes and allows users to zoom in using their fingers. Currently, I have this code: < ...

What could be causing my border to spill over into the adjacent div?

Trying to set up the contact section of my portfolio but running into an issue where the border of one div is overflowing into the next. Here's a snippet of the code: //CSS .contact-cont { padding: 4rem 12rem 0rem; height: 90vh; ...

Switch the div class when clicked, and revert it when the body is clicked

Allow me to elaborate on the issue: I currently have <div class="contact"> <div id="form"></div> <div id="icon"></div> </div> My goal is to change the class of .contact to .contactexpand (or simply ...

Prevent ng-repeat from rendering the contents of my div

Currently, I am in the process of modifying a website for which I need AngularJS. I am trying to use the ng-repeat command to display some documentation on the site. However, whenever I add the ng-repeat within a div, it seems to "destroy" the layout and I ...

Spacing vertically within a table cell

I'm currently experimenting with creating a vertical text-align: justify effect between divs. I am working with a structure that includes 4 div elements inside a td, like this: <td> <div></div> <div></div> <div ...

Having trouble with positioning divs on top of each other? Struggling to get position absolute to work correctly?

Currently, I am utilizing flexbox to construct a grid layout. However, I have hit a roadblock when attempting to make the divs stack on top of each other. The overflow:auto is hidden and position relative has been added to the carddiv. While the divs are s ...

Choosing the right framework for implementing push notifications can be a critical decision. Should

I am currently working on a Java application that requires the server to send push notifications to the client every one second. To achieve this, I am using HTML5 server-sent events for one-way communication from the server to the client. However, my conce ...

If TextBoxes overlap, the one that is defined earlier in the ASPX file cannot be clicked on

Managing the visibility of multiple controls can be tricky, especially when they have overlapping coordinates. Take for example two TextBoxes, txtName and txtEmail, positioned at the same x/y coordinates: <div style="position: absolute; top: 55px; ...

Issues with CSS transitions/animations not functioning properly across different browsers

Take a look at this FIDDLE Link. I'm facing an issue where the animation works smoothly in Firefox, but it's not functioning as expected in Chrome. Kindly open the fiddle in both browsers (Chrome and Firefox), hover over the image, and notice t ...

Adjust transparency according to the information extracted from the AnalyserNode

Currently, I am exploring ways to animate text opacity based on the volume of an audio file. While browsing online, I came across this specific codepen example, which showcases a similar concept. However, as I am relatively new to JavaScript, I find it ch ...

Issue with a Jquery toggleClass function not working properly on hover

Hello everyone! I am a newbie in the community and relatively new to the world of web programming. I am encountering an issue that is really frustrating me. I attempted to use this code to add a class when hovering over the ul list element so that I can d ...

Is there a way to upload numerous images from my local disk onto a canvas using Fabric.js?

I'm currently working on an innovative Image Collage application using the power of HTML5 canvas and Fabric.js. One of the key features I want to implement is the ability for users to simply drag and drop files into the designated 'File Drag and ...

How can I modify the parent form element to only evaluate the expression at the text node, without affecting Angular interpolation?

Currently, I am in the process of developing an eCommerce platform and utilizing angular to construct a widget on product detail pages. Unfortunately, my control over the initial HTML rendered for the browser is quite limited. While most tasks related to m ...

When attempting to open the HTML file through an Express application, the background image specified by `background-image: url()` may

Upon opening the html file by directly clicking on it, the background-image styled with url() functions correctly. However, when I open this file within an express app using res.sendFile(), the background-image fails to display. Interestingly, all other CS ...

Can a variable containing HTML code be iterated through with a loop?

I am currently working on setting up a select button that will receive options from an ajax call. The ajax call is functioning properly. However, I am facing a challenge when trying to create a variable containing the HTML. I am unsure of how to iterate a ...

What methods does the W3C CSS Validator use to recognize a CSS3 document?

Help Needed! Can someone tell me how to specify a CSS3 file for validation by the W3C? In HTML5, we use <!DOCTYPE html>, but what should we use in a CSS file? Whenever I try to validate my CSS file containing CSS3 elements like @font-face and box- ...

Converting Data from MySQL and PHP to Excel

Is there a way to export human resource applications stored in my MySQL table as an Excel file? function exportExcel($filename='ExportExcel',$columns=array(),$data=array(),$replaceDotCol=array()){ global $connect; header('Content-Enc ...

Bootstrap - the input group button is designed to align to the right side

Currently, I am in the process of constructing a navigation bar for a website that includes a search bar and a grouped button. Everything functions properly when viewed on a desktop, but as soon as I switch to mobile view and the collapsible menu is activa ...

Using jQuery to modify CSS attributes is proving to be more challenging than I anticipated

I am attempting to modify a CSS attribute using jQuery. I have used the following code: wrapperInner.css('overflow', 'visible'); However, the value remains unchanged. When I use an alert alert(wrapperInner.css('overflow')), ...

What events precede and follow a keydown action in a Textarea field?

I need to prevent users from pressing function keys (F1, F2, etc.), the tab key, and any other characters from being added. The code below is supposed to achieve this on my website but it's not working. document.getElementById("code").addEventList ...