Tips for replicating background-image appearance while ensuring compatibility with printing requirements

Is there a way to crop images horizontally using CSS, while keeping the images visible during printing? I am currently utilizing background-image to crop images to a width of 200px without knowing the image widths in advance. My goal is to have the middle of the image displayed.

 .cropped-image {
    width: 200px;
    height: 270px;

    /* Centered, cropped image */
    background-position: center;
    background-repeat: no-repeat;
}

This is how my HTML template is structured:

    <div class="cropped-image" style="background-image: url({{ product.image_thumb_url }})">
    </div>

An example showcasing this functionality can be viewed here: http://jsfiddle.net/hRSdY/

While this method works effectively, default web browsers do not print background images. It is possible to use list-style-image as an alternative to emulate background-image, however, cropping to the center of the image becomes challenging: http://jsfiddle.net/KP7ng/1/.

Answer №1

If the images have arbitrary dimensions, CSS alone cannot reposition them.

To achieve this, javascript must be used to adjust their position after they are loaded.


An example using jQuery would look like:

$(window).load(function(){
    $('.cropped-image img').each(function(){
        var self = $(this);
        self.css({
            marginLeft: (-this.width/2),
            marginTop: (-this.height/2),
            visibility: 'visible' /*set to visible once loaded and repositioned*/
        })
    });
});

The HTML structure should be:

<div class="cropped-image">
   <img src="{{ product.image_thumb_url }}"/>
</div>

And the necessary CSS:

.cropped-image {
    width: 200px;
    height: 270px;
    position:relative;
    overflow:hidden;
}
.cropped-image img{
    left:50%;
    top:50%;
    position:absolute;
    visibility:hidden; /*set to hidden until the page is loaded to avoid moving images*/
}

You can see a demo at http://jsfiddle.net/gaby/3Yg7V/

Answer №2

Is this the kind of thing you were looking for?

http://example.com/abcd123

Here is the HTML code snippet:

<section class="box">
<img src="https://example-image.jpg" />
</section>

CSS styling for the section:

section.box {
    height: 100px;
    border: 2px solid blue;
    overflow: hidden;
}

section.box img {
    margin-left: -10%;
}
​

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

Automatic page updates triggered by code modifications

I am currently working on a .NET ASPX page where I usually write my classes separately in the app_code folder. However, for this specific page, I needed a temporary class that is only required by this page. So instead of creating a separate file, I decid ...

Is there a method in AngularJS to submit form data when the input fields are pre-populated using a GET request?

How do I submit form data in AngularJS? I have a div that populates the input field from a GET request. Now, I want to send this data using a POST method. How can I achieve this? Below is the form div: <div class="row" ng-app="myApp" ng-controller="myC ...

Add PHP functionality to insert a jQuery array of select2 tags

After browsing numerous threads on various platforms regarding this issue, I have yet to find a solution. Therefore, I am reaching out for help once again. I am currently utilizing the select2 jQuery plugin and aiming to insert tags into an SQL database us ...

Google Chrome Back Button not saving radio button selections

We have noticed an issue with Google Chrome wherein the back button does not retain radio box selections when submitting a form that contains all radio boxes. This seems to be specific to Chrome and is not observed in other browsers like IE and Firefox. B ...

Table positioned with a slight gap between its edges and the browser border

I am facing an issue with a table that extends beyond the browser edges, and I am unsure how to fix this. Here is the code snippet for the table: It's important to note that I am using PHP. function create_Chat_Table($text){ $pseudo=$_GET['D ...

Display a progress bar in an application to visualize the loading of database records

Within my application, I am accepting a numerical input that is used to generate results on a separate page. The process involves running multiple sequential JDBC queries against Oracle database tables, resulting in a significant delay (1-2 minutes) for th ...

HTML: What is the functionality of the reset button?

What is the inner workings of the reset button (input type="reset)? (I am interested in enhancing it to clear inputs after a post in ASP.NET MVC.) ...

The webpage functions properly when viewed locally, but only displays the background color when accessed online

My friend created a website using Microsoft Publisher a while back, and upon revisiting it recently, discovered that it doesn't load properly on the latest versions of Chrome and Firefox. When viewed locally, everything appears to work fine, but onlin ...

Incorporating pre-designed elements into the bottom section of my

I'm currently facing an issue while trying to integrate a footer content from a template into my slideout footer. The problem is that the template footer is currently spanning across the entire width of the page, and I am struggling to contain it with ...

Storing information in a database

Need help troubleshooting why my form isn't saving data to the database. <div class="container"> <div class="row" style="margin-top:200px;"> <form ENCTYPE="multipart/form-data" ACTION="upload.php" METHO ...

Mobile Devices Image Gallery Optimization

Just starting out with HTML, CSS, Bootstrap, and JavaScript and I've put together this gallery. It looks good, but I need to optimize it for mobile devices and smaller screens. Any suggestions on what I should change? For items #myImg4, #myImg5, #myI ...

What is the correct method to properly encode JSON that may include HTML when displaying it in an HTML file?

After searching through numerous questions, none seem to address this specific scenario. app.get('/', function(req, res) { res.set('Content-Type', 'text/html'); res.send(`<html> <body> Hello, World! </body&g ...

Showcasing Portfolio Work in a User-Friendly Mobile Design

Currently revamping my portfolio website and looking for ways to optimize the display of my personal projects. I have a card-like interface in place that works well on desktop but only shows one project at a time on mobile devices. Seeking solutions to imp ...

CSS: Creating a dynamic layout to center the card on screens of all sizes

My code currently places the profile card in the center of the screen regardless of screen size, but it often appears too small. How can I adjust the positioning of the profile card to almost fill the screen? @import url("https://fonts.googleapis.com/cs ...

Tips for transferring PHP results to a database

I am facing an issue with writing data to a database. I need to store the first integer, second integer, and the result in the database. However, despite my attempts at coding, only the first and second integers are being written, not the actual result. A ...

Stop iOS Safari from automatically capitalizing the initial letter in input fields

My web app has a username field that is case sensitive. Everything works smoothly in most cases, but there seems to be an issue with Safari on iOS and occasionally Chrome on Android. These browsers automatically capitalize the first letter of any text ente ...

Animation issue in Material UI autocomplete label feature

Hello, I am currently delving into the world of React and Material UI. I have been working on implementing the Material UI auto complete feature with chip functionality. You can see my progress here: https://codesandbox.io/s/runh6. Everything seems to be w ...

Utilizing CSS nth-child on dynamically generated HTML in Angular

Ensuring that the columns are floated to the left without large gaps between rows is my current challenge. Here's the HTML structure I'm working with: <div class="row hide-for-small"> <div ng-repeat="module in modules"> ...

"Troubleshooting a Placement Concern within the ExpressionEngine Content Management System

I am facing a positioning problem with my ExpressionEngine. If you click here, you will see that I am looping elements within the <div id="services-container">. The loop works fine on the first row, but misaligns on the second row. I have tried using ...

A step-by-step guide on showcasing database data on an HTML page using jQuery

I am currently using jQuery to make an HTTP GET request and fetch JSON data from a database. After executing the request, I received the JSON array successfully. Now, I am attempting to display this JSON data on an HTML page by using the jQuery $(newData ...