Is there a way so that when I open my menu, it does not go beyond the first section or scrolling will be disabled once the menu is open? I tried giving every section z-index 999 but that does not work
HTML:
<section class="intro">
<label>
<input type="checkbox">
<span class="menu">
<span class="hamburger"></span>
</span>
<ul>
<li>
Home
</li>
<li>
About
</li>
<li>
Contact
</li>
</ul>
</label>
<div class="text">
<h1>my<br>example<br>text</h1>
</div>
</div>
</section>
<section class="first">
</section>
<section class="second">
</section>
<section class="third">
</section>
CSS:
/* Section styling */
body, html {
height: 100%;
margin: 0;
}
* {
box-sizing: border-box;
}
section {
width: 100%;
display: table;
margin: 0;
max-width: none;
height: 100vh;
}
.intro {
background-color:#291411;
}
.first {
background-color:#834940;
}
.second {
background-color:#291411;
}
.third {
background-color:#834940;
}
/* Hide scrollbars */
html {
overflow: scroll;
overflow-x: hidden;
}
::-webkit-scrollbar {
width: 0px;
background: transparent;
}
::-webkit-scrollbar-thumb {
background: #FF0000;
}
/* Menu before */
label .hamburger {
position: absolute;
top: 135px;
left: 50px;
width: 30px;
height: 2px;
background: #291411;
display: block;
-webkit-transform-origin: center;
transform-origin: center;
-webkit-transition: .5s ease-in-out;
transition: .5s ease-in-out;
}
label .hamburger:after, label .hamburger:before {
-webkit-transition: .5s ease-in-out;
transition: .5s ease-in-out;
content: "";
position: absolute;
display: block;
width: 100%;
height: 100%;
background: #291411;
}
label .hamburger:before { top: -10px; }
label .hamburger:after { bottom: -10px; }
label input { display: none; }
label input:checked + .menu {
box-shadow: 0 0 0 100vw #E0C9B7, 0 0 0 100vh #E0C9B7;
border-radius: 0;
}
label input:checked + .menu .hamburger {
-webkit-transform: rotate(45deg);
transform: rotate(45deg);
}
label input:checked + .menu .hamburger:after {
-webkit-transform: rotate(90deg);
transform: rotate(90deg);
bottom: 0;
}
label input:checked + .menu .hamburger:before {
-webkit-transform: rotate(90deg);
transform: rotate(90deg);
top: 0;
}
label input:checked + .menu + ul { opacity: 1; }
/* Menu after */
label .menu {
position: absolute;
right: -100px;
top: -100px;
z-index: 100;
width: 200px;
height: 200px;
background: #E0C9B7;
border-radius: 50% 50% 50% 50%;
-webkit-transition: .5s ease-in-out;
transition: .5s ease-in-out;
box-shadow: 0 0 0 0 #E0C9B7, 0 0 0 0 #E0C9B7;
cursor: pointer;
}
label ul {
z-index: 200;
position: absolute;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
opacity: 0;
-webkit-transition: .25s 0s ease-in-out;
transition: .25s 0s ease-in-out;
list-style-type:none;
}
label a {
margin-bottom: 1em;
display: block;
color: #291411;
text-decoration: none;
}
/* Footer styling */
footer {
padding: 1% 5%;
text-align:center;
background-color: #291411;
color: #E0C9B7;
}
To disable the scroll when the menu is open I tried to add overflow-x:hidden to the html and body but this makes the whole page weird
Did a simple solution with jquery, you can do this with javascript. I used jquery just to save time.
$(".js-change").on("change", function() {
$("html").toggleClass("menu-open");
});
/* Section styling */
body, html {
height: 100%;
margin: 0;
}
* {
box-sizing: border-box;
}
section {
width: 100%;
display: table;
margin: 0;
max-width: none;
height: 100vh;
}
.intro {
background-color:#291411;
}
.first {
background-color:#834940;
}
.second {
background-color:#291411;
}
.third {
background-color:#834940;
}
/* Hide scrollbars */
html {
overflow: scroll;
overflow-x: hidden;
}
::-webkit-scrollbar {
width: 0px;
background: transparent;
}
::-webkit-scrollbar-thumb {
background: #FF0000;
}
/* Menu before */
label .hamburger {
position: absolute;
top: 135px;
left: 50px;
width: 30px;
height: 2px;
background: #291411;
display: block;
-webkit-transform-origin: center;
transform-origin: center;
-webkit-transition: .5s ease-in-out;
transition: .5s ease-in-out;
}
label .hamburger:after, label .hamburger:before {
-webkit-transition: .5s ease-in-out;
transition: .5s ease-in-out;
content: "";
position: absolute;
display: block;
width: 100%;
height: 100%;
background: #291411;
}
label .hamburger:before { top: -10px; }
label .hamburger:after { bottom: -10px; }
label input { display: none; }
label input:checked + .menu {
box-shadow: 0 0 0 100vw #E0C9B7, 0 0 0 100vh #E0C9B7;
border-radius: 0;
}
label input:checked + .menu .hamburger {
-webkit-transform: rotate(45deg);
transform: rotate(45deg);
}
label input:checked + .menu .hamburger:after {
-webkit-transform: rotate(90deg);
transform: rotate(90deg);
bottom: 0;
}
label input:checked + .menu .hamburger:before {
-webkit-transform: rotate(90deg);
transform: rotate(90deg);
top: 0;
}
label input:checked + .menu + ul { opacity: 1; }
/* Menu after */
label .menu {
position: absolute;
right: -100px;
top: -100px;
z-index: 100;
width: 200px;
height: 200px;
background: #E0C9B7;
border-radius: 50% 50% 50% 50%;
-webkit-transition: .5s ease-in-out;
transition: .5s ease-in-out;
box-shadow: 0 0 0 0 #E0C9B7, 0 0 0 0 #E0C9B7;
cursor: pointer;
}
label ul {
z-index: 200;
position: absolute;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
opacity: 0;
-webkit-transition: .25s 0s ease-in-out;
transition: .25s 0s ease-in-out;
list-style-type:none;
}
label a {
margin-bottom: 1em;
display: block;
color: #291411;
text-decoration: none;
}
/* Footer styling */
footer {
padding: 1% 5%;
text-align:center;
background-color: #291411;
color: #E0C9B7;
}
.menu-open,
.menu-open body {
overflow:hidden;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section class="intro">
<label>
<input type="checkbox" class="js-change">
<span class="menu">
<span class="hamburger"></span>
</span>
<ul>
<li>
Home
</li>
<li>
About
</li>
<li>
Contact
</li>
</ul>
</label>
<div class="text">
<h1>my<br>example<br>text</h1>
</div>
</div>
</section>
<section class="first">
</section>
<section class="second">
</section>
<section class="third">
</section>
Related
This question already has answers here:
Is there a CSS parent selector?
(33 answers)
Closed 7 months ago.
I have a navigation bar and a background (class="modal") which are set to
display: none;
How do I make it
display: block;
when the input checkbox menu-check is checked using only CSS.
[I have only shown the class modal since both navigation and modal classes act similarly]
.modal {
display: none;
position: absolute;
height: 100%;
top: 0;
left: 0;
right: 0;
background: #00827f;
}
.menu-btn {
position: fixed;
display: flex;
flex-direction: column;
gap: 5px;
bottom: 20px;
right: 20px;
padding: 15px;
border-radius: 20px;
z-index: 10;
background-color: #08ccc9;
cursor: pointer;
}
.menu-btn:hover>span:nth-of-type(1) {
transform-origin: right;
transform: scaleX(0.5);
}
.menu-btn:hover>span:nth-of-type(3) {
transform-origin: left;
transform: scaleX(0.5);
}
.menu-btn input {
display: none;
}
.menu-btn span {
width: 25px;
height: 4px;
border-radius: 999px;
background-color: black;
transition: all 0.3s ease;
}
.menu-btn input:checked~span:nth-of-type(1) {
transform: rotate(45deg) translate(5px, 7px);
transform-origin: center;
transition: all 0.3s ease, transform-origin 0s;
}
.menu-btn input:checked~span:nth-of-type(2) {
transform: translate(30px, 0px);
opacity: 0;
}
.menu-btn input:checked~span:nth-of-type(3) {
transform: rotate(-45deg) translate(5px, -7px);
transform-origin: center;
transition: all 0.3s ease, transform-origin 0s;
}
<label for="menu-check" class="menu-btn">
<input type="checkbox" id="menu-check">
<span></span>
<span></span>
<span></span>
</label>
<div class="modal"></div>
You need to change your HTML so that the checkbox is at the same level in the DOM as the modal. You can only select subsequent or child elements, not previous or ancestral elements.
#menu-check {display:none;}
.modal {
display: none;
position: absolute;
height: 100%;
top: 0;
left: 0;
right: 0;
background: #00827f;
}
.menu-btn {
position: fixed;
display: flex;
flex-direction: column;
gap: 5px;
bottom: 20px;
right: 20px;
padding: 15px;
border-radius: 20px;
z-index: 10;
background-color: #08ccc9;
cursor: pointer;
}
.menu-btn:hover>span:nth-of-type(1) {
transform-origin: right;
transform: scaleX(0.5);
}
.menu-btn:hover>span:nth-of-type(3) {
transform-origin: left;
transform: scaleX(0.5);
}
.menu-btn span {
width: 25px;
height: 4px;
border-radius: 999px;
background-color: black;
transition: all 0.3s ease;
}
/*Check Box CSS Manipulation*/
#menu-check:checked ~ .modal{
display:block;
}
#menu-check:checked ~ .menu-btn span:nth-of-type(1) {
transform: rotate(45deg) translate(5px, 7px);
transform-origin: center;
transition: all 0.3s ease, transform-origin 0s;
}
#menu-check:checked ~ .menu-btn span:nth-of-type(2) {
transform: translate(30px, 0px);
opacity: 0;
}
#menu-check:checked ~ .menu-btn span:nth-of-type(3) {
transform: rotate(-45deg) translate(5px, -7px);
transform-origin: center;
transition: all 0.3s ease, transform-origin 0s;
}
<input type="checkbox" id="menu-check">
<label for="menu-check" class="menu-btn">
<span></span>
<span></span>
<span></span>
</label>
<div class="modal"></div>
I had codyhouse's css animations tutorial which was really nice. We wrote this navigation menu and I wanted to tweak it further to my likes.
I guess I'm trying to show the navigation links on screens that are bigger than 640px and make it invisible (like this current state) on all other screen sizes.
Codepen
var navTrigger = document.getElementsByClassName('nav-trigger')[0],
body = document.getElementsByTagName('body')[0];
navTrigger.addEventListener('click', toggleNavigation);
function toggleNavigation(event) {
event.preventDefault();
body.classList.toggle('nav-open');
}
*, *::after, *::before {
box-sizing: border-box;
}
body {
font-family: sans-serif;
line-height: 1;
margin: 0;
width: 100%;
height: 100%;
background: #333;
}
main {
position: relative;
z-index: 1;
height: 100vh;
overflow: hidden;
transition: transform .5s;
box-shadow: 0 0 50px #000;
}
.nav-open main {
transform: scale(.8)
}
.intro {
height: 100vh;
width: 100%;
display: table;
background: #f3f3f3;
}
h1 {
display: table-cell;
vertical-align: middle;
text-align: center;
font-size: 45px;
color: #ddd;
}
.nav-trigger {
position: fixed;
z-index: 4;
top: 40px;
right: 40px;
height: 44px;
width: 44px;
overflow: hidden;
color: transparent;
white-space: nowrap;
text-indent: 100%;
}
.nav-trigger span,
.nav-trigger span::before,
.nav-trigger span::after {
position: absolute;
height: 4px;
width: 36px;
background: #111;
transition: all .3s;
}
.nav-trigger span {
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.nav-trigger span::before,
.nav-trigger span::after {
content: '';
top: 0;
left: 0;
}
.nav-trigger span::before {
transform: translateY(-12px);
}
.nav-trigger span::after {
transform: translateY(12px);
}
.nav-trigger:hover span,
.nav-trigger:hover span::before,
.nav-trigger:hover span::after {
background: #888;
}
.nav-open .nav-trigger span {
background: transparent;
}
.nav-open .nav-trigger span::before,
.nav-open .nav-trigger span::after {
background: #888;
}
.nav-open .nav-trigger span::before {
transform: rotate(-45deg);
}
.nav-open .nav-trigger span::after {
transform: rotate(45deg);
}
.overlay {
position: fixed;
z-index: 2;
top: 0;
left: 0;
height: 100%;
width: 100%;
background: #333;
opacity: 0;
visibility: hidden;
transition: all .5s;
}
.nav-open .overlay {
opacity: .5;
visibility: visible;
}
.nav-container {
position: fixed;
z-index: 3;
top: 0;
right: 0;
height: 100%;
width: 100%;
padding: 3em 3.5em;
background: #343434;
overflow: auto;
transform: translateZ(0);
transform: translateY(-100%);
transition: transform .5s cubic-bezier(.07,.23,.34,1);
}
.nav-open .nav-container {
transform: translateX(0);
}
.nav {
list-style: none;
padding: 0;
}
.nav a {
display: block;
padding: .6em 0;
font-size: 30px;
font-family: bold;
font-family: sans-serif;
text-decoration: none;
color: #747474;
transform: translateZ(0);
}
.nav-open .nav a {
animation: slide-in .4s .2s backwards;
}
.nav-open .nav li:nth-of-type(2) a {
animation-delay: .3s;
}
.nav-open .nav li:nth-of-type(3) a {
animation-delay: .4s;
}
.nav-open .nav li:nth-of-type(4) a {
animation-delay: .5s;
}
.nav-open .nav li:nth-of-type(5) a {
animation-delay: .6s;
}
.nav li {
text-align: center;
}
#keyframes slide-in {
0% {
opacity: 0;
transform: translateY(-20px);
}
100% {
opacity: 1;
transform: translateY(0);
}
}
#media only screen and (min-width : 640px) {
/* .nav-trigger {
visibility: hidden;
} */
/* .nav li {
display: inline-block;
z-index: 60;
} */
}
<body>
<a href="#navigation" class="nav-trigger">
Menu<span></span>
</a>
<main>
<section class="intro">
<h1>Final Project</h1>
</section>
</main>
<nav class="nav-container" id="navigation">
<ul class="nav">
<li>Work</li>
<li>About</li>
<li>Contact</li>
</ul>
</nav>
<div class="overlay"></div>
</body>
I have a Mobile menu that should be forming X when clicked, however it would not respond and is stuck on its shape. I don't know why it acted that why, but as far as I have analyzed, this code should animate the Menu to form X when clicked. I am attaching the code for reference.
These are the CSS and HTML
/* Checkbox Hack */
input[type=checkbox] {
display: block;
width: 40px;
height: 32px;
position: absolute;
top: -7px;
left: -5px;
cursor: pointer;
opacity: 0;
z-index: 2;
-webkit-touch-callout: none;
}
/* Default State */
.divko {
background: green;
width: 400px;
height: 100px;
line-height: 100px;
color: white;
text-align: center;
}
/* Toggled State */
.myspan {
display: block;
width: 33px;
height: 4px;
margin-bottom: 5px;
position: relative;
background: #cdcdcd;
border-radius: 3px;
z-index: 1;
transform-origin: 4px 0px;
transition: transform 0.5s cubic-bezier(0.77, 0.2, 0.05, 1.0), background 0.5s cubic-bezier(0.77, 0.2, 0.05, 1.0), opacity 0.55s ease;
}
.myspan:first-child {
transform-origin: 0% 0%;
}
.myspan:nth-last-child(2) {
transform-origin: 0% 100%;
}
input[type=checkbox]:checked ~ .myspan {
opacity: 1;
transform: rotate(45deg) translate(-2px, -1px);
background: #232323;
}
input[type=checkbox]:checked ~ .divko {
background: red;
}
<div for="toggle-1"> <span class="myspan"></span>
<span class="myspan"></span>
<span class="myspan"></span></div>
<input type="checkbox" id="toggle-1">
<div class="divko">I'm controlled by toggle. No JavaScript!</div>
Here is the dabblet
http://dabblet.com/gist/5b4667ecc7255c228cd488c080140d95
Put the checkbox before the div I've give the .menu class to, and apply the rule on input[type=checkbox]:checked ~ .menu > .myspan.
/* Checkbox Hack */
input[type=checkbox] {
display: block;
width: 40px;
height: 32px;
position: absolute;
top: -7px;
left: -5px;
cursor: pointer;
opacity: 0;
z-index: 2;
-webkit-touch-callout: none;
}
/* Default State */
.divko {
background: green;
width: 400px;
height: 100px;
line-height: 100px;
color: white;
text-align: center;
}
/* Toggled State */
.myspan {
display: block;
width: 33px;
height: 4px;
margin-bottom: 5px;
position: relative;
background: #cdcdcd;
border-radius: 3px;
z-index: 1;
transform-origin: 4px 2px;
transition: transform 0.5s cubic-bezier(0.77, 0.2, 0.05, 1.0), background 0.5s cubic-bezier(0.77, 0.2, 0.05, 1.0), opacity 0.55s ease;
}
input[type=checkbox]:checked ~ .menu > .myspan {
opacity: 1;
background: #232323;
}
input[type=checkbox]:checked ~ .menu > .myspan:first-child {
transform: rotate(45deg);
}
input[type=checkbox]:checked ~ .menu > .myspan:nth-child(2) {
transform: translate(-100px);
}
input[type=checkbox]:checked ~ .menu > .myspan:last-child {
transform: rotate(-45deg);
}
input[type=checkbox]:checked ~ .divko {
background: red;
}
<input type="checkbox" id="toggle-1">
<div for="toggle-1" class="menu">
<span class="myspan"></span>
<span class="myspan"></span>
<span class="myspan"></span>
</div>
<div class="divko">I'm controlled by toggle. No JavaScript!</div>
I've created a Slanted Div, however I ran into problem I cannot solve, I've googled this but did not find any answers.
body {
background: black;
}
#slantedwrapper {
overflow: hidden;
margin-left: 50px;
}
#slanted {
display: inline-block;
/* margin-right:-4px; */
width: 400px;
margin-left: -45px;
/* background-image: url("http://www.keenthemes.com/preview/conquer/assets/plugins/jcrop/demos/demo_files/image2.jpg"); */
}
#slanted a {
position: relative;
background-color: #1d1d1d;
/* background-image: url("http://www.keenthemes.com/preview/conquer/assets/plugins/jcrop/demos/demo_files/image2.jpg"); */
box-sizing: border-box;
background-size: cover;
/* padding:1em; */
display: block;
transform: skewX(-30deg);
width: 100%;
min-height: 3.5em;
text-align: center;
border-right: 5px solid #20c397;
height: 150px;
/* line-height: 110px; */
overflow: hidden;
}
#slanted span {
color: white;
position: absolute;
box-sizing: border-box;
transform: skewX(30deg);
left: 0;
width: 100%;
/* height: 150px; */
/* background-image: url("http://www.keenthemes.com/preview/conquer/assets/plugins/jcrop/demos/demo_files/image2.jpg"); */
}
}
}
.current a {
background:#70cb00;
}
#slanted a img {
transform: skewX(30deg);
overflow: hidden;
margin-top: -20px;
padding-top: 0px;
width: 123%;
height: 123%;
margin-left: -50px;
opacity: 0.6;
-webkit-transition: opacity 0.3s linear 0s;
-o-transition: opacity 0.3s linear 0s;
transition: opacity 0.3s linear 0s;
}
#slanted img:hover {
opacity:1;
}
#caption {
background-color: #333333;
width: 100%;
height: 25px;
display: block;
position: absolute;
bottom: 0;
z-index: 99;
opacity: 0.7;
color: #D2D2D2;
-webkit-transition: background-color 0.3s linear 0s;
-o-transition: background-color 0.3s linear 0s;
transition: background-color 0.3s linear 0s;
}
/*Combination hover effects*/
#slanted:hover #caption {
background-color: #20c397;
opacity:1.0;
}
#slanted:hover img {
opacity:1.0;
}
/* END OFCombo hover effects*/
p.nonskew {
transform: skewX(30deg);
color: White;
margin: 0;
margin-left: 22%;
padding: 1.5%;
text-align: left;
font-size: 0.8em;
}
<div id="slantedwrapper">
<div id="slanted">
<a href="#">
<div id="caption">
<p class="nonskew">A Caption: Description</p>
</div>
<img src="http://www.keenthemes.com/preview/conquer/assets/plugins/jcrop/demos/demo_files/image2.jpg" alt="SLANTED DIV"></a>
</div>
<!--end of wrapper-->
</div>
JSFiddle version
here's the problem:
Hover over the div, it hovers fine, but at the bottom right corner, where nothing is there (where the overflow is hidden) still hovers if you place your mouse over the blank area where the angle begins, how do I solve this into when it hovers- it only applies to shape of the div only?
Thank you
You seem to have the right idea, using both the unskew and intuitive to using the skew, however, something like the below example may work for you:
html {
background: radial-gradient(#222, blue);
height: 100%;
}
div.wrap{
height: 150px;
width: 300px; position: relative;
overflow: hidden;
}
div.innerwrap {
height: 100%;
width: 100%;
transform: skewX(-30deg);
position: absolute;top:0;left:0;
overflow: hidden;
margin-left: -70px;
transition: all 0.4s;
border-right: 5px solid tomato;
cursor:pointer;
}
div.innerwrap:hover span {
background: gold;
}
div.innerwrap:before {
content: "";
position: absolute;
top: 0;
right: 0;
width: 100%;
height: 100%;
background: url(http://lorempixel.com/300/300);
transform: skewX(30deg);
transform-origin: top left;
}
div span {
position: absolute;
bottom: 0;
left: 0%;
width: 120%;
transform: skewX(30deg);
background: red;
text-align:center;
transition: all 0.4s;
}
<div class="wrap">
<div class="innerwrap">
<span>TITLE</span>
</div>
</div>
For further information, #Harry has created a wide variety of examples here in which you may find useful.
I am currently having problems centering my caption inside a div upon hover. The image inside the div is bigger than the div and I want the caption to be vertically centered inside that div?
HTML
<div class="container-fluid">
<div id="page">
<!-- GRID ITEM -->
<div class="item s1">
<a href="#">
<div class="grid-image">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/4/4e/Pleiades_large.jpg/1024px-Pleiades_large.jpg">
</div>
<div class="item-caption">
<h5>I want this to be center</h5>
</div>
</a>
</div>
<!-- /GRID ITEM -->
</div>
</div>
CSS
#page .item {
width: calc(16.66% - 10px);
display: inline-block;
height: 0;
float: left;
padding-bottom: 16.66%;
overflow: hidden;
background-color: salmon;
margin: 5px;
position: relative;
}
#page .item.s1 {
width: calc(50% - 10px);
padding-bottom: 50%;
overflow: hidden;
background-color: navy;
}
.item > a {
position: relative;
display: block;
overflow: hidden;
color: white;
}
.item:hover .grid-image:after {
background: rgba(0, 0, 0, .7);
}
.item:hover .grid-image > img {
-webkit-transform: scale(1.1);
-moz-transform: scale(1.1);
-ms-transform: scale(1.1);
-o-transform: scale(1.1);
transform: scale(1.1);
}
.item:hover .item-caption {
opacity: 1;
z-index: 3;
visibility: visible;
}
.item-caption,
.grid-image > img,
.grid-image:after {
-webkit-transition: all 0.3s ease-in-out 0s;
-moz-transition: all 0.3s ease-in-out 0s;
-ms-transition: all 0.3s ease-in-out 0s;
-o-transition: all 0.3s ease-in-out 0s;
transition: all 0.3s ease-in-out 0s;
}
.item-caption {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
background: rgba(29, 106, 154, 0.72);
color: #fff;
visibility: hidden;
text-align: center;
opacity: 0;
}
.grid-image {
position: relative;
overflow: hidden;
}
.grid-image img {
display: block;
overflow: hidden;
}
.grid-image:after {
position: absolute;
display: block;
content: "";
height: 100%;
width: 100%;
top: 0;
left: 0;
}
JavaScript
var $container = $('#page');
$container.masonry({
columnWidth: '.grid-sizer',
itemSelector: '.item',
percentPosition: true,
gutter: 10
});
Here's a fiddle
Thank you!
try this: http://jsfiddle.net/2stywe2t/1/
What I did:
Add a new div to your HTML, right under .item-caption.
Gave .item-caption the display: table attribute and a few others
Gave the new div display: table-cell, and vertical-align: middle
Removed position:relative from the a ancestor
Make sense?
Got the answer !! Just tidy up your code a bit.
var $container = $('#page');
$container.masonry({
columnWidth: '.grid-sizer',
itemSelector: '.item',
percentPosition: true,
gutter: 10
});
#page .item {
width: calc(16.66% - 10px);
display: inline-block;
height: 0;
float: left;
padding-bottom: 16.66%;
overflow: hidden;
background-color: salmon;
margin: 5px;
position: relative;
}
#page .item.s1 {
width: calc(50% - 10px);
padding-bottom: 50%;
overflow: hidden;
background-color: navy;
position: relative!important;
}
.item > a {
position: absolute;
display: block;
width: 100%;
height: 100%;
overflow: hidden;
color: white;
}
.item:hover .grid-image:after {
background: rgba(0, 0, 0, .7);
}
.item:hover .grid-image > img {
-webkit-transform: scale(1.1);
-moz-transform: scale(1.1);
-ms-transform: scale(1.1);
-o-transform: scale(1.1);
transform: scale(1.1);
}
.item:hover .item-caption {
opacity: 1;
z-index: 3;
visibility: visible;
}
.item-caption, .grid-image > img, .grid-image:after {
-webkit-transition: all 0.3s ease-in-out 0s;
-moz-transition: all 0.3s ease-in-out 0s;
-ms-transition: all 0.3s ease-in-out 0s;
-o-transition: all 0.3s ease-in-out 0s;
transition: all 0.3s ease-in-out 0s;
}
.item-caption {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
background: rgba(29, 106, 154, 0.72);
color: #fff;
visibility: hidden;
text-align: center;
opacity: 0;
display: table;
height: 100%;
width: 100%;
}
.grid-image {
position: relative;
overflow: hidden;
}
.grid-image img {
display: block;
overflow: hidden;
}
.grid-image:after {
position: absolute;
display: block;
content:"";
height: 100%;
width: 100%;
top: 0;
left: 0;
}
h5 {
display: table-cell;
vertical-align: middle;
}
<div class="container-fluid">
<div id="page">
<!-- GRID ITEM -->
<div class="item s1"> <a href="#">
<div class="grid-image">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/4/4e/Pleiades_large.jpg/1024px-Pleiades_large.jpg">
</div>
<div class="item-caption">
<h5>I want this to be center</h5>
</div>
</a>
</div>
<!-- /GRID ITEM -->
</div>
</div>
</body>
</html>
You simply need to add one line of code to your CSS to make this work:
h5 {
margin-top: 50%;
transform: translateY(-50%); /* not entirely necessary, but makes centering precise */
}
DEMO: http://jsfiddle.net/2stywe2t/4/