Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ensure any content is resized to fit within a fixed div

Ok, so here's the problem: I have a div which is set to almost the entire screen size (on a mobile device), content will be placed within it, however the content itself is not under my direct control. There will be no inline styles on images or links inside. I need to ensure that:

1 - Everything is visible within the div, and if there are long lines they are forced to wrap, or are simply cut-off.

2 - Images should maintain their aspect ratio but be resized to stay within the confines of the div.

3 - The content should "fill" the space, becoming larger or smaller as needed.

One last bonus: There will be a maximum of ONE image in the content, but there could be multiple links.

I've found some other answers on here, but nothing that meets this particular challenge.

jQuery is an option, but I've found it sluggish on mobile devices, jQuery Mobile is an option, but same basic problem.

What's the best combination of CSS and Javascript to perform this task in the most efficient way possible.

like image 903
Eric Avatar asked Feb 15 '11 20:02

Eric


People also ask

How do I resize a content to fit in a div?

Answer: Use the CSS max-width Property You can simply use the CSS max-width property to auto-resize a large image so that it can fit into a smaller width <div> container while maintaining its aspect ratio.

How do I auto-resize an image to fit a div container?

To auto-resize an image or a video to fit in a div container use object-fit property. It is used to specify how an image or video fits in the container. object-fit property: This property is used to specify how an image or video resize and fit the container.

How do I make an image not overflow in a div?

Give the container a fixed height and then for the img tag inside it, set width and max-height . The difference is that you set the width to be 100%, not the max-width .

How do you fit content and height in CSS?

If height: auto; the element will automatically adjust its height to allow its content to be displayed correctly. If height is set to a numeric value (like pixels, (r)em, percentages) then if the content does not fit within the specified height, it will overflow.


1 Answers

#wrapper {
 width: 960px;
 margin: 0 auto;
 overflow: hidden;
}

.img_class {
 height:auto;
 width:auto;
 max-width:960px;
}

/*If you have a div inside your wrapper then you would need this*/

#wrapper .contained_div {
 width: auto;
 display: inline-block:
 margin: 0;
}

/*If you want to stack stuffs one after another in a column then use this div */
.column {
 margin: 0 10px;
 overflow: hidden;
 float: left;
 display: inline;
}
like image 71
Arindam Paul Avatar answered Sep 27 '22 16:09

Arindam Paul