Specify Priority on Div - css

I have a
<div class="banner-right">
and a
<div class="logo">
The Div Class Logo overlaps the banner-right, but the problem is that the right banner covers the logo. Is there a way to set the logo to say that it's more important and it will show over the banner?
div.logo
{
display: block;
position: absolute;
top: 11px;
left: 483px;
margin-left: 472px;
width: 214px;
height: 169px;
background-image:url(images/doxramos_logo.png);
}
div.head-banner-right
{
display: block;
position: absolute;
top: 0px;
left: 159px;
margin-left: 472px;
width: 50%;
height: 60px;
background-image:url(images/metal_grid.jpg)
}

You can set the z-index css property which will cause elements styled with a higher z-index to appear above those with a lower z-index. This only works for absolutely and relatively positioned elements. Since your elements have position: absolute you should be able to just add the z-index property to your css styles for each element.
div.logo
{
display: block;
position: absolute;
top: 11px;
left: 483px;
margin-left: 472px;
width: 214px;
height: 169px;
background-image:url(images/doxramos_logo.png);
z-index: 100;
}
div.head-banner-right
{
display: block;
position: absolute;
top: 0px;
left: 159px;
margin-left: 472px;
width: 50%;
height: 60px;
background-image:url(images/metal_grid.jpg)
z-index: 50;
}
Working Example: http://jsfiddle.net/HTM5v/
MDN Documentation: https://developer.mozilla.org/en-US/docs/CSS/z-index
The z-index CSS property specifies the z-order of an element and its
descendants. When elements overlap, z-order determines which one
covers the other. An element with a larger z-index generally covers an
element with a lower one.

Related

Stacking elements with CSS

Is there a property that allows one element in a div to stack over another element in the same div?
I know with two divs you can use position: relative; & position: absolute; to get this working, but with two elements in the same div I'm not sure what to do.
If you would like to "stack" elements on top of each other, yes you can use the position property.
You would use z-index to alter stack order; the element with the higher z-index would be in front of an element with the lower z-index.
You can see this here, in this fiddle.
.el1 {
height: 100px;
width: 100px;
background: yellow;
position: relative;
z-index: 1;
}
.el2, .el3 {
height: 50px;
width: 50px;
position: absolute;
top: 0;
left: 0;
background: blue;
z-index: 2;
}
.el3 {
background: green;
z-index: 3;
top: 5px;
left: 5px;
}
<div class="el1">
<div class="el2">el2</div>
<div class="el3">el3</div>
</div>

avoid superimposition of div in position:asbolute

I have many div, which are in position:absolute.
I try to avoid their superimposition only with CSS rule. I don't want to change the top value.
.try {
/*some magic?*/
}
#pos1 {
position: absolute;
background-color: blue;
width: 100px;
height: 100px;
top: 50px;
left: 30px;
}
#pos2 {
position: absolute;
background-color: red;
width: 100px;
height: 100px;
top: 90px;
left: 30px;
}
#pos3 {
position: absolute;
background-color: blue;
width: 100px;
height: 100px;
top: 300px;
left: 30px;
}
#pos4 {
position: absolute;
background-color: red;
width: 100px;
height: 100px;
top: 400px;
left: 30px;
}
#current {
position: absolute;
top: 5px;
left: 50px;
}
#expected {
position: absolute;
top: 200px;
left: 50px;
}
<h1 id="current">Current</h1>
<div id="pos1" class="try"></div>
<div id="pos2" class="try"></div>
<h1 id="expected">Expected</h1>
<div id="pos3"></div>
<div id="pos4"></div>
Here is also a jsfiddle :
ps: I supose this behavior it's stupid because I ask for position absolute.
Absolutely positioned elements are removed from the normal flow of the document. That means surrounding content ignore them and occupy their place:
9.6 Absolute positioning
In the absolute positioning model, a box [...] is removed from the
normal flow entirely (it has no impact on later siblings). [...] The
contents of an absolutely positioned element [...] may obscure the
contents of another box (or be obscured themselves), depending on the
stack levels of the overlapping boxes.
So either don't use absolutely positioning, or move your elements (e.g. with margins).

CSS fixed element in section

I have this div here...
<div class="gallery"></div>
and here is the CSS:
.gallery {
background-color: #000;
height: 125px;
text-align: center;
position: fixed;
width: 100%;
z-index: 99999999;
top: 10%;
}
Now my site is broken up into <section> and I am trying to have that element at the top of the section at all times, not that top of the page. How would I accomplish this ?
Add css position: relative to your <section>. Then for .gallery, change fixed to position: absolute; top: 0; left; 0;
Remove that top; 10% stuff too...
You would have to modify the position to be absolute and and top: 0;

Resize <img/> using absolute positioning

div#ipsko changes width and height to satisfy absolute positioning.
Why img#utas doesn't?
JSFiddle: http://jsfiddle.net/pejh7/1/
HTML code:
<div id="upsko">
<img id="utas" src="http://kharg.czystybeton.pl/108.png" />
<div id="ipsko"></div>
</div>
CSS code:
div#upsko {
position: relative;
top: 200px; left: 200px; width: 100px; height: 100px;
background: rgba(255,0,0,0.5);
}
img#utas {
position: absolute;
top: 10px; left: 10px; right: 10px; bottom: 10px;
}
div#ipsko {
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
background: rgba(0,255,0,0.5);
}
Put the img tag in a div, give the image 100% width and height, and then absolute position the container div, e.g.
HTML:
<div id="upsko">
<div id="utas">
<img src="http://kharg.czystybeton.pl/108.png" />
</div>
<div id="ipsko"></div>
</div>
CSS:
#upsko {
position: relative;
top: 200px; left: 200px; width: 100px; height: 100px;
background: rgba(255,0,0,0.5);
}
#utas {
position: absolute;
top: 10px; left: 10px; right: 10px; bottom: 10px;
}
#utas img { height: 100%; width: 100%; }
#ipsko {
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
background: rgba(0,255,0,0.5);
}
Fiddle
The issues you describe are cause by the image width being unspecified (as other answers have stated) unfortunately without stating a px value for the image size (or converting the top/left/bottom/right and height+width to %) there's no way around this without adding an extra div.
I know adding extra div's is generally considered bad practice, but when it gives you flexibility as above, I think it's generally fine to do.
see the the div "div#ipsko" does not has its own height and width so it inherit its parent height and width . But the image has its own height and width . so you have to specify the height and width of image to make in fit in the div.
img#utas {
position: absolute;
top: 0px;
left: 0px;
right: 0px;
bottom: 0px;
width: 100%;
height: 100%;
}
div#upsko {
position: relative;
top: 20px;
left: 20px;
width: 100px;
height: 100px;
background: rgba(255,0,0,0.5);
}
img#utas {
position: absolute;
top: 0px;
left: 0px;
right: 0px;
bottom: 0px;
width: 100%;
height: 100%;
}
iv#ipsko {
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
background: rgba(0,255,0,0.5);
}
please try above code.
You image's actual width and height are overriding things. (The browser will adjust an img element's dimensions to match that of the actual image, once it's downloaded it and can tell what they are, if no width and height are specified as attributes of the img or in the CSS.)
With a normal div rather than an image, you could reset the width and height back to auto if they were being set somewhere else, but auto for an image takes you back to the actual image dimensions. If you just wanted the image to match the size of the container, a 100% width/height would fix things, but that's not going to work if you want a different size implied by fixed positioning.
The only thing I can think of would be to change the markup so that your image loads inside a div, and then has 100% width.
Example jsFiddle here:
<div id="container">
<img id="utas" src="http://kharg.czystybeton.pl/108.png" />
</div>
div#container {
position: absolute;
top: 10px;
left: 10px;
right: 10px;
bottom: 10px;
width: auto;
height: auto;
}
img#utas {
width: 100%;
height: 100%;
}

Absolute Div Fill Remainder of Window

I have 3 divs, all positioned: absolute, but the div I want to fill the width of the window will only adapt to the length of the text within it. I want the yellow div #help to fill the remainder of the window.
I know this sounds noob but I cannot find the solution anywhere.
<div id="tab1">tab1</div>
<div id="tab2">tab2</div>
<div id="help">help</div>
#tab1 {position: absolute;
bottom; 0px;
right: 0px;
width: 50px;
height: 20px;
background-color: green;
}
#tab2 {position: absolute;
bottom; 0px;
right: 50px;
width: 50px;
height: 20px;
background-color: yellow;
}
#help {position: absolute;
bottom; 0px;
right: 100px;
height: 20px;
background-color: red;
}
JS Fiddle: http://jsfiddle.net/FBWzX/
If you want #help to stretch, you can set the left and right values at the same time. This trick also works with top and bottom. Absolute positioned elements are quite flexible.
#help {
position: absolute;
left: 0;
right: 100px;
}
jsFiddle Demo

Resources