How to create an image grid with images nicely lined up in same height rows and same width columns, giving we could have pictures of different aspect ratios in responsive design, in CSS?
Let's take this example page with grid, it's self-explanatory:
http://destadesign.com/test/capricorn/test.html
The image in second row sticks out of the grid.
Responsive design rather require from us to use percent(%) values instead of fixed pixel values, and only for width, so that height is calculated automatically, which in this case complicate things.
I'd think of a clipping mask(?) for images to accomplish that (divs of the same size, holding different size images), however I lack the skill for creating such complex CSS. Any specific help would be highly appreciated, but also some general ideas and guidelines would come in handy.
HTML div-imgage boxes has such (rather natural?) structure:
<div id="1" class="figure">
<a href="#" class="link1">
<img src="images/pic_mountain.jpg" alt="TARGI W PARYŻU">
<div class="figcaption">
<h4>test 1</h4>
</div>
</a>
</div><!-- /end .figure -->
CSS with non-essential hover and text-centering effect:
.figure {
position: relative;
float: left;
width: 10%;
margin-right: 1%;
left:20px;
}
.figure a{
display:block;
width:100%;
height:100%;
position:relative;
z-index:2;
}
.figure a img{
width:100%;
display:block;
}
.figcaption {
font-size: 0.8em;
letter-spacing: 0.1em;
text-align: center;
position: absolute;
top: 0;
left:0;
width:100%;
z-index: 2;
height:100%;
background-color:rgba(0,0,0,.4);
transition:all 0.4s ease;
}
.figcaption h4{
position:absolute;
top:50%;
left:50%;
padding:0;
margin:0;
-moz-transform:translate(-50%, -50%);
-webkit-transform:translate(-50%, -50%);
transform:translate(-50%, -50%);
}
.figure a:hover .figcaption {
opacity:0;
}
Please go ahead and use this CodePen for convenience:
http://codepen.io/anon/pen/GopQPZ
Without using CSS masks, an example:
body {
background-color: black;
text-align: center;
}
.figure {
display: inline-block;
margin: 2vw;
}
a {
border: 2px solid grey;
border-radius: 5px;
display: inline-block;
height: 10vw;
overflow: hidden;
position: relative;
width: 10vw;
}
a img {
height: 150%;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
}
<div id="figure1" class="figure">
<a href="http://static.comicvine.com/uploads/scale_super/8/82727/1525513-the_moutain____by_vincentfavre.jpg" class="link1" target="_blank">
<img src="http://static.comicvine.com/uploads/scale_super/8/82727/1525513-the_moutain____by_vincentfavre.jpg" alt="TARGI W PARYŻU">
<div class="figcaption">
<h4>test 1</h4>
</div>
</a>
</div>
<div id="figure2" class="figure">
<a href="http://www.britishairways.com/cms/global/assets/images/content/760x350_beach_Pod-3.jpg" class="link2" target="_blank">
<img src="http://www.britishairways.com/cms/global/assets/images/content/760x350_beach_Pod-3.jpg" alt="TARGI W PARYŻU">
<div class="figcaption">
<h4>test 2</h4>
</div>
</a>
</div>
<div id="figure3" class="figure">
<a href="http://www.barnesandnoble.com/blog/barnesy/wp-content/uploads/2013/08/country_western.jpg" class="link3" target="_blank">
<img src="http://www.barnesandnoble.com/blog/barnesy/wp-content/uploads/2013/08/country_western.jpg">
<div class="figcaption">
<h4>test 3</h4>
</div>
</a>
</div>
<div id="figure4" class="figure">
<a href="http://junebugweddings.com/img/photobug/January2013/beautiful-landscape-wedding-portrait-by-julian-kanz.jpg" class="link4" target="_blank">
<img src="http://junebugweddings.com/img/photobug/January2013/beautiful-landscape-wedding-portrait-by-julian-kanz.jpg">
<div class="figcaption">
<h4>test 4</h4>
</div>
</a>
</div>
Here's an extremely nice CSS grid that's completely responsive. It takes images of any size and you can choose the amount of columns and the space between the images. With media queries you can also make it so that the number of columns changes depending on the size of the device.
https://goo.gl/5SFndB
Credit goes to Chris Coyier. I've used that grid a countless amount of times! It's great.
Also, here's a pen from the same post. It's a beauty!
#photos {
/* Prevent vertical gaps */
line-height: 0;
-webkit-column-count: 5;
-webkit-column-gap: 0px;
-moz-column-count: 5;
-moz-column-gap: 0px;
column-count: 5;
column-gap: 0px;
}
http://codepen.io/team/css-tricks/pen/pvamyK
Related
This question already has answers here:
CSS-only masonry layout
(4 answers)
Closed 2 years ago.
I have 4 divs that lay two in row and two columns. They are different height and I want it to be taken into consideration when displaying on page. I always want the lower divs to stick vertically to upper ones like in the left side.
Now the code.
app.component.html
<div id="wrapper">
<div id="content">
<div id="first-row">
<div class="col">
<br>
<a routerLink="/projects/endless-blow">Endelss Blow</a>
<br>
<a routerLink="/projects/endless-blow"><img src="assets/images/endlessblow_icon.png"></a>
<br><br>
<hr>
<br>
<div class="technology_images">
<img src="https://freeiconshop.com/wp-content/uploads/edd/android-flat.png" width="75px" height="75px">
<img src="https://cdn.iconscout.com/icon/free/png-256/java-23-225999.png">
<img src="https://inforce.rs/wp-content/uploads/2018/07/spring-boot-logo-e1530901263416.png" width="188px" height="75px">
<img src="assets/images/postgresql_icon.png" width="75px" height="75px">
</div>
</div>
<div class="col">
<br>
<a routerLink="/projects/book-library">Book Library API</a>
<br>
<a routerLink="/projects/book-library"><img src="assets/images/win_console_icon.png"></a>
<br><br>
<hr>
<br>
<div class="technology_images">
<img src="assets/images/java_icon.png">
<img src="assets/images/jetty_icon.png" width="265px" height="75px">
</div>
</div>
</div>
<div id="second-row">
<div class="col">
<br>
<a routerLink="/projects/endless-blow">Endelss Blow</a>
<br>
<a routerLink="/projects/endless-blow"><img src="assets/images/endlessblow_icon.png"></a>
<br><br>
<hr>
<br>
<div class="technology_images">
<img src="assets/images/android_icon.png" width="75px" height="75px">
<img src="assets/images/java_icon.png">
<img src="assets/images/spring_boot_icon.png" width="188px" height="75px">
<img src="assets/images/postgresql_icon.png" width="75px" height="75px">
</div>
</div>
<div class="col">
<br>
<a routerLink="/projects/book-library">Book Library API</a>
<br>
<a routerLink="/projects/book-library"><img src="assets/images/win_console_icon.png"></a>
<br><br>
<hr>
<br>
<div class="technology_images">
<img src="assets/images/java_icon.png">
<img src="assets/images/jetty_icon.png" width="265px" height="75px">
</div>
</div>
</div>
</div>
</div>
app.component.css
html, body {
margin: 0;
padding: 0;
align-items: center;
text-align: center;
}
body {
border: 3px solid green;
max-width: 600px;
margin: 0 auto;
color: #fff;
}
#wrapper {
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
z-index: -1;
background: darkcyan;
}
#content {
border: 3px solid red;
box-sizing: border-box;
position: absolute;
padding: 0;
margin-left: 0;
margin-right: 0;
text-align: center;
overflow: auto;
left: 0;
right: 0;
top: 70px;
width: 100%;
}
#content a {
font-size: 25px;
}
#first-row {
margin-top: 20px;
}
.col {
border-radius: 15px;
-moz-border-radius: 15px;
display: inline-block;
width: 350px;
min-height: 350px;
border: 3px solid green;
background: rgba(150, 150, 50, 0.8);
vertical-align: top;
text-align:center;
margin: 0 40px;
/* margin-right: 100px; */
}
.col a img {
padding:0;
left:0;
right:0;
margin: 0 auto;
}
.technology_images {
text-align: left;
}
div:not(.technology_images, .col) {
min-height: 400px;
box-sizing: border-box;
width: 100%;
}
If you need code uploaded with result just take a look here
I did not see any topic that corresponds to my problem so please do not close this topic.
Thank you! :)
This is really hard to achieve with HTML/CSS, and be flexible at the same time, nearly to impossible I would say, because in the real world, you might not know what height and width your elements really are and how many there are on each column.
The only HTML/CSS solution (if this fits your case) would be to create a single row, with multiple columns in it and display all the items by column. For this one, you really need to know beforehand exactly how many elements you would have on each column.
For a flexible solutions, I would really recommend using Masonry. This library would help you achieve the result you need really fast without having to do a complicated layout, which will also be complicated to maintain in the long run when multiple items might get added.
I know this question has been asked a lot, but I haven't been able to find a solution among the answers. I'm trying to center a text on an image, but I'm having some trouble with the vertical alignment. This is my code:
<div class="col-sm-12 col-md-12 col-lg-12 main_category_container">
<div class="col-sm-4 col-md-4 col-lg-4">
<div class="wrap">
<h3 class="sub_category_title">Centered text</h3>
<img src="some image">
</div>
</div>
</div>
and
.wrap {
height:auto;
margin: auto;
text-align:center;
position:relative;
}
h3.sub_category_title {
vertical-align: middle;
}
Right now the text is horizontally centered above the image, but I can't figure out how to vertically align it in the middle of the image.
Thanks in advance!
Generally if you're trying to put one element overlapping another, you'll end up using position: absolute or position: relative - otherwise, the contents will naturally arrange themselves in a block flow.
You could try something like this:
.image-wrapper {
position: relative;
text-align: center;
width: 300px; /* change or remove as needed */
margin: auto;
}
.image-link { display: block; }
.image-text {
position: absolute;
top: 50%;
left: 0;
right: 0;
margin: auto;
/* The rest of this is just for this demo,
you can change it for your situation. */
font-weight: bold;
color: white;
font-family: sans-serif;
background: rgba(0, 0, 0, 0.5);
}
<div class="image-wrapper">
<a class="image-link" href="#">
<img class="image" src="http://loremflickr.com/300/200" alt="kitten!">
<h3 class="image-text">A Centered Label</h3>
</a>
</div>
Okay, so right now I'm building a website. and the thing is, I have a hover element on parts of it. Now when I hover over it I want these two icons to appear, which, I'm perfectly capable of doing. But the problem is, I have two icons and I want them centered. But I'm having trouble centering them. This is what my code looks like by the way. I want them both centered in the middle, but not overlapping. Thanks!
<p>
<div class="golf">
<img src="http://i.imgur.com/aJi53jD.jpg" alt="Wingardium Leviosa!" align="left">
<div class="charahover">
<img src="https://raw.githubusercontent.com/Clarachun/framefinds.com/gh-pages/Pictures/heart.png" id="class" onclick="myFunction()">
<img src="https://raw.githubusercontent.com/Clarachun/framefinds.com/gh-pages/Pictures/locations%20sign.png" id="class">
</div>
</div>
<div class="foam">
<span style="font-size:30px;">The Rainbow Crosswalk</span><br>
Be prideful at this brightly colored crosswalk in West Hollywood!<br><br><br>
Santa Monica Boulevard (at San Vicente Boulevard)<br>
West Hollywood, CA 90069<br>
United States<br><br><br>
<a href="">
<span style="font-size: 20px;">Click here for Map!</span>
</a>
</div>
</p>
and this is my css
.foam {
width: 40%;
margin-left: auto;
padding-left: 30px;
text-align: center;
}
.charahover {
position:absolute;
z-index:10;
background-color:#333;
opacity:0;
transition:1s opacity;
overflow:hidden;
width: 432px;
position:absolute;
}
.charahover:hover {
opacity:0.9;
}
.charahover img {
width: 33.3%;
padding: 10px;
display: block;
margin-left: auto;
margin-right: auto;
}
.golf {
width: 60%;
}
I've edited your code in a jsFiddle (and removed extraneous markup/CSS) - https://jsfiddle.net/Shuaso/9g99ugsh/
By applying the CSS property "display: inline-block;" to the container div, it's width is set to the width of it's children, which in this case is the width of the image of "The Rainbow Crosswalk." And then, setting the width of the .charahover div to 100% makes it span the full width of it's container, which is the same width as the image, thus centering the hovered images. And you have to apply positioning to the container div as well so that when you set the .charahover div to 100% it can take that width of it, as this is only possible with a positioned element. Also, you have multiple ID's of the same name, so I would change that as it's not valid markup.
HTML:
<div class="golf">
<img src="http://i.imgur.com/aJi53jD.jpg" alt="Wingardium Leviosa!" align="left">
<div class="charahover">
<img src="https://raw.githubusercontent.com/Clarachun/framefinds.com/gh-pages/Pictures/heart.png" id="class" onclick="myFunction()">
<img src="https://raw.githubusercontent.com/Clarachun/framefinds.com/gh-pages/Pictures/locations%20sign.png" id="class">
</div>
</div>
CSS:
.charahover {
position: absolute;
z-index: 10;
background-color: #333;
opacity: 0;
transition: 1s opacity;
overflow: hidden;
width: inherit;
}
.charahover:hover {
opacity: 0.9;
}
.charahover img {
width: 100%;
padding: 10px;
display: block;
margin-left: auto;
margin-right: auto;
}
.golf {
display: inline-block;
position: relative;
}
I need to display a gallery of images where over each image I have an icon and a number.
These elements are inside an overlay DIV which shows when the mouse is over.
I am trying to do two things:
The vote div should be vertically aligned inside the overlay div;
I would like the heart font size to adapt depending on the image width.
Can these two problems be solved?
My code and JSFiddle Example:
<div class="gallery">
<div class="image">
<img src="http://placehold.it/600x600" alt="" />
<div class="overlay">
<div class="vote">
<i class="fa fa-heart"></i>
<span>350</span>
</div>
</div>
</div>
<div class="image">
<img src="http://placehold.it/600x600" alt="" />
<div class="overlay">
<div class="vote">
<i class="fa fa-heart"></i>
<span>350</span>
</div>
</div>
</div>
<div class="image">
<img src="http://placehold.it/600x600" alt="" />
<div class="overlay">
<div class="vote">
<i class="fa fa-heart"></i>
<span>350</span>
</div>
</div>
</div>
<div class="image">
<img src="http://placehold.it/600x600" alt="" />
<div class="overlay">
<div class="vote">
<i class="fa fa-heart"></i>
<span>350</span>
</div>
</div>
</div>
<div class="image">
<img src="http://placehold.it/600x600" alt="" />
<div class="overlay">
<div class="vote">
<i class="fa fa-heart"></i>
<span>350</span>
</div>
</div>
</div>
</div>
* {
-webkit-box-sizing: box-model;
-moz-box-sizing: box-model;
box-sizing: box-model;
}
*:before, *:after {
-webkit-box-sizing: inherit;
-moz-box-sizing: inherit;
box-sizing: inherit;
}
.gallery {
overflow: hidden;
}
.image {
position: relative;
text-align: center;
float: left;
width: 100%;
}
#media screen and (min-width: 600px) {
.image {
width: 50%;
}
}
img {
display: block;
height: auto;
max-width: 100%;
outline: 0;
}
.overlay {
background-color: #404040;
color: #FFFFFF;
display: none;
height: 100%;
font-size: 0.75rem;
padding: 4px;
position: absolute;
top: 0;
overflow: hidden;
width: 100%;
}
.image:hover .overlay {
display: block;
-moz-opacity: 0.8;
-khtml-opacity: 0.8;
-webkit-opacity: 0.8;
opacity: 0.8;
}
.vote {
}
.vote a {
text-decoration: none;
}
.vote i {
color: red;
display: block;
line-height: 1.0;
font-size: 8rem;
}
.vote span {
display: block;
font-size: 2rem;
}
You can center the vote class like this:
.vote {
position: relative;
top: 50%;
transform: translateY(-50%)
}
See http://zerosixthree.se/vertical-align-anything-with-just-3-lines-of-css/ for how this works.
Scaling the font is trickier since you're using font-awesome. I don't think you can do so with CSS.
Here's a JavaScript solution, which sets the font-size as 30% of the height of each image:
var images= document.querySelectorAll('.image');
for(var i = 0 ; i < images.length ; i++) {
var height= images[i].offsetHeight;
var heart= images[i].querySelector('.fa');
var span= images[i].querySelector('span');
heart.style.fontSize= span.style.fontSize= (height*0.3)+'px';
}
Fiddle
Ok, #1 is easy: there are multiple possible solutions. Actually was a a really good extensive article about that on the internet some time ago: Vertical Centering With CSS
Or, you could use a modern solution like flexbox:
<div class="wrapper">
<div>
Centered vertically and horizontally
</div>
</div>
.wrapper {
display: flex;
align-items: center;
justify-content: center;
}
Please remember to include prefixed versions for that if and where needed and check caniuse.com to see what browsers don't support this solution before deploying it.
As for #2: Took me quite some time to figure out that it's actually much easier than I thought:
.vote {
font-size: 30vw;
}
#media screen and (min-width: 600px) {
.vote {
font-size:15vw;
}
}
Adjust the value to your liking. Again: Viewport units are not totally supported by all browsers (cough IE cough), please check caniuse. I don't have a copy of IE handy just now, but I think vw should be supported, so I see no problems here. And keep in mind that Safari on iOS does some weird stuff, although to my knowledge that only affects vh and not vw.
I have a fixed positioned div at the bottom of my web (oriented just for mobiles). Inside I have 5 links, and I would like these links to be perfect squares always but I want to avoid to use any fixed size so I'm trying always to use "%". These squares needs to be always distribute using the full width of the container.
html so simple:
<div class="container">
<a href="#" class="facebook">
</a>
<a href="#" class="info">
</a>
<a href="#" class="contacto">
</a>
<a href="#" class="telefono">
</a>
<a href="#" class="galeria">
</a>
</div>
and my css's so far:
.container {
width:90%;
height:20%;
position:fixed;
bottom:5%;
left:5%;
overflow:hidden;
}
.container a {
width: 18.4%;
margin-right: 2%;
height:100%;
background-color: blue;
float:left;
}
.container a:last-child {margin-right: 0;}
Here you have the jsfiddle: http://jsfiddle.net/7jJsf/3/
So, could it be possible to make the links perfect squares whatever width or height the browser have?
or, if maybe my aproach is not good enough, any other way to make it?
note: I think I could do it probably using a square img inside every link but I would love to avoid the use of innecesary images.
thanks in advance and excuse my english, hope the question is clear enough.
I have already answered this issue here.
This solution uses a dummy div and position: absolute; to make responsive elements that keep their aspect ratio. This is a CSS solution.
To adapt it to your situation, you can do this :
FIDDLE
HTML :
<div class="container">
<div class="wrap">
<div class="dummy"></div>
</div>
<div class="wrap">
<div class="dummy"></div>
</div>
<div class="wrap">
<div class="dummy"></div>
</div>
<div class="wrap">
<div class="dummy"></div>
</div>
<div class="wrap">
<div class="dummy"></div>
</div>
</div>
CSS :
.container {
width:90%;
position:fixed;
bottom:5%;
left:5%;
}
.container .wrap {
float:left;
position: relative;
width: 30%;
margin-right:1%;
width: 18.4%;
margin-right: 2%;
height:100%;
float:left;
}
.container .dummy {
margin-top: 100%;
}
.container a {
display:block;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
background-color: blue;
}
.container>div:last-child {
margin-right: 0;
}
I would set the width and height to the viewport width (a percentage of the viewport width) like this:
width:16vw;
height:16vw;
Your fiddle
Use this little trick
.container a {
margin-right: 2%;
background-color: blue;
float:left;
position: relative;
width: 18.4%;
padding-bottom: 18.4%;
}