how to position a div on top section of its parent div - html

Somewhere in a page, how to make div-a2 position above div-a1? Of course, I cannot make div-a2 above div-a1 in the layout below.
<div id=a>
<div id=a1> something here
</div>
<div id=a2> show this part first
</div>
</div>
still looking for better solution. thanks

You can achieve this with pure css. Write like this:
#a{
display:-moz-box;
display:box;
display:-webkit-box;
-moz-box-direction:reverse;
box-direction: reverse;
-moz-box-direction:reverse;
-webkit-box-direction:reverse;
-moz-box-orient:vertical;
-webkit-box-orient:vertical;
box-orient:vertical;
}
'
Check this http://jsfiddle.net/ASVtx/1/

You will need to give the elements absolute position css, and then position them appropriately depending on the content size of each like:
#a1,#a2{position:absolute;}
#a2{ top: 0; }
#a1{ top: 200px;}
OR, within the parent:
#a1,#a2{position:relative;}
#a2{ top: 0; }
#a1{ top: 200px;}
Or, a perhaps better alternate is to change the layout order (but I assume that is not possible for some reason not stated).
Here is an example: http://jsfiddle.net/MarkSchultheiss/CRCvU/
See this updated example where I used em instead of px for the position, gave the parent a border so you see its scope, and added stuff around the parent. http://jsfiddle.net/MarkSchultheiss/CRCvU/1/
Just in case you want to go that way, here is the jQuery code to do this:
var ah1 = $('#a1').height();
var ah2 = $('#a2').height();
var ah = $('#a').height();
var relpos = {float:"left",
display: "inline-block",
position: "relative",
clear: "both"
};
$('#a').css({
height: ah
});
$('#a1, #a2').css(relpos);
$('#a2').css('top', -ah1);
$('#a1').css('top', ah2);
Working scripted example here: http://jsfiddle.net/MarkSchultheiss/CRCvU/3/

Related

How to place an html element outside of a newly generated one

Is it possible to place an Html element outside of a newly generated one?
Well, I have an IONIC2 app that generates a new element <scroll-content>, the issue is that this element has some CSS properties that affects the child elements.
So, what I would like to do it either to place that my div element outside of that <scroll-content> or even better to disable the CSS properties of <scroll-content> on the div
Here is the code, so I can make things clearer:
HTML
<ion-content id="contentPadding">
<div class="header">
</div>
</ion-content>
When Ionic renders the above code, the browser generate something like this:
HTML
<ion-content id="contentPadding">
<scroll-content>
<div class="header">
</div>
<scroll-content>
</ion-content>
CSS:
.top{
background:black;
}
//generated
scroll-content{
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
z-index: 1;
display: block;
overflow-x: hidden;
overflow-y: scroll;
-webkit-overflow-scrolling: touch;
will-change: scroll-position;
}
I guess, it's clearly shown that a new element called <scroll-content> is being created and <div class="header"> inherits all the css properties of <scroll-content> which I would like to avoid in my case.
Your header (child) is inheriting its parent's (scroll-content) CSS styling. You need to clear any unwanted inherited rules by explicitly changing the inherited styles. For example, if you want to reset the css-display, write
.header {
display: initial;
}
Hopefully in the future we can avoid this with the all:initial trick - however, it currently isn't supported enough.

z-index issues in safari

I am having some trouble with z-index working on a fixed element in safari. I created the scrolling site in firefox and wasn't even needing to specify z-index for the simple text div to be behind the other content until scrolled down to. But for some reason in Safari it shows up in front of everything else. I have tried creating a negative z-index for it and positive z-index for everything else but no change. Here is the code for it.
Thanks for any help!
Also here is the link to view it if that helps make more sense (you have to login to view the site - use username stackoverflow password:stackoverflow
http://lynchbryan.com/wp-login
<div id="tagline">
<span class="tags">We</span><span class="tagl">partner</span> <span class="tags">with clients to</span> </br><span class="tagl">cultivate</span> <span class="tags">the</span> <span class="tagl">potential</span><span class="tags"> of people</span>
</div>
#post-16 #tagline {
position:fixed;
bottom:50%;
text-align: center;
left: 50%;
z-index:-999;
}
#post-16 .tags {
font-family:'AndesLight';
font-size:23px;
color:#ffffff;
padding:0 10px;
}
#post-16 .tagl {
font-family:'ThirstyRoughReg';
font-size:50px;
color:#ffffff;
}
I wouldn't suggest trying to hide something with z-index. Instead you should try display: none;. If that's not the issue you're encountering then I still wouldn't suggest setting a negative z-index value. It's good practice to keep these values at 0 or above. Try setting the z-index of everything else to a higher number like 3 or 4 and then setting #post-16 #tagline to 1 or 2.
EDIT
According to your comments, you want to hide .tags and .tagl. You should do the following CSS rule, ignoring the z-index:
.tags, .tagl { display: none; }
Based off of what you have already. A really simple example would be. You may want to throttle of often the scroll event is called since it will be called a lot.
#post-16 #tagline {
position:fixed;
bottom:50%;
text-align: center;
left: 50%;
display: none;
}
//cache the element so you only need to look it up once.
var $tag = $('#post-16 #tagline');
//name space the scroll event
$(document).on('scroll.show-tagline' function (e) {
//this is how far the document has been scrolled in pixels
var scrolled = $(this).scrollTop();
//what ever value you want
if ( scrolled > 300 ) {
$tag.show();
} else {
$tag.hide();
}
})

How can I get a child of an element with lower z-index on front of a child of another element with higher z-index?

I have the following setup:
<div style="z-index: 10">
<div>Whatever</div>
</div>
<div style="z-index: 9">
<div><div>Haaaleluia</div></div>
</div>
Of course... I oversimplified the setup but that's the main idea. The "whatever" div is overlapped by the "Haaaaleluia" div. Of course because the first parent has bigger z-index "whatever" is visible and "haaaleluia" is not.
Without changing the setup (and to be clear that includes keeping the parents z-indexes), how can I make "Haaaaleluia" to be on top?
For those asked for print here it is... also thank you for help:
The big bad map is the second div.
The tutorial is the first div.
The panel with orders is child of the map. I need it to be on top. If I set whole map on top, the tutorial is not visible any more. If I keep the map behind the orders panel is not visible any more.
In short: you can't. It's not possible to have a child of a parent element with a z-index lower than a sibling, and give that child a higher z-index value than the parent's sibling, as the inherited z-index is relative to the parent (see this question for someone coming up against the same thing).
However, depending on your setup, you can remove the z-indices completely, and let the browser place your top div above the one below. You could then play around with giving just the children a z-index value.
Josh Comeau has a great article going into depth on the problem and various solutions/workarounds.
Without changing the locations, z-index, or rearranging any of the elements, the only way I can think of that would allow the div underneath to appear would be to either change the visibility, display or opacity of the div overlapping the one you want to see.
That is, use one of the following CSS styles on the parent div of "Whatever":
visibility: hidden;
display: none;
opacity: 0;
You're basically asking to display an element above another element that has a higher z-index, which defeats the purpose of having a z-index. If you're trying to control how elements overlap, it really should be done with z-index. I would suggest you rethink how your elements are organized (maybe move the "Haaaleluia" div outside of its parent and assign it its own z-index). Otherwise, I might suggest you consider creating a duplicate of "Haaaleluia" to display above "Whatever", which may or may not suit your situation.
Edit: Looking at the image you've provided, changing the parent of the order box to the parent of the tutorial div may work for you. Here is a demo using jQuery that may help illustrate the point - just change the hover event to whatever it is that starts the tutorial. Another option may be to clone the order box to lay on top of the one below, but with a higher z-index (so that the user only sees one, but the clone overlaps everything else). This is assuming that your map div is positioned either absolutely or relatively. Hope that helps.
Nice design.
Ok so I'll first say that I believe the solution to your layering problem, as others have already suggested, is moving that box outside of its parent (the map).
But you set some constraints, so I'll try to break as few as possible. I don't know how to do this without breaking any of your constraints. Z-index is inherited (again, others have pointed this out), so you can't break out of your parents' layer with only that tool.
So here's one way you could get the same effect, using javascript. It's ugly, and might cause you more headaches later, but it should at least work:
Find out the absolute position of the div that you want to put on top.
Make a copy of it.
Hide the original (optional if it's opaque).
Insert the copy on top of everything.
If you're using jQuery, you can get elements' position relative to the document with the .offset() function. After that it's fairly simple:
$(document).ready( function() {
$("a[href='#overlay']").click( function() {
// 1: get the position
pos = $('.wrap').offset();
// 2: make a copy
halecopy = $('.bottom .wrap').clone();
// 3: hide the original
$('.bottom .wrap').css({opacity: 0});
// 4: Insert new label on top of everything
$('body').prepend(halecopy);
// position the new label correctly
halecopy.css({position:'absolute',
top: pos.top,
left: pos.left,
'z-index': 2});
// show the "top" layer
$('.top').fadeIn();
});
$("a[href='#hide']").click( function() {
$('.top').fadeOut( function() {
// remove the label copy
halecopy.remove();
// show the original again
$('.bottom .wrap').css({opacity: 1});
});
});
});​
That script works for me on this markup:
<div class="top">
<div class="label">
<p>Whatever</p>
</div>
</div>
<div class="bottom">
<div class="wrap">
<div class="label">
<p>Haaaleluuuuia!</p>
</div>
</div>
</div>
show
hide​
With these styles:
.top,
.bottom {
position: absolute;
top: 10%;
left: 3%;
}
.top {
z-index: 1;
padding: 2%;
background: rgba(0, 127, 127, 0.8);
display:none;
}
.bottom {
z-index: -1;
padding: 3%;
background: rgba(127, 127, 0, 0.8);
}
.label {
color: #fff;
}
.wrap {
outline: 1px dashed rgba(127, 0, 0, 0.8);
background: rgba(127, 127, 127, 0.8);
}
.bottom .label {
z-index: 10;
}
​
And here's a handly jsfiddle demo.
Added colour to the div's to demonstrate the layering:
<div style="position:absolute;background:blue;z-index: 10">
<div>Whatever</div>
</div>
<div style="position:absolute;background:red;z-index: 11">
<div><div>Haaaleluia</div></div>
</div>
This is just an idea which may work.
Get the div having z-index = 9 using jquery.
Then select the 1st child of 1st child of the div having z-index as 9.
Then apply the style as follows:
$(element which you got).attr("style", "position: absolute; z-index: 12;")
The style will be applied to the small element and it will be visible on the red box.

Changing position of div using its Id in javascript

This is my html code :
<div id="d1" class="division">
<input type="button" value="one" class="division">
</div>
<div id="d2" class="division">
<input type="button" value="two" class="division">
</div>
This is my css code
.division
{
top:50px;
left:100px;
display: none;
}
But my div element part is not at all changing its top and left position.If its not the right method then How to change the location of a element using its class?
display: none will make the element invisible.
Try this instead:
.division
{
top:50px;
left:100px;
position: relative;
}
You would need to define is position like so:
position: relative;
or
position: absolute;
Depending on how exactly you want to position the elements. Also using .division will position both elements.
Use the following if you want to just position one:
#d1 { ... }
(Assuming display:none is an initial setting modified by jQuery, for example. Otherwise, you won't see the element at all.)
You need to explicitly use position:relative (or similar, depending on the rest of your CSS) for the browser to render the position offsets.

stack 'a' tag over a 'p' tag

What I am trying to do is to stack an 'a' tag on top of a 'p' tag using the z-index property. So my html goes like this
<div id="personalText" >
edit
<p id="descText">{{profile.desc}}</p>
</div>
and my CSS goes like this
#editButton
{
position:relative;
z-index:2;
}
#descText
{
position:relative;
margin-top: 5px;
margin-bottom:5px;
z-index:1;
}
I believe this should stack the a on top of the p tag but that is not happening. Can anybody please explain what is that I am doing wrongly?
position: relative doesn't detach the element from the layout, so by default the element still takes up the same spot it would otherwise. relative has two purposes: to offset an element relative to its "real" position in the layout (which would require setting top, left, etc), and to serve as a non-static value so that child elements with position: absolute would position themselves relative to it.
With all that said, what you probably want in order to do what you're trying to do, is to set position: relative on the parent, and position: absolute on the edit link (at least). But that'd probably be quite ugly, as the text would likely overlap and be unreadable.
You have to also put
#personalText
{
position:relative;
}
#editButton
{
position:absolute; /* change */
top:0; /* new */
left:0; /* new */
z-index:2;
}
As Mihalis Bagos states, you need to push your #descText element upwards.
Here's the resulting CSS:
#editButton
{
position:relative;
z-index:2;
}
#descText
{
position:relative;
margin-top: 5px;
margin-bottom:5px;
bottom:25px;
z-index:1;
}
Here's the jsFiddle resulting from it.
This is a perfect use for JavaScript:
CSS
.hidden { display: none; }
jQuery
$('#descText').hover(function() {
$(this).find('a').removeClass('hidden');
}, function() {
$(this).find('a').addClass('hidden');
});
DEMO
Here's how you can put the <a> tag on top of the <p> tag: http://jsfiddle.net/gSWJB/1/
The example shows one possible use case: putting the link on top of the description, where the link might only be shown when the user hovers over it.