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

I am having trouble with $(this) in my jQuery click event handler

When I click my function, I want to change a parent element that contains this function. However, $(this) is not working. What could be the issue? function accountConfirm(message, title, yes_label, no_label, callback) { console.log($(this)); $(&qu ...

Tips for updating the appearance of material-ui table pagination

I'm looking to enhance the way table pagination is presented in material-ui. The current default display by material-ui looks like this: https://i.stack.imgur.com/d4k4l.png My goal is to modify it to look more like this: https://i.stack.imgur.com/A ...

Firebase Authentication error code "auth/invalid-email" occurs when the email address provided is not in a valid format, triggering the message "The email address is

Currently, I am working on integrating Firebase login and registration functionality into my Angular and Ionic 4 application. Registration of user accounts and password retrieval are functioning properly as I can see the accounts in my Firebase console. Ho ...

Using pure CSS to style sibling span elements in a unique color

Looking to change the color of a specific span based on the title attribute of another span in a different div. The code below successfully turns text2 red if it has the title of "Red". span[title=Red] ~ span { color: red; } <span title="Red" ...

Can AdonisJS 4.1.0 support conditional queries?

I am exploring the capabilities of AdonisJs query builder by referring to their documentation at Currently, I am attempting to replicate a scenario similar to the one demonstrated under the 'Conditional Queries' section: const query = Database. ...

How can I prevent the expansion of elements in a drop-down menu when using display:

I've been struggling to implement a drop-down feature in my menu. The issue is that when I use display: flex, hovering over the drop-down also expands all other boxes without drop-downs. Any ideas on how to fix this easily? Additionally, is there a w ...

Navigating directories and file locations in a Node/Express application

An example of my Node/Express app's file structure is shown below: /lib coolStuff.js /routes funRoute.js /views sickView.hbs app.js In the past, I have been using relative paths like var coolStuff = require('../lib/coolStuff'); to re ...

Updating Kendo by modifying the Angular model

While working on a project with Angular, I recently discovered the Kendo-Angular project available at . I successfully integrated Angular-Kendo into my project and it seems to be functioning well, except for updating models in the way I am accustomed to. ...

Unable to generate webpage source code

Having some trouble with my Next.js project as I'm using getStaticProps and getStaticPath to build a page, but unfortunately, the HTML is not being generated in the page source. This issue is causing problems with SEO, and I haven't implemented r ...

Is it achievable to create shadows that do not overlap?

When you hover your mouse over the pill-shaped object, it should smoothly move over the circle as desired. However, the shadow of the circle still remains visible creating an unwanted effect. I am looking for a solution where the shadows do not overlap but ...

What steps can I take to make sure JSON keys containing only digits are treated as Strings instead of Numbers?

Within a JSON String, I have IDs as keys, represented as Strings of numbers, while the values are actual Numbers (Float). The problem arises when parsing this information to obtain an object in Safari 10.1.2... var jsonData = "{\"53352\":0.6, ...

CSS changes triggered by JQuery are ineffective

Having trouble modifying the CSS of a class in my HTML file. I've been struggling with this for quite some time and can't figure out what I'm doing wrong. (I've already attempted multiple versions, but nothing seems to work) Here' ...

Leverage the key-value pairs in JSON to automatically suggest types within the function parameters

If we have data structured like this: { "key1": "hardcoded string", "key2": "another hardcoded string", } Imagine a function with 2 parameters where the first parameter should refer to key1 and the second to i ...

The FreeTextBox Editor suddenly stopped functioning after the jquery post method was used

Thank you in advance. I have been utilizing the FreeTextBox editor within my asp.net web application, and it has been functioning well. I use a jquery Post method to populate the editor as well as other form fields with values retrieved from the database. ...

The Challenge of CSS Transition and Checkbox Label Discrepancies

I'm looking to adjust the position of my label when a checkbox is checked. Everything works smoothly if I don't include a transition for the top offset property in the CSS of my label. However, when this transition is added (as seen in the commen ...

What advantages and disadvantages come with using AJAX requests to fetch an HTML page as opposed to utilizing window.location or a hyperlink to navigate to the page?

I have been exploring the various techniques for switching pages in a web browser. Here is what I have gathered so far: Using anchor tags <a href = "#">...</a> allows redirection with text and images (non-buttons) Button elements lik ...

Having trouble with my Angular application, seems to be stuck at the loading screen. Not sure what's causing it, possibly

Hey there, I'm hoping to create a straightforward app that showcases posts. However, I've encountered an issue when deploying the initial page which is stuck on "Loading...". I believe it's a minor problem and would appreciate your assistan ...

Tips for generating a fixed-length array from multiple arrays with different lengths, focusing on selecting items from each array according to their significance

In order to create a quiz, I am looking to extract 'questions' from various 'topic' arrays. These topics are selected based on the user's preference and are used to populate a question bank for a 20-question quiz. The topics rated ...

What is the best method for converting a string picture URL into an image and showcasing it in an image tag?

I have a URL for an image that looks like this: C:\inetpub\MaujApp\OMSAPI\ManualReceivingImages\WhatsApp Image 2021-03-24 at 12.07.41 PM.jpeg My goal is to convert the original image and display it to the user using jQuery. I woul ...

Having an Issue with the jQuery .get Method

Here is some HTML code I have <ul> <li> <a href='#' class='approch' alt='1' > 111111 </a> </li> <li> <a href='#' class='approch' alt='2' > 222222 < ...