Adjust div to match the width of the image

My portfolio page features an image display with zoom functionality. Here is the code I am currently using: http://codepen.io/Mpleandro/pen/LvrqJ

The division that showcases the image has a class called .display, located on line 13 of the HTML file and CSS formatting for this div can be found on line 90.

Since the image width is flexible, I am looking to make the containing div inherit the width of the image.

I have attempted using CSS properties such as auto, inherit, and min-width, but unfortunately, none of them seem to work!

If anyone could provide me with some assistance on this matter, it would be greatly appreciated.

P.S.: It is important that any solution provided is responsive in nature.

Thank you!

Answer №1

It's been a year since this solution was shared, but hopefully it can still help someone else out there.

I once faced a similar situation where I needed a div to match the width of an image in a responsive way.

In my case, I decided to assign a specific width to the div:

.some-div{
   width: 250px;
}

Additionally, I had a responsive image defined as follows:

img{
   display: block;
   max-width: 100%;
   height; auto;
}

To address any issues with responsiveness due to the fixed width of the div, I included a media query like so:

@media screen and (max-width: 917px){
    .some-div{
        width: 100%;
    }
}

In my project, I found that setting the threshold at 917px worked well when the fixed width began impacting responsiveness.

This solution may work for others too, as setting width: 100% beyond the threshold will always align the div's width with that of the responsive image.

Answer №2

Providing you with the perfect answer might be tricky, but I can certainly guide you in the right direction. Forget about using inherit or min-width as they may not suit your needs.

The default value of auto is likely what you are looking for, where the width of the div adjusts to its content. If this isn't happening, it could be due to various factors such as the positioning of the div itself. Achieving proper centering and sizing of the <div class="display"> may require an explicit width declaration rather than relying solely on CSS.

Since you're already utilizing Javascript to show/hide images, consider using it to adjust the width whenever the displayed image changes.

My suggestion would be to opt for established solutions that have been thoroughly tested, endorsed, and offer a visually appealing result. A quick Google search led me to Fresco, which you may find useful to try out.

Answer №3

Perhaps this could be the solution you're looking for. Give it a try and see if it works!

To make your image fill the entire div while adjusting to the size of the browser, experiment with setting the width of your div accordingly. Then add max-width="100%"; height: auto; to the CSS properties of your image.

Answer №4

To make the layout responsive, a simple solution is to set .display as display: inline-block;, adjusting its size to fit the image it contains. For responsiveness, define an upper limit with max-height: 80%.

Combined, the code would appear like this: http://codepen.io/anon/pen/IluBt

JavaScript on line 17:

$(".display").css("display","inline-block");

CSS for .display

.display {
    position: relative;;
    background: rgba(255,255,255,0.5);
    border-radius: 10px;
    -moz-border-radius: 10px;
    -webkit-border-radius: 10px;
    max-height:80%; /* <-- limit the height */
    top:10%;
    left:0;
    margin:auto;
}

To ensure proper alignment:

.loader {
    color: red;
    position: fixed;
    width: 100%; height: 100%;
    background: rgba(0,0,0, 1) url(../http://www.mpleandro.com.br/images/new/loader.gif) no-repeat center center;
    text-align: center;
}

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

Challenge encountered while attempting to implement grid system in ionic framework

I'm completely new to the world of ionic framework, and I really need some assistance in completing this view. Please see the image reference https://i.stack.imgur.com/WOBFw.png I have made an attempt at it with the following code: <div class ...

Customize the width and padding of your data tables

I have several tables with different padding and width settings. Previously, I had set these inline without any issues. However, now that I am using data tables, the inline styles are not working. If you need more information about Datatables, check out t ...

My approach to using this style in React involves utilizing the class attribute as follows: "class[attribute]" [Updated version 2]

When trying to customize an element based on its attribute, the process is a bit different in React compared to pure CSS. Here's an example: <div class='something' data-test='4'></div> ... .something[data-test] { bac ...

Learning how to access my CSS file using Express and Node.js

I am new to using express and node.js. I am trying to develop my app, but for some reason, my style.css file is not being recognized and I am unsure why. Initially, I attempted to use .scss files, but after researching, I discovered that it was not possi ...

Trouble obtaining AJAX result using onClick event

As a newbie to AJAX, I am still trying to grasp the concept. My task involves using an AJAX call to extract specified information from an XML file. Even after carefully parsing all tag elements into my JavaScript code, I encounter a problem when attempting ...

Establish a timeout period for ajax requests using jQuery

$.ajax({ url: "test.html", error: function(){ //do something }, success: function(){ //do something } }); At times, the success function performs well, but sometimes it does not. How can I add a timeout to this ajax re ...

Optimizing web design with Chrome's developer tools for varying screen resolutions

Recently, I was working on a responsive website and encountered an issue that I can't quite pinpoint. When using the Chrome website developer tool to resize the screen to a smartphone resolution, I noticed some strange behavior at times. It's har ...

Position a div in the center and add color to one side of the space

I am seeking a way to create a centered div with the left side filled with color, as shown in examples. I have devised two solutions without using flexbox, but both seem somewhat like hacks. body { margin: 0; padding: 0; } .header { width: ...

Issue with displaying tooltip on jQuery select2 in Bootstrap 3

I am customizing my selectbox with the select2 plugin. I now want to display a tooltip above the selectbox. Here is the HTML code: <select class="selectD input-sm" name="newsoptions_amount" data-toggle="tooltip" data-placement="bottom" title="Gallery ...

Top method for utilizing jQuery to locate, sift through, and implement CSS classes

Let's consider the following scenario: <span class="foo">7</span> <span class="foo">2</span> <span class="foo">9</span> We want to assign a CSS class of 'highest' to 'span.foo' with value great ...

Print page headers are being overlapped by THEAD elements

I have created a table and am considering using the thead as page headers if the table spans multiple pages. However, I am encountering an issue where the content of the table overlaps with the table header on the second page. Here is a snippet of my HTML ...

Tips for Preventing Ant Design from Overriding Existing CSS Styles

Currently, I am working on a ReactJS project. Initially, the entire project was designed using pure CSS. However, I decided to incorporate a small Antd component into my project. Following the provided instructions, I imported the component like this in on ...

Hindered from implementing a linear gradient

The UI team has provided a linear gradient for the button background in the following format: linear-gradient(bottom, #E4E8EA 0%, #E8EBED 50%, #F1F3F4 96.36%, #FFFFFF 100%), linear-gradient(bottom, #E4E8EA 0%, #e8ebed 50%, #F1F3F4 96.36%, #FFFFFF 100%) I ...

I am experiencing difficulty with loading the local images stored in the /public/images directory of my React app

I am currently working on a component called Portafolio.js where I am utilizing the map function to retrieve each object from a file named "trabajos.js" that contains all the objects with their respective properties. My goal is to display an image for each ...

Angular 13: Problems arise with custom theme in Angular Material version 13

I've set up a custom theme palette for my project that works perfectly with version ^12.2.13 of Angular Material, but not with ^13.2.3. Here's the SCSS code for my custom theming: my-custom-theme.scss @import '~@angular/material/theming&apo ...

Combine object attributes to create a new column in a table

Check out this plunker I created. What is the most efficient way to merge the contents of $scope.blacklist into $scope.friends when ng-click="showColumn('Blacklist');" is triggered, and also add a new column named Coming to the table. Ng-App &am ...

Moving smoothly while retaining absolute positioning

I have a question about a particular scenario. I have a div element that is styled like a ball and I want it to transition diagonally across the page to the bottom right corner instead of moving suddenly. However, this is not happening as expected. Can you ...

Is JavaScript necessary for this task in PHP?

Hi everyone, I recently discovered a way to style a PHP echo in a more visually appealing manner. Instead of presenting an unattractive colored box right away, I was wondering if there is a way to only display the box when the user selects the calculation ...

Is it possible to make changes to local storage data without impacting the rest of the data set?

https://i.sstatic.net/BBcJF.pngI am looking for a way to modify specific data in the local storage without affecting any other stored information. However, I have encountered an issue where editing values works correctly for the first three attempts, but ...

Continuous Div Carousel with Looping in jQuery

I have developed a basic carousel to cycle through a series of divs, but I am facing some issues. I want the carousel to loop continuously so that after reaching "slide 07," it goes back to "slide 01." Unlike using a carousel plugin, I prefer having an ove ...