I have an issue with a container on 100% height not working on Chrome.
In short, it's a caption from an image which is appearing over the image while hovering it.
.item {
position: relative;
}
.caption {
width: 100%;
height: 100%;
position: absolute;
background-color: rgba(0, 0, 0, 0.6);
display: table;
top: 0;
left: 0;
box-sizing: border-box;
border-radius: 4px;
opacity: 0;
transition: 0.2s;
}
a:hover .caption {
opacity: 1;
transition: 0.2s;
}
.caption .caption-inter {
display: table-cell;
vertical-align: middle;
}
.item img {
width: 100%;
}
<div class="item">
<a href="#blabla">
<img src="//i.stack.imgur.com/tiQ1S.jpg">
<div class="caption">
<span class="caption-inter">caption of the image</span>
</div>
</a>
</div>
It works on Firefox, IE, but for Chrome, the caption with background only appear at the top of the image.
Any idea how I could make it work in Chrome?
Looks like Chrome doesn't apply the height:100% when position:absolute and display:table is also being set at the same time, and of course there is also position:relative set on the wrapper.
I would suggest to use flexbox for the caption for easy centering, and use the HTML5 semantic <figure> + <figcaption> elements for the markup.
.caption {
...
display: flex;
justify-content: center;
align-items: center;
}
Follow this post to find more ways of centering for both horizontally and vertically.
Snippet
.figure {
position: relative;
display: inline-block; /*NEW*/
margin: 0; /*NEW*/
}
.image {
border-radius: 4px;
width: 100%;
height: auto;
display: block;
}
.caption {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.6);
color: white;
box-sizing: border-box;
border-radius: 4px;
opacity: 0;
transition: 0.2s;
display: flex; /*NEW*/
justify-content: center; /*NEW*/
align-items: center; /*NEW*/
}
a:hover .caption {
opacity: 1;
}
<a class="item" href="#">
<figure class="figure">
<img class="image" src="//i.stack.imgur.com/tiQ1S.jpg">
<figcaption class="caption">
caption of the image
</figcaption>
</figure>
</a>
It's sometimes needed to give the container element, that's wrapping an absolute element, position: relative, to wrap the absolute element as expected.
Besides that, you should change the caption to display: block so it can actulaly apply the width: 100%.
/* added this */
#blabla {
/* its important to give the container position: relative,
when it's wrapping an absolute element */
position: relative;
/* It's needed to give the anchor tag "inline-block" attribute so it
can receive width and height, since it's an inline element */
display: inline-block;
}
.caption {
width: 100%;
height: 100%;
position: absolute;
background-color: rgba(0, 0, 0, 0.6);
/* changed from table to block */
display: block;
top: 0;
left: 0;
box-sizing: border-box;
border-radius: 4px;
opacity: 0;
transition: 0.2s;
}
a:hover .caption {
opacity: 1;
transition: 0.2s;
}
.caption .caption-inter {
display: table-cell;
vertical-align: middle;
text-overflow: ellipsis;
}
Related
I have this image which is inside div with class="image-wrapper". All I am trying to do is:
display .image-wrapper::before while being hovered on image so it works like title of image
also use transform: scale(1.1) on img so it grows little on being hovered
Description of problem from my side
In order to display title inside image (part 1), using ::before, we cannot use it directly on img as they are replaced elements, so I enclosed img with div with class="image-wrapper" and used ::before on div. And it worked fine as I expected. Now for the second part, I am not able to render both property same time. I am kinda confused.
.image-wrapper {
border: 2px solid black;
display: inline-block;
overflow: hidden;
}
.image-wrapper::before {
content: 'Hello';
height: auto;
width: auto;
padding: 4px;
margin-left: 8px;
background: rgba(0, 0, 240, 0.3);
color: white;
font-size: 20px;
position: absolute;
opacity: 0;
}
img {
width: 100%;
}
img:hover {
transform: scale(1.1);
transition: all 0.2s;
cursor: pointer;
}
/*HERE IS THE PROBLEM*/
.image-wrapper:hover::before {
opacity: 1;
}
<div class="image-wrapper">
<img src="https://images.unsplash.com/photo-1624215824600-ed3118b76873?ixid=MnwxMjA3fDB8MHx0b3BpYy1mZWVkfDMwfDZzTVZqVExTa2VRfHxlbnwwfHx8fA%3D%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=500&q=60">
</div>
It's kind of all there in your code, but the before pseudo element wont show up above its element, unless you give it a higher z-index.
Also this snippet slightly alters the CSS so that what it's looking for is hover on the image-wrapper class both for the Hello and the img scaling.
.image-wrapper {
display: inline-block;
overflow: hidden;
}
.image-wrapper::before {
content: 'Hello';
height: auto;
width: auto;
padding: 4px;
background: pink margin-left: 8px;
background: rgba(0, 0, 240, 0.3);
color: white;
font-size: 20px;
position: absolute;
opacity: 0;
}
img {
width: 100%;
}
.image-wrapper:hover img {
transform: scale(1.1);
transition: all 0.2s;
cursor: pointer;
}
.image-wrapper:hover::before {
opacity: 1;
z-index: 100;
}
<div class="image-wrapper">
<img src="https://images.unsplash.com/photo-1624215824600-ed3118b76873?ixid=MnwxMjA3fDB8MHx0b3BpYy1mZWVkfDMwfDZzTVZqVExTa2VRfHxlbnwwfHx8fA%3D%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=500&q=60">
</div>
I'm having troubles getting a z-index value element with a higher integer to place over another.
This is the issue I am facing.
The bottom half of the register button is being overlapped by the background image.
The code below has been adjusted a bit to only show the code that is being used here. You can see the full site by visiting: https://stangline.com/.
Here is the code:
CSS
.buttonFrame {
margin: 80px auto 50px auto;
display: block;
z-index: 4;
position: relative;
}
.buttonList {
display: inline-block;
vertical-align: top;
}
.homeButton {
font-size: 2rem;
text-decoration: none;
text-transform: uppercase;
letter-spacing: .2rem;
padding: 20px;
cursor: pointer;
box-sizing: border-box;
-webkit-transition: all 0.3s ease-in-out;
transition: all 0.3s ease-in-out;
position: relative;
z-index: 4;
}
#homeRight {
width: 60%;
height: 100%;
position: absolute;
right: 0;
top: 0;
display: block;
z-index: 2;
}
#homeRightImgFill {
background-position: center center;
background-repeat: no-repeat;
background-size: contain;
width: 95%;
position: relative;
margin: 0 auto;
padding-top: 50%;
z-index: 2;
}
HTML
<div class="buttonFrame">
<a href="/forums" class="homeButton homeButtonGradient buttonList">
Visit Forum
</a>
<a href="/register/" class="homeButton buttonList homeButtonBlue p-navgroup-link--register" data-xf-click="overlay" data-follow-redirects="on">
<span>Register</span>
</a>
</div>
</div>
</div>
</div><div class="homeCont" id="homeRight">
<div id="homeRightImgFill"></div>
</div>
Apply z-index:4 on homeCont class
I am trying to have a square-shaped div (the red box) on the page by default. When the user hover the mouse over it, a second div should display with a semi-transparent black background and some text/content. I'm trying to imitate Devon Stank's project section on his website.
The code I have right now increases the height of the default square red box and the second div doesn't cover the whole of the red box. What's wrong with the code?
Fiddle
.project-box {
position: relative;
width: 30%;
padding-top: 30%;
overflow: hidden;
margin-right: 20px;
margin-bottom: 15px;
display: inline-block;
}
.default-box {
background-color: red;
}
.hover-content {
position: relative;
width: 100%;
height: 100%;
padding-top: 30%;
overflow: hidden;
display: inline-block;
}
.default-hover {
opacity: 0;
-webkit-transition: 0.5s;
-moz-transition: 0.5s;
-o-transition: 0.5s;
-ms-transition: 0.5s;
transition: 0.5s;
background-color: rgba(0, 0, 0, 0.5);
}
.default-box:hover .default-hover {
opacity: 1;
}
<div class="default-box project-box">
<div class="default-hover hover-content">hello</div>
</div>
height: 100%; won't work on the element if the parent's height isn't defined.
Also, if you stick with position: relative with a padding on the parent, you won't be able to cover it all.
If you want to cover all the .project-box (parent) no matter its padding values,
I suggest you to use an absolute positioning on its child:
(I've done it by adding the new class .veil, but it could be done within your existing class)
.project-box {
position: relative; /* ADDED so that the absolute positioning refers to this parent */
width: 30%;
padding-top: 30%;
overflow: hidden;
margin-right: 20px;
margin-bottom: 15px;
display: inline-block;
}
.default-box {
background-color: red;
}
.hover-content {
/* REMOVED position and sizes */
padding-top: 30%;
overflow: hidden;
display: inline-block;
}
.default-hover {
opacity: 0;
transition: 0.5s;
background-color: rgba(0, 0, 0, 0.5);
}
.default-box:hover .default-hover {
opacity: 1;
}
/* ADDED this class */
.veil {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
<div class="default-box project-box">
<!-- Added a class for the child here -->
<div class="default-hover hover-content veil">hello</div>
</div>
Hope it helps.
Here, it may help you, try it.
.project-box {
position: relative;
width: 30%;
height:100%
}
.default-box {
background-color: red;
}
.hover-content {
position: relative;
width: 100%;
height: 100%;
text-align:center;
padding-top: 30%;
}
I know this question has been asked before (here and here), but for some reason I can't get mine to work when using the same techniques. Basically when you hover over this,
You should get this:
By the way, if there's a simpler way to do this without loading a new image when hovering, please let me know.
Here's what I tried:
HTML
<div class="image">
<a href="#">
<img class="image" src="wp-content/themes/TheBullshitCollection/Images/bs-1.jpg">
</a>
</div>
CSS
.image {
width: 100%;
margin-right: 28px;
margin-bottom: 28px;
display: inline-block;
}
.image a:hover {
display:block;
background-image:url("/wp-content/themes/TheBullshitCollection/Images/bs-1.5.jpg");
margin-right:28px;
margin-bottom:28px;
transition: all 0.2s ease-in-out;
position: absolute;
z-index:1;
width: 100px;
height: 120px;
}
JS Fiddle Link:
https://jsfiddle.net/ot8a5oba/
You can see that the width and height is also confusing me - I'm not sure how to make sure it stays the same size, and that it appears on top. Thanks in advance!
I would do it like this using a pseudo element to apply an overlay. Simplifies things quite a bit.
.imageContainer a {
width: 100%;
display: inline-block;
position: relative;
}
.imageContainer a:after {
position: absolute;
top: 0; left: 0; right: 0; bottom: 0;
background: rgba(139,69,19,0.5);
content: 'Buy';
display: flex;
justify-content: center;
align-items: center;
color: white;
font: 5em cursive;
opacity: 0;
transition: opacity .5s;
}
.imageContainer a:hover:after {
opacity: 1;
}
.imageContainer img {
max-width: 100%;
vertical-align: top;
}
/*
.image a:hover {
display: block;
background-image: url("http://i.imgur.com/ARiA0ua.jpg");
margin-right: 28px;
margin-bottom: 28px;
transition: all 0.2s ease-in-out;
position: absolute;
z-index: 1;
width: 100px;
height: 120px;
}
*/
<div class="imageContainer">
<a href="#">
<img class="image" src="http://i.imgur.com/F2PaGob.jpg">
</a>
</div>
IMPORTANT NOTE: only CSS/HTML, but not javascript (client does not allow it)
recently started the homepage of my site and wanted to make a centrally divided layout. Want to place the logo on the middle and one picture on top of it with a button to lead to the page, and one below it with the same function.
The animation should be a hover over the buttons (or defined area around it) as seen in the comp I did here:
GIF ANIMATION OF WHAT IS INTENDED
Similar to what is intended: https://jsfiddle.net/vja85zgL/
* {
margin: 0px;
padding: 0px;
}
#wrapper {
width: 80%;
height: 200px;
margin: auto;
}
#wrapper:hover #left:not(:hover) {
width: 25%;
transition: width 1s linear;
}
#wrapper:hover #right:not(:hover) {
width: 25%;
transition: width 1s linear;
}
#left {
width: 50%;
height: 100%;
background-color: lightblue;
display: inline-block;
position: relative;
transition: width 1s linear;
}
#innerleft {
position: absolute;
top: 100px;
width: calc(100% - 4px);
text-align: center;
border: 2px solid black;
}
#right {
width: 50%;
height: 100%;
background-color: lightgrey;
display: inline-block;
position: relative;
transition: width 1s linear;
}
#innerright {
position: absolute;
top: 100px;
width: calc(100% - 4px);
text-align: center;
border: 2px solid black;
}
#right:hover {
width: 75%;
transition: width 1s linear;
}
#left:hover {
width: 75%;
transition: width 1s linear;
}
<div id="wrapper">
<div id="left">
<div id="innerleft">
TITLE 1
</div>
</div><div id="right">
<div id="innerright">
TITLE 2
</div>
</div>
</div>
As seen, if hovering over the button, the background image behind the button shows itself "fully" (no rescaling), the logo (showed as B) moves down to 25% and the button not hovered scales down in size and font size and the image behind it slides under it where the user cannot see it properly (the user cannot see it by scrolling down).
If hovered on the other button the animation goes to the other button and image background.
There should be a delay of 2 seconds before the layout comes back to the starting position
DETAILS:
NO JAVASCRIPT ALLOWED, client doesn't allow usage
Should be responsive, so no hovering on touch devices
The image must not resize or loose proportion
Full screen layout
Mouse simulates user.
Animation should return to initial position after 3 seconds if mouse is not hovering
Initial position is 50%-50% (almost, due to the logo which is 150x150px)
End position should be 75%-25%.
Have been loosing my mind the last 2 days wondering how to do it as I am a beginner at CSS but didn't find a way yet.
Any help would be appreciated!
Available code of what has been done already to the design (total mess...):
#business-top {
height:50%
width:100%
}
#business-button {
height:3em;
width:12em;
background-color:#2B2A2A;
border: .2em solid #ff7600;
border-radius:1.8em;
margin:auto;
margin-top:3em;
}
#logo-separator {
text-align:center;
}
.separator {
display: block;
position: relative;
padding: 0;
height: 0;
width: 100%;
max-height: 0;
font-size: 1px;
line-height: 0;
clear: both;
border: none;
border-top: 1px solid #ff7600;
border-bottom: 1px solid #ff7600;
}
#logo {
margin:auto;
max-width:150px;
display:inline-block;
overflow:hidden
margin-top:-75px
}
#photography-bottom {
height:50%
width:100%
}
#photography-button {
height:3em;
width:12em;
background-color:#2B2A2A;
border: .2em solid #ff7600;
border-radius:1.8em;
margin:auto;
margin-top:3em;
}
h1 {
color:#ff7600;
text-align:center;
font-size:1.4em;
vertical-align:middle;
line-height:2.2em
}
<div id="business-top"
<a href="www.lluisballbe.smugmug.com">
<div id="business-button">
<h1>BUSINESS</h1>
</div>
</a>
</div>
<div id="logo-separator">
<div class="separator"></div>
<div id="logo"><img src="https://lluisballbe.smugmug.com/Assets-for-website/i-CsMnM3R/0/Th/800x800-round-Th.png"</div>
</div>
<div id="photography-bottom"
<a href="www.lluisballbe.smugmug.com">
<div id="photography-button">
<h1>PHOTOGRAPHY</h1>
</div>
</a>
</div>
If you increase area of buttons once hovered , then you may have some results in CSS only:
trick comes from pointer-events and pseudo to increase hoverable area
/* tricky part */
div {
width: 100%;
pointer-events: none;
overflow: hidden;
transition: 1.5s;
}
div:hover {/* will be catch by child with pointer-events auto */
flex: 3;
}
div:first-child {
background: turquoise;
}
div a {
pointer-events: auto;/* if a is hovered, so is its parent */
position: relative;/* this could be for the div parent if you want to cover its area */
padding: 5px;
border-radius: 0.25em;
border: 3px solid;
box-shadow: 2px 2px 5px;
color: #222;
background: #aaa;
}
div a:hover:before {/* increase size of button only once hovered */
content: '';
position: absolute;
top: -200px;/* coordonates used to increase pseudo size from its relative position parent. tune to your needs */
bottom: -200px;
width: 100%;
}
/* layout */
body,
div {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
div {
flex: 1;
}
hr {
width: 100%;
height: 5px;
background: red;
text-align: center;
}
hr:before {
content: url(http://dummyimage.com/50x50);
display: inline-block;
height: 50px;
border-radius: 50%;
overflow: hidden;
margin-top: -25px;
}
html,
body {
height: 100%;
width: 100%;
margin: 0;
}
<div>
button style
</div>
<hr/>
<div>
button style
</div>
From your update snippet :
html,
body {
height: 100%;
width: 100%;
margin: 0;
}
body {
display: flex;
flex-direction: column;
}
h1 {
margin: 0;
}
a {
display: inline-block;
}
#business-top {
display: flex;
flex: 1;
width: 100%;
align-items: center;
justify-content: center;
text-align: center;
}
#business-button {
height: 3em;
width: 12em;
background-color: #2B2A2A;
border: .2em solid #ff7600;
border-radius: 1.8em;
margin: auto;
}
#logo-separator {
text-align: center;
}
.separator {
display: block;
position: relative;
padding: 0;
height: 0;
width: 100%;
max-height: 0;
font-size: 1px;
line-height: 0;
flex: 0;
border-top: 1px solid #ff7600;
border-bottom: 1px solid #ff7600;
}
#logo {
margin: auto;
max-width: 150px;
display: inline-block;
overflow: hidden;
margin: -75px;
position: absolute;
}
#photography-bottom {
display: flex;
flex: 1;
width: 100%;
align-items: center;
justify-content: center;
}
#photography-button {
height: 3em;
width: 12em;
background-color: #2B2A2A;
border: .2em solid #ff7600;
border-radius: 1.8em;
margin: auto;
}
h1 {
color: #ff7600;
text-align: center;
font-size: 1.4em;
vertical-align: middle;
line-height: 2.2em
}
#business-top,
#photography-bottom {
pointer-events: none;
position: relative;
z-index: 1;
transition: 1s;
/* min-height: 200px; you ma want this to avoid logo and button to overlap */
}
#business-top a,
#photography-bottom a {
pointer-events: auto;
}
#business-top:hover,
#photography-bottom:hover {
flex: 3;
}
#business-top a:hover:before,
#photography-bottom a:hover:before {
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
<div id="business-top">
<a href="www.lluisballbe.smugmug.com">
<div id="business-button">
<h1>BUSINESS</h1>
</div>
</a>
</div>
<div id="logo-separator">
<div class="separator"></div>
<div id="logo"><img src="https://lluisballbe.smugmug.com/Assets-for-website/i-CsMnM3R/0/Th/800x800-round-Th.png"> </div>
</div>
<div id="photography-bottom">
<a href="www.lluisballbe.smugmug.com">
<div id="photography-button">
<h1>PHOTOGRAPHY</h1>
</div>
</a>
</div>
http://codepen.io/anon/pen/qbvgga