I use a masonry-look for my cards like that here https://codepen.io/daiaiai/pen/VPXGZx
There I want to add some infos via figcaptions on top of the image/figure. I tried to do that with position:relative but that doesn't work. I wanna avoid using a negative margin-top but I can't think of a different solution. What/How should one do that?
Same as on codepen, but the raw "code" for css:
body {
margin: 0; background: #131212;
}
div#masonry {
display: flex;
flex-direction: column;
flex-wrap: wrap;
height: 100vw;
max-height: 800px;
font-size: 0;
}
div#masonry figure {
width: 33.3%;
transition: .8s opacity;
}
figcaption{
position:relative;
top:100px;
left:50px;
}
figure img{
max-width:100%;
}
and that html-code
<div id="masonry">
<figure><figcaption>This girl</figcaption><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/4273/1.jpg"></figure>
<figure><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/4273/2.jpg"></figure>
<figure><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/4273/3.jpg">
</figure>
</div>
Thanks for your help!
The reason you are not seeing the figcaption is because you have set the font-size of #masonry to 0, perhaps to remove spaces between columns.
You need to assign a font-size to figcaption to correct this.
I would also recommend changing how you position it.
Add position: relative to figure, and position: absolute to figcaption.
CODEPEN
SNIPPET:
body {
margin: 0;
background: #131212;
}
div#masonry {
display: flex;
flex-direction: column;
flex-wrap: wrap;
height: 100vw;
max-height: 800px;
font-size: 0;
}
div#masonry figure {
width: 33.3%;
transition: .8s opacity;
position: relative;
}
figcaption {
position: absolute;
top: 0;
left: 0;
font-size: 22px;
width: auto;
background: black;
color: white;
}
figure img {
max-width: 100%;
}
/* fallback for earlier versions of Firefox */
#supports not (flex-wrap: wrap) {
div#masonry {
display: block;
}
div#masonry img {
display: inline-block;
vertical-align: top;
}
}
<div id="masonry">
<figure>
<figcaption>This girl</figcaption>
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/4273/1.jpg">
</figure>
<figure>
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/4273/2.jpg">
</figure>
<figure>
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/4273/3.jpg">
</figure>
<figure>
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/4273/4.jpg">
</figure>
<figure>
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/4273/6.jpg">
</figure>
<figure>
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/4273/8.jpg">
</figure>
<figure>
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/4273/9.jpg">
</figure>
<figure>
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/4273/7.jpg">
</figure>
<figure>
</div>
Related
I have two images I want to place on top of each-other e.g a product (inner) and a "discount"-banner (outer).
Currently I have the image in an a-tag where the a-tag is a flex-box container such that I can use align-items and justify-content.
My HTML is the following
.image-wrapper {
height: 50%;
margin-top:20px;
flex-grow: 1;
}
.image-wrapper a{
display: flex;
align-items: center;
justify-content: center;
width: 100%;
height: 100%;
}
.image-wrapper img{
width: auto;
max-height: 100%;
}
<div class="image-wrapper">
<a href="{{p.link}}" target="_blank" rel="noopener">
<img src="https://lh3.googleusercontent.com/proxy/9R1yfiOspfOVdrip4b4kF5i1cCaJWmdyKRJbtXw6bnmXiPqYRLZyUYYJL3MwiRWay8JjDNmdT8cpgxB7pBgWdLz04s6mjEYDE-DD5btDasFt7j-KPraA0YIFiF2rN2kSq9uq0PYNHc46NVmEokNuc1GbhujtP_NKyg72rnM" alt="Image of product" /></a>
<img src="https://lh3.googleusercontent.com/proxy/9R1yfiOspfOVdrip4b4kF5i1cCaJWmdyKRJbtXw6bnmXiPqYRLZyUYYJL3MwiRWay8JjDNmdT8cpgxB7pBgWdLz04s6mjEYDE-DD5btDasFt7j-KPraA0YIFiF2rN2kSq9uq0PYNHc46NVmEokNuc1GbhujtP_NKyg72rnM" alt="Discount-banner" />
</div>
I would prefer still having the image in the a-tag, but if that cannot be done, it's fine (first priority is to have the two images on top of each-other)
Put your second image inside a div, and apply the same CSS as your a tag to have both of them centered
.image-wrapper {
height: 50%;
margin-top: 20px;
flex-grow: 1;
}
.image-wrapper a,
.image-wrapper div {
display: flex;
align-items: center;
justify-content: center;
width: 100%;
height: 100%;
}
.image-wrapper img {
width: auto;
max-height: 100%;
}
<div class="image-wrapper">
<a href="{{p.link}}" target="_blank" rel="noopener">
<img src="https://lh3.googleusercontent.com/proxy/9R1yfiOspfOVdrip4b4kF5i1cCaJWmdyKRJbtXw6bnmXiPqYRLZyUYYJL3MwiRWay8JjDNmdT8cpgxB7pBgWdLz04s6mjEYDE-DD5btDasFt7j-KPraA0YIFiF2rN2kSq9uq0PYNHc46NVmEokNuc1GbhujtP_NKyg72rnM" alt="Image of product" /></a>
<div>
<img src="https://lh3.googleusercontent.com/proxy/9R1yfiOspfOVdrip4b4kF5i1cCaJWmdyKRJbtXw6bnmXiPqYRLZyUYYJL3MwiRWay8JjDNmdT8cpgxB7pBgWdLz04s6mjEYDE-DD5btDasFt7j-KPraA0YIFiF2rN2kSq9uq0PYNHc46NVmEokNuc1GbhujtP_NKyg72rnM" alt="Discount-banner" />
</div>
</div>
To overlay the second image over the first, you can use position: absolute
.image-wrapper {
height: 50%;
margin-top: 20px;
flex-grow: 1;
}
.image-wrapper a {
position: relative;
display: flex;
align-items: center;
justify-content: center;
width: 100%;
height: 100%;
}
.image-wrapper img {
width: auto;
max-height: 100%;
}
.image-wrapper .overlay {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
<div class="image-wrapper">
<a href="{{p.link}}" target="_blank" rel="noopener">
<img src="http://placekitten.com/300/300" alt="Image of product" />
<img class="overlay" src="http://placekitten.com/100/100" alt="Discount-banner" />
</a>
</div>
So I want these 3 div's to work side by side and the images to be responsive. when I try to set max-width or something it breaks up. Help me out on this one.
.fl {
display: flex;
flex-wrap: no-wrap;
height: 35%;
flex-direction: row;
}
.pic {
width: 34%;
}
.caro {
/* border: 2px solid black; */
height: 100%;
width: 100%;
display: block;
/* z-index: ; */
}
.caro img {
height: 100%;
width: 100%;
filter: brightness(0.5);
transition: all .3s;
}
.caro img:hover {
filter: brightness(1);
}
<div class="fl">
<div class="pic">
<img src="https://via.placeholder.com/1920x1080.jpg" alt="">
</div>
<div class="pic">
<img src="https://via.placeholder.com/1920x1080.jpg" alt="">
</div>
<div class="pic">
<img src="https://via.placeholder.com/1920x1080.jpg" alt="">
</div>
</div>
I would suggest two things:
Use calc to get a third with calc(100% / 3), instead of setting the width to a total of (3*34=) 102%.
Use object-fit: cover on the images to make them fit. I used different sizes on the images, so it's obvious that they are scaling.
Don't use all for transition, because that can cause janky animations due to the browser having to loop through all animatable properties. Always specify what you animate; it's easier to understand the code as well.
Using percentage for height doesn't automatically take the parent's height, so I had to improvise and use vh (viewport height).
body {
margin: 0px;
min-height: 100vh;
}
.fl {
display: flex;
flex-wrap: no-wrap;
height: 100vh;
max-height: 35vh;
}
.fl > .pic {
flex-basis: calc(100% / 3);
}
.caro img {
height: 100%;
width: 100%;
object-fit: cover;
filter: brightness(0.5);
transition: filter .3s;
}
.caro img:hover {
filter: brightness(1);
}
<div class="fl">
<div class="pic">
<img src="https://picsum.photos/100/300">
</div>
<div class="pic">
<img src="https://picsum.photos/50/80.jpg" alt="">
</div>
<div class="pic">
<img src="https://picsum.photos/100/100" alt="">
</div>
</div>
I have a gallery page. I have tiles that are a preset width. Some images fit it exactly, others are too wide and are set to width 100%. I want to center the image in the tile so it will have black bars on the top and bottom
HTML:
<figure class="gallery-tile">
<img src="images/gallery/lulBBQ.JPG" class="gallery-tile-image">
<figcaption class="gallery-tile-description">
Sample Text.
</figcaption>
</figure>
CSS:
.gallery-tile{
display: block;
position: relative;
overflow: hidden;
float:left;
width: var(--halfwebpageWidth);
height: 640px;
background-color: black;
margin: auto;
}
.gallery-tile-image{
display: block;
overflow: hidden;
width: var(--halfwebpageWidth);
margin: auto;
vertical-align: middle;
}
Use display:flex to align your content vertically centered
.gallery-tile {
position: relative;
overflow: hidden;
float: left;
width: var(--halfwebpageWidth);
height: 640px;
background-color: black;
margin: auto;
display: flex;
align-items: center;
}
.gallery-tile-image {
display: block;
overflow: hidden;
width: var(--halfwebpageWidth);
margin: auto;
vertical-align: middle;
}
<figure class="gallery-tile">
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSqrfVof7CYYRtsdCu1VD4AWoPB2gs25PP5hQAuhOwhZngrOhsS" class="gallery-tile-image">
<figcaption class="gallery-tile-description">
Sample Text.
</figcaption>
</figure>
Set height,with and line-height properties according to your needs.
figure {
width: 400px;
height: 300px;
line-height:300px;
text-align: center;
border:1px solid red;
}
figure img {
vertical-align: middle;
max-height:250px;
}
figcaption{
line-height:20px;
height:20px;
margin-top:10px;
}
<figure>
<img src="https://www.w3schools.com/html/pic_trulli.jpg" alt="Trulli" style="width:100%">
<figcaption>Fig.1 - Trulli, Puglia, Italy.</figcaption>
</figure>
There are some extra space on the right side in my gallery...
My images' container:
.my-gallery figure {
display: block;
float: left;
width: 150px;
}
Is it possible make images always horizontal center in different sized screen without using % percent value? Or someone has a genius idea that makes extra space not so weird?
Or % percent value trick is the only way?
In screen A:
In screen B:
.my-gallery {
width: 100%;
float: left;
}
.my-gallery img {
width: 100%;
height: 112px;
}
.my-gallery figure {
display: block;
float: left;
margin: 0 5px 5px 0;
width: 150px;
}
.my-gallery figcaption {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
min-height: 26px;
}
.my-gallery img {
max-width: 100%;
}
<div class="my-gallery">
<figure>
<a href="big1.jpg">
<img src="http://placehold.it/112x150" alt="1" />
</a>
<figcaption>111111111111111111111111</figcaption>
</figure>
<figure>
<a href="big2.jpg">
<img src="http://placehold.it/112x150" alt="2" />
</a>
<figcaption>222222222222222222222222</figcaption>
</figure>
<figure>
<a href="big3.jpg">
<img src="http://placehold.it/112x150" alt="3" />
</a>
<figcaption>3333333333333333333333333333333</figcaption>
</figure>
<figure>
<a href="big4.jpg">
<img src="http://placehold.it/112x150" alt="4" />
</a>
<figcaption>444444444444444444444444</figcaption>
</figure>
...
</div>
If using % is a problem, you could use css flexbox to get this done.
https://jsfiddle.net/76dybc3p/1/
Change css of .my-gallery and remove the float in figure
.my-gallery {
width: 100%;
display: flex;
justify-content: space-between;
flex-wrap: wrap;
}
.my-gallery figure {
display: block;
margin: 0 5px 5px 0;
width: 150px;
}
The most practical approach is to use #media query
I also changed the .my-gallery rule to
.my-gallery {
margin: 0 auto
}
Sample snippet
.my-gallery {
margin: 0 auto
}
.my-gallery img {
width: 100%;
height: 112px;
}
.my-gallery figure {
display: block;
float: left;
margin: 0 5px 5px 0;
width: 150px;
}
.my-gallery figcaption {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
min-height: 26px;
}
.my-gallery img {
max-width: 100%;
}
#media screen and (min-width: 310px) {
.my-gallery {
width: 310px;
}
}
#media screen and (min-width: 465px) {
.my-gallery {
width: 465px;
}
}
#media screen and (min-width: 620px) {
.my-gallery {
width: 620px;
}
}
<div class="my-gallery">
<figure>
<a href="big1.jpg">
<img src="http://placehold.it/112x150" alt="1" />
</a>
<figcaption>111111111111111111111111</figcaption>
</figure>
<figure>
<a href="big2.jpg">
<img src="http://placehold.it/112x150" alt="2" />
</a>
<figcaption>222222222222222222222222</figcaption>
</figure>
<figure>
<a href="big3.jpg">
<img src="http://placehold.it/112x150" alt="3" />
</a>
<figcaption>3333333333333333333333333333333</figcaption>
</figure>
<figure>
<a href="big4.jpg">
<img src="http://placehold.it/112x150" alt="4" />
</a>
<figcaption>444444444444444444444444</figcaption>
</figure>
</div>
I have no idea why you wouldn't use %, but this is another alternative: use a table to scaffold your items and set the property table-layout: fixed;
HTML
<table>
<tr>
<td>
1
</td>
<td>
2
</td>
<td>
3
</td>
</tr>
</table>
CSS
table{
table-layout: fixed;
}
I am unable to get one of my images to line up correctly. I have done most of the align vertical-align: middle; and display display: block;options to remove the bottom padding. I have also set the container to width: 100%; as well as the image width: 100%; so it should be responsive. I'm not sure what is causing this image to have the gap. I have set the background color to yellow so it shows the area that needs to be filled in. I have also commented out all of the alternative options I attempted with no success.
Example:
* {
box-sizing: border-box;
font-size: 100%;
}
.container {
display: flex;
flex-direction: column;
max-width: 90%;
margin: 0 auto;
background-color: white;
}
.img__container {
display: flex;
/* justify-content: space-between; */
align-content: center;
background: yellow;
}
a .img_item {
width: 100%;
height: auto;
vertical-align: middle;
/* display: block; */
/* flex-grow: 1;
flex-shrink: 0;
flex-basis: auto; */
}
.img_item_1,
.img_item_3,
.img_item_2 {
width: 33.33%;
}
.img_item_4,
.img_item_5 {
width: 50%;
}
.img_item img {
vertical-align: middle;
/* display: block; */
max-width: 100%;
}
<div class="container">
<div class="img__container">
<img src="https://images.unsplash.com/photo-1460626399219-57a00a2361cb?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&w=1080&fit=max&s=ea8025ac5c503a77aaf3197534af535b" alt="">
<img src="https://images.unsplash.com/photo-1460400355256-e87506dcec4f?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&w=1080&fit=max&s=65ebb274e22b4db0f6cef789563020c5" alt="">
<img src="https://images.unsplash.com/photo-1453668069544-b8dbea7a0477?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&w=1080&fit=max&s=3693c161a8cf1e3299c913eede08005a" alt="">
</div>
<div class="img__container">
<img src="https://images.unsplash.com/photo-1428189923803-e9801d464d76?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&w=1080&fit=max&s=467ee7b8a091aa5cb8bc9b496aada853" alt="">
<img src="https://images.unsplash.com/photo-1458724338480-79bc7a8352e4?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&w=1080&fit=max&s=0e8fe82e7f50091319fdc635582bf62d" alt="">
</div>
<div class="img__container">
<img src="https://images.unsplash.com/photo-1421749810611-438cc492b581?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&w=1080&fit=max&s=072549d9d9ee6a1f78d91081068c6ad1" alt="">
<img src="https://images.unsplash.com/photo-1433190152045-5a94184895da?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&w=1080&fit=max&s=57115141c5d099ff83a0aa55c0b219a9" alt="">
</div>
</div>
The problem is you're trying to fit images of different width to height ratios into boxes of the same size (I say images because it also appears your first top two images have a 1 pixel gap at the bottom.)
Your options are:
Crop all images to the same size at the CMS end.
or
Attach your images as inline background images and in your CSS file set the background-size to cover and background-position to center center. Some other things you'll need to adjust as well. If you're interested in this approach, let me know in the comments and I can edit my answer with a snippet.
EDIT
Background image (for the sake of brevity I only did the one):
* {
box-sizing: border-box;
font-size: 100%;
}
.container {
display: flex;
flex-direction: column;
max-width: 90%;
margin: 0 auto;
background-color: white;
}
.img__container {
display: flex;
align-content: center;
background: yellow;
}
.img_item {
background-size: cover;
background-position: center center;
}
.img_item_4,
.img_item_5 {
width: 50%;
}
.img_item img {
vertical-align: middle;
max-width: 100%;
}
<div class="container">
<div class="img__container">
<img src="https://images.unsplash.com/photo-1460626399219-57a00a2361cb?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&w=1080&fit=max&s=ea8025ac5c503a77aaf3197534af535b" alt="">
<img src="https://images.unsplash.com/photo-1460400355256-e87506dcec4f?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&w=1080&fit=max&s=65ebb274e22b4db0f6cef789563020c5" alt="">
<img src="https://images.unsplash.com/photo-1453668069544-b8dbea7a0477?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&w=1080&fit=max&s=3693c161a8cf1e3299c913eede08005a" alt="">
</div>
<div class="img__container">
<img src="https://images.unsplash.com/photo-1458724338480-79bc7a8352e4?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&w=1080&fit=max&s=0e8fe82e7f50091319fdc635582bf62d" alt="">
</div>
<div class="img__container">
<img src="https://images.unsplash.com/photo-1421749810611-438cc492b581?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&w=1080&fit=max&s=072549d9d9ee6a1f78d91081068c6ad1" alt="">
<img src="https://images.unsplash.com/photo-1433190152045-5a94184895da?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&w=1080&fit=max&s=57115141c5d099ff83a0aa55c0b219a9" alt="">
</div>
</div>
EDIT 2
* {
box-sizing: border-box;
font-size: 100%;
}
.container {
display: flex;
flex-direction: column;
max-width: 90%;
margin: 0 auto;
background-color: white;
}
.img__container {
display: flex;
align-content: center;
background: yellow;
}
.img_item {
background-size: cover;
background-position: center center;
}
.img_item_1,
.img_item_2,
.img_item_3 {
padding-bottom: 22%;
width: 33.333%;
}
.img_item_4,
.img_item_5,
.img_item_6,
.img_item_7 {
padding-bottom: 33.333%;
width: 50%;
}
.img_item img {
vertical-align: middle;
max-width: 100%;
}
<div class="container">
<div class="img__container">
</div>
<div class="img__container">
</div>
<div class="img__container">
</div>
</div>