Place image behind divs using CSS - html

I have the following markup (Fiddle Example Here: http://jsfiddle.net/9gvj11o5/8/)
<div class="header">
<div class="image">
<img src="http://placehold.it/2200x800" alt=""/>
</div>
<div class="menu">This is the menu</div>
<div class="tools">These are the tools</div>
</div>
<div class="content">content</div>
And the following CSS:
.header {
position: relative;
}
.image {
position: absolute;
}
img {
width: 100%;
height: auto;
outline: 0;
}
I need the image to be responsive and have 100% width aligned to top.
But I also need the menu and tools to be over the image and having their normal flow.
Content should be after the image, so after the header.
The image would be the "background" of the header div. (I cannot use background-image)
I am using position but the menu and tools disappear the moment I use it.
What am I missing? Do I need another wrapper div somewhere?

I would wrap the 2 divs .menu & .tools so you need only to apply z-index to the wrapper div instead of each child. which make .menu & .tools (wrapped) in front of the .image.
then change position:absolute to position:relative to .image in order to have .content below header.
Below you can see the snippet, very lightweight.
.header {
position: relative;
}
.image {
position: relative;
z-index:1
}
#menu-all {
position:absolute;
top:0;
z-index:2
}
img {
width: 100%;
height: auto;
outline: 0;
}
<div class="header">
<div class="image">
<img src="http://placehold.it/2200x800" alt="" />
</div>
<div id="menu-all">
<div class="menu">This is the menu</div>
<div class="tools">These are the tools</div>
</div>
</div>
<div class="content">content</div>

You can use z-index to define the layer order of your elements. The smaller the number, the closer to the "bottom" of the stack. So we give the img a very small number, and menu and tools a very large one.
.header {
position: relative;
}
.image {
position: absolute;
z-index: 1; /* here you can use -1 as Paulie_D points out in the comments */
}
img {
width: 100%;
height: auto;
outline: 0;
z-index: 2; /* here you can use -1 as Paulie_D points out in the comments */
}
.menu {
position: absolute;
top: 0;
left: 0;
z-index: 888; /* You can remove this declaration entirely if you set -1 above */
}
.tools {
position: absolute;
top: 0;
right: 0;
z-index: 888; /* You can remove this declaration entirely if you set -1 above */
}

Related

How to overlay text on an image in a container--and keep the text in the container

I'm following the solution given in a bagillion places for how to overlay text on an image, using relative and absolutepositioning. The problem is that the text with position: absolute pops out of its container and goes to the utmost top, right, etc.
I'd be happy to use a background image, but then I need tricks to get the container to match the size of the image, and I don't know of a way to make it fluid.
Seems like a pretty simple problem that folks are always looking for a solution to. Also the thing of overlaying text on an opaque image, and needing to use :after. Wish there were straightforward options for these situations.
.container {
margin: 0 10%;
}
.container img {
position: relative;
width: 100%;
}
#div1 .text {
position: absolute;
bottom: 0;
right: 0;
}
#div2 .text {
position: absolute;
top: 0;
left: 0;
}
<div id="div1" class="container">
<img src="http://placehold.it/800x200" />
<div class="text">
<h3>Top Image</h3>
<p>This text should be in the bottom right of the top image.</p>
</div>
</div>
<div><p>A bunch of miscellaneous text here.</p></div>
<div id="div2" class="container">
<img src="http://placehold.it/800x200" />
<div class="text">
<h3>Lower Image</h3>
<p>This should be in the top left of the lower image.</p>
</div>
</div>
You need to set position:relative on .container which is the correct container for .text. Note that the img is a sibling of .text and not its container.
.container {
margin: 0 10%;
position: relative;
}
.container img {
width: 100%;
}
#div1 .text {
position: absolute;
bottom: 0;
right: 0;
}
#div2 .text {
position: absolute;
top: 0;
left: 0;
}
<div id="div1" class="container">
<img src="http://placehold.it/800x200" />
<div class="text">
<h3>Top Image</h3>
<p>This text should be in the bottom right of the top image.</p>
</div>
</div>
<div><p>A bunch of miscellaneous text here.</p></div>
<div id="div2" class="container">
<img src="http://placehold.it/800x200" />
<div class="text">
<h3>Lower Image</h3>
<p>This should be in the top left of the lower image.</p>
</div>
</div>
I am not quite sure there is enough information. I am assuming that your containers are 800px wide and 200 px tall (I have used a min-height in case your container increases its height). If you could provide more specific information that would be great (there are more complicated ways using object-position property (etc) to make this a little bit "smarter".
img {
max-width: 100%;
display: block;
height: auto;
}
.container {
position: relative;
min-height: 200px;
width: 800px;
margin: 0 10%;
}
.container img {
z-index: 500;
top: 0;
left: 0;
}
.container .text {
position: absolute;
z-index: 1000;
}
#div1 .text {
bottom: 0;
right: 0;
}
#div2 .text {
position: absolute;
top: 0;
left: 0;
}

Responsive, pure CSS hover image gallery

I want to make responsive image gallery. That will display extended image on thumbnail hover. Gallery can't use any JS this is requirement.
But there is 1 little problem. Gallery needs to be responsive.
That means expanded image have to be the same size as the default image that is responsive and resize on smaller devices.
Here is my html code
<div class="container"></div>
<div class="container">
<div id="gallery-photo-container">
<img src="http://imgur.com/60BBDre.jpg">
<div class="gallery-thumbnail-image">
<img src="http://imgur.com/60BBDre.jpg">
<div class="gallery-main-image">
<img src="http://imgur.com/60BBDre.jpg">
</div>
</div>
<div class="gallery-thumbnail-image">
<img src="http://i.imgur.com/C7SFJxy.jpg">
<div class="gallery-main-image">
<img src="http://i.imgur.com/C7SFJxy.jpg">
</div>
</div>
<div class="gallery-thumbnail-image">
<img src="http://i.imgur.com/aa5kiAi.jpg">
<div class="gallery-main-image">
<img src="http://i.imgur.com/aa5kiAi.jpg">
</div>
</div>
<div class="gallery-thumbnail-image">
<img src="http://imgur.com/TWLJOVv.jpg">
<div class="gallery-main-image">
<img src="http://imgur.com/TWLJOVv.jpg">
</div>
</div>
</div>
</div>
Absolute approach
I have almost done it, using absolute position and positioning with top attribute. But on resize expanded image is the size of left container beginning to the right end of the page.
Here is my DEMO1 and CSS.
.gallery-thumbnail-image:hover > .gallery-main-image {
visibility: visible;
opacity: 1;
}
.gallery-thumbnail-image {
display: inline-block;
}
#gallery-photo-container .gallery-thumbnail-image > img {
width: 79px;
}
#gallery-photo-container img {
max-width: 100%;
}
.gallery-main-image {
position: absolute;
visibility: hidden;
top: 18px;
left: 18px;
margin-right: 8px;
}
.container {
width: 50%;
}
#gallery-photo-container {
border: 1px solid black;
padding: 10px;
}
Relative approach
I think it can be done it too, by using relative position and positioning with bottom attribute. But here the problem is that thumbnail image container is resizing to the expanded image size on hover. And the bottom attribute value is screen size dependent.
In this DEMO2 you have to click on a thumbnail because they are jumping. And here is CSS for relative approach.
.gallery-thumbnail-image:hover > .gallery-main-image {
display: block;
opacity: 1;
}
.gallery-thumbnail-image {
display: inline-block;
}
#gallery-photo-container .gallery-thumbnail-image > img {
width: 79px;
}
#gallery-photo-container img {
width: 100%;
}
.gallery-main-image {
position: relative;
display: none;
bottom: 373px;
}
So, could it be done responsive way with one of these two approaches? Or maybe you have another idea. I'm looking forward for your help.
See this update of your plunk.
https://plnkr.co/edit/6EOKiKEQcxDiuIXApPLo?p=preview
the main changes are here:
.gallery-main-image {
position: absolute;
display: none;
top: 10px;
left: 10px;
right: 10px;
}
#gallery-photo-container {
border: 1px solid black;
padding: 10px;
position: relative;
}
Use of an absolute element positioned within relatively positioned element.
You need a margin-right: 8px;, because of the top: 8px; left: 8px; Plunkr:
.gallery-thumbnail-image:hover > .gallery-main-image {
visibility: visible;
opacity: 1;
margin-right: 8px;
}
Slightly off-topic, but... just in case you're interested in simulating an onclick event with CSS, see this SO answer.

How do I position a div below an image with changing height?

In my new site I have an image set to width 100% and then a some text underneath that. How to I change the position of the text based on the height of the image?
.pic {
position: absolute;
left: 0;
width: 100%;
}
.pic img {
position: absolute;
width: 100%;
}
.text {
position: relative;
margin-top: 10px;
}
<div class="pic">
<img src="#" />
</div>
<div class="text">
<h3>Some Important Message</h3>
</div>
As requested, here is the fiddle https://jsfiddle.net/sLx3n2vz/
Please try this:
Remove all you css and add only
.pic{
width:100%;
}
.pic img{
width:100%;
}
.text{
margin-top:10px;
}
DEMO
The way you are positioning your HTML element is wrong. Please correct it.

CSS - Positioning Conundrum

I'm working with absolute positioning within a relative div. The code is as such: http://jsfiddle.net/32mq5v6L/1/
HTML
<div id="container">
<div id="featured-posts">
<div class="slide"><img src="http://alien.devprose.com/starwars/wp-content/uploads/2014/12/star-wars-droid.jpg" /></div>
<div class="slide"><img src="http://alien.devprose.com/starwars/wp-content/uploads/2014/12/han-solo-1140x350.jpg" /></div>
</div>
<div id="other-content">
Other Content
</div>
</div>
CSS
#container { width: 100%; margin: 0 auto; background: #eee; }
#featured-posts { position: relative; width: 100%; height: auto;}
.slide { width: 100%; height: 20px; position: absolute; top: 0; }
#other-content { }
My problem is the other-content div appears underneath #featured-posts unless I apply a set height to that container, which I can't do since the goal is to make all of this responsive.
Where am I going wrong?
If you plan to have #other-content after positioned container, you will have to create new stacking context in order to move it above. One way to do it since it's not positioned is to set very little opacity:
#other-content {
z-index: 10;
opacity: .99;
}
Demo: http://jsfiddle.net/32mq5v6L/1/

CSS covering a div with other that got opacity set with css

Hi Folks Here is what i got in css:
#loading {
background:#000 url(loading.png) center;
opacity:0.5;
cursor:auto;
min-height:250px;
z-index:15;
}
#main {
padding: 10px;
z-index:1;
}
and in html:
<div id="loading">
<div id="main">Something here</div>
</div>
and i expect the loading.png to cover the div#main but it doesn't and "Something here" stays on the top of loading.png !?
Update: background is in CSS not an image in loading div.
Your HTML is wrong. The div main should be outside the div loading:
<div id="main">
<div id="loading"></div>
Something here
</div>
You also need to position the latter div using CSS so that it does not just push the main content out from underneath it, as well as sizing the div at 100% of its container's width and height:
#main { position: relative; }
#loading {
background: url("loading.png");
opacity: 0.5;
cursor:auto;
width: 100%;
height: 100%;
z-index:15;
/* Positioning */
position: absolute;
left: 0;
top: 0;
}