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 */
}
Related
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;
}
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
This question already has answers here:
Positioning <div> element at center of screen
(14 answers)
Closed 7 years ago.
I found a jquery code that rotate images inside a div and I want to position this div to the center of the page. I have this CSS for the images
#rotating-item-wrapper {
position: relative;
}
and this for the div
.rotating-item {
display: none;
position: absolute;
top: 150px;
left: 200;
}
How I manage to center the whole process to the center?
using margin and position tend to work in these sorts of situations:
HTML:
<div id="center-div"></div>
CSS:
#center-div {
position: relative;
margin: 0 auto;
width: 50px;
height: 50px;
background: red;
}
here's a jsfiddle: http://jsfiddle.net/mwyLz9rt/
Add this code to the div you wish to center:
.centered {
position: absolute;
margin-left: 50vw;
margin-top: 50vh;
transform: translate(-50%, -50%);
}
Also, next time try to provide us with a jsfiddle example so we could understand the problem more vividly :)
Here's a demo of the above code JSFIDDLE
I need help getting dropdown menu expanding on top. I put z-index on it, yet for some reason I'd like to understand it blends with the rest. The dropdown expands when one types in "Enter your region" on my site 33hotels.com.
I am sorry to post no code but the main problem is I don't know which part of it is responsible for this effect.
Thanks!
EDIT.
Turned out that setting position: absolute instead fixed solved the problem! Also strangely it only was a problem in Chrome and Safari but not Firefox!
Add z-index property to #location-search element. You may just set it to 1.
#location-search {
position: fixed;
z-index: 1;
top: 45px;
margin: 0 5px;
}
Remember, that z-index only works for a fraternal elements - those, who have the same parent. It means, if you have two non-static positioned elements in same parent you can use z-index to place them inside their parent. But as long as they have different parents - you can only rule with their parents z-indecies.
<div id="Wrapper">
<div id="Test1">blue</div>
<div id="Test2">red</div>
</div>
div { position: absolute; }
#Test1 { background: blue; z-index: 10; }
#Test2 { background: red; z-index: 9; }
/* blue is over red, though declared earlier */
But
<div id="Wrapper">
<div id="InnerWrapper">
<div id="Test1">blue</div>
</div>
<div id="Test2">red</div>
</div>
div { position: absolute; }
#InnerWrapper { z-index: 10; }
#Test1 { background: blue; z-index: 15; }
#Test2 { background: red; z-index: 11; }
/* red is over blue, for blue's wrapper has no z-index or lower */
Set z-index to -1 in this css, because your dropdown-menu class is having -z-index of 1000 so you need to set your main content should get behind the autocomplete dropdown.
#row-content {
position: fixed;
top: 85px;
bottom: 0;
width: 100%;
z-index: -1;
}
Also remember to set element positions, because z-index only works on positioned elements (position:absolute, position:relative, or position:fixed).
I have a question ....
In my webpage here
But when you scroll how can i do so the fixed header is on top of all content ?
Use the z-index CSS property on the element you want to put over the others.
The z-index property specifies the stack order of an element.
An element with greater stack order is always in front of an element
with a lower stack order.
#sticky {
z-index: 1;
}
Use 1 or more, depending on the value of the other z-indexed elements.
Use z-index to place your header on top:
#sticky{
z-index: 1;
}
Add this CSS
#sticky {
background-color: #FFFFFF;
display: inline;
margin-top: -15px;
position: fixed;
width: 1200px;
z-index: 1;
}
add z-index with a large number like this:
#sticky {
position: fixed;
background-color: white;
width: 1200px;
margin-top: -15px;
display: inline;
z-index: 99999; /* added */
}
z-index reference