I'm trying to layer 2 div's so that the content of one is above another, and the other has an opacity setting to make it translucent. However, no matter which way around I put the HTML for the layers the translucent layer is always above the content. This is the way I would assume to be correct in HTML ordering:
<div id="translucent"></div>
<div id="content">...</div>
However it doesn't seem to be working - The basic styling I'm using to overlap the layers is here - This works to put one over the other, but the translucent one seems to stay above the other one
<style>
#content
{
margin-top:-525px;
}
#translucent
{
height:525px;
opacity:0.8;
}
</style>
Any Ideas?
Why don't you use position and z-indexes? It should work like this:
#content {
margin-top:-525px;
// positioning something allows you to do more accurate placements
position: relative;
// adding a z-index allows you to play with the layers (because... z-axis.)
z-index: 1;
}
#translucent {
height:525px;
opacity:0.8;
position: relative;
z-index: 0;
}
Basically, the HTML DOM works as following: if it's later in the DOM, it is on top of items earlier in the DOM. Turning the opacity down makes the element transparent, but not non-existent. The best way to do this is add z-index and a position, or just use display: block; and display: none; To hide one of yours DIVs (but you want the transparency, so I guess thats not an option).
However if you'd use position: absolute; you could place the elements in the same place without doing the margin. Then wrap it in another element (say #wrapper) and then you can move both boxes at the same time. Add a width and height of 100% for both boxes and you can use the wrapper to define both boxes' height and width and the same time! Ahh. CSS Magic.
<div id="wrapper">
<div id="content">...</div>
<div id="translucent"></div>
</div>
Heres the CSS:
#wrapper {
position: relative;
}
#content {
position: absolute;
left: 0;
top: 0;
z-index: 1;
}
#translucent {
height:525px;
opacity:0.8;
position: absolute;
left: 0;
top: 0;
z-index: 0;
}
Related
I'm working on this page and trying to get the slideshow to display correctly at tablet and mobile widths with media queries. However, all of the slider container elements are setting their height to 590px and this is creating a large gap beneath the slider and its content. I don't belive any of the elements have a fixed height set, but I have used some max-height:590px here and there. Any thoughts on how to get rid of that gap and force the containers to resize correctly?
Slider uses Cycle2.
Some HTML code
<div id="slider" class="cycle-slideshow" data-cycle-pager="#adv-custom-pager" data-cycle-slides="> div" data-cycle-timeout="7000">
<div class="singleSlide">
<!-- content goes in here -->
</div>
And some CSS that I think is important:
#homeslider {
height: auto;
}
#homeslider, #slider img {
width: 100%;
height: auto;
}
#homeslider {
width: 1090px;
margin: 0px auto;
max-height: 590px;
}
For reference, this slideshow is the expected behavior.
ETA: Added some of the code that I think is important?
In your .slidercaption you have a top:-200px which is causing the issue. Unlike margin, elements with position:relative won't physically move when you set a top or left style. That means the occupied space for that element will still remain on that position.
So to fix that, remove top: -200px and replace with margin-top: -200px instead.
From this:
.slidercaption {
position: relative;
top: -200px;
}
To this:
.slidercaption {
margin-top: -200px
}
Take note, in your css there's a margin:0 set in that element. Make sure your update will override that existing style.
Update:
A far better solution is to use position:absolute instead, since having a negative margin or position is more likely to get an issue with that huge value.
So...
From:
.slidercaption {
position: relative;
top: -200px;
}
To:
.slidercaption {
position: absolute;
bottom:0;
}
Then what was causing the below elements to go up is because of this:
#sliderNav {
margin-top: -190px;
}
Change that to:
#sliderNav {
position: absolute;
bottom: 168px;
z-index: 99;
width: 100%;
margin: 0;
}
When you came to a point where you are using large negative values, you can use position:absolute instead which is very helpful and less likely to have some issues if used properly.
I want to make this shopping cart's div follow the user's viewport (http://testshop.michaelkenji.com/), so I tried to simply injecting div { position:fixed} to it's stylesheet, it worked, but there are complications which I am here to ask.
Q: Given two fixed elements, and they collide, which one will be on top?
Q: How do I make an element be the absolute "top" (with only css)
When you want to overlap the element in top, you should use a higher z-index value for eg:
div{
position: fixed;
top: 0;
width: 100%;
z-index: 1;
}
div{/*this div will be on top layer of previous div*/
position: fixed;
top: 0;
width: 100%;
z-index: 2;/*because of higher z-index*/
}
I have a layout containing two columns. The left column scrolls vertically and the right column is fixed. The right column contains multiple sections.
At some point, an overlay will be displayed. I want it to cover all content except for the first section in the right column (<aside class="one">...</aside>).
Right now, when the overlay is displayed it covers everything, including the aside.one element even though it has a higher z-index value.
HTML:
<!-- ... -->
<section id="sidebar">
<aside class="one">...</aside>
<aside class="two">...</aside>
</section>
<!-- ... -->
<div class="overlay"></div>
CSS:
#sidebar {
position: fixed;
}
.one {
z-index: 3;
}
.overlay {
bottom: 0;
left: 0;
position: absolute;
right: 0;
top: 0;
z-index: 2;
}
Here is a jsFiddle illustrating a more complete example of my problem: http://jsfiddle.net/ncSWz/10/
I know I will need to change some of my HTML structure to get this working, but I'm unsure exactly where to start.
Thanks in advance.
There's actually a bit of stacking context that's involved in this problem.
The idea is essentially, you need to have both the .overlay and aside.one in the same stacking context. You can accomplish that by moving the .overlay into the #sidebar (since it is position: fixed - this creates a stacking context).
Next, you need to create a couple more nested stacking contexts inside the sidebar. By changing the positioning on aside.one to relative and .overlay to fixed, you create another 2 stacking contexts that allow you to play with z-indexing of the elements.
Finally, you need to change the z-index on #header so that its below the overlay.
the root element (HTML),
positioned (absolutely or relatively) with a z-index value other than "auto",
elements with an opacity value less than 1.
on mobile WebKit and Chrome 22+, position: fixed always creates a new stacking context, even when z-index is "auto"
Source: MDN: The stacking context
Updated jsFiddle: http://jsfiddle.net/ncSWz/17/
HTML
<section id="sidebar">
<!-- Should be on top of the overlay -->
<aside class="one">...</aside>
<aside class="two">...</aside>
<!-- Should cover all elements except for aside.one -->
<div class="overlay"></div>
</section>
CSS
header {
z-index: 0;
}
...
.one {
position: relative;
width: 100%;
z-index: 3;
}
.overlay {
bottom: 0;
display: none;
left: 0;
position: fixed;
right: 0;
top: 0;
z-index: 1;
}
Put position: absolute on both one and two, and set the top of two; since their parent is fixed, they're absolutely positioned to it.
Running Demo
CODE ADDED
.one {
position: absolute;
}
.two {
position: absolute;
top: 25px;
}
Back to basics, I have a situation whereby I have an image which needs to appear over the background of an element just below it. However, I do not want the image to be over the top of the content of that element, only the element (and background properties) itself:
http://jsfiddle.net/chricholson/4twr5/1/
HTML:
<img src="https://www.google.com/images/srpr/logo3w.png" />
<div>
Hello World
</div>
CSS:
img { position: absolute; z-index: 20; }
div { position: relative; top: 45px; z-index: 10; padding: 30px; background: red; }
a { position: relative; z-index: 30; padding: 10px; background: yellow; display: block; }
Expected behaviour would be the image to appear over the top of the div background [check], but then appear behind the yellow link, which it isn't.
Found my "answer" (more confirming my doubts) here Can't get z-index to work for div inside of another div. The key sentence being "Once you set position:relative on test_1 you are causing its z-index to be in a different world than the z-index of test_2."
It seems the reason I have a problem is because once I have set the image higher than the div, no matter what z-index value I set to the contents of the div it will always be covered. I didn't think z-indexes worked like this, I thought everything was "separate" and so long as a value was higher than a value elsewhere (regardless of parent containers) things would be fine.
img { position: absolute; z-index: 15; }
div { position: relative; top: 45px; z-index: 20; padding: 30px; background: red; }
a {z-index: 30; padding: 10px; background: yellow; display: block; }
You can try this code. By the way a is a child of div. You don't need to type position: relative; Because you wrote for div.
Put the image inside the div like so:
<div>
<img src="https://www.google.com/images/srpr/logo3w.png" />
Hello World
</div>
Most of the answers here are pointing out the base truth: in straight up HTML + CSS, this is probably only possible if the <img> is inside of the <div> and a sibling to <a>.
Since you've indicated that you can't change the HTML, you could instead apply a simple JavaScript that would reorder the DOM as necessary for you: $('div').prepend($('img'));. (This is JQuery, by the way.) What this does is it takes the <img> and sticks it as the first child in <div>.
Of course, if you were to use this in production code, you'd want to append id's to the elements and select off that otherwise you'd have images being stuck into divs willy nilly.
Here is a JSFiddle demo. The JQuery is called onDomReady(). The HTML itself is unchanged.
http://jsfiddle.net/4twr5/21/ Look this jsfiddle
Update according to comment http://jsfiddle.net/4twr5/22/
How we can add a div tag over a slideshow like in the following link
http://www.hellofresh.com/
Here the div with title "DISCOVER THE JOY OF COOKING " is placed over a slideshow.
How might I do this?
First of all, welcome to Stackoverflow (oops; this isn't your first question!). The key to placing your div over the slideshow (or over any other element) is using absolute positioning. Absolute positioning enables you to specify the exact position for an element instead of leaving it with the flow of the document. If you take a look at your example website's CSS, you can see that the div that has "Discover the joy of cooking" is styled basically like this:
position: absolute;
top: 0px;
left: 0px;
top and left act somehow like x and y in a 2-dimensional grid system, except that the origin is placed differently. top: 0px; pulls the div up and left: 0px; pulls the div left, so all-in-all, it's placed on the upper-left corner.
To achieve the effect of the translucent black, you use the opacity property. opacity: 0.5; means that the div is half-opaque, while opacity: 0; means it's not visible at all. Your favorite value might be something like opacity: 0.7; -- anything in the range 0...1.
The last piece here is to tell the browser that the div should be over the slideshow, not behind it. To do that, use the z-index property. z-index specifies the relative "stack order" of elements. So if you want your div to be over the slideshow, style it with z-index: 5; while styling the slideshow with z-index: 1;, for instance.
Hope that helped at all!
Ok, you can do something like this ( http://jsfiddle.net/YgpqX/ ):
<div class="div1"></div>
<div class="div2"></div>
.div1 {
width: 320px;
height: 200px;
background: #aa5;
}
.div2 {
width: 100px;
height: 100px;
margin-top: -200px;
background: #5aa;
}
Or ( http://jsfiddle.net/YgpqX/1/ )
<div class="div1"></div>
<div class="div2"></div>
.div1 {
position: relative;
width: 320px;
height: 200px;
background: #aa5;
}
.div2 {
position: relative;
width: 100px;
height: 100px;
top: -200px;
background: #5aa;
}
And if your block in html should be earlier then slider block, then use z-index: 9999; to get it up.
And also abolute position:
<div class="div1">
<div class="div2"></div>
</div>
.div1 {
position: relative;
width: 320px;
height: 200px;
background: #aa5;
}
.div2 {
position: absolute;
width: 100px;
height: 100px;
background: #5aa;
}
By setting the opacity property in CSS
This is some basic info on this one W3schools Css opaque
They are accomplishing this effect using CSS Positioning. Basically they are absolutely positioning the discover the joy of cooking block over the slide show. You can use z-index on the absolute position div to bring it over the relative position (slideshow) div. Basically you need to use a combination of position and z-index. I have a basic example of the CSS/HTML here: http://jsfiddle.net/jqVAe/1/
HTML:
<div id="slideshow">
Scrolling sideshow goes here. Scrolling sideshow goes here. Scrolling sideshow goes here. Scrolling sideshow goes here. Scrolling sideshow goes here. Scrolling sideshow goes here.Scrolling sideshow goes here.
<div id="over-slideshow"></div>
</div>
CSS:
#slideshow{
width: 400px;
height: 200px;
position: relative;
background: green;
}
#over-slideshow{
position: absolute;
top: 10px;
left: 10px;
width: 100px;
height: 100px;
background: red;
}
This provides a basic structure in which to put your slideshow. I would recommend finding a good slide show plugin and not trying to reinvent that functionality. I'm sure there are plenty of Jquery (Javascript Framework) plugins that will accomplish this task for you.
You may try position: absolute and z-index. z index is used to align a layer over or under a layer. You may go through;
Lesson 15: Layer on layer with z-index (Layers), Understanding CSS z-index, and A Detailed Look at the z-index CSS Property