Creating Responsive Images with Bootstrap at Specific Breakpoints

As a newcomer to Bootstrap, I am embarking on my first website creation experience. The header of my site is designed to look like the image below:

https://i.sstatic.net/WTp2g.jpg

I want this image to adjust responsively on medium-sized and larger devices, but on smaller screens, I require it to be cropped from the center, resembling the one shown in this image:

https://i.sstatic.net/GeYa3.jpg

Despite my attempts, I have not been successful in achieving this effect. How can I go about doing this?


Below is the code I have implemented. While it functions to some extent, the visual outcome is less than desirable.

<div class="row">
    <div class="col" id="header-image">
      <img src="images/head.jpg" class="img-fluid" style="z-index: -1; visibility: hidden">
    </div>
</div>

<style type="text/css">
@media only screen and (max-width: 768px) {
  #header image{
    background-position: center !important;
    background-size: auto 100% !important;
    height: 217px;
  }
}
#header #header-image{
   background-image: url("images/head.jpg");
   background-size: 100% auto ;
   background-position: center;
}

Answer №1

To achieve a cropped image that maintains its ratio, apply the CSS property object-fit:cover to your image. This technique is effective for creating responsive designs.

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

As soon as I inserted the image, the text vanished into thin air

The phrase "welcome" is not displaying after I included the av tag <!DOCTYPE html> <html> <style> @font-face { font-family: OpenSans; src: url(OpenSans-Bold.ttf); } * { ...

What is the best way to animate a three.js object to the right using GSAP without obstructing the surrounding HTML text?

As a newcomer to web design, I seek forgiveness if my question appears basic. I am working on an animated object in three.js that I want to move to the left side of the screen using GSAP. Additionally, I have a text "Hello" on the left side of the screen. ...

The compatibility of Datatables responsive feature with ajax calls appears to be limited

I recently started using the datatables plugin and have encountered an issue with responsive tables. While I successfully implemented a responsive table and an AJAX call on a previous page, I am facing difficulties with it on a new page for unknown reasons ...

Why isn't my function being triggered by the Click event?

I can't figure out why the click event isn't triggering the btn() function. function btn() { var radio = document.getElementsByTagName("input"); for (var i = 0; i > radio.length; i++){ if (radio[i].checked){ alert(radio[i].value); } ...

Yii2 Gridview - Horizontal Scroll Bar at the Top of the Page

I found a helpful post on Stack Overflow about creating a Yii2 Gridview with a fixed first column (Yii2 : Gridview with fixed first column), and now I'm looking to add a horizontal scrollbar at the top of the grid. My users really appreciate being ab ...

As my character slides off the moving platform in this exciting Javascript canvas game, my heart

Can anyone help me figure out how to keep my player on the moving platform? I'm not sure if I need to add gravity or something else. I'm still learning the ropes. export function checkTopCollision({ item1, item2 }) { return ( item1.y + item ...

Stop closing the bootstrap modal popup when the space key is pressed

Is there a way to prevent the model popup from closing when the space or enter key is pressed on the keyboard? I have already tried using data-backdrop="static" data-keyboard="false" but it still closes. Additionally, I have ensured that the form tag is no ...

Using JQuery to Execute Matching Based on Text Within <A> Elements

After reviewing the resources on Jquery Extract URL from Text and jquery match() variable interpolation - complex regexes, I am still a bit confused. The issue at hand is that I have a webpage with a dropdown menu at the top. Inside the dropdown, there ...

Is it advisable for a component to handle the states of its sub-components within the ngrx/store framework?

I am currently grappling with the best strategy for managing state in my application. Specifically, whether it makes sense for the parent component to handle the state for two subcomponents. For instance: <div> <subcomponent-one> *ngIf=&qu ...

Enable form submission using the ENTER key when the focus is outside of an input field

Within my form, I have a div element that is focusable using tabindex="0". While this setup works well overall, I am facing an issue with submitting the form using the enter key. The submission works when an input field is in focus but not when the div has ...

Tips for customization: Altering HTML table borders and column widths

I am looking to update the appearance of an HTML table. Currently, it looks like this: https://i.sstatic.net/pueyW.png Here is the code snippet: <div className="student-schedule"> <table className="student-calendar-table" ...

Combining color and typography classes using Tailwind merge is currently not supported

In my custom styling setup, I have created a custom color class called text-green (colors) and a custom typography class called text-card-highlight (utilities), which includes font size and weight attributes. However, when using tailwind-merge, only one of ...

When a div element is clicked, it becomes the selected element and borders are displayed around it

Within a div on my wordpress site, the logo is prominently displayed. Feel free to check it out here: Strange issue - whenever I click on the logo, a large rectangular box suddenly appears. I've experimented with the outline and user-select properti ...

JSON retrieved images are failing to appear in a grid layout

Hey there, I'm facing an issue with the layout of images on my website. I am fetching data from a JSON file to create a grid of images, but unfortunately, all the images are displaying in a single column instead of a grid. I am aiming for a 4 wide ima ...

What is the best way to horizontally align an inline div using CSS?

Is it possible to apply CSS to center an inline div that has an unknown width? Note: The width of the div cannot be specified beforehand. ...

Picture not adapting correctly to various screen sizes

I'm looking to adjust the size of an image based on the user's browser window. Here is the code I have so far: .image { max-height: 15%; width: auto; } The image class is used in this section of my code: <ul> <li> <img c ...

HTML row with interactive functionality that sends data via POST method to another PHP file

I am currently working on an HTML table where I am displaying data from a database. My goal is to make the entire row clickable so that I can trigger a PHP script using the data from that specific row as variables. However, I am facing a challenge in figur ...

Tips for incorporating a multimedia HTML/JavaScript application within C++ programming

I possess the source code for a JavaScript/HTML5 application that operates on the client-side and manages the transmission/reception of audio and video data streams to/from a server. My objective is to develop a C++ application that fully integrates the c ...

CSS-only checkboxes are not functioning properly in WordPress

I discovered some amazing pure CSS checkboxes that I wanted to implement (http://codepen.io/imohkay/pen/zFwec) on a WordPress website utilizing the Contact Form 7 plugin. However, after selecting these checkboxes, nothing seems to happen. To resolve this ...

Using the on-change event with a textfield can help ensure that the field is cleared if the min-date condition is not met

I recently encountered an issue with an input field that had the type "datetime" and a min-date condition set to today's date. The flow was such that if a valid date was entered, the submit button would be enabled, otherwise it would remain disabled. ...