Why does a background with a lower z index show on top? - html

I have two overlapping backgrounds with absolute positioning and I'm trying to styling them such that they overlap and one is on top of the other. This is the CSS I wrote so far.
.answered {
position: absolute;
top: 0;
}
.asked {
position: absolute;
top: 0;
}
h1{
display: block;
position: relative;
width: 120px;
border-radius: 5px 5px 0px 0px;
border: 1px solid black;
}
.in-back {
display: block;
background: grey;
z-index: -1;
}
.in-front {
display: block;
z-index: 1;
background: tan;
}
<section class="history">
<div class="asked">
<h1 class="user-show-tab-title in-front">Questions</h1>
<div>
<ul class="question-index-false in-front">
<li>
<a class="question" href="http://localhost:3000/#questions/1">what train runs faster from wycoff?</a>
</li>
<li>
<a class="question" href="http://localhost:3000/#questions/5">What is the history of the Block Island Ferry?</a>
</li>
<li>
<a class="question" href="http://localhost:3000/#questions/78">What are the major human features in New York City?</a>
</li>
<li>
<a class="question" href="http://localhost:3000/#questions/199">How much do greyhound bus tickets from California to Phoenix Arizona?</a>
</li>
</ul>
</div>
</div>
<div class="answered">
<h1 class="user-show-tab-title in-back">Answers</h1>
<div>
<ul class="question-index-false in-back">
<li>
<a class="question" href="http://localhost:3000/#questions/129">What is the state bird of New York?</a>
</li>
</ul>
</div>
</div>
</section>
The grey keeps appearing on top of the tan even though its z-index is lower. I don't understand why.

The elements are not positioned absolute, only their parents .asked and .answered. The position property is not inherited by child elements. Both <ul> have the default position: static; which is always applied unless you specify a different value for the position property. So adding position: relative; makes your z-index way work the way you expect.
.answered {
position: absolute;
top: 0;
}
.asked {
position: absolute;
top: 0;
}
h1 {
display: block;
position: relative;
width: 120px;
border-radius: 5px 5px 0px 0px;
border: 1px solid black;
}
.in-back {
display: block;
background: grey;
position: relative;
z-index: -1;
}
.in-front {
display: block;
z-index: 1;
position: relative;
background: tan;
}
<section class="history">
<div class="asked">
<h1 class="user-show-tab-title in-front">Questions</h1>
<div>
<ul class="question-index-false in-front">
<li>
<a class="question" href="http://localhost:3000/#questions/1">what train runs faster from wycoff?</a>
</li>
<li>
<a class="question" href="http://localhost:3000/#questions/5">What is the history of the Block Island Ferry?</a>
</li>
<li>
<a class="question" href="http://localhost:3000/#questions/78">What are the major human features in New York City?</a>
</li>
<li>
<a class="question" href="http://localhost:3000/#questions/199">How much do greyhound bus tickets from California to Phoenix Arizona?</a>
</li>
</ul>
</div>
</div>
<div class="answered">
<h1 class="user-show-tab-title in-back">Answers</h1>
<div>
<ul class="question-index-false in-back">
<li>
<a class="question" href="http://localhost:3000/#questions/129">What is the state bird of New York?</a>
</li>
</ul>
</div>
</div>
</section>

Related

z-index doesn't work on my menu

I have a menu icon that when I click on it my list items appear;
but it pushes down other elements. I want to set my z-index to it hasn't any effect on other element. but it doesn't work.
<div class="dropdown hidden-md hidden-lg ">
<ul>
<li>
<div class="dropbtn">
<span class="bar1"></span>
<span class="bar2"></span>
<span class="bar3"></span>
</div>
</li>
<li class="dropdown-content">
<ul>
<li>خانه</li>
<li>توانایی ها</li>
<li> <a href='products.php'>محصولات</a></li>
<li><a href='projects.php'>پروژه ها</a></li>
<li><a href='aboutus.php'>درباره ما</a></li>
<li><a href='contactus.php'>تماس با ما</a></li>
</ul>
</li>
</ul>
</div>
CSS:
/* Dropdown Content (Hidden by Default) */
.dropdown-content {
display: none;
clear: both;
float: right;
direction: rtl;
background-color: #f9f9f9;
min-width: 75px;
font: 1.2em Yekan;
}
.dropdown {
position: relative;
z-index: 999999999999;
float: right;
text-align: right;
}
JS
$(".dropbtn").on("click",
function() {
$(".dropdown-content").toggle();
}
);
z-index is a relative property that display your index according to the order where you put your element in your page.
<div>here the z-index is equal to 1</div>
<div>here the z-index is equal to 2</div>
If you want to be sure that your z-index is take in consideration you can put an absolute position to your html element but you break the index stack of your element.
For sample in your case if you want to do that your .dropdown is still above all the other element you can do :
.dropdown {
position: absolute;
z-index:999999999999;
float: right;
text-align: right;
}
But it's will also run with a relative element position and in than case you have to be careful of teh impact because you break the "z-index stack". One advice is that case is to set all the z-index of all your element impact by your design.
First, I put your code on embed and add some style to see your btn.
Second, I fix your bug by replacing position:relative by position:absolute
$(".dropbtn").on("click", function(){
$(".dropdown-content").toggle();
});
/* Dropdown Content (Hidden by Default) */
.dropdown-content {
display:none;
clear: both;
float: right;
direction: rtl;
background-color: #f9f9f9;
min-width: 75px;
font:1.2em Yekan;
}
.dropdown {
position: absolute;
z-index:999999999999;
float: right;
text-align: right;
}
ul {
list-style-type:none
}
.dropbtn {
cursor:pointer;
}
.bar {
display:block;
height: 4px;
width: 24px;
background: black;
margin-bottom: 5px;
border-radius: 2px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="dropdown hidden-md hidden-lg ">
<ul>
<li>
<div class="dropbtn">
<span class="bar1 bar"></span>
<span class="bar2 bar"></span>
<span class="bar3 bar"></span>
</div>
</li>
<li class="dropdown-content">
<ul>
<li>خانه</li>
<li>توانایی ها</li>
<li> <a href='products.php'>محصولات</a></li>
<li><a href='projects.php'>پروژه ها</a></li>
<li><a href='aboutus.php'>درباره ما</a></li>
<li><a href='contactus.php'>تماس با ما</a></li>
</ul>
</li>
</ul>
</div>

How can I make floated link clickable for whole row?

CodePen is here: http://codepen.io/anon/pen/BKVMoY
ul {
width: 40%;
border: 1px solid black;
list-style: none;
padding: 0;
}
span:last-of-type {
float: right;
}
<ul>
<li>
<a href="#">
<span>New York</span>
<span>$489</span>
</a>
</li>
<li>
<a href="#">
<span>New York</span>
<span>$489</span>
</a>
</li>
<li>
<span>New York</span>
<span>$489</span>
</li>
<li>
<span>New York</span>
<span>$489</span>
</li>
</ul>
Why isn't the floated element underlined?
How can I make it clickable for space between the spans?
Why isn't the floated element underlined?
16.3.1 Underlining, overlining, striking, and blinking: the 'text-decoration' property
Note that text decorations are not propagated to floating and absolutely positioned descendants, nor to the contents of atomic inline-level descendants such as inline blocks and inline tables.
To fix that, you can set text-decoration: inherit on the floated span.
span:last-of-type {
float: right;
text-decoration: inherit;
}
How can I make it clickable for space between the spans?
You can set the <a> to display:block, it will the occupies the entire width available.
a {
display: block;
}
ul {
width: 40%;
border: 1px solid black;
list-style: none;
padding: 0;
}
a {
display: block;
}
span:last-of-type {
float: right;
text-decoration: inherit;
}
<ul>
<li>
<a href="#">
<span>New York</span>
<span>$489</span>
</a>
</li>
<li>
<a href="#">
<span>New York</span>
<span>$489</span>
</a>
</li>
<li>
<span>New York</span>
<span>$489</span>
</li>
<li>
<span>New York</span>
<span>$489</span>
</li>
</ul>
I think you can just add display: block; to the anchor tags in order to make the entire row clickable. I'm not exactly sure why the floated element removes the underline.
<ul class="whole-row-link">
<li>
<span>New York</span>
<span>$489</span>
</li>
<li>
<span>New York</span>
<span>$489</span>
</li>
</ul>
ul.whole-row-link li {
position: relative;
}
ul.whole-row-link li a {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0
}
I was able to make the space between the link clickable but it stillls looks weird when you dont have underline.
I used the flexbox to acheive the effect of the clickable.
`http://codepen.io/Ebeldev/pen/BKVMwP`

CSS nested ul flowchart from bottom up

I'm trying to figure out how to position a flowchart built with ul list growing like a tree, from bottom-up.
It's a genealogical family tree. I've made it from top down using this great code here but I want the first element in the bottom, above it the parents and above it the grandparents and so on...
Here is the code:
HTML
<div class="tree">
<ul>
<li>
<div class="dados_membro">Me</div>
<ul>
<li>
<div class="dados_membro">Father</div>
<ul>
<li>
<div class="dados_membro">Grandfather</div>
</li>
<li>
<div class="dados_membro">Grandmother</div>
</li>
</ul>
</li>
<li>
<div class="dados_membro">Mother</div>
<ul>
<li>
<div class="dados_membro">Grandfather</div>
</li>
<li>
<div class="dados_membro">Grandmother</div>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
CSS
.tree ul {
padding-top: 20px;
position: relative;
}
.tree li {
float: left;
text-align: center;
list-style-type: none;
position: relative;
padding: 39px 5px 0 5px;
}
.dados_membro {
border: 1px solid;
border-radius: 20px;
width: 300px;
margin: auto;
text-align:left;
padding:10px
}
I'm testing some rules in this fiddle and could position the first node "Me" in the bottom and the other ones up with the CSS bellow, but all the nodes in the same level (parents, grandparents and so on..) get pilled up.
.tree ul {
position: absolute;
bottom:0;
}
.tree ul li ul{
position:relative;
}
.tree li {
float: right;
text-align: center;
list-style-type: none;
position: relative;
padding: 20px 5px 0 5px;
margin-top:-120px
}
.dados_membro {
border: 1px solid;
border-radius: 5px;
width: 100px;
margin: auto;
text-align:left;
padding:10px
}
I don't know how big will the tree grow up nor if all the nodes will have elements. Can't use javascript for this. Any ideas?
You could change the order of the elements using the flexbox property flex-direction: column-reverse, if that is an option for your project (see compatibility table).
To revert the order of the list I gave the main container position: absolute with bottom:0, the <ul>s a position:relative and all <li>s position:absolute with bottom:50px, except for the first level that I want to be in the bottom of the container.
This way every <li> is positioned 50px above its parent <ul> that has relative position.
One last tweak to achieve the tree style was adding a class for female and male parents in order to position each one in the left and in the right above its child node. Since I'm generating this tree dinamically I can count how many levels does the tree has and calculate the distance between father/mother nodes. If I could not control the html dinamically, I would need to use javascript to count the nodes and apply the css rules on the fly (maybe I will do ti in the future, just to animate the tree generation).
The new code is here
HTML
<div class="tree">
<ul>
<li>
<div class="dados_membro">Me</div>
<ul>
<li class="f1">
<div class="dados_membro">Father</div>
<ul>
<li class="f2">
<div class="dados_membro">Grandfather f</div>
</li>
<li class="m2">
<div class="dados_membro">Grandmotherf </div>
</li>
</ul>
</li>
<li class="m1">
<div class="dados_membro">Mother</div>
<ul>
<li class="f2">
<div class="dados_membro">Grandfather m</div>
</li>
<li class="m2">
<div class="dados_membro">Grandmother m</div>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
CSS
.tree {
position:absolute;
bottom: 0;
left:300px
}
.tree ul {
position: relative;
list-style:none;
}
.tree ul li ul li{
position: absolute;
bottom: 50px;
}
.f1{
left:-150px;
}
.m1 {
left:150px;
}
.f2{
left:-80px;
}
.m2 {
left:80px;
}
.dados_membro {
border: 1px solid;
border-radius: 5px;
width: 100px;
margin: auto;
text-align:left;
padding:10px
}

CSS Drop Down Menu - List elements are static

I may seem really silly or outright wrong in the way I code. However, when I create a drop down menu in CSS the new li elements get pushed to the other side of the page and not in the container box. How would I fix this?
Here is the code:
<nav>
<div class="wrapper">
<div class="brand">
<img class="UKLogo" src="images/logo.png" alt="">
</div> <!-- brand -->
<div class="navigation">
<ul class="nav-ul">
<li> HOME </li>
<li> ABOUT </li>
<a href="#">
<li class="course-li">
COURSES
<ul class="drop-down">
<li class="list-item"> Driver CPC </li>
<li> First Aid </li>
<li> Other </li>
</ul>
</li>
<li> CONTACT </li>
<!-- <li> TESTOMONIALS </li> -->
<!-- <li> FAQs </li> -->
</ul>
</div> <!-- Navigation -->
</div> <!-- Wrapper -->
</nav>
nav {
margin: 0px;
padding: 0px;
height: 75px;
background-color: #FFF;
}
.brand {
margin: auto;
width: 960px;
}
.company-name {
padding: 0px;
margin: 0px;
}
.UKLogo {
padding: 0px;
margin: 0px;
position: relative;
top: 11px;
}
.navigation ul li {
display: inline-block;
margin: 10px;
position: relative;
left: 380px;
top: -46px;
}
.navigation ul a {
color: black;
margin-left: 40px;
text-decoration: none;
font-family: Lato;
font-weight: 300;
}
.navigation ul a:hover {
color: #169ec5;
font-weight: 300;
}
.course-li:hover .drop-down {
left: 0px;
}
.drop-down {
position: absolute;
left: -5px;
z-index: 1;
background-color: white;
left: -9999px;
}
Thank you ever so much for looking and helping. Always open to criticism whether its the way I code or anything else.
Here is a JSFiddle https://jsfiddle.net/vj41qLts/
Many Thanks!
You need to declare a position in the parent, for the child to reside in. An element with position: absolute; will position itself to the first parent with position: relative;. If there is no parent with position: relative;, it will use the browser window instead.
See fix example here: https://jsfiddle.net/vj41qLts/1/
I think there are two thing you need to change:
ul li will select everything li in the navigation even the dropdown, ul>li will only select the immediate child, instead of running down the nested elements.
you need to add position:relative; in your dropdown's parent.
One of the first issues I see is the fact that your markup for your main links isn't setup correctly. Following a structure more link the below should give make it work the way you want it to:
<nav>
<ul>
<li><a href="#">Home<a></li>
<li><a href="#">About<a></li>
<li>
<a href="#">Courses<a>
<div>
<ul>
<li>A link</li>
<li>A link</li>
</ul>
</div>
</li>
</ul>
</nav>
Then use CSS or JS to control showing and hiding the dropdown of links.

Expand the clickable area of a html link to the size of the wrapping li element that contains other content

I have basically this html code:
<ul class="unordered-list">
<li class="list-item">
<div class="list-item-block">
Sometimes a wrapped link to Google
<p class="list-item-link-description">Short description of the link above</p>
</div>
</li>
</ul>
Is there a way to expand the clickable area of the link to the size of the li element by keeping the links position and the description nicely below the link?
I tried to use absolute positioning for both the link and the description but this fails if for example the link text has a line break. As you can see in this jsFiddle: http://jsfiddle.net/xgcjngvs/3/
I would love to find a solution for this problem without javascript.
EDIT: I should have mentioned that the link tag should only contain the plain text and not any other html code.
Given your new requirement there is another way that this can be achieved without changes to your existing HTML structure:
Remove the absolute positioning from .list-item-link and .list-item-link-description, position: absolute; takes the elements out of the document flow and these two need to be aware of how much space each of them take up
Add a pseudo element to .list-item-link using .list-item-link:after, make this position: absolute; and set the height and width to take up the dimensions of the container.
.unordered-list {
list-style: none;
padding-left: 0;
}
.list-item {
min-height: 50px;
position: relative;
}
.list-item-link {
width: 100%;
}
.list-item-link:after {
content: "";
height: 100%;
left: 0;
position: absolute;
top: 0;
width: 100%;
}
.list-item-link-description {
margin: 0;
}
<ul class="unordered-list">
<li class="list-item">
<div class="list-item-block">
Sometimes a wrapped link to Google
<p class="list-item-link-description">Short description of the link above</p>
</div>
</li>
<li class="list-item">
<div class="list-item-block">
Sometimes a wrapped link to Google. It is very very very very very very long to demonstrate the linke break.
<p class="list-item-link-description">Short description of the link above</p>
</div>
</li>
</ul>
JS Fiddle: http://jsfiddle.net/5s44c95q/
This is possible with a few changes to your markup and css:
Change list-item-block into the a element and set it as display: block;
Change list-item-link and list-item-link-description into span elements as only inline elements are valid in a elements
Style list-item-link to look like the link
Style list-item-link-description to look like the paragraph
.unordered-list {
padding-left: 0;
list-style: none;
}
.list-item {
position: relative;
height: 50px;
}
.list-item-block {
display: block;
min-height: 100%;
text-decoration: none;
}
.list-item-link {
text-decoration: underline;
}
.list-item-link-description {
color: #000000;
display: block;
text-decoration: none;
}
<ul class="unordered-list">
<li class="list-item">
<a href="www.google.com" class="list-item-block">
<span class="list-item-link">Sometimes a wrapped link to Google</span>
<span class="list-item-link-description">Short description of the link above</span>
</a>
</li>
<li class="list-item">
<a href="www.google.com" class="list-item-block">
<span class="list-item-link">Sometimes a wrapped link to Google. It is very very very very very very long to demonstrate the linke break.</span>
<span class="list-item-link-description">Short description of the link above</span>
</a>
</li>
</ul>
If your short description will be on 1 line, you can add padding-bottom to the list-item-link and then move the description up by the same amount and also set a negative margin-bottom for the block as a whole. If you do the padding in ems, it should take care of different font sizes.
To make the short description clickable, you need to make the z-index of the link higher than the description.
.unordered-list {
padding-left: 0;
list-style: none;
}
.list-item {
margin-top:10px;
margin-bottom:-2em;
position:relative;
}
.list-item-link {
display:block;
position:relative;
border:1px #000 solid; /*to show link area */
padding-bottom:2em;
z-index:1;
}
.list-item-link-description {
position:relative;
top:-2em;
}
<ul class="unordered-list">
<li class="list-item">
<div class="list-item-block">
Sometimes a wrapped link to Google
<p class="list-item-link-description">Short description of the link above</p>
</div>
</li>
<li class="list-item">
<div class="list-item-block">
Sometimes a wrapped link to Google. It is very very very very very very long to demonstrate the linke break.
<p class="list-item-link-description">Short description of the link above 2</p>
</div>
</li>
</ul>
EDIT: I removed the paragraph tags as you have requested but I can not get it to work any other way without the span, so the span would have to stay in place.
<ul class="unordered-list">
<li class="list-item">
<div class="list-item-block">
<span class='anchor-control'><a href="www.google.com" class="list-item-link">Sometimes a wrapped link to Google</span>
Short description of the link above
</a>
</div>
</li>
<li class="list-item">
<div class="list-item-block">
<span class='anchor-control'><a href="www.google.com" class="list-item-link">Sometimes a wrapped link to Google. It is very very very very very very long to demonstrate the linke break This needs to be a longer link then .</span>
Short description of the link above
</a>
</div>
</li>
</ul>
And here's your CSS.
EDIT: New styles to match the top, it's pretty straight forward stuff
a{
text-decoration: none;
color: #000;
}
.anchor-control a{
text-decoration: underline;
width: 100%;
float: left;
color: #00f;
}
.unordered-list {
padding-left: 0;
list-style: none;
}
.list-item {
position: relative;
padding; 0;
margin: 0;
}
.list-item-link {
position: relative;
}
a .list-item-link-description {
position: relative;
color: #000;
margin: 0;
margin-bottom: 10px;
}
EDIT: This should be what you're after.
http://jsfiddle.net/xgcjngvs/9/
css
a{
text-decoration: none;
}
.anchor-control{
text-decoration: underline;
}
.unordered-list {
padding-left: 0;
list-style: none;
}
.list-item {
position: relative;
padding; 0;
margin: 0;
}
.list-item-link {
position: relative;
}
#content {
display: none;
}
#show:target #content {
display: inline-block;
}
#show:target #open {
display: none;
}
.btn-open:after,.btn-close:after{
position:absolute;
right:280px;
top:0;
}
.btn-open:after{
content: "\25BC";
}
.btn-close:after{
content: "\25B2";
}
<ul class="unordered-list">
<li class="list-item">
<span class='anchor-control'>Sometimes a wrapped link to Google</span>
<div id="show">
<div id="content">
Short description of the link above
</div>
</div>
</li>
</ul>
try this JSFIDDLE