Text Hover Reveals Image Behind (z-index) - what's the issue? [duplicate] - html

This question already has answers here:
Why does z-index not work?
(10 answers)
Closed 4 years ago.
The goal is to have the image show up BEHIND the text upon hover. I've tried several different scenarios, but the z-index doesn't take.
Here is the site: http://lawnyavawnya.com/2018/artists/
.artisthover {
display: none
}
h2.two:hover img {
display: block;
z-index: -5;
}
<h2 class="two">
<h2>ALBERT DALTON</h2>
<img src="http://lawnyavawnya.com/2018/wp-content/uploads/2018/04/Weary_Thumb.jpg" class="artisthover">
</h2>
What am I missing?

You cant nest h2 inside h2 because the like adding h3 inside h2 the second heading will be displayed smaller in some browsers.
z-index works with positioned elements only.
check this code see how it works:
.artisthover {
display: none;
}
h2.two:hover img {
position: relative;
display: block;
z-index: -1;
}
<h2 class="two">
ALBERT DALTON
<img src="http://via.placeholder.com/1000" class="artisthover">
</h2>

its not using Z-index but by using CSS background image this can be done easily
h2:hover {
height: 200px;
background-image: url('http://lawnyavawnya.com/2018/wp-content/uploads/2018/04/Weary_Thumb.jpg');
background-position: 0px 15px;
background-repeat: no-repeat;
background-size: auto;
}
<div>
<h2>ALBERT DALTON</h2>
</div>

In order to the z-index to work it needs a position different than static. Add position: relative; to the img and you'll get it to work.
h2.two:hover img {
display: block;
z-index: -5;
position: relative;
}
You could also add z-index to the h2 if you want the picture over the text:
h2.two {
position: relative;
z-index: 1;
}

Related

Changing the display type of image to "block" rounded its lower corners, how? [duplicate]

This question already has answers here:
Image inside div has extra space below the image
(10 answers)
Closed 1 year ago.
I am not asking how to round corners of image. In given case when I mouse-hover on the image you can see that the image is not covering whole div as well as the lower corners are not rounded, but if I add display: block to the image, it covers the div and the lower corners seems to be rounded, I am unable to understand why this is happening?
See images for before and after display:block.
HTML:
<div class="row">
<div class="campus-col">
<img src="./images/london.png" alt="london campus" />
<div class="layer">
<h3>LONDON</h3>
</div>
</div>
CSS:
.row {
margin-top: 5%;
display: flex;
justify-content: space-between;
}
.campus-col {
flex-basis: 32%;
border-radius: 10px;
margin-bottom: 30px;
position: relative;
overflow: hidden;
}
.campus-col img {
width: 100%;
display: block; /**** THIS PROPERTY ROUNDS THE LOWER CORNER *****/
}
.layer {
background: transparent;
height: 100%;
width: 100%;
position: absolute;
top: 0;
left: 0;
transition: 0.5s;
}
.layer:hover {
background: rgba(226, 0, 0, 0.7);
}
BEFORE display:block on mouse-hover the image is-
AFTER display:block on mouse-hover the image is -
You can just write the following code instead of display: block;
opacity:1;
This will also work the same as display block. Just try this code and check wether again it is rounding the corners?

How to change only the background image opacity? [duplicate]

This question already has answers here:
How do I give text or an image a transparent background using CSS?
(29 answers)
Can I set an opacity only to the background image of a div?
(8 answers)
Set opacity of background image without affecting child elements
(15 answers)
Closed 4 years ago.
I am trying to change the opacity of my background image to make it match the website. The problem is that the opacity: 0.5; change everything within the element. So not only the background image change but also the text and every other element in the section. How am i supposed to change only the color of the image? Here is my code:
section {
background-image: url(../IMG/Photo_NR1.jpg);
padding: 15px;
width: 100%;
height: 700px;
}
I have done some research but i couldn't find anything. I tried to have all my elements out of the <section> tag but then i was forced to change the position of the elements again. Thanks for your time :)
You could also do this by a pseudo element. Something like this:
section {
position: relative;
padding: 15px;
width: 100%;
height: 700px;
}
section::before {
content: "";
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: -1;
opacity: .5;
background-image: url(../IMG/Photo_NR1.jpg);
}
So you do not need an image tag and you separating the design from content.
Try to seperate the image.
Html:
<div class="my-container">
<img src="your/image">
</div>
Css:
.my-container {
position: relative;
overflow: hidden;
}
.my-container img {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: auto;
opacity: 0.5;
}
Source:
https://scotch.io/tutorials/how-to-change-a-css-background-images-opacity

How to write on the heart? [duplicate]

This question already has answers here:
Why does z-index not work?
(10 answers)
Closed 6 years ago.
Following this: Did CSS break my heart?, I now have this JSFiddle, which has this HTML:
<div id="heart"><span>M + G</span></div>
and this (new) CSS:
span {
z-index: 4;
}
However, the letters are still behind the heart, while my goal is to place them in the centre of it and for a start I thought I had to get them in front of the heart, but this fails, any idea please?
z-index only works on positioned elements (position:absolute, position:relative,...... ) and your span elemet dosnt have position property.
#heart:before, #heart:after {
z-index: -1; // because the text will be placed behind heart.
}
WORKING DEMO
or you just add position: relative to your span.
According to the MDN docs, z-index can only be applied to a positioned element.
Simply set the span to position:relative;:
span {
position:relative;
z-index: 4;
}
JSFiddle
span {
position: absolute;
z-index: 4;
}
Update to center text:
span {
position: relative;
line-height: 60px;
z-index: 4;
}
#heart {
text-align: center;
position: relative;
width: 100px;
height: 90px;
margin-top: 10px;
/* leave some space above */
}

image appears when hover over text

I'm not super comfortable with JS , but that seems to be the best way to do this , having a hard time applying other peoples solutions to my scenario.
Want an image to appear when hover over text.
I can get the image to appear on hover, but it appears up way up at top of page, and I am having a hard time getting it to appear in the viewport without indicating what the top margins is. Is that the best way to do it?
So far I have:
<div id="popup">
<div class="large-6 columns">
Bristol Hayward-Hughes <span> <img src="bristol.jpg" alt="Bristol" id="bristol"> </span>
</div>
</div>
and
#popup span {
display: none;
}
#popup a:hover span {
display: block;
position: absolute;
top: 0px;
left: 170px;
width: 400px;
margin: auto;
}
#bristol {
position: absolute;
z-index: 1;
margin-top: 100px;
}
If I'm understanding the question correctly, you'll need to place position:relative; in the parent Div: #popup that the image is residing in.
Check this Fiddle for reference: https://jsfiddle.net/rjschie/q87um7wd/2/
For an example: comment the position:relative; line under #popup and re-run the example. You'll see that the Image appears at the top of the window. Then uncomment it, and re-run and it will appear relative to the #popup div.
Please give relative positioning to your span that holds your image.
#popup a:hover span {
display: block;
position: relative; // Changed absolute to relative
//Give top and left position with respect to your anchor tag.
top: 0px;
left: 170px;
width: 400px;
margin: auto;
}
Remove the margin-top from the image tag as well.
#bristol {
position: absolute;
z-index: 1;
/*margin-top: 100px;*/ //Removed margin-top on the image
}

Positioning image next to centered text

I'm having some trouble positioning my image next to my h1. The h1 is centered, and I would like to have my image placed on the right side of it. However, The position of the h1 may not be changed (thus, the image may not affect the position of the h1).
Relevant code I have so far:
<div id="header">
<h1>Header </h1><img src="pencil.jpg" alt="">
</div>
h1 {
text-align: center;
position: relative;
}
img {
position: relative;
top: 20px;
left: 20px;
}
This code doesn't work at all; the image appears on the left side of the web page and is not being positioned relative to the h1 as I would like to.
I tried fixing this by putting the image into the h1 (to make it it's parent element), but by doing this the position of the h1 element changes (because the reserved space for the image is still preserved in the h1 element).
I hope someone can help me.
Kind regards,
Nick
That's because you're using a block level tag with another block level tag.
Check out W3 Schools for more info pertaining to inline VS. block level elements. :)
And if you want a more direct example using your code, here is a jsFiddle. This has it so the text is centered and the image is next to it, centered as well.
h1 {
text-align: center;
position: relative;
}
img {
position: relative;
top: 20px;
left: 20px;
display: inline-block;
}
One solution is to give #header position: relative and position: absolute to the img:
h1 {
text-align: center;
position: relative;
}
img {
position: absolute;
top: -80px;
left: 60%;
}
#header {
position: relative;
}
fiddle
You cannot absolutely position elements with position relative. You should use position absolute.
This wont resize very well though. Hope it helps. :)
img {
position: absolute;
right: 20px;
top: 0;
width: 100px;
}
Example
Since you want them right next to eachother...
Wrap the header text in a span.
<div id="header">
<h1>
<span>Header</span>
<img src="http://placekitten.com/g/400/300" alt="" />
</h1>
</div>
Set both the span and the image to display: inline-block;
h1 span, h1 img {
display: inline-block;
}
If you want the text dead center then add some padding to the left, equal to the width of the image.
h1 span {
padding-left: 36px;
}
h1 img {
width: 36px;
}
Example 1
Alternatively
Put the image inside the span
<div id="header">
<h1>
<span>
Header
<img src="http://placekitten.com/g/400/300" alt="" />
</span>
</h1>
</div>
And set it to position absolute, so it hangs out the right hand side.
h1 {
text-align: center;
overflow: hidden;
}
h1 span {
position: relative;
}
h1 img {
position: absolute;
right: -36px;
width: 36px;
}
Example 2
Neither are perfect solutions, but hopefully one will be right for you. :)