First, the image is reduced by width until it reaches its maximum width, then the height is adjusted proportion

I have an image with the dimensions of 1200 x 200px. In the center of this image, there is a space measuring 500 x 200px which represents the main content of the entire image. Flanking this main content are additional contents on each side. It is important to note that all these elements exist within a single image.

When the window width is reduced, the first thing to be cut off should be the additional content, while maintaining the height of the image. However, once the window width is reduced below the width of the main content (resulting in the cutting off of all additional content), the image should then be resized proportionally, affecting its height as well.

My questions:

  1. Is it possible to achieve this using only CSS?
  2. If not, are there any JS libraries available for this purpose?
  3. If not, how should I structure the HTML, CSS, and JS to make it work?

Example of a full-width banner: Take note that there is a main content area surrounded by additional content on either side.

https://i.sstatic.net/csRha.png

The following image should help illustrate: I have attempted to simulate a resizing of the window width from 1200px to 1000px and eventually down to 500px (without affecting the height), followed by a further reduction to 350px where the image is resized proportionally.

https://i.sstatic.net/KSHEZ.png

Answer №1

#banner {
  background-image: url("https://i.sstatic.net/csRha.png");
  width: 100%;
  height: 200px;
  background-repeat: no-repeat;
  margin: 0 auto;
  display: block;
  border: 1px solid red;
}

@media all and (min-width: 1200px) {
  #banner {
    width: 1200px;
  }
}

@media all and (max-width: 1200px) {
  #banner {
    background-position: 50% 0;
  }
}

@media all and (max-width: 500px) {
  #banner {
    background-size: 240%;
  }
}
<div id="banner"></div>

Answer №2

Can this be achieved using only CSS?

Absolutely! You can accomplish this with just one <div>. Simply treat the image as a background image and position it perfectly in the center using background-position. Then, adjust the size of the <div> using media queries by setting specific widths at breakpoints.

Answer №3

If you want to adjust the size of an image based on the screen width, consider using a basic media query such as this:

@media max-width: 500px {
  #your-image {
    width: 100vw;
  }
}

Answer №4

Implement media queries to incorporate images/elements and modify CSS styles. To address your issue with image resizing, consider using percentages. I recommend researching progressive enhancement and Responsive design principles. Additionally, familiarize yourself with the view-port if you haven't already. Avoid utilizing VW units as they lack sufficient support in browsers like IE, EDGE (no VMAX support), Opera Mini, and IE8 (no support at all).

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

Having difficulty breaking down values from an object

Attempting to destructure the data object using Next.js on the client side Upon logging the data object, I receive the following: requestId: '1660672989767.IZxP9g', confidence: {…}, meta: {…}, visitorFound: true, visitorId: 'X9uY7PQTANO ...

How can I implement a jQuery modal dialog that loads a form page with pre-existing jQuery code for Autocomplete functionality?

I am facing an issue where I have a dynamically generated form on a web page and I want to display it using the jQuery UI modal dialog. Previously, I was suggested a solution that worked when my form did not already contain jQuery UI components like Autoc ...

Adjust the image size in the jumbotron to fit perfectly without the need for scrolling

I'm in the process of creating a website with a prominent jumbotron section on the landing page. I am looking to adjust the height of the image within the jumbotron without cutting off any parts of the picture, and I do not want it to scroll to displa ...

Display a notification next to the pinned location on the map using the Google Maps API

I'm trying to get this popup to show up close to the marker. Can anyone help me figure out how to do it? Here is my current setup: .html <agm-map [latitude]="lat" [longitude]="long" style="height: 350px;" [fitBounds]="true"> <agm-marker ...

What is the best way to implement dynamic variables in ng-model-options?

I am experiencing an issue with the ng-model-options not updating as expected. For instance, if you input 4:00 pm in both time fields in the snippet below, you'll notice that the UTC output differs - the first is 6 and the second is 8. This behavior i ...

Is there a way to narrow down Drive API list results based on a specific domain that has write permission?

I am currently working on retrieving a list of files from the drive API using a service account, with permissions granted to a specific domain for editing. While I have successfully implemented this feature for individual emails, I am facing a challenge in ...

Creating an HTML list based on the content of a textarea input

Looking for some guidance in creating a dynamic list using JavaScript. The goal is to have user input data into a textarea, and upon clicking the submit button, generate a list in HTML. HTML: <body> <div class="container"> <di ...

How do I retrieve and pass values from multiple inputs in my React application?

I've implemented the <autocomplete> as an input field, and I'm currently troubleshooting my submit method by logging values to the console. Unfortunately, I can only submit empty data to my axios instance without any Traceback errors for gu ...

json undefined result or results when viewing in MVC

After passing the LINQ result to my dropdown list, I am seeing "undefined" displayed. The issue could be because I didn't specify a specific class after "select new," although that is how I need it. var newlist_of_device_types = (from z in DB.t_d ...

`How can I extract HTMLElements from slots in vue3?`

When attempting to develop a Layer component, I encountered some challenges. Here is the code: // Wrapper.vue <template> <slot v-bind="attrs"></slot> </template> <script lang="ts" setup> import { defi ...

The Ajax deletion request yields a 200 response code; however, it triggers an error instead of the

Hi, I am struggling to identify where I went wrong with the delete function. The function itself works fine, but I keep getting an error message even though the JSON response shows success. $(document).on('click', '.remove-btn', functio ...

What steps are needed to set up my Express server so that it can render all my content, rather than just my HTML file?

I need help setting up a server for my project using Express. Here is the structure of my file directory: root-directory ├── public │ ├── css │ │ └── styles.css │ ├── images │ │ └── img1.png | | └ ...

PHP code to launch a pop-up on a remote computer

Let me start by explaining the concept of Flow. Imagine UserX is online in country X, and UserY is online in country Y. ---------------- Here's what I want: When UserX clicks on an ajax button, a popup opens on UserY's PC. --- ...

Utilizing Node.js callback for validating JWT tokens

In my Node.js server, I have set up an authentication route to authenticate requests: app.get('/loggedin', auth, function(req, res){ console.log(req.authenticated); res.send(req.authenticated ? req.authenticated: false) }) From what I u ...

Is there any advantage to placing buttons within an HTML form?

What benefits are there to placing a button within an HTML form as opposed to directly in the body? <form id="viewForm" method="post"> <input id="editBtn" type="button" value="Edit" /> </form> ...

Trouble with CSS display in Internet Explorer 8

My website located at this link is working perfectly in IE 10 and IE 11, however, any version lower than that is causing the content to be displayed on the far left of the screen instead of centering it. I have tried tweaking the CSS but can't seem to ...

Looking to retrieve object values in JavaScript in a dynamic manner?

Imagine having an array of objects with nested objects. The data is always changing, meaning the keys may vary each time. For instance: [ { "uuid": "53e7202c-28c8-4083-b910-a92c946a7626", "extraIdentifiers": { "National ID": "NAT2804" }, ...

Discover the ultimate guide on developing a personalized file management system using the powerful node.js platform!

I have undertaken a rather ambitious project for myself. There exists a comprehensive tutorial on the website that outlines the process of establishing an online environment (resembling a user panel) where registered users can effortlessly upload various ...

Integrating fresh models into Django and creating dedicated pages for each

I have successfully set up a model in my Django project where I can create and display posts. However, when I created a new Model, I encountered issues replicating the same process. I tried setting up html files, modifying urls.py, and views.py similar to ...

Interpreting an undefined HTTP GET request within a Node.js server

I am encountering an issue within my Node.js application. When I send an http get request through an ajax call on the client-side, the server-side does not recognize the request data and returns an "undefined" error message. This problem is puzzling to me ...