Center div vertically and adapt font size - html

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.

Related

How do I align images in a row with equal height?

It seems no matter what I do, I cannot get my images to line up in height. Am I missing something? Originally all images were varying sizes, but I adjusted the resolution in a photo editing software so they are all the same height (1500px) and, for added reinforcement, I have put the height in the image tag as well, yet they are still not lining up and seem to retain their original height/size. It is not a file naming issue as I have checked and double checked these are correct.
Is it a max-width vs max-height issue? They either need to line up uniformly in width or in height, and can't be both unless the images are the exact same sizes? This is my HTML and CSS. I am using a framework called "uiKit" (https://getuikit.com/) and have included its applicable CSS in the code below.
html,
body {
margin: 0;
background-size: cover;
}
.wrapper {
margin-top: 75px;
}
nav {
padding: 25px 25px 75px 175px;
}
address,
dl,
fieldset,
figure,
ol,
p,
pre,
ul {
margin: 0 0 0 0!important;
}
li {
list-style-type: none;
}
.nav-wrapper {
display: flex;
justify-content: space-between;
}
.nav-right {
display: flex;
font-size: 0.875rem;
color: #999;
}
.leftnavpadding {
padding: 0px 0px 0px 20px;
}
footer a {
color: purple;
}
/*Photos page*/
.photoswrapper {
margin: 75px;
}
.photo-grid-padding>* {
padding: 0!important
}
.uk-lightbox-toolbar {
padding: 10px 10px;
background: rgba(0, 0, 0, 0.0);
color: rgba(255, 255, 255, 0.7);
}
.uk-lightbox {
background-color: white;
}
#photomenuwrapper {
padding: 10px;
}
h3.photolink:hover,
.photolink:active {
color: white;
}
/*footer*/
footer {
padding: 150px 100px 100px 100px;
}
.footersocial {
width: 100%;
display: flex;
justify-content: center;
}
.uk-child-width-1-2>* {
width: 50%;
}
.uk-grid {
display: flex;
/* 1 */
flex-wrap: wrap;
/* 2 */
margin: 0;
padding: 0;
list-style: none;
}
[class*='uk-inline'] {
/* 1 */
display: inline-block;
/* 2 */
position: relative;
/* 3 */
max-width: 100%;
/* 4 */
vertical-align: middle;
/* 5 */
-webkit-backface-visibility: hidden;
}
<div class="photowrapper">
<div class="photo-grid-padding uk-child-width-1-2#l" uk-grid>
<div>
<a class="uk-inline" href="houses.htm">
<img src="img/houses/houses01.jpg" height="1500" alt="">
<div class="centered">
<h3 class="photolink">houses</h3>
</div>
</a>
</div>
<div>
<a class="uk-inline" href="yangshuo.htm">
<img src="img/yangshuo/yangshuo1.jpg" height="1500" alt="">
<div class="centered">
<h3 class="photolink">yangshuo</h3>
</div>
</a>
</div>
<div>
<a class="uk-inline" href="nature.htm">
<img src="img/nature/nature1.jpg" height="1500" alt="">
<div class="centered">
<h3 class="photolink">nature</h3>
</div>
</a>
</div>
<div>
<a class="uk-inline" href="buildings.htm">
<img src="img/buildings/buildings1.jpg" height="1500" alt="">
<div class="centered">
<h3 class="photolink">buildings</h3>
</div>
</a>
</div>
<div>
<a class="uk-inline" href="lasvegas.htm">
<img src="img/lasvegas/lasvegas1.jpg" height="1500" alt="">
<div class="centered">
<h3 class="photolink">lasvegas</h3>
</div>
</a>
</div>
<div>
<a class="uk-inline" href="tripinterrupted.htm">
<img src="img/tripinterrupted/tripinterrupted2.jpg" height="1500" alt="">
<div class="centered">
<h3 class="photolink">trip interrupted</h3>
</div>
</a>
</div>
<div>
<a class="uk-inline" href="otherstuff.htm">
<img src="img/otherstuff/other1.jpg" height="1500" alt="">
<div class="centered">
<h3 class="photolink">other stuff</h3>
</div>
</a>
</div>
<div>
<a class="uk-inline" href="design.htm">
<img src="img/design/infographic.jpg" height="1500" alt="">
<div class="centered">
<h3 class="photolink">design
<h3>
</div>
</a>
</div>
To let you know: I tried this suggestion:
https://codepen.io/blimpage/embed/obWdgp?default-tab=result&theme-id=dark
and the images lined up perfectly on a test page with no framework added.
But unfortunately when I inserted that solution into my page with the framework, and specifically when I put the display: flex style in the div around my rows in the grid it messed up the alignment of both the framework and the attempted solution, and neither was then working as they should have been.
I have put the HTML and CSS for that attempted solution below.
Any info or knowledge that would make my images align up in their height to create a clean row would help me out. Please if someone could let me know if I am missing something! Also, am a DIY beginner coder, please be gentle with me, I still have lots to learn!
img {
width: 100%;
height: auto;
vertical-align: middle;
}
.picsinarow {
display: inline-flex
}
}
.container {
background: white;
margin: 0 auto;
padding: 5%;
width: 75%;
}
.b1 {
flex: 1.3431
}
.n1 {
flex: 1.3333
}
<div class="photowrapper">
<div class="photo-grid-padding uk-child-width-1-2#l" uk-grid>
<div class="picinarow">
<a class="uk-inline" href="houses.htm">
<img src="img/houses/houses01.jpg" height="1500" alt="">
<div class="centered">
<h3 class="photolink">houses</h3>
</div>
</a>
</div>
<div>
<a class="uk-inline" href="yangshuo.htm">
<img src="img/yangshuo/yangshuo1.jpg" height="1500" alt="">
<div class="centered">
<h3 class="photolink">yangshuo</h3>
</div>
</a>
</div>
<!-- end of pic in a row -->
</div>
<!-- end of photowrapper -->
To be clear, I am no longer using this code as it caused problems once I tried to integrate it into the uikit framework I am using.
Thank you so much!!

setting an image vertically aligned with text

EDIT: Please disregard the phone/email row.
I would like to reach this kind of result:
Since I need the side texts to be as the same height of the icon, I am having some issues with the relative and absolute div wrappers and how to set them right.
HTML
<div class="row topWrapper">
<div class="iconWrapper">
<i class="fa fa-user-circle-o fa-2x"></i>
</div>
<div class="captionWrapper">
<h5><strong>Liat </strong></h5>
<h5>Your recruitment counsel</h5>
</div>
</div>
SCSS
.topWrapper {
background-color: #d9534f;
height: 150px;
.iconWrapper {
background-color: #f7ecb5;
display: inline-block;
position: relative;
width:16.67%;
i{
position: absolute;
top: 50%;
#include translate(0, -50%);
}
}
.captionWrapper{
background-color: #245580;
display: inline-block;
}
}
You don't need to use absolute position to get what you want, just a few things and you are good to go.
See code snippet:
Note: i didn't use SCSS for demonstration purpose
.topWrapper {
background-color: #d9534f;
height: 150px;
padding: 5px;
}
.iconWrapper {
display: inline-block;
font-size: 17px;
}
.captionWrapper {
display: inline-block;
}
.captionWrapper h5 {
margin: 2px;
/*reduce the margin to make the two texts aligned with the icon*/
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<div class="row topWrapper">
<div class="iconWrapper">
<i class="fa fa-user-circle-o fa-2x"></i>
</div>
<div class="captionWrapper">
<h5><strong>Liat </strong></h5>
<h5>Your recruitment counsel</h5>
</div>
</div>

CSS mask for image grid

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

Scalable circles with dynamic numbers inside

Apologies if this has been asked and answered already, as I was not able to find an appropriate solution to this problem.
I need to work on a site navigation that require some numbers to be placed inside circles. The circles will increase in diameter based on the length of the text inside. I'm looking for a very elegant, preferably css only solution for this. Please note the alignment of the circles here with respect to the entire row and label text on the mock up attached.
It required some CSS trickery to get this working, but this works in the latest version Chrome and Firefox. Let me know if you have any other questions.
html,
body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
color: #9653DA;
font: 600 14px sans-serif;
}
.nav-table {
display: table;
text-align: center;
}
.nav-row {
display: table-row;
}
.nav-col {
display: table-cell;
}
.text {
margin: 1em;
}
.icon {
display: inline-block;
border-radius: 100%;
border: 2px solid;
min-width: 10px;
padding: 0.5em;
margin: 0.5em;
}
.icon div {
position: relative;
height: 0;
padding: 50% 0;
top: -7px; /* Half of font-size, in our case it is (14px / 2) */
}
<div class="nav-table">
<div class="nav-row">
<div class="nav-col">
<div class="icon">
<div>20</div>
</div>
</div>
<div class="nav-col">
<div class="icon">
<div>300</div>
</div>
</div>
<div class="nav-col">
<div class="icon">
<div>50</div>
</div>
</div>
<div class="nav-col">
<div class="icon">
<div>1</div>
</div>
</div>
</div>
<div class="nav-row">
<div class="nav-col">
<div class="text">Japanese</div>
</div>
<div class="nav-col">
<div class="text">Main Course</div>
</div>
<div class="nav-col">
<div class="text">Non Vegetarian</div>
</div>
<div class="nav-col">
<div class="text">Beginners</div>
</div>
</div>
</div>

Displaying images in a row and indicating selection - HTML and CSS

What I would like to achieve (the goal)...
I'm trying to display a row of images (with the image name below the image) in html, like so:
When a user clicks on an image I want a square to appear over the image, to indicated selection like so (user has clicked on Tile1):
What I have done so far...
So far I have managed to display the tiles in a row:
Here's the html code that produced the image above:
<div id='default_tiles_view'>
<div class="default_tiles_view_square" id="tile1">
<img src="https://raw.github.com/andrespagella/Making-Isometric-Real-time-Games/master/img/tile.png">
<p>Tile1</p>
</div>
<div class="default_tiles_view_square" id="tile2">
<img src="https://raw.github.com/andrespagella/Making-Isometric-Real-time-Games/master/img/dirt.png">
<p>Tile2</p>
</div>
</div>
And the CSS:
#default_tiles_view {
width: 490px;
height: 160px;
overflow-y: auto;
}
.default_tiles_view_square {
display: inline-block;
}
.default_tiles_view_square p {
text-align: center;
}
And a fiddle showing the example above: http://jsfiddle.net/jamiefearon/t8d6U/
The strategy to achieve the goal...
I was thinking about wrapping the image and its title in a div, and then changing the background colour of the div. Here is the result and the code:
HTML:
<div id='default_tiles_view'>
<div class="tile_wrap" id="tile1">
<div class="default_tiles_view_square">
<img src="https://raw.github.com/andrespagella/Making-Isometric-Real-time-Games/master/img/tile.png">
<p>Tile1</p>
</div>
</div>
<div class="tile_wrap" id="tile2">
<div class="default_tiles_view_square">
<img src="https://raw.github.com/andrespagella/Making-Isometric-Real-time-Games/master/img/dirt.png">
<p>Tile2</p>
</div>
</div>
</div>
CSS:
#default_tiles_view {
width: 490px;
height: 160px;
overflow-y: auto;
}
.tile_wrap {
display: inline-block;
}
.default_tiles_view_square p {
text-align: center;
}
#tile1 {
background-color:red;
}
The Problem..
It does not look good, and the actual image is not covered by the red colour. Maybe it would be possible to overlay a div over the wrap div, set it's opacity < 1 and change its background colour.
What do think? Does anyone have any ideas of a good way to achieve the goal?
Something like this should do the trick: http://jsfiddle.net/t8d6U/1/
So just hide the overlay DIVs initially with display:none (or e.g. left:-9999px) then show them onClick.
CSS:
#default_tiles_view {
overflow: auto;
}
.default_tiles_view_square {
float: left;
margin: 5px 10px 10px 10px;
position: relative;
height: 128px;
width: 128px;
}
.default_tiles_view_square p {
text-align: center;
}
.content {
position: absolute;
z-index: 1;
top: 0;
left: 0;
}
.overlay {
position: absolute;
z-index: 2;
background: red;
opacity: 0.5;
height: 128px;
width: 128px;
top: 0;
left: 0;
}
HTML:
<div id='default_tiles_view'>
<div class="default_tiles_view_square" id="tile1">
<div class="content">
<img src="https://raw.github.com/andrespagella/Making-Isometric-Real-time-Games/master/img/tile.png">
<p>Tile1</p>
</div>
<div class="overlay"></div>
</div>
<div class="default_tiles_view_square" id="tile2">
<div class="content">
<img src="https://raw.github.com/andrespagella/Making-Isometric-Real-time-Games/master/img/dirt.png">
<p>Tile2</p>
</div>
<div class="overlay"></div>
</div>
</div>
I have taken what Gaurav said in the comments, changing the opacity:
HTML
<div id='default_tiles_view'>
<div class="tile_wrap" id="tile1">
<div class="default_tiles_view_square">
<img src="https://raw.github.com/andrespagella/Making-Isometric-Real-time-Games/master/img/tile.png">
<p>Tile1</p>
</div>
</div>
<div class="tile_wrap" id="tile2">
<div class="default_tiles_view_square">
<img src="https://raw.github.com/andrespagella/Making-Isometric-Real-time-Games/master/img/dirt.png">
<p>Tile2</p>
</div>
</div>
</div>
CSS
#default_tiles_view {
width: 490px;
height: 160px;
overflow-y: auto;
}
#tile1:hover{
background:red;
opacity:0.4;
}
.tile_wrap {
display: inline-block;
}
.default_tiles_view_square p {
text-align: center;
}
Fiddle: http://jsfiddle.net/jamiefearon/BY9Fp/
Hover over Tile1 to see the effect.