What is the Best Way to Apply a Mask or Clip to an Image Using CSS?

Current Scenario:

I am working within the Bootstrap framework, meaning that everything needs to be responsive to fit within the column container.

I have images that are in a 16:9 ratio, but I need them clipped to be around 40:27 while still being responsive as per the requirements above.

After researching clipping and masking options, it seems that neither will work without customizing the code based on the site's width. However, since I aim for a completely fluid site, this is not a viable option.

Does anyone have any suggestions?

Additional Information: The source images are always 1920x1080.

Update: I have created a fiddle with my concept at http://jsfiddle.net/sckdd6hq/1/. My idea was to use standin.png to define the container size, so the background image should resize to fit within it. Now, the question is how can I adjust the background size to the correct height and center it within the resulting container?

Here is the HTML code:

<div class="container">
<div class="flexcon>
<img src="http://s28.postimg.org/l0hz0do5p/standin.png" />
</div>
</div>

And here is the CSS:

.flexcon { 
    background-image: url("http://s1.postimg.org/ffw8n4yjz/Google_Logo_Loop_GIF_01.gif");
    background-size: 100% 100%;
}

.container { 
width: 400px;
}

Answer №1

It might be challenging to achieve this with CSS clip, and honestly, I'm not entirely convinced that there is a completely reliable pure CSS method to accomplish it. However, here is a close alternative that you can try.

HTML

<div class="wrapper">
    <div class="dummycell"></div>
    <div class="content">
        <img src="http://s1.postimg.org/ffw8n4yjz/Google_Logo_Loop_GIF_01.gif" />
    </div>
</div>

CSS

.wrapper {
    width: 100%;
    display: inline-block;
    position: relative;
}
.dummycell {
    padding-top: 6.75%;
    /* Maintain 40:27 aspect ratio */
}
.content {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
}
.content img {
    width:100%;
    height:auto;
}

Check out the Fiddle here

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

What are some effective ways to implement a real-time tracking system for shipments using Angular JS?

https://i.sstatic.net/Bg0Gy.png I am currently working on developing a shipment tracker using Angular, HTML5, and CSS3. Does anyone have any suggestions on how to dynamically create the shipment tracker using AngularJS? My initial plan is to incorporate ...

Move your cursor over the top and bottom of the image to experience distinct effects

Trying to create an effect where the top 50% of an image shows a different color and text when hovered over, and same for the bottom 50%. Currently, only able to achieve this effect for the lower half of the image. Is there a way to make it work for both h ...

iPhone - touchstart event not functioning in JavaScript

Looking for a solution to dynamically resize a div in JavaScript when clicking on a link element (<a>). However, the code doesn't seem to function properly on my iPhone. Let's start with a basic link: <a href="#" onfocus="menu()">ME ...

How can the printing of content be adjusted when the browser zoom function is activated?

Is there a way to prevent the content from zooming when printing while the browser is zoomed in? The goal is for the printing (using iframe) to remain unchanged even if the browser is zoomed. I attempted: document.body.style.transformOrigin = 'top le ...

Adjust the code to enhance the functionality of web components in Sharepoint

I recently came across some code online that I'm trying to modify in order to add an expanding button next to a web part on my Sharepoint site. However, the problem is that by default, all web parts are already expanded and require a click to collapse ...

What is the best way to move the numerical range value into a span element?

How can I implement the "update" function to retrieve the current slider value and transfer it to a Meter element with the message "Value: [current value]". The indicator color should be #ffff00 if the current value is at least 85, otherwise, it should b ...

Customizing a responsive background image within a div using Bootstrap 3

Is there a way to style the background image of my header div like the one featured on Mr. Bill Gates' website at ? I noticed that the height of Mr. Bill Gates' header background image remains consistent, even on smaller screens. I've atte ...

Implementing a tooltip popup inside the header cell of the ordering table

Apologies in advance for my lack of experience with coding more complex website features. I'm attempting to incorporate a tooltip popup into one of the header cells to provide additional information to users. While I have all the necessary component ...

Layering an object on top of a clone using CSS

I am working on a webpage with a jQuery menu located at the following link: Menu To accommodate more items and have greater control, I duplicated the menu twice. However, when hovering over them, the duplication is visible in the background. Is there a ...

Having difficulty with setting up the Webpack css-loader, encountering an error during compilation

As a newcomer to webpack, I have been experimenting with creating my own build by modifying an existing one. One issue I encountered was the css not compiling, so I took the following steps: Confirmed that there were no css loaders already included in th ...

What could be causing the span class data to not be retrieved by the getText method

Looking to retrieve the value of a span class called 'spacer-right big project-card-measure-secondary-info' <span class="spacer-right big project-card-measure-secondary-info">1</span> snippet of code: browser.waitForEl ...

Something is off with the CSS height property

I am facing an issue with setting the height of a div inside another div. To better illustrate my problem, here is the code snippet: .prodContent1 { position: absolute; background-color: #e1e1e1; border: 1px solid #ddd; width: 100%; box-sizi ...

What is the best way to include fixed text before a value?

Hey there, I'm working on a project where the output value changes based on the selection made. I'd like to display the text "Total €" in front of the value like this: . However, the current output looks like this: . Additionally, I want t ...

JavaScript script to modify the parameter 'top' upon clicking

Here is the pen I've made. HTML <div class = 'cc'> <div class = 'bb'><div class = 'aa'> Some word </div></div> </div> CSS .cc { width: 100%; min-height: 90px; margin: 0; ...

What is the best way to center a progress bar vertically using Twitter Bootstrap?

I'm currently utilizing the progress bar feature in twitter-bootstrap. My goal is to align it vertically to match the appearance in the following image: While I came across this discussion thread, it seems to be outdated. As a workaround, I have im ...

Can a line be created using only CSS without the need for an additional HTML element?

There have been many inquiries regarding drawing lines without adding an additional HTML element. Consider this scenario with an HTML div element: <div class="myDiv"></div> Accompanied by the following CSS: .myDiv{ width: 100px; height: ...

Internet Explorer experiencing printing issues

Can anyone explain why IE (8,9 Tested) is refusing to print the right side of this coupon on the red background? The left side (phone number) prints fine. Other browsers like Chrome and Firefox print the whole document correctly. To see the bug, you can ...

Ensuring the aspect ratio of images stays consistent within a flexbox using CSS

How can I create a grid of images without any spacing in between them? I want the width to be 100% of the parent element and each column to have the same size, for example, 20%. Using flex takes care of the columns, but I'm struggling with making the ...

The Navbar in Bootstrap 3 remains expanded and does not collapse

It seems like there's a simple solution I'm overlooking. When I resize my screen, the navbar collapse toggle stops working. I've searched various forums but couldn't find a fix that works for me. Could someone lend a hand in identifyin ...

Tips for integrating Less and Bootstrap in an Angular 6 project

After making changes to angular.json to use less as the styleext, I tested it by creating a component. However, now I want to incorporate Bootstrap classes into my component's css/less. I specifically want all buttons in my component to have the .mx-1 ...