Ways to adjust the background image size based on the dimensions of the parent container

Is there a way to make an image fill a div without overflowing and have it positioned 50% off screen, similar to the example in this bootply? The current solution works but I'm looking for a more efficient method with less code.

Bootply

Answer №1

After some experimentation, I managed to solve the issue. A simple adjustment was all that was required.

background-position: top right;
    background-size: contain;

I decided to simplify my approach by removing unnecessary code from the content div and instead made a minor modification in Photoshop to crop the image for optimal display without needing to adjust the horizontal position manually.

Answer №2

If you're looking for an easy way to achieve this, consider utilizing the CSS3 FlexBox property. Check out the code snippet and fiddle link provided below:

HTML:

<link rel="stylesheet" type="text/css" href="style.css" /> 

  <div class="container">
        <div class="div2">
           <p>Sed ut perspiciatis unde omnis iste natus error sit <br>
              accusantium doloremque laudantium, <br>
              totam rem aperiam, eaque ipsa quae
            </p>
      </div>
    <div class="inner-container">
      <ul>
                 <li>List Item</li>
              <li>List Item</li>
              ...
              (list items repeated for brevity)
              ...
      </ul>
      <div class="img-container">

      </div>
    </div>
      <div class="div2">
         <p>Sed ut perspiciatis unde omnis iste natus error sit <br>
        accusantium doloremque laudantium, <br>
        totam rem aperiam, eaque ipsa quae</p>
      </div>
    </div>

CSS :

.div2 {background: #777;}

.container{
  display:flex;
  flex-direction:column;
}

.inner-container{
  display:flex;
}

ul{
    min-width:50%;
}

.img-container{
    width: 50%;
    background-image: url('http://image.shutterstock.com/z/stock-photo-lion-head-this-animal-is-considered-to-be-the-king-of-animals-and-white-albino-lion-is-endangered-74806333.jpg');
    background-size:cover;
}

For the Fiddle demonstration, click on the following link:

https://jsfiddle.net/smlrolland/rqu2a0g0/4/

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

The jQuery height() function may not always provide the expected height value, often returning either 0, -2, or 2 instead

I need a solution to display an icon in front of headlines that vary in length, sometimes spanning one line and other times extending to two lines. The challenge is to ensure the icon resizes accordingly to match the height of the headline element. Curren ...

Adding an Image to an InfoWindow on Google Maps

Hey there! I'm attempting to insert an image into a Google Maps infoWindow. Here's my code snippet: var ContactUs = function () { return { //main function to initiate the module init: function () { var map; $(document). ...

The code for the bouncing image isn't functioning properly outside of the JSFiddle environment

I'm having issues with this jQuery snippet in my web application. It works fine on jsfiddle, but not when I add it to my project. Here's the code: $('.myimage').mouseenter(function() { $(this).effect('bounce',500); }); Her ...

Tips for inverting the z-index order of stacked divs

On my webpage, I have set up multiple columns enclosed in divs like this: <div class="first column" style="width:33%; float: left;"></div> <div class="column" style="width:33%; float: left;"></div> <div class="last column" style ...

What is the best way to display various titles on separate cards using an API?

I attempted to display various titles fetched from jsonplaceholder on separate cards using jQuery and JavaScript Below is the HTML section: <div class="album py-5"> <div class="container"> <div class="row" ...

Tips for increasing the size of Material-ui Rating icons

I am currently utilizing npm to develop a widget. I aim to utilize the material-ui Rating component and have successfully integrated it. However, when I embed the widget on a webpage, the html font-size is set at 62.5%, causing the component to appear too ...

Delay the jQuery event handler for a few milliseconds before triggering

I'm currently working on a navigation menu, but I've encountered some bugs and I'm struggling to fine-tune it completely. Here are the issues I'm facing, and any help or solutions would be greatly appreciated. The menu items (About ...

Refreshing the images array in the DOM using ajax technology

I have a webpage with various images scattered under a table, like in the example of homepage.htm: <table id="imagesTable"> <tr> <td> <img src="img1.png"> <div> <img src= ...

Why does Bootstrap not have the left offset?

Using Bootstrap 4.0 I created a row with two columns and added an offset. However, I am experiencing an issue with the offset at the left side. You can view my jsFiddle here. Has anyone encountered why the offset (col-md-offset-4) is not working on the l ...

How to Locate a Particular HTML Element in AngularJS

Working with angularJS, I am looking to alter the class of a specific child element whose identity (class/id) is unknown due to dynamic addition to the parent element. I am aware that in Angular, one can execute something along these lines: angular.e ...

Tips for showcasing a block of text within a table cell

I am having trouble displaying a small paragraph within a td table cell. The issue I'm facing is that the paragraph does not appear unless the td cell is flexible. I've attempted using (white-space:pre, and white-space: word-wrap) but it has not ...

When using the `display: table-cell` property on a div element, it does not automatically respect

How can I restrict the width of columns by defining a width attribute on the div.dcolumn DOM element to preserve the layout with overflowing cells? I want the content of any overflowing cell (like the 9px column) to be hidden while maintaining the specifie ...

What is the best way to move menu content downward when the hamburger icon is pressed?

Is it possible to make the hamburger icon push down the content, including the background and text, when clicked? I've attempted to adjust padding on the body and set the width of the hamburger icon, but so far it hasn't been successful. I'v ...

Is the Bootstrap 4 dropdown within a card causing column breaks?

Having an issue with using Bootstrap 4, particularly reactstrap but the problem can be recreated in regular Bootstrap, where I am utilizing 'card-columns' to make a grid of cards, each card containing a dropdown menu in the 'card-footer&apos ...

Tips for extracting data from a <li> tag and storing it in a jQuery array

I'm struggling to extract the text from a list and store it in an array. However, instead of getting the content itself, I end up with a list of numbers. Does anyone have any suggestions on how I can successfully place the content in the array? var a ...

Tips on triggering ng-click event before the page finishes loading

Is there a way to have the "edit" button appear in a "clicked" state when the page first loads? <div class="buttons" ng-show="!rowform.$visible" style="width: 20%"> <button class="btn btn-primary" ng-click="rowform.$show()">edit</butt ...

jQuery - Can you identify the mistake in this code?

I've encountered a strange issue while working with jQuery's .get() function. Here is the frontend code snippet... <body> <button title="minor">Minor Armor</button> <br /><br /> <button title="medium">Medium ...

Increased height of iPad screen in landscape mode with no body content displayed (iOS7)

The issue: An unusual problem occurs when the specified URL is loaded on an iOS7 iPad in landscape mode; a vertical scrollbar appears. There is absolutely no content within the body, and it seems to be adjusting the body/html margin/padding. It should be ...

Uploader and viewer tool for HTML5 documents

My website is currently built using PHP and allows users to add and view documents. The current upload process is basic, requiring users to manually input information and then view the document with minimal formatting on a webpage. I am looking to upgrade ...

I am having trouble with my scroll reveal effects on JavaScript, how can I troubleshoot and fix the issue?

I'm currently making updates to my portfolio website, but for some reason, the scroll reveal animation has suddenly stopped working. I made a few edits and now it seems to be broken. /*===== SCROLL REVEAL ANIMATION =====*/ const sr = ScrollReveal({ ...