div to cover entire area of image only - css

I made an image that when hovered upon will change the opacity of the div on top of it. The div should be the same size as the image. I have managed to place the div on top of the image. However, when I set the width and height to 100%, the div covered the image INCLUDING the image's margin. I want to know how to fix it so that the div can only cover the image with the margin not included. Please note that I need the image to be responsive, so I do not want to set the height in pixels as much as possible.
Here's the fiddle:
https://jsfiddle.net/gsuxlzt/77vn1uyg/
Here's the code:
.margin {
margin-bottom: 10px;
}
.photo-thumbnail {
position: relative;
}
.photo-title {
width: 100%;
height: 100%;
background-color: #cbe1f4;
z-index: 10;
position: absolute;
top: 0;
left: 0;
color: #18121e;
text-align: center;
opacity: 0;
transition: opacity 0.5s linear;
}
.photo-title:hover {
opacity: .9;
}
<div class="photo-thumbnail">
<img class="img-responsive img-thumbnail margin photo-thumbnail" src="http://i36.photobucket.com/albums/e20/kingjami/photo-frame_zpsljshbjdq.jpg" />
<a href=#>
<div class="photo-title">
<h2 style="padding: 20% 0 20% 0;">Project Title</h2>
</div>
</a>
</div>

You can try this code:
Html Code:
<div class="photo-thumbnail"><img class="img-responsive img-thumbnail margin photo-thumbnail" src="http://i36.photobucket.com/albums/e20/kingjami/photo-frame_zpsljshbjdq.jpg"/><a href=#>
<div class="photo-title">
<h2 style="padding: 20% 0 20% 0;">Project Title</h2>
</div>
</a>
CSS Code:-
.margin {
margin-bottom: 10px;
}
.img-thumbnail{padding:0px;}
.photo-thumbnail {
position: relative;
}
.photo-title {
width: 100%;
height: 100%;
background-color: #cbe1f4;
z-index: 10;
position: absolute;
top: 0;
left: 0;
color: #18121e;
text-align: center;
opacity: 0;
transition: opacity 0.5s linear;
}
.photo-title:hover {
opacity: .9;
}
https://jsfiddle.net/Dhavalr/77vn1uyg/8/

first of all, don't use padding and margin for <img> instead use it for .photo-thumbnail
and use this code.
.photo-thumbnail {
position: relative;
display: inline-block;
vertical-align: top;
}
using inline-block for the parent can make image flexible as well as only occupy the necessary area as image.
try this, it will work.

In your example you are missing the closing of your "photo-thumbnail"
You are not obligated to use "Width: 100%", "Height: 100%", when you have an absolutely positioned element, instead you can make it take all of the space with
top: 0;
right: 0;
bottom: 0;
left: 0;
And in your case you can set the
bottom: 10px;
since that is how much your div gets out of the picture
here is an example in a fiddle: https://jsfiddle.net/77vn1uyg/3/

Related

Is there a way to change position on the page of an image and the image that changes on hover with CSS?

I am trying to change the position of both images (image and image hover change) to elsewhere on my site. However when I edit the positions code one image moves and the other stays. Any help greatly appreciated
.figure {
position: relative;
/* can be omitted for a regular non-lazy image */
width: 300;
height: auto;
}
.figure img.image-hover {
position: absolute;
top: 0;
right: 0;
left: 0;
bottom: 0;
object-fit: contain;
opacity: 0;
transition: opacity .2s;
}
.figure:hover img.image-hover {
opacity: 1;
}
<div class="figure">
<img class="Sirv image-main sirv-image-loaded sirv-image-loading" src="https://freight.cargo.site/t/original/i/4867b4e577f4d455e8e84acf0935e4fb0f6367d4edd5d45a15dc4035c95187fc/war-on-bacteria.png" data-src="https://freight.cargo.site/t/original/i/4867b4e577f4d455e8e84acf0935e4fb0f6367d4edd5d45a15dc4035c95187fc/war-on-bacteria.png" referrerpolicy="no-referrer-when-downgrade" id="responsive-image-4609053" style="width: 30%;" alt="Bose 700 headphones, front">
<img class="Sirv image-hover sirv-image-loaded sirv-image-loading" data-src="https://freight.cargo.site/t/original/i/30c4299c88a8aba512919f8e9d7607340223ea4f4bb0032c8bb42fb622fac9d2/Screenshot-2022-04-27-at-13.18.43.png" referrerpolicy="no-referrer-when-downgrade" id="responsive-image-9283795" src="https://freight.cargo.site/t/original/i/30c4299c88a8aba512919f8e9d7607340223ea4f4bb0032c8bb42fb622fac9d2/Screenshot-2022-04-27-at-13.18.43.png" style="width: 30%;">
</div>
Just use absolute positioning on the parent element.
.figure {
position: absolute;
top:100px;
left:300px;
}
You do not need to change the position on the img as it is already positioned relative to its container.
put images in same container.
.figure {
position: relative;
height: auto;
}
.figure img{
position: absolute;
/* change position for both images*/
top: 20px;
right: 0;
left: 0;
bottom: 0;
object-fit: contain;
transition: opacity .2s;
}
.figure:hover img.image-hover {
z-index: 1;
}
<div class="figure">
<a href="Revenge-of-the-Bacteria" class="image-link" rel="history"><img class="Sirv image-hover sirv-image-loaded sirv-image-loading" data-src="https://freight.cargo.site/t/original/i/30c4299c88a8aba512919f8e9d7607340223ea4f4bb0032c8bb42fb622fac9d2/Screenshot-2022-04-27-at-13.18.43.png" referrerpolicy="no-referrer-when-downgrade" id="responsive-image-9283795" src="https://freight.cargo.site/t/original/i/30c4299c88a8aba512919f8e9d7607340223ea4f4bb0032c8bb42fb622fac9d2/Screenshot-2022-04-27-at-13.18.43.png" style="width: 30%;">
<img class="Sirv image-main sirv-image-loaded sirv-image-loading" src="https://freight.cargo.site/t/original/i/4867b4e577f4d455e8e84acf0935e4fb0f6367d4edd5d45a15dc4035c95187fc/war-on-bacteria.png" data-src="https://freight.cargo.site/t/original/i/4867b4e577f4d455e8e84acf0935e4fb0f6367d4edd5d45a15dc4035c95187fc/war-on-bacteria.png" referrerpolicy="no-referrer-when-downgrade" id="responsive-image-4609053" style="width: 30%;" alt="Bose 700 headphones, front"></a>
</div>

Working on a simple slider menu, but the menu items are hidden even after slider and page translation

The div tags are nested as
<div class="Site-wrapper" id="Site-wrapper">
<div id="Main-Menu-mobile" class="Main-Menu-mobile">
<div class="Site">
</div>
</div>
</div>
The logic for the menu is just these 4 CSS classes
.Site-wrapper {
position: relative;
width: 100%;
overflow: hidden;
}
.nav-open .Site {
left: auto;
transform: translate3d(70%, 0, 0);
}
.Site {
position: relative;
left: 0;
min-height: 100%;
background-color: #e4e4e4;
transition: 0.2s ease all;
}
.Main-Menu-mobile {
width: 70%;
position: absolute;
top: 0;
left: 0;
padding: 15px;
}
https://codepen.io/taufeq-orangejulius-razakh/pen/dypgjNO
Your Site-Wrapper div (The parent one) only has a height of 41px. And this stays the same even when the toggler button translates to right. This is where the implementation is a bit off.
Try removing your overflow:hidden from .Site-wrapper
.Site-wrapper {
position: relative;
width: 100%;
/* overflow: hidden; This needs to be implemented well*/
}
What you can do, is once the Hamburger icon is clicked change the overflow property value in Javascript.

Making a square div with inheritance or sth? (no js)

Is there a way to read the width and set a height with the same value ?
I know the trick with viewportheight[vh] and [vm], but it won't work well here.
You can do it by utilizing the fact that in padding-bottom: 100% the percentage is percentage of the width, and not the height. So setting a pseudo-element with padding-bottom: 100% will change the height according to width. The content can be in an absolutely position layer.
(I've added a little hover animation to demonstrate that changing width also changes the height)
.container {
position: relative;
width: 100px;
}
.container:hover {
width: 150px;
transition: width 2s;
}
.container::before {
display: block;
padding-bottom: 100%;
content: '';
}
.content {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
background: red;
}
<div class="container">
<div class="content">
</div>
</div>

Z-index not hiding background

I am trying to over lap a div on another div by using css, while background should become blur, like modal pop up show.
But the background of modal pop is still getting displayed through the modal pop up.
As u can see background is visible through the modal pop up!!
I have setted z-index of pop up more than the background
CSS:
.MoreDetails
{
background-color: #000;
z-index: -1;
width: 100%;
height: 100%;
top: 0px;
left: 0px;
position: fixed;
display: block;
opacity: 0.7;
z-index: 100;
text-align: center;
}
.tblView
{
position: fixed;
top: 10%;
left: 30%;
z-index:1;
opacity: 2.0;
}
My design:
<div id="MoreDetails" class="MoreDetails" >
<div id="tableDetails" class="tblView">
</div>
</div>
Child element cannot be stacked below parent element, even by using z-index.
Use z-index for maintaining stack level of absolute positioned elements that are siblings.
http://jsfiddle.net/TWLgc/
HTML
<div class="wrapper">
<div id="MoreDetails" class="MoreDetails" >
<div id="tableDetails" class="tblView">
</div>
</div>
<div id="tableDetails2" class="tblView2">
</div>
</div>
CSS
.MoreDetails
{
/*background-color: #000;*/
background-color: rgba(0,0,0,0.8);
z-index: -1;
width: 100%;
height: 100%;
top: 0px;
left: 0px;
position: fixed;
display: block;
/*opacity: 0.7;*/
z-index: 100;
text-align: center;
}
.tblView
{
position: fixed;
top: 10%;
left: 30%;
z-index:1;
opacity: 1;
background-color: red;
height: 100px;
width: 100px;
}
.tblView2
{
position: fixed;
margin:auto;top:0;bottom:0;left:0;right:0;
z-index: 101;
opacity: 1;
background-color: red;
height: 100px;
width: 100px;
}
The biggest issue is that you're nesting the tableDetails inside the MoreDetails div. Any opacity or z-index you apply to tableDetails will affect MoreDetails. Another approach might be to use the ::before pseudo class on tableDetails and position the two with CSS.
Some other tips:
Don't share id and class names. Using MoreDetails as both an id and
a class may end up breaking things as you progress.
opacity can
only have a value from 0 - 1.
Hope this helps! Good luck!

How to set a lower width when one div is on top of the other and the bottom slides out on hover?

I have one div on top of another. On hover, the bottom div (the right-side gray bar) slides out and is the same width as the top div: http://d.pr/OB6N. I'm trying to create a grid of images with slide-outs. Is there a way to set the width to be as much as is shown in the image? Because right now, when I inspect the element, the width is the total of the top div and the slide-out div (when fully slid out).
HTML:
<div class="poster">
<img src="example.png" />
</div>
<div class="bottombox"></div>
CSS:
.poster {
float: left;
width: 250px;
height: 375px;
display: inline-block;
position: relative;
left: 0px;
z-index: 1;
-webkit-transition-duration: 0.2s;
}
.bottombox {
float: left;
width: 250px;
height: 375px;
display: inline-block;
position: relative;
left: -225px;
-webkit-transition-duration: 0.2s;
}
.bottombox:hover {
left: 0px;
}
Hope that makes sense. Thanks!
So it would be much easier to help you if you created a jsfiddle to play with. That said, one approach is to make .bottombox a child of the .poster element
<div class="poster">
<img src="example.png" />
<div class="bottombox"></div>
</div>
then change the css of .poster to
width: auto
and change the css of .bottombox to
width: 100%;
position: absolute;
left: 25px;
and last set the hover definition to
left: 100%;

Resources