showing square images in a grid in a responsive layout - css

I have a bunch of images that I want to display neatly in a grid.
Actually the images are near square (but some of them are off by some pix in height)
It would obviously be easy to just set them a fixed width and height via css,
but I want to scale them with the width of the container.
I arrange them in a table (or within inline divs -- does not matter for the problem)
img.width:100% does not help here. Since it also scales the height,
so we end up with some different heights for all the images.
setting img.height:100% does not help either, as they are not square any more.
I finally ended up in adjusting this via coffescript.
But my question is, if there really is no way to accomplish this with pure css.
My solution is to attach .square_all as a marker to the container,
and then retrieving the width of the first image of the container,
and setting that as height of all images in the container.
Not field tested, works for me
square_all.coffee:
#
# make all images in a group the same height as the first's width
#
$ ->
square_all = ->
size = $('.square_all').each (index, element) ->
imgs = $(element).find('img')
size = imgs.first().width()
imgs.height size
true
square_all()
$(window).bind 'resize', (event) -> square_all()
Sidenote:
It might be tempting to also set the width of the imgs to size,
but this does not work. Because your images will than have a fixed width,
instead of being width:100%, so they will no longer adjust to the width of the container.
In my environment we scale the images down by width,
so that they are all the same width, but we scale the height proportionally.
I don't care about the heights in this solution, I just scale the height.
In other contexts, cropping the height might be more apropriate.

I would do it in pure css with percentages and a div type row
DEMO http://jsfiddle.net/kevinPHPkevin/R8Rrf/
.row {
width:100%;
}
img {
width:18%;
max-width:100px;
}

Related

Scale content responsively within an absolutely-positioned, responsive outer container

I have a project that involves having a sidebar that floats over an image. The sidebar is set to position: absolute to keep it over the image and to help it scale along with it when the screen size changes.
Here is a codepen that basically recreates what I'm working on: https://codepen.io/gojiHime/pen/JmYqaz
The issue I'm having is with controlling the size of the contents within the wrapper container. I want the preview div to scale along with the wrapper container. Currently, it does not work as expected in that the preview div does not start scaling as the width and height change for wrapper and for thumbs-inner. The thumbs-inner div scales correctly for the most part, but the bottom of div is cut off so you can't see the bottom of the scroll bar in smaller screens.
I know I set overflow: hidden on wrapper but without it the content in preview would extend outside of it as the height of wrapper changed.
So, I'm looking for ideas on how to fix the aforementioned issues. wrapper must stay absolutely positioned and the thumbs-inner div needs to have a vertical scrolling feature, so I can't do anything with those. I don't think setting a height makes sense for wrapper since it needs to scale responsively in height and width.
EDIT: Not sure how much this will help but this is a screenshot of what the layout of everything should look like: enter image description here
The Kraftmaid logo, full-size thumbnail and the text below it (which are in the .preview div in the codepen) have to be visible at all times when changing the screensize.
I'm not sure if this is exactly what you're looking for, but generally for responsive layouts you would want to avoid fixed dimensions, such as specific widths set in x number of pixels.
This shows your code with responsive layouts for .wrapper and .thumbs-inner (note that I haven't addressed any content issues within those two divs since I have no idea what your intended layout is):
https://codepen.io/anon/pen/ZqrZaj
Note that:
I've switched the two layout divs to use box-sizing: border-box; which will allow you to use pixels for margin and padding but still use percentages for width.
I've removed width from .wrapper and switched to percentage based absolute left and right declarations - if you modify these values, the layout should still work.
I've added borders to make the layout more obvious.

Displaying an image larger than it is with CSS

I have written a fairly basic javascript function that when an image is clicked on a full sized version appears in the foreground.
The image is set with max-width and max-height numbers in a CSS file such that it leaves some space around the outside and it preserves it's own proportions.
The problem is that if the image happens to not be large, or the screen of the users device has a very high pixel density then the 'larger' image might not be any larger.
Is there a way I can keep proportions, not exceed say 90% on either side, but set the largest dimension to be 90%.
The closest similar method I have found is the fill option for backgrounds.
Cheers
Set the width and height of the image to a relative value like 100% - (margin + border + padding) so that it will be stretched regardless of its actual dimensions.
Using max-width and max-height is a nice way to restrain your image from growing beyond specific proporitions, but leaves room for the image to decide what size it wants to be within those bounds. You say you do not want this, thus set width and height as well.
You can embed that image in div tag and apply css property width:100% to image and on click of image increase the width of div proportionately as per the resolution. In this scenario image with less width than parent div gets adjusted as per width of parent div.

How to keep html table on same line without specifying a width?

I can't seem to keep my (dynamic width) table on the same line as a previous element and have it extend to it's parent container without exceeding it and overflowing. I don't want a horizontal scrollbar as the table should just break the lines and/or words up to make it more narrow.
However, it's not doing that.
jsfiddle
In the fiddle, the table overflows and extends beyond it's parent container's width. The parent container is using white-space: nowrap to keep it on the same line as the content next to it.
Why is it not sizing it's width correctly?
If I set a fixed width on the table, it works and sizes the width correctly, but I need the width of the table to be dynamic. Only the outermost containing div is fixed.
Any ideas?
If you add
.listInfoTbl {
[...]
max-width: 142px;
[...]
}
then you'll see everything is working. But you may wonder why is that?
The answer is that you set a certain width for your div.listPropertyDiv therefore it won't grow beyond this and additionally there's some padding to take into the formula:
innerWidth(.listPropertyDiv) = innerWidth(#left) - border(.listPropertyDiv) - padding(.listPropertyDiv) - margin(.listPropertyDiv)
innerWidth(.listPropertyDiv) = 397px
Therefore:
width(table.listInfoTbl) <= innerWidth(.listPropertyDiv) - width(img.listImage)
width(table.listInfoTbl) <= 142px
You should overthink having a fixed width on #left, if your thinking about dynamically changing the content's width because if the parent doesn't shrink it's children can't.
A fixed fiddle
But maybe this is what you're looking for a solution with max-width and percentage so objects can shrink accordingly.

use vertical percentage for css - height: x%;

I am trying to make a fluid layout for an app and am having some trouble with using css for height percentages. It is using the horizontal size of the window to specify the height % when I want it to be getting this percentage from the vertical size of the window. Is this possible or am I out of luck?
width: x%;
height: y%;
x and y being the percentage that I want the element to be. Both are being determined by the horizontal size of the window, and I want each to use it's respective axis.
If you use JavaScript to update the absolute height of the parent node, height % begins to mean something. From playing around, a P or DIV as first generation child of the body ignores any CSS height%; Wrap it in something with an absolute height. If you don't know the height before hand, use an onload JS function to set it.

resizing an unknown image size using CSS?

I am trying to resize an image using only CSS, the problem is I don't know it's dimensions.
What I have tried so far is putting the into a and then making the image have 105% width. The idea was that the containing div would have no size other than it's contents, but this is only make the image the size of the next ancestor that does have an explicit size.
In order to resize something in CSS you have to either give it an exact pixel value or base the size on something else.
If you were to set an image to have a width of 105% of its container then that container must have some width for you to use. If it's a regular div with no styling applied then its width will be the full width of that divs parent and your img will be 105% of that.
If the div holding the img is floated then it will be getting its width from its contents (aka the img). This won't work because you can't have two elements getting their widths from each other. One of them has to be constrained somehow.

Resources