Carousel position images off-center to the right on smaller screens

I created a fullscreen slider using the carousel bootstrap feature. However, when the screen size shrinks, the background image resizes and centers itself. Instead of centering the image on smaller screens, I want it to focus on the right side.

Here is how the background image appears on a normal screen (fullscreen slider): Normal Screen

And this is how the background image appears on a smaller screen (still fullscreen): Smaller Screen

This is the CSS code I have used:

@media (max-width: 768px) {      
  .carousel-inner>.item{
        position: relative;
    }
    .carousel-inner>.item>img{
        position: absolute;
        top: 0;
        right: 0;
    }
}
<div class="carousel-inner">
<div class="item active">
<img id="h-1" src="Assets/1.jpg" alt="" />
<div class="container">
  <div class="carousel-caption">
    <h1>C</h1>
    <hr>
    <ul class="descslider">
  <li>asd</li>
  <li>qwe</li>
  <li>zxcv</li>
    </ul>
  </div>
</div>
</div>

<div class="item">
<img id="h-2" src="Assets/2.jpg" alt="" />
<div class="container">
  <div class="carousel-caption">
    <h1>C</h1>
    <hr>
    <ul class="descslider">
  <li>asd</li>
  <li>qwe</li>
  <li>zxcv</li>
    </ul>
  </div>
</div>
</div>

<div class="item">
<img id="h-3" src="Assets/3.jpg" alt="" />
<div class="container">
  <div class="carousel-caption">
    <h1>C</h1>
    <hr>
    <ul class="descslider">
  <li>asd</li>
  <li>qwe</li>
  <li>zxcv</li>
    </ul>
  </div>
</div>
</div>
</div>

I have attempted to implement the above code, but it did not achieve the desired effect. Any assistance would be greatly appreciated. Thank you :)

Answer №1

Now that I understand your needs, you're looking for the resizing of an image to be synchronized with the image on the right at a specific breakpoint when the window is resized. Bootstrap offers classes such as pull-right and pull-left for this purpose.

To achieve this, I have utilized jQuery to dynamically apply these classes when the viewport size is less than 768px, along with some additional custom CSS.

Here is the JavaScript code:

$(window).on('resize', function() {
    if($(window).height() > 768) {
        $('img').addClass('pull-right');
        $('.carousel-caption').addClass('pull-left');
    } else {
        $('img').removeClass('pull-right');
        $('.carousel-caption').removeClass('pull-left');
    }
})

And here is the CSS code:

@media (max-width: 768px) {      
  .carousel-inner >.item > img {
      max-width: 40%;
    }
   .carousel-caption {
      max-width: 40%;
   }
}

You have the flexibility to adjust the percentage values in the CSS to fine-tune the width of each element according to your preferences.

Feel free to check out the updated CodePen Demo

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

It is necessary to render React Native text strings within a text component

Greetings! The React Native code snippet below is responsible for rendering a user interface. However, upon running the code, an error occurred. How can I resolve this issue? The error message indicates that text strings must be rendered within a text comp ...

What is the best way to arrange an array using AngularJs or Javascript?

When a user makes a selection, I want to sort and display an array in alphabetical order. Specifically, when we render data from the backend, I would like to display the fullName in alphabetical order. The $scope.selectedControlOwner is the ng-click event ...

Using CSS/HTML5 with either the :target or :active selector can allow for the display of different text content when a

Is it possible to change the text displayed on a website without reloading the page when a link is clicked? For instance, if I have an "About" or "Contact" link on my personal website, can the text in the body of the page be switched dynamically? In the i ...

What is the best way to retrieve the second element based on its position using a class name in Jquery?

DisablePaginationButton("first"); The statement above successfully disables the first element that is fetched. DisablePaginationButton("second"); ===> not functioning function DisablePaginationButton(position) { $(".pagination a:" + position).ad ...

Navigating File Paths in Node.js

My directory structure is as follows: Main > models > file1.ejs | |> routes > file2.ejs In my code, I'm trying to require file1 from file2 like this: const variable = require("../models/file1.ejs). But what if I don't want to ...

Removing an object from an array if it does not exist in another array: A step-by-step

Looking to remove an object from an array if it's not present in another array. After doing some research, I came across a similar question on this link, but with a different array source. Below is the example from the link: var check = [1, 2, 3]; ...

jQuery sends ajax success to input type number

I am encountering an issue with the ajax success loading function when using input type number. Interestingly, when I switch the input type to text, it works perfectly fine. However, once I change the input type back to number, the loading ceases. <s ...

Uploading Files Using Ajax to a PHP Class

Having some trouble with my file upload using AJAX to post to an object-oriented PHP class. Here's the AJAX code: var handleUpload = function(event) { event.preventDefault(); event.stopPropagation(); var fileInput = document.getElement ...

The Intex XDK is experiencing difficulties with establishing an Ajax connection

Encountering an issue with connecting to a php page through Ajax on my phone. I've simplified the process as much as possible. Here is the server-side code: <?php echo(json_encode('success')); ?> And here is the client-side code: ...

Storing approximately 1 kilobyte of information throughout various pages

Is it feasible to store approximately 1kb of data while transitioning between two pages on the same domain using Javascript, Jquery (1.7), and Ajax? For instance, a user inputs data into a textbox on one page and then moves to another specific page. Can ...

Tips for Selecting the DIV Element using jQuery

I am faced with the challenge of integrating this particular set of div tags: <div id="target-share"> <a href="#" title="" id="to-chosen" class="default"><span class="to-admin">Admin</span></a> <ul id="select-shareto"> ...

Integrating redux-form with the react-widgets DateTimePicker component

I am currently working on a project using ReactJS with Redux and I am trying to incorporate a form similar to the one shown in this example: Most of the components are working well, but I am encountering an issue with the DateTimePicker due to the momentL ...

Difficulty with Bootstrap's media object

When commenting on my blog, the media object is used as shown here: . However, if the comment contains a large object, the media object ends up overlapping the sidebar like in this example: . Any suggestions on how to resolve this issue? Thank you! ...

Creating a JQuery statement to conditionally change CSS values

Is there a way to determine if a div element with a CSS class of "x" has a height set to "auto"? If so, I would like a jQuery script to remove the CSS class "a" from all elements with the class "y". If not, the script can remain unchanged. Thank you. ...

Guide on changing the order of Vue sibling components when rendering a shared array within a parent component list

Currently facing a unique challenge and seeking input: Within the 'App', utilize 'TestListItem' for odd item indexes and 'TestListBetterItem' for even indexes. The same index must be used for both components. Initial attemp ...

Trouble obtaining output from chrome.tabs in browser console

Currently, I am experimenting with the browser's console and attempting to retrieve all the details of the active tabs. In order to do so, I open the browser's console and input the following: However, I encountered the following error: VM713:1 ...

Adjusting the width of the header in Wordpress themes like Genesis and Foodie Pro

Struggling with the header appearance on my website. I want the blue section to be full width while keeping the logo and navigation the same width as the content area (1040px). I'm new to Wordpress and unsure how to make this adjustment. Someone sugg ...

Employ the JQuery Datepicker functionality to enhance the user experience

Is it possible to integrate jQuery Datepicker into a web page that includes an iframe? And where is the optimal placement for linking the CSS and script references? ...

A tutorial on creating circular patterns within a pyramid structure using p5.js and matter.js

I've been attempting to create a pyramid shape using multiple circles, similar to this: balls in pyramid shape To achieve this, I've developed a 'Balls' class: class Balls { constructor(x, y, radius, color, ballCount) { this.x = ...

Experiencing difficulty in parsing the JSON document

I am struggling with reading a .JSON file (todo.json) in my Angular.Js project. The file is stored on the server, and all static files are saved within the 'public' folder of my basic Express server. Even though I can access the todo.json file di ...