Adjust top position dynamically during resizing

When resizing the window, I am facing an issue with the image going outside of the box. I believe adjusting the position top of the image based on the box height might help in fitting the image properly within the box.

$(window).resize(function() {
  var boxHeight = $('.box').innerHeight();
  var imgHeight = $('.img').height();
});
.container {
  max-width: 850px;
  margin: 0 auto;
}

.box {
  background: #fff;
  padding: 69px 55px;
  border: 1px solid #000;
  margin-top: 150px;
  border-radius: 6px;
}

.image {
  position: absolute;
  top: -90px;
  right: -41px;
}

@media only screen and (max-width:767px) {
  .image {
    position: relative;
    max-width: 100%;
    top: -88px
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
  <div class="box">
    <div class="row">
      <div class="col-sm-6 col-sm-push-6">
        <img src="https://i.imgur.com/uHoTAhr.png" class="image">
      </div>
      <div class="col-sm-6 col-sm-pull-6">
        It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.
      </div>
    </div>
  </div>
</div>

Answer №1

If you're encountering difficulties due to the padding in your .box container, try removing it.

I recommend adjusting your image using transform properties and enclosing the content within <p> tags while centering it with position:absolute. To ensure responsiveness, set position:relative for the content as well. Some modifications have been made to your HTML markup as well.

Stack Snippet

.container {
  max-width: 850px;
  margin: 0 auto;
}

.box {
  background: #fff;
  padding: 0;
  border: 1px solid #000;
  margin-top: 150px;
  border-radius: 6px;
  position: relative;
}

.image {
  position: relative;
  max-width: 100%;
  transform: translateY(-9%) translateX(0%);    
  margin-bottom: -20px;
}

.text {
  position: absolute;
  width: 50%;
  top: 50%;
  transform: translateY(-50%);
  left: 30px;
}

@media only screen and (max-width:767px) {
  .text {
    position: relative;
    padding: 20px 50px;
    width: auto;
    top: auto;
    transform: none;
    left: auto;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
  <div class="box">
    <div class="row">
      <div class="col-sm-10 col-sm-offset-2 text-right">
        <img src="https://i.imgur.com/uHoTAhr.png" class="image">
      </div>
      <p class="text">It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.</p>
    </div>
  </div>
</div>

Answer №2

It seems like the issue lies within the overlapping head in the image, apart from what Bhuwan pointed out. I have observed that it covers 9.3% of the total height of the image, which we can utilize as a constraint to determine the offset from the top.

Referring to your initial code with a 69px top padding, you should add this line inside the .resize() event listener:

$(".image").css(
    "top", 
    parseInt($(".box").css("padding-top")) - ($(".image").height() * 0.093)
);

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

Employing jQuery, how can one assign attributes to appended HTML and store them

So, I am currently working on a backend page for managing a blog. This page allows users to create, edit, and delete articles. When the user clicks the "edit" button for a specific article named 'foo', the following actions are performed: The ...

Why isn't this code for hiding the animation and displaying it not functioning properly?

Why isn't this animation from display none to block working properly? The initial code appears as follows, and it functions correctly: $(".box_outer").stop().animate({top: '25px' , opacity: 1}, 100); When I add display: none; to the class ...

Having trouble displaying images in Express JS

Here are the lines of code that I wrote in Express: res.write("The current temperature is "+temp+". "); res.write("Weather is currently "+weatherDes); res.write("<img src=" +imageURL+ ">"); res.send() ...

Whenever the page is refreshed, the vertical menu bar with an accordion feature hides the sub

I have designed a vertical accordion menu bar following the example at http://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_accordion_symbol However, I am encountering an issue where clicking on a button to display a submenu causes the page to refr ...

Guidance on Configuring Django Static Files

For setting up my Django static files, I added the following code to settings.py: STATIC_URL = '/static/' STATIC_DIRS = [ os.path.join(BASE_DIR, 'static') ] STATIC_ROOT = os.path.join(BASE_DIR, 'assets') To implement this ...

What is the best way to trigger a method once an image has completed loading and rendering?

Is there a way to trigger a method once an image has finished loading and displaying on the web browser? Here's a quick example using a large image: http://jsfiddle.net/2cLm4epv/ <img width="500px" src="http://www.digivill.net/~binary/wall-cover ...

Strategies for managing large numbers within WebGL GLSL shader programs

What is the best approach to handle large numbers like the one provided in GLSL? I have a shader that requires Date.now() as a uniform, which is defined as: The Date.now() method returns the number of milliseconds elapsed since January 1, 1970 00:00:00 ...

Is there a way to automatically update the child option when the parent option is altered?

I am attempting to modify the child option based on the parent option selection, similar to how it works in Bootstrap. ` <div class="wrap-input100 input100-select bg1"> <span class="label-input100">Indus ...

Tips for managing various errors in an Express API - such as addressing 404, 405, 400, and 500 errors

I am still learning the ropes of node.js and am using the express framework to create a REST API. I am looking to manage multiple errors for my API, specifically handling 404, 405, 400, and 500 errors. While express provides a default error handler, I am u ...

Exploring the implementation of Chain Map or Chain Filter within an Angular Http request that delivers a promise

I have a dataset in JSON format that I am working with, and I need to filter out specific key values using lodash. I want to reject multiple keys that I don't need. My initial approach is to either chain the map function and then use the reject funct ...

To view the following set of three images, simply click on the "load more" button

I'm looking to add a load more button to reveal additional images. Currently, the page loads with 3 images visible, and upon clicking the load more button, the next set of 3 images should be displayed on the screen. Unfortunately, the code I've ...

Excluding a Spec File in Your Protractor Configurations

I have a scenario where I have 10 spec files all named *********.test.js. I need to run tests on all 9 of these files, excluding the file named Idontwantyou.test.js. Currently, I am locating my spec files in the config.file using: specs: ['*.test.js ...

How can I arrange selected options at the top in MUI autocomplete?

I am currently working with mui's useAutocomplete hook https://mui.com/material-ui/react-autocomplete/#useautocomplete Is there a way to programmatically sort options and place the selected option at the top using JavaScript sorting, without resorti ...

What is the process for triggering a sorted output from my MongoDB database by simply clicking a button?

I'm struggling with sorting my collection by rating in my code. I have this snippet where I'm sending my collection to index.ejs: router.get('/', (req, res) =>{ FILM .find().limit(6) .then(films => res.render(& ...

"Converting Text from a Streamed XML-Like File into CSV: A Step-by-Step Guide

I have log files that contain C++ namespace artifacts (indicated by the double colons ::) and XML content embedded within them. After loading and displaying the logs in a browser application, the content has been separated from the Unix timestamps like so: ...

Utilizing ng-class for dynamic routing and controlling

I am currently in the process of developing a dynamic framework using AngularJS. My plan involves allowing users to add new templateUrl and controller from a JSON file, like the one shown in templates.json: { "pages" : [ { "name" : "home" ...

What is the best way to smoothly reveal images one by one?

My goal is to smoothly slide three images inside a div, one by one. However, I'm facing an issue where the images stack on top of each other and end up stuck in their original positions. Here is the code snippet I've been working with: <div ...

Issue with dynamic creation of menu in React Material UI where onClose function is unable to reference a variable inside the function

As I develop an app, I have chosen to dynamically construct the settings menu based on a JSON file. { categories: [ { name: "General", index: 1, items: [{ name: "Dark Mode", index: 1 ...

Unable to fetch JSON data from PHP with the help of AJAX

I seem to be encountering an issue with retrieving JSON string data from my PHP script using an AJAX call. Here are the relevant scripts: $.ajax({ type: "POST", async: false, dataType: "json", url: "databas ...

The implementation of an onclick event in a freshly created HTML element is functioning solely on the final element

One issue I encountered was that when I generated page number buttons at the bottom of a page, the onclick event only worked with the last button created. Below is an example of how the page buttons were displayed: https://i.stack.imgur.com/wHwI0.png ...