I'm trying to do a pure css image gallery for a static site. I want to create square elements with centered lazily loaded image previews in them.
My markup works fine when proportional width and fixed height are set, but I can't find a way to achieve fixed aspect ratio for image container.
I've tried:
Rewrite everything to flexboxes. It breaks lazy image loading - seems like at some point every image appears on the viewport causing it to load. Generating previews on server side is not an option for me.
padding-bottom trick. It totally breaks the behavior of object-fit: cover; and image preview centering.
vw kinda works, but my container is already in a flexible container and I can't calculate it accurately.
calc. As expected, it does not work with dynamic values, but I tried it just in case:)
Is where a way to get height to depend on width using pure css?
Here is the code snippet (same on JsFiddle):
div.image_list {
display: block;
width: 300px; /* for example, not set in real case */
}
.image_wrapper {
float: left;
width: 30%;
height: 100px; /* FIXME: make height always equal to width */
padding: 2%;
background: blue; /* for example only */
}
img {
object-fit: cover;
max-width: 100%;
width: 100%;
height: 100%;
}
<div class="image_list">
<div class="image_wrapper">
<img src="https://bigmemes.funnyjunk.com/pictures/Warning+long+post+short+postmedium+post_ba781a_5089470.jpg" loading="lazy">
</div>
<div class="image_wrapper">
<img src="https://images.opencollective.com/jsbin/fef9bb5/logo/256.png" loading="lazy">
</div>
<div class="image_wrapper">
<img src="http://voidcanvas.com/wp-content/themes/reader/images/logo.png?v=2" loading="lazy">
</div>
<div class="image_wrapper">
<img src="http://voidcanvas.com/wp-content/themes/reader/images/logo.png?v=2" loading="lazy">
</div>
<div class="image_wrapper">
<img src="https://bigmemes.funnyjunk.com/pictures/Warning+long+post+short+postmedium+post_ba781a_5089470.jpg" loading="lazy">
</div>
<div class="image_wrapper">
<img src="https://images.opencollective.com/jsbin/fef9bb5/logo/256.png" loading="lazy">
</div>
</div>
Related
I currently have a blog page in Wordpress where my original images are all different sizes. I am having trouble getting the image thumbnails on my blog home page to all be the same width and height without stretching the image and also being able to remain responsive when resizing the screen.
Current blog page can be seen at http://studio10interiordesign.com/blog-2/
Current html:
<article class="slide-entry flex_column post-entry post-entry-3536 slide-entry-overview slide-loop-2 slide-parity-even av_one_third real-thumbnail" itemscope="itemscope" itemtype="https://schema.org/BlogPosting" itemprop="blogPost">
<img width="495" height="400" src="http://studio10interiordesign.com/wp-content/uploads/2018/03/1260-S-Bellaire-Denver-CO-print-002-1-Clubhouse-2700x1800-300dpi-495x400.jpg" class="attachment-portfolio size-portfolio wp-post-image" alt="" srcset="http://studio10interiordesign.com/wp-content/uploads/2018/03/1260-S-Bellaire-Denver-CO-print-002-1-Clubhouse-2700x1800-300dpi-495x400.jpg 495w, http://studio10interiordesign.com/wp-content/uploads/2018/03/1260-S-Bellaire-Denver-CO-print-002-1-Clubhouse-2700x1800-300dpi-845x684.jpg 845w" sizes="(max-width: 495px) 100vw, 495px"><span class="image-overlay overlay-type-extern" style="left: -5px; top: 0px; overflow: hidden; display: block; height: 261px; width: 333px;"><span class="image-overlay-inside"></span></span>
<div class="slide-content">
<header class="entry-content-header">
<h3 class="slide-entry-title entry-title" itemprop="headline">My Hygge</h3>
<span class="blog-categories minor-meta">
Uncategorized
</span>
<span class="av-vertical-delimiter"></span>
</header>
<div class="slide-entry-excerpt entry-content" itemprop="text"></div>
</div>
</article>
<article class="slide-entry flex_column post-entry post-entry-3436 slide-entry-overview slide-loop-3 slide-parity-odd post-entry-last av_one_third real-thumbnail" itemscope="itemscope" itemtype="https://schema.org/BlogPosting" itemprop="blogPost">
<img width="495" height="330" src="http://studio10interiordesign.com/wp-content/uploads/2017/03/Historic-Remodel-1home-image_Large-1.jpg" class="attachment-portfolio size-portfolio wp-post-image" alt="" srcset="http://studio10interiordesign.com/wp-content/uploads/2017/03/Historic-Remodel-1home-image_Large-1.jpg 2048w, http://studio10interiordesign.com/wp-content/uploads/2017/03/Historic-Remodel-1home-image_Large-1-300x200.jpg 300w, http://studio10interiordesign.com/wp-content/uploads/2017/03/Historic-Remodel-1home-image_Large-1-768x512.jpg 768w, http://studio10interiordesign.com/wp-content/uploads/2017/03/Historic-Remodel-1home-image_Large-1-1024x683.jpg 1024w, http://studio10interiordesign.com/wp-content/uploads/2017/03/Historic-Remodel-1home-image_Large-1-700x466.jpg 700w" sizes="(max-width: 495px) 100vw, 495px"><span class="image-overlay overlay-type-extern"><span class="image-overlay-inside"></span></span>
<div class="slide-content">
<header class="entry-content-header">
<h3 class="slide-entry-title entry-title" itemprop="headline">Sui Generis: A person or thing that is unique, in a class by itself.</h3>
<span class="blog-categories minor-meta">
Uncategorized </span><span class="av-vertical-delimiter">
</span>
</header>
<div class="slide-entry-excerpt entry-content" itemprop="text"></div>
</div>
</article>
CSS
.avia-content-slider .slide-image, .avia-content-slider .slide-image img {
border-radius: 3px;
display: block;
position: relative;
-webkit-perspective: 1000px;
-webkit-backface-visibility: hidden;
margin: 0 auto;
vertical-align: middle;
}
I've thought about adding width: 100% and height: 255px or height: auto but defining a set pixel height seems to stretch some images and I don't want that, especially when resizing the screen. The auto property doesn't seem to give the same height to all images, so I can't use that either.
Any info is appreciated.
Dependens on your css file, you have a two ways to solve it:
Just add the width and height witch you want to give for image show size (fast, but no the good solution for responsive site);
Wrapp the image tag in the support div tag with given width and height for it in your category layout file of WP theme.
For first case, just add to your css file:
.avia-content-slider .slide-image, .avia-content-slider .slide-image img {
border-radius: 3px;
display: block;
position: relative;
-webkit-perspective: 1000px;
-webkit-backface-visibility: hidden;
margin: 0 auto;
width: 100%;
height: 200px;
}
For second solution try:
<div style=" width: 100%; height: 200px; ">
<img width="495" height="400" src="http://studio10interiordesign.com/wp-
content/uploads/2018/03/image006-495x400.png" class="attachment-portfolio
size-portfolio wp-post-image" alt="">
</div>
with css for img tag:
.avia-content-slider .slide-image, .avia-content-slider .slide-image img {
border-radius: 3px;
display: block;
position: relative;
-webkit-perspective: 1000px;
-webkit-backface-visibility: hidden;
margin: 0 auto;
position: relative;
min-height: 100%;
min-width: 100%;
top: -50%;
}
Basically there is no way you can resize all images to the same size and at the same time, maintain their ratios and layouts. However, there is a couple of techniques to make the images look pretty fine and not pixelated.
Using the image as a background, if you have a container with fixed height and width, use the image as a background together with background-size: cover will make sure that the image will always be automatically resized to fit inside the container. background-position then can also be used to move the background image inside the container and display the most prominent part.
Using object-fit. This css property will cut off the sides of the image, preserving the aspect ratio, and also filling in the space. However, it is only supported in IE 16, so if you need to support older versions, it might be a problem.
I have no idea why this is such a struggle. I want to place an image inside a container. That image should be responsive, meaning when the column gets smaller in width, the image should also get smaller. It does that now, but it maintains the height, meaning it will look stretched.
https://codepen.io/anon/pen/vmZKyM
<div class="container-fluid">
<div class="col-md-8 offset-md-2">
<div class="row">
<div class="col-md-4 offset-md-2">
<div class="row">
<p>Left column</p>
<img src="http://placehold.it/600x400" />
</div>
</div>
<div class="col-md-5 offset-md-1">
<div class="row">
<p>Right column</p>
<img src="http://placehold.it/700x400" />
</div>
</div>
</div>
</div>
</div>
I do not know the aspect ratio of the image (in this case I do), so I cannot achieve this with the padding-bottom trick (normally used for images).
How can I achieve this?
change max-width: 100%; to width: 100%; also change height: auto; to height: 100%;
If the container should have a fixed height, then give it an ID (or a class) and change the .img-responsive restrictions the other way around eg
.container {
height: 600px;
}
/*And then change */
.container .img-responsive {
display: block;
height: auto;
max-width: 100%;
}
/*To */
.container .img-responsive {
display: block;
width: auto;
max-height: 100%;
}
Not sure how it will work with a mix of orientations but if they are floated it shouldnt really matter
Here is plunker example, the left (yellow) cell is the relevant one. It isn't bootstrap-specific but I've used its classes to make the image responsive.
<div class="container">
<div class="row">
<div class="col-xs-6" style="padding:30px; background:yellow">
<div class="wrapper">
<img class="center-block img-responsive" style="position: absolute" src="..."">
<img class="center-block img-responsive" style="visibility: hidden" src="...">
</div>
</div>
<div class="col-xs-6" style="padding:30px; background:lime">
<img class="center-block img-responsive" src="...">
</div>
</div>
</div>
And relevant css, borrowed from bootstrap
.center-block {
display: block;
margin-left: auto;
margin-right: auto;
}
.img-responsive {
display: block;
max-width: 100%;
width:90%;
height: auto;
}
.img-responsive:hover {
min-width:110%;
}
.wrapper {
position: relative;
height: 100%;
}
The thing I'm trying to accomplish:
Fit the image into the container (bootstrap column with some padding here) proportionally (ok)
Make it a bit smaller while keeping it at center, container's size is unchanged (ok)
Make it bigger on hover while still keeping it at center, container's size is unchanged (fixed with transform: scale)
The second layered image (absolute-positioned) should fit the size and the position of the first (not ok)
I've made placeholder<img> hidden to keep the container's size, but <img> with position: absolute aligns to the left anyway.
How this can be solved? I will use JS on page but clearly I'm not eager to use it to pin that image to the placeholder programmatically.
Make it bigger on hover while still keeping it at center, container's size is unchanged
Use CSS3's transform attribute :
.img-responsive:hover {
transform: scale(1.1);
}
https://developer.mozilla.org/fr/docs/Web/CSS/transform
Make sure to prefix the transform property:
http://caniuse.com/#search=transform
Plunker version: http://plnkr.co/edit/HYJ9XJ8rAc3cgVI0uKFe?p=preview
The second layered image (absolute-positioned) should fit the size and the position of the first
Add left: 0 and right: 0 to your absolute positioned element.
I have a responsive image list. Each image is inside a container.
I want the image container to be 75% of its first container (unit container in this case)
the image ration is 1:1
I played a little with the image container percentage width but it feels like this is not the solution.
<ul class="list-inline unit_list ">
<li class="unit_list_item col-xs-24 col-sm-12 col-md-8">
<a href='#' alt="unit">
<div class="unit_container">
<div class="icon_container unit_icon">
<img class="img-responsive unit_image" src="http://placehold.it/60X60" />
</div>
<div class="unit_name">FREE</div>
</div>
</a>
</li></ul>
Btw, I'm using bootstrap if that's matter.
http://jsfiddle.net/wmu3w3ej/1/
Thanks to #Mary Melody
transform: scale(0.75);
works like magic
I'm a little afraid to use it since it's so simple.
any thoughts?
Using the logic from here: https://stackoverflow.com/a/20117454/3389737
I have applied it to your situation: http://jsfiddle.net/phwaLmen/1/
#wrapper
{
position: relative;
width: 50%;
overflow: hidden;
}
#wrapper:before
{
content: "";
display: block;
padding-top: 75%;
}
#image
{
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
height: 100%;
width: 100%;
}
<div id="wrapper">
<img src="http://placehold.it/350x350" id="image">
</div>
Add relative positioning to the parent, set its width as you'd like and make sure the overflow is hidden.
Create a :before element for the wrapper with a padding-top of 75%. Since there is no height specified for the #wrapper, this 75% is based on the width of the element :)
Then you have your image, positioned absolutely and then fitted to the container. If you want the image to be cropped instead of resized, remove the height: 100% and width: 100% style rules from it.
You can do it like this (in your html):
<img src="img.jpg" height="75%" />
Good luck!
in the image gallery i'm working on, I want a horizontal scroll (ie. the thumbnails are listed horizontal) and the area containing them should have a fixed width with scroll if there are to many to fit the area.
Below is the CSS code so far, but it doesn't seem to work as you can see on the snapshot below the code. What can I write to accomplish what I want?
Thanks in advance!
#thumbnailArea {
padding: 5px;
width: 600px;
height: 90px;
overflow-x: scroll;
overflow-y: hidden;
border: 1px solid black;
}
The HTML code for the thumbnail area (generated with ASP.net webforms) is as follows:
<div id="thumbnailArea">
<a id="ImageRepeater_ImageHyperLink_0" class="thumbnails" href="default.aspx?name=Winter.jpg"><img id="ImageRepeater_Image1_0" class="thumbnail" src="Images/Thumbnails/Winter.jpg" /></a>
<a id="ImageRepeater_ImageHyperLink_1" class="thumbnails" href="default.aspx?name=Autumn.jpg"><img id="ImageRepeater_Image1_1" class="thumbnail" src="Images/Thumbnails/Autumn.jpg" /></a>
and so on...
</div>
That's because the container fits to the width you've provided. To achieve the desired effect, you should use two nested divs: outer with given width and inner holding the images.
Example: http://jsfiddle.net/jTJFa/1/
Html:
<div class="box">
<div class="area">
<img/>
...
</div>
</div>
Css:
.box {
width: 500px;
overflow-x: scroll;
}
.area {
width: 1000px;
}
Although you've turned off vertical scrolling, the #thumbnailArea width is not affected (and as a result, forces wrap). This should do the trick:
#thumbnailArea {
white-space:nowrap;
}
For this to work you need a div inside the thumbnail div with a width so that all pictures fit inside:
<div id="thumbnailArea">
<div style="width: 1000px;">
<a id="ImageRepeater_ImageHyperLink_0" class="thumbnails" href="default.aspx?name=Winter.jpg"><img id="ImageRepeater_Image1_0" class="thumbnail" src="Images/Thumbnails/Winter.jpg" /></a>
<a id="ImageRepeater_ImageHyperLink_0" class="thumbnails" href="default.aspx?name=Winter.jpg"><img id="ImageRepeater_Image1_0" class="thumbnail" src="Images/Thumbnails/Winter.jpg" /></a>
and so on...
</div>
</div>
That should do the trick, alter the width so that you don't have a massive empty space at the end