Sibling element inheriting opacity? - html

I'm working on a site right now where I've got to have some images that, on hover, dim and display some text (price, item name, etc.). So I've got a for the wrapper, and then a for the image itself and a for the overlay text. The image div and the overlay div are children of the wrapper.
The wrapper has position: relative, and the image and overlay have position: absolute. The image div has a z-index of 10, and the overlay a z-index of 0. On hover, the image dims to 50% opacity, and the text appears, just like it should. Except... wherever the text is in contact with the image, it also has 50% opacity. Text outside of the image has normal opacity.
How do I make it so that all of the text has an opacity of 1?
HTML:
<section class="first-section">
<div class="outfitcontainer">
<div class="outfit">
<img src="purelyoutfit1.png" width="100%" height="100%"/>
</div>
<div class="overlay">
<p class="price">$350<s> $4200</s></p>
<p class="item">FURR COAT</p>
<p class="designer">Antonio Marras</p>
</div>
</div>
</section>
CSS:
.first-section {
width: 80%;
margin-left: 10%;
background-color: #FFFFFF;
height: auto;
}
.outfitcontainer {
position: relative;
float: left;
width: 20%;
height: auto;
background-color: #FFFFFF;
margin: 25px;
}
.outfit, .overlay {
position: absolute;
width: 200px;
height: auto;
top: 0;
left: 0;
}
.outfit {
z-index: 10;
background-color: white;
}
.outfit:hover {
opacity: .5;
cursor: pointer;
}
.overlay {
z-index: 0;
text-align: center;
font-weight: bold;
}
.overlay p {
display: block;
padding: 30px 0;
color: black;
}

The easiest route to go would be to not make the image opaque, but have the overlay create that opacity for you. If you give the overlay an opaque background(either with css or an png image), you will be able to pull off exactly what you need.
A few code modifications should help solve the problem.
HTML
<section class="first-section">
<div class="outfitcontainer">
<div class="overlay">
<div class="text">
<p class="price">$350<s> $4200</s></p>
<p class="item">FURR COAT</p>
<p class="designer">Antonio Marras</p>
</div>
</div>
<img src="http://i.imgur.com/hDLwTh8.gif" width="100%" height="100%"/>
</div>
</section>
CSS
.first-section {
width: 80%;
margin-left: 10%;
background-color: #FFFFFF;
height: auto;
}
.outfitcontainer {
position: relative;
float: left;
width: 20%;
height: auto;
background-color: #FFFFFF;
margin: 25px;
}
.outfit, .overlay {
position: absolute;
width: 200px;
height: auto;
top: 0;
left: 0;
}
.overlay {
height: 400px;
display: none;
background: rgba(255, 255, 255, 0.5);
text-align: center;
font-weight: bold;
}
.overlay p {
display: block;
padding: 30px 0;
color: black;
}
img {
width: 200px;
position: absolute;
height: 400px;
}
.outfitcontainer:hover > .overlay {
display: block;
}
.outfit {
z-index: 10;
background-color: white;
}
.outfit:hover {
cursor: pointer;
}

Try this:
.yourClass > *{
opacity:1;
}.yourClass > *:hover{
opacity:1;
}
Good Luck!

Related

How to place sibling img/div on top of each other with identical size if parent has padding

I'd like to have an image with a div that covers the image exactly. I can get the div to overlay the image by using position: relative in the parent and position: absolute for the div, but background-color fills out the padding in the parent so they aren't overlayed properly.
Here's a snippet that demonstrates the problem.
.parent {
position: relative;
padding: 10px;
width: 40%;
}
.image {
width: 100%;
height: 100%;
border-radius: 13px;
}
.overlay {
position: absolute;
background-color: red;
width: 100%;
height: 100%;
border-radius: 13px;
left: 0;
top: 0;
opacity: 0.2;
}
<div class="parent">
<img class="image" src="https://cards.scryfall.io/normal/front/4/f/4f3deefe-28bc-4e45-a0a0-ab03167e2e81.jpg?1561942156">
<div class="overlay"></div>
</div>
I'm able to get it pretty close with some calc()'s to subtract the padding. This almost works, but the div fills out a little too much at the bottom. I'd like to not have a bunch of hardcoded values for padding anyway, so I wouldn't really like this solution even if it did work entirely.
Here's a snippet that shows the calc() approach.
.parent {
position: relative;
padding: 10px;
width: 40%;
}
.image {
width: 100%;
height: 100%;
border-radius: 13px;
}
.overlay {
position: absolute;
background-color: red;
width: calc(100% - 2 * 10px);
height: calc(100% - 2 * 10px);
border-radius: 13px;
left: 10px;
top: 10px;
opacity: 0.2;
}
<div class="parent">
<img class="image" src="https://cards.scryfall.io/normal/front/4/f/4f3deefe-28bc-4e45-a0a0-ab03167e2e81.jpg?1561942156">
<div class="overlay"></div>
</div>
This snippet does things a slightly different way, putting the img inside the overlay div and putting the actual green, lower opacity overlay as the overlay div's after pseudo element.
This way you don't have to build in any knowledge of the parent's padding.
.parent {
position: relative;
padding: 10px;
width: 40%;
background: red;
height: fit-content;
}
.image {
width: 100%;
border-radius: 13px;
top: 0;
left: 0;
}
.overlay {
position: relative;
padding: 0;
width: fit-content;
height: fit-content;
}
.overlay::after {
content: '';
position: absolute;
background-color: green;
width: 100%;
height: 100%;
border-radius: 13px;
left: 0;
top: 0;
opacity: 0.2;
padding: 0px;
}
<div class="parent">
<div class="overlay"> <img class="image" src="https://cards.scryfall.io/normal/front/4/f/4f3deefe-28bc-4e45-a0a0-ab03167e2e81.jpg?1561942156"></div>
</div>
When using HTML5, browser adds some padding to the bottom of the img tag. This can be avoided by making the image a block element. So just adding display: block to class .image and then it good.
And btw, to define witdh/height of an absolute element, beside calc() you can also define 4 values top, right, bottom, left of it.
:root {
--custom-padding: 10px;
}
.parent {
position: relative;
padding: var(--custom-padding);
width: 40%;
}
.image {
width: 100%;
height: 100%;
border-radius: 13px;
display: block;
}
.overlay {
position: absolute;
background-color: red;
border-radius: 13px;
bottom: var(--custom-padding);
right: var(--custom-padding);
left: var(--custom-padding);
top: var(--custom-padding);
opacity: 0.2;
}
<div class="parent">
<img class="image" src="https://cards.scryfall.io/normal/front/4/f/4f3deefe-28bc-4e45-a0a0-ab03167e2e81.jpg?1561942156">
<div class="overlay"></div>
</div>

Image-text change color when hover an image

I have an image and some text. What I want is when hover the image, the image will have opacity and the color of the text change. My problem is when the image has opacity, the text also have opacity so it seem I can't see the text. How can I solve that problem? Note: I use image instead of background-image in this case because of some problem, so just help me solve this with image. Thanks a lots!
This picture is exactly what I want:
Here is my code
.categories {
height: 450px;
position: relative;
width: 256px;
cursor: pointer;
color: white;
}
.categories:hover {
opacity: 35%;
color: black;
}
.categories__text {
font-size: 2.5rem;
position: absolute;
bottom: 1%;
left: 20%;
width: 50%;
line-height: 50px;
}
<div class="categories">
<div class="categories__child">
<img alt="img" src="https://www.petcity.vn/media/news/923_gia_mua_ban_cho_phoc_soc_thukieng_12.jpg" />
</div>
<div class="categories__text">HERE IS THE TEXT</div>
</div>
You have to separate the image and text styles:
.categories {
height: 450px;
position: relative;
width: 256px;
cursor: pointer;
color: white;
}
.categories:hover {
color: black;
}
.categories:hover img {
opacity: 35%;
}
.categories__text {
font-size: 2.5rem;
position: absolute;
bottom: 1%;
left: 20%;
width: 50%;
line-height: 50px;
}
<div class="categories">
<div class="categories__child">
<img alt="img" src="https://www.petcity.vn/media/news/923_gia_mua_ban_cho_phoc_soc_thukieng_12.jpg" />
</div>
<div class="categories__text">HERE IS THE TEXT</div>
</div>

img file pushes down to hover. how could i properly appear hover on image and place hover

Screenshot:
img file pushes down to hover. How could I properly appear hover on image and place hover on image?
<div class="colunm5">
<img src="images/picture1.jpg">
<p class="colunm5_centered">aaaa</p>
</div>
.colunm5 {
width:340px;
height:378px;
border: 1px solid #000000;
display:inline-block;
position: relative;
bottom:155px;
}
.colunm5_centered {
width:340px;
height:378px;
vertical-align: top;
margin: 0;
text-align: center;
}
.colunm5_centered{
visibility: hidden;
}
.colunm5:hover .colunm5_centered {
background-color:rgba(0,0,0,0.3);
visibility:visible;
}
Your question is not too clear, but I guess that you want the partially transparent p to appear in the same location as the image, overlapping it, is that right?
In that case, all you need to do is position the p absolutely, since its parent is already positioned relatively.
position: absolute;
left: 0; top: 0;
Snippet:
.colunm5 {
width: 340px;
height: 378px;
border: 1px solid #000000;
display: inline-block;
position: relative;
bottom: 155px;
}
.colunm5_centered {
width: 340px;
height: 378px;
position: absolute;
left: 0; top: 0;
margin: 0;
text-align: center;
visibility: hidden;
}
.colunm5:hover .colunm5_centered {
background-color: rgba(0, 0, 0, 0.3);
visibility: visible;
}
<div class="colunm5">
<img src="http://i.stack.imgur.com/8w571.png">
<p class="colunm5_centered">aaaa</p>
</div>
Please note that I had to change the image URL to have something we can see.

Firefox and IE border-box not working properly?

So I have been trying to figure this out for a day or so without any luck, and figured I would turn to the CSS masters of the universe here.
Anyway, in Chrome my page looks fine (like always), but Firefox and IE both seem to have issues w/resizing images. I basically have 2 parts, a 'left div' and a 'right div', and the on the left just has right-padding to make it be the entire width, minus the width of the 'right div'.
Inside 'left div', there is an image who's size is set to be 100% of the width and height of the containing element, which in Chrome, works out wonderfully, and leaves the image in the center and looking good. FF and IE don't resize it at all, and worse, they don't respect the padding set on 'left div' so it looks even more weird.
The simplified HTML:
<div>
<div class="dialog-bg"></div>
<div id="view-larger-dialog" class="mc_report_dialog dialog-container">
<div class="details-container staticimage">
<span id="openPostModal">
<span class="modal-body cardDetails">
<div class="closeOpenModal">×</div>
<div class="cardContent">
<div class="cardBody">
<div id="card-content" class="card-content-staticimage">
<span class="image">
<img class="annotatable" src="https://s-media-cache-ak0.pinimg.com/originals/5a/28/22/5a282241e64e41d605384bb261ea581f.jpg">
</span>
</div>
</div>
</div>
</span>
</span>
<span class="detailBox">
<div class="cardContent cardDetails">
<div class="content">
<p>
blank white space
</p>
</div>
</div>
</span>
</div>
</div>
</div>
The CSS:
.dialog-bg {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: black;
opacity: 0.6;
z-index: 1001;
}
.mc_report_dialog .details-container {
padding: 0px;
}
span#openPostModal {
position: fixed;
top: 0;
left: 0;
height: 800px;
margin-top: 0px;
margin-left: 0px;
display: table;
z-index: 5000;
height: 100%;
background: none;
width: 100%;
box-sizing: border-box;
padding-right: 24rem;
border: none;
}
span.detailBox, span.shareNewBox {
width: 24rem;
height: 100%;
padding: 0;
position: fixed;
top: 0px;
right: 0px;
background-color: white;
z-index: 5005;
}
span#openPostModal .modal-body {
border: 0px solid #ffffff;
padding: .6rem 1rem;
display: table-cell;
vertical-align: middle;
width: 100%;
max-height: 50%;
background: none;
overflow-y: visible;
}
.closeOpenModal {
font-size: 2rem;
color: #fff;
float: right;
position: absolute;
right: 1rem;
top: 1rem;
font-weight: 700;
cursor: pointer;
padding-right: 24rem;
opacity: 0.8;
}
span#openPostModal .cardContent {
background: none;
border: none;
position: relative;
width: 90%;
margin: auto;
}
span#openPostModal .cardContent .cardBody {
padding: 0;
}
span#openPostModal .cardContent .cardBody #card-content {
height: 100%;
padding: 0;
}
#card-content.card-content-staticimage .image {
position: relative;
display: inline-block;
width: 100%;
height: 100%;
}
#card-content.card-content-staticimage .image img {
max-height: 100%;
max-width: 100%;
}
You can see the result of that here on my jsFiddle
Any help would be greatly appeciated.
Apparently the whole display: table; and display: table-cell were messing it up. Just changing those to display as block worked. Sorry for the question.
Your problem isn't box-sizing:border-box, it's display:table.
Just add table-layout:fixed; right after the display:table declaration and you should be ok.

make two divs overlap each other

I want to make two divs overlap each other using css. I used the following code but when some text or content is added to the blue box it overflows the gray box while I want to keep it inside the the gray box and stretch it as the inner content is stretched.
.gray {
position: relative;
background-color: #818181;
}
.white {
background-color: #fff;
}
.blue {
position: absolute;
background-color: #0090ff;
top: 0;
right: 10px;
left: 100px;
}
<div class="gray">
<div class="white">
left text
</div>
<div class="blue">
<p>some text goes here</p>
<p>some text goes here</p>
<p>some text goes here</p>
</div>
</div>
here is my satisfactory result:
How can I correct the css to get the above result?
Change your CSS to this.
The gray will autosize in height when you add more content to the blue div.You may need to change some with and margin values to get the layout you want, but the mechanism is there.
.gray {
background-color: #818181;
z-index: -1;
height: auto;
width: 300px;
position: absolute;
overflow: hidden;
}
.white {
background-color: #fff;
z-index: 0;
height: 150px;
width: 280px;
position: absolute;
margin-left: 10px;
margin-top: 10px;
}
.blue {
background-color: #0090ff;
top: 0;
height: auto;
width: 180px;
z-index: 1;
position: relative;
float:left;
margin-left: 60px;
margin-top: 10px;
}
See it work: http://jsfiddle.net/djwave28/dj9wo8ak/4/
So you need to define blue box as position relative the overflow will be stopped and and when you add some content to blue div it will not overflow.
If you want to get white div under a blue div you need to set it to position:absolute and set it z-indx lesser than blue div has
try this
.gray {
position: relative;
background-color: #818181;
height: 200px;
width: 100%;
}
.white {
background-color: #fff;
float: left;
width: 97%;
position: relative;
z-index: 2;
height: 50%;
left: 1%
}
.blue {
position: relative;
background-color: #0090ff;
z-index:3;
width:40%;
height:100%;
top: -9%;
left: 8%;
}
Play with the height and width sizes to reach your desired dimensions.
Do the same with the position values to place the divs the way you want
see this fiddle http://jsfiddle.net/u50jj2e1/1/
.gray {
background-color: #818181;
z-index: -1;
height: 300px;
width: 300px;
position: absolute;
overflow: hidden;
/* Instead of hidden it could be "overflow: auto;"*/
}
.white {
background-color: #fff;
z-index: 0;
height: 150px;
width: 280px;
position: absolute;
margin-left: 10px;
margin-top: 10px;
}
.blue {
background-color: #0090ff;
top: 0;
height: 290px;
width: 180px;
z-index: 1;
position: absolute;
margin-left: 20px;
margin-top: 10px;
}
<div class="gray">
<div class="white">
</div>
<div class="blue">
</div>
</div>
I create exact shape for you: http://jsfiddle.net/dj9wo8ak/1/