The picture above just won't stay within the div

Ensuring my website is responsive across all devices has been a priority for me. However, I have encountered an issue where, upon scaling down the size of the web browser, an image positioned within a div starts to overflow. While attempting to address this problem by adding overflow: hidden to the div, it did not effectively contain the image. To observe this issue in action, visit the jsfiddle link provided and resize the square frames to witness the image escaping the confines of the div.

https://jsfiddle.net/view02/hzv2jht0/

#deets img {
  width: 100%;
  height: 100%;
  display: block;
}

#deets {
  margin-top: -40px;
  width: 100%;
  height: 100%;
  position: relative;
}

#text { 
  margin-left: 250px; 
}

#text img { 
  margin-top: -55%; 
  position: absolute;
  min-width: 100px;
  max-width: 50%; 
}
<div id="deets">
  <img src="http://lorempixel.com/output/technics-q-c-640-480-1.jpg">
  <div id="text">
    <img src="http://lorempixel.com/output/abstract-q-c-640-480-6.jpg">
  </div>
</div>

Answer №1

To ensure the #deets div stays within bounds, include overflow: hidden; in its CSS.

#deets {
  margin-top: -40px;
  width:100%;
  height:100%;
  position: relative;
  /* This prevents overflow */
  overflow: hidden;
}

If you prefer both images to be fully visible, adjust the CSS for #text.

#text  {     
  /* margin-left: 250px;  */
  margin-left: 20%;
} 

For a specific distance from the left, use a media query targeting smaller screens like this:

@media (max-width:480px) {
  ...
}

Answer №2

The issue is arising due to the margin-left: 250px applied to the #text. Instead of using fixed units like px, consider using relative units such as %:

#deets img {
  width: 100%;
  height: 100%;
  display: block;
}

#deets {
  margin-top: -40px;
  width: 100%;
  height: 100%;
  position: relative;
}

#text {
  margin-left: 25%; /* adjusted */
  /* display: flex; a more efficient way to horizontally center the content */
  /* justify-content: center; if you prefer using these two lines, then comment out the margin-left above */
}

#text img {
  display: block;
  margin-top: -55%; 
  position: absolute;
  min-width: 100px;
  max-width: 50%; 
}
<div id="deets">
  <img src="http://lorempixel.com/output/technics-q-c-640-480-1.jpg">
  <div id="text">
    <img src="http://lorempixel.com/output/abstract-q-c-640-480-6.jpg">
  </div>
</div>

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

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! ...

Navigation through Image Filters

Everything seems fine with my code, but when I click on the navigation links, they direct me to index.html or the landing project instead of filtering the images. How can I modify the links to properly filter the images? This is the Javascript and html co ...

Currently focusing on improving the responsiveness of the navigation bar and grid layout

I'm facing a new challenge I can't seem to crack: Despite setting my grid boxes to be responsive with 1fr column and (6, fr) row, they are displaying poorly on mobile devices. Additionally, my navbar remains fullwidth and mobile-like even when i ...

Transferring data from a text area to a table with AngularJS

Creating a C# MVC App with AngularJS My goal is to populate a table with values entered in a text area using Angular. Here is the process: Users input values into a text area like this: A B C When a user clicks a button, a modal window should open and ...

What is the best way to use jQuery to increment the number in an input field by a specific value?

My goal is to utilize jQuery to increment a number in an input field and then display the updated value in the same input field. The specific id of this input field is "total". Here's my attempt so far: function addBag(){ var bagprice = 79.99; ...

Exploring the functionality of the SVG tag tref in Microsoft Edge

I experimented with using the svg tag tref according to the W3C SVG specification 1.1 in the following manner: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>< ...

The SimpleHTTPServer is causing Google Maps Places Autocomplete feature to malfunction

Currently, I am implementing a location search feature along with form auto-fill using the google.maps.places.Autocomplete functionality. While accessing the HTML file directly (file:///location.html), everything is functioning as expected. However, when ...

To activate an underline on text, simply right-click and select "text

All the anchor tags on my website have been styled with text-decoration: none. Additionally, I have added a CSS rule based on a helpful answer: a, a:hover, a:active, a:visited { text-decoration:none; } However, whenever I right-click on a link on my ...

When implementing jQuery for scrolling on a website, touch gestures may become unresponsive on Safari for iOS devices

While developing a custom website with Angular, I encountered an issue specifically with Safari on iOS. The website is a single-page app with multiple menu sections that trigger a scroll animation when selected using jQuery. Everything was working smoothly ...

Tips for designing a HTML form using classes

I need help styling this form using CSS because I have similar areas like a search submit button that require different classes. Any assistance would be greatly appreciated. <perch:form id="contact" method="post" app="perch_forms"> <perch:cont ...

What causes the difference in CSS border-bottom-width rendering between IE and Chrome?

My ASP:MENU is styled with some CSS properties, such as setting the 'height' and 'line-height' of each element to 15px. This design appears fine in Internet Explorer but seems too cramped in Chrome. Additionally, a 'border-bottom-s ...

Aligning a block heading in the middle of the page

I developed a basic flex-based table. section { display: flex; flex-direction: row; align-items: stretch; width: 100%; margin: 0; background-color: green; } h2, ul { width: 50%; padding-left: 1.5em; padding-right: 1.5em; } h2 { tex ...

What about a sliding element feature?

Is there a popular slider component that many startups are using, or are these types of sliders typically built from scratch? It seems like they are everywhere on startup websites. I would appreciate it if someone could point me in the right direction with ...

What is the best way to modify an image in a column when I hover over a table row that was dynamically inserted using a button function?

Is there a way to dynamically change an image in a column when hovering over a table row that was added through a button function? Here is the current code that I am using, but it does not seem to work as intended when I hover over the row. This function ...

Unable to establish connection between Router.post and AJAX

I am currently in the process of developing an application to convert temperatures. I have implemented a POST call in my temp.js class, which should trigger my ajax.js class to handle the data and perform calculations needed to generate the desired output. ...

The capability to scroll within a stationary container

Whenever you click a button, a div slides out from the left by 100%. This div contains the menu for my website. The problem I'm encountering is that on smaller browser sizes, some of the links are hidden because they get covered up. The #slidingMenu ...

The Angular 2 view will remain unchanged until the user interacts with a different input box

I am currently working on implementing form validation using Reactive Forms in Angular 2. Here is the scenario: There are two input fields Here are image examples for step 1 and step 2: https://i.stack.imgur.com/nZlkk.png https://i.stack.imgur.com/jNIFj ...

Changing Image to Different File Type Using Angular

In my Angular Typescript project, I am currently working on modifying a method that uploads an image using the input element of type file. However, I no longer have an input element and instead have the image file stored in the assets folder of the project ...

Changing the order on mobile devices in Bootstrap 4: A quick guide

Currently, I am working on a responsive layout using Bootstrap 4 with two columns. Each column contains a total of 9 divs - four in the first column and five in the second column. My goal is to rearrange the order of the divs within the columns when the vi ...

The pre-line white-space property is not functioning as anticipated in my CSS code

content: string; this.content = "There was an issue processing your request. Please try using the browser back button."; .content{ white-space: pre-line; } <div class="card-body text-center"> <span class="content"> {{ content }} </span& ...