CSS problem about show / hide - css

I have realized show/hide function in css. But it's not what i need. i want to expand the content layer by layer and hide them all in one click. And now i can't expand layer by layer.
Can you help me with that? Thanks a lot. :)
here is the code and it's tested:
<script language="JavaScript">
function toggle(id) {
var state = document.getElementById(id).style.display;
if (state == 'block') {
document.getElementById(id).style.display = 'none';
} else {
document.getElementById(id).style.display = 'block';
}
}
</script>
<style type="text/css">
#main{
position:relative;
top:20px;
left:20px;
width:200px;
background: lightblue;
}
#hidden {
position:relative;
top:0px;
left:280px;
width:200px;
background: lightgrey;
display: none;
}
#hidden2 {
position:relative;
top:-20px;
left:580px;
width:200px;
background: lightgreen;
display: none;
}
#hidden3 {
position:relative;
top:100px;
left:0px;
width:200px;
background: lightpink;
display: none;
}
</style>
<div id="main" onclick="toggle('hidden');toggle('hidden2');toggle('hidden3');">
1
</div>
<div id="hidden" onclick="toggle('hidden2');toggle('hidden3');">
1.1
</div>
<div id="hidden2"onclick="toggle('hidden3');">
1.1.1
</div>
<div id="hidden3">
1.1.1.1
</div>

Have you considered using a plugin from a framework like jQuery, Mootools, or ExtJS instead of trying to reinvent the wheel?
It looks like you want a treeviewer.
btw, you are using javascript, it's not only CSS... please tag javascript as well.

Related

Change header background colour when page scrolls on sticky header

i have a sticky transparent header using the following css code on my website www.obviagency.com
CSS CODE:
#site-header-inner {
height:0px;
z-index:170;
margin:0 auto;
width:100%;
position:fixed;
top:0;
margin-top:10px;
}
i would like to change the background color on scroll to white. can someone please help me because nothing i've tried works:/
thank you
You would have to use JavaScript with a scroll event listener. I used blue as an example so you can see the change and added a transition property to the header so it would transition smoothly.
let header = document.getElementById('site-header-inner');
document.addEventListener('scroll', function() {
// Get the scroll position
let scrollPos = window.pageYOffset;
if ( scrollPos > 100 ) {
header.style.backgroundColor = "white";
} else {
header.style.backgroundColor = "blue";
}
});
#site-header-inner {
height:50px;
z-index:170;
margin:0 auto;
width:100%;
position:fixed;
top:0;
margin-top:10px;
background-color: blue;
transition: all 0.3s;
}
#section {
height: 1000px;
}
<header id="site-header-inner">
</header>
<section id="section">
</section>

Designer going to CSS. Fundamental flaws with links in nested centered divs

I want to set up a full width slideshow based on the Design I've prepared. I'm building the page on the Unsemantic CSS framework as I was already familiar with 960 gridsystem (960.gs).
What I'm missing is how to write physically correct HTML & CSS. I don't want to build these unstable workarounded glued pages like most.
I've tried my best to build it but nothing seems to work the way it should. Here is a part of the page where a slideshow should appear.
However, it looks like this.
Here's the CCS:
<style>
.slide-wrapper{
background-color:black;
width:100%;
overflow:hidden;
}
.slide-item{
width:100%;
overflow:hidden;
color:white;
}
.slide-img-wrapper{
position:relative;
}
.slide-img{
width:100%;
float:left;
position:relative;
}
.slide-description{
position:absolute;
float:left;
text-align:center;
width: 100%;
margin-top:16%;
}
.description-wrapper{
}
</style>
<div id="slide-wrapper">
<div class="slide-item">
<img class="slide-img" src="../villas.praivit/img/lead.jpg" />
<div class="slide-description">
<p class="location">SWITZERLAND, CERN</p>
<h1>VILLA PANORAMA</h1>
<p class="details">Local Time <b>4:10 am</b>, Temperature <b>32F°</b></p>
<div class="button-1">VIEW VILLA</div>
</div>
</div>
</div>
Is this what you are looking for?
<div id="slide-wrapper">
<div class="slide-item">
<img class="slide-img" src="http://dreamatico.com/data_images/mountain/mountain-1.jpg" />
<div class="slide-description">
<p class="location">SWITZERLAND, CERN</p>
<h1>VILLA PANORAMA</h1>
<p class="details">Local Time <b>4:10 am</b>, Temperature <b>32F°</b></p>
<div class="button-1">VIEW VILLA</div>
</div>
</div>
</div>
.slide-wrapper{
background-color:black;
width:100%;
overflow:hidden;
}
.slide-item{
width:100%;
overflow:hidden;
color:white;
}
.slide-img-wrapper{
position:relative;
}
.slide-img{
width:100%;
float:left;
position:relative;
}
.slide-description{
position:absolute;
float:left;
text-align:center;
width: 100%;
margin-top:16%;
}
.description-wrapper{
}
.button-1{
display: inline-block;
background-color: #b8955d;
color: #fff;
box-sizing: border-box;
padding: 10px;
}
.button-1 a{
color: #fff;
text-decoration: none;
}

z-index overlap different element parent versus different parent nth child

Overview:
There are 2 containers which called top-parent and middle-parent and middle-parent's child should overlap the top-parent.
HTML Code
<div id="top-parent" class="wuuh">
<div id="top-child">
I SHALL BLOCK YOU
</div>
</div>
<div id="middle-parent" class="wuuh">
<div id="middle-child">
<div id="middle-cousin">
<div id="middle-niece">
<div id="middle-priest">
NO!
</div>
</div>
</div>
</div>
</div>
CSS
#top-parent { z-index:102; }
#top-parent #top-child { background:#ea1248; width:400px; height: 200px; }
#middle-parent { z-index:101; background:#ccc; width:400px; height:200px; text-align:right; margin-top:10px; }
#middle-parent #middle-child { background:#333; height:100px; }
#middle-child #middle-priest { z-index:200; width:95%; color:#fff; background:#4679bd; padding-right:5%; }
.wuuh { position:absolute; }
This what it looks like if we position both parents into absolute.
What I want to happen is for the middle-priest should overlap the parent 1 while overlapping the parent 2's child except for middle-priest.
Desired Result: (photoshopped)
Is there any way to do this?
Here's a fiddle link CLICK ME
hey wesley i guess you are looking like this :- DEMO
I have removed the height & Z-indexing see mentioned below the css how i achieved your desired result....
CSS
#top-parent { }
#top-parent #top-child {
background:#ea1248;
width:400px;
height: 200px; }
#middle-parent {
z-index:101;
background:#ccc;
width:400px;
text-align:right;
}
#middle-parent #middle-child {
background:#333;
}
#middle-child #middle-priest {
z-index:200;
width:95%;
color:#fff;
background:#4679bd;
padding-right:5%; }
.wuuh {
position:absolute;
}

Any reliable, cross-browser way to distribute the remaining space in parent element between several DIVs?

Background
I am working on a browser-based UI that needs to fill the entire screen without any scrolling. The basic layout is like this:
What I want to achieve
The title div should has a fixed height (2em) and the rest 4 divs/panels should devide the remaining space of the screen according to percentages I set.
What I've tried
The best solution I've found is " CSS - How to force elements to 100% of remaining/available space of parent element without extending beyond it? ", which involves using a container div with position:absolute. This works across all browsers, but requires some additional DIVs to be created. Also, panel 2 can sometimes be forced to start on the next line due to inaccuracies in percentage widths.
My previous solution was based on CSS3 Flexbox, but the model is flawed as it does not resize child elements that have a percentage height after stretching the container boxes (at least Chrome doesn't). (The newer flex-* attributes are only implemented in Chrome and the standard is still changing.)
I have also tried the calc() function; however, it's not yet implemented in Chrome. Also, it requires hard-coding the height of the title element in two places, which I've been trying to avoid.
Edit:
What am I looking for
Just to be clear, I am not asking for a perfect/pure-CSS solution (as none seems to exist). If anyone can suggest any jQuery plug-in or open-source framework that can do this, it would be good enough for me.
In addition, I don't require any backwards compatibility with browser releases before 2012. (As long as the solution uses technology that is implemented in some browser and is going to be implemented by Firefox and Chrome in the near future, it's good enough for me.)
A little something thrown together:
http://jsfiddle.net/gDTGn/2/
Here is a pure CSS version:
http://jsfiddle.net/t0nyh0/KHzsg/63/
The trick to this technique is using position:absolute and using top, bottom, and height to create a fixed header with expanding panels. It is also really important to use:
box-sizing: border-box;
-moz-box-sizing: border-box;
to make the height and width calculations consistent across browsers. Tested and works in IE9, Firefox, and Chrome.
Pure CSS solution: http://jsfiddle.net/ehqcx/7/embedded/result/
This assumes you set width that don't sum up than more than 100%, the small gap at the right side can usually be fixed by using the same background or the background of the page. An alternative is to introduce some Javascript that sets the width of the last panel correctly, but that should be some trivial jQuery code... $("#panels .small:last").width(browser width - other small panels);
Should work correctly for the height, think away the jsFiddle header which takes away some height...
Edit:
Meh, seems the #title is bugging me... http://fiddle.jshell.net/ehqcx/7/show/light/
ECMAScript is the way to go, leaving my answer in place because of the other simplicity... :(
HTML:
<div id="content">
<div id="title">Title!</div>
<div id="panels">
<div id="panel0" class="small">0</div>
<div id="panel1" class="small">1</div>
<div id="panel2" class="small">2</div>
<div id="panel3" class="wide">3</div>
</div>
</div>​
CSS:
* { margin, padding: 0px; }
#content { background-color: black; }
#title { background-color: red; }
#panels { background-color: orange; }
#panel0 { background-color: purple; }
#panel1 { background-color: brown; }
#panel2 { background-color: orange; }
#panel3 { background-color: green; }
html, body, #content, #panels { max-height: 100%; height: 100%; max-width: 100%; width: 100%; }
#panels .small { float: left; }
#panels .wide { clear: both; }
#title { height : 2em; }
#panels .small { height: 75%; }
#panels .wide { height: 25%; }
#panel0, #panel1, #panel2 { width: 33.33%; }
It's possible using the new CSS3 flexbox model. It was basically designed to solve the problem you are facing.
Here is a simple example:
CSS:
*{margin:0 padding:0;}
html{height:100%;}
body{height:100%; display:box; box-orient:vertical;}
body > div {box-flex:1; border:1px solid black;}
.header {box-flex:0; height:4em;}
.content {
display: box;
box-orient: horizontal;
}
.content div {
box-flex: 1;
border:1px solid black;
}
HTML:
<html>
<body>
<div class="header">Title</div>
<div class="content">
<div>Panel 0</div>
<div>Panel 1</div>
<div>Panel 2</div>
</div>
<div>Panel 3</div>
</body>
</html>
It has good support in Chrome, Safari, and Firefox, with planned support in IE.
edit 2:
Tested it in
Chrome/Safari: some 1 or two pixel failure, because of percent calculations
FireFox: Perfect
IE9: Perfect
Opera: Can't have decimal places in percentage width values. This is bad
lte IE8: Does not support Array reduce function. One has to make one up (like from here: Array.reduce), Then it works at least in IE8
edit 1:
I added horizontal layout and window resize function
I've fiddled around a bit:
This is just a demonstration: To have a full fledged application you have to add the programming for the horizontal layout. But it's start
http://jsfiddle.net/HerrSerker/PmHtf/
Here is the code
HTML
<div class="full-stretch">
<div class="flex-layout flex-layout-vertical">
<div class="flex-layout-fixed" style="height:50px; text-align: center">
<div class="padding">Title</div>
</div>
<div class="flex-layout-consume flex-layout-consume-3" style="text-align: center">
<div class="flex-layout flex-layout-horizontal">
<div class="flex-layout-consume flex-layout-consume-1" style="text-align: center">
<div class="padding">Panel 0</div>
</div>
<div class="flex-layout-consume flex-layout-consume-1" style="text-align: center">
<div class="padding">Panel 1</div>
</div>
<div class="flex-layout-consume flex-layout-consume-1" style="text-align: center">
<div class="padding">Panel 2</div>
</div>
</div>
</div>
<div class="flex-layout-consume flex-layout-consume-1" style="text-align: center">
<div class="padding">Panel 3</div>
</div>
</div>
</div>​
CSS
.full-stretch {
position: absolute;
top: 2px;
right:2px;
bottom:2px;
left: 2px;
}
.padding {
position: absolute;
top: 2px;
right:2px;
bottom:2px;
left: 2px;
border: 1px solid darkGray;
background: lightBlue;
border-radius: 10px;
}
.flex-layout {
position: absolute;
top: 0;
right:0;
bottom:0;
left: 0;
overflow: hidden;
}
.flex-layout-consume {
height: 100%;
float:left;
overflow: hidden;
position: relative;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.flex-layout-vertical > .flex-layout-consume {
width: 100%;
}
.flex-layout-fixed {
height: 100%;
float:left;
overflow: hidden;
position: relative;
}
.flex-layout-vertical > .flex-layout-fixed {
width: 100%;
}
jQuery
(function($) {
var flex = function() {
$('.flex-layout').each(function() {
var fixed = $(this).children('.flex-layout-fixed');
if ($(this).hasClass('flex-layout-horizontal')) { // horizontal
var fixed_widths = $(this)
.children('.flex-layout-fixed')
.get()
.reduce(function(total, elem) {
return (total + $(elem).outerWidth())
},0)
;
var remain_width = ($(this).outerWidth() - fixed_widths)/$(this).outerWidth() * 100; // percent
var consumers = $(this)
.children('.flex-layout-consume')
.get()
;
var count_consumers = consumers
.reduce(function(total, elem) {
var cm = parseInt($(elem).attr('class').match(/flex-layout-consume-(\d+)/)[1]);
$(elem).data('consume_multiplicator', cm);
return total + cm;
},0)
;
var consumers_tic = (remain_width/count_consumers)
$(consumers).each(function() {
$(this).width(Math.round((consumers_tic * $(this).data('consume_multiplicator'))*1000)/1000+'%')
})
} else if ($(this).hasClass('flex-layout-vertical')) { // vertical
var fixed_heights = $(this)
.children('.flex-layout-fixed')
.get()
.reduce(function(total, elem) {
return (total + $(elem).outerHeight())
},0)
;
var remain_height = ($(this).outerHeight() - fixed_heights)/$(this).outerHeight() * 100; // percent
var consumers = $(this)
.children('.flex-layout-consume')
.get()
;
var count_consumers = consumers
.reduce(function(total, elem) {
var cm = parseInt($(elem).attr('class').match(/flex-layout-consume-(\d+)/)[1]);
$(elem).data('consume_multiplicator', cm);
return total + cm;
},0)
;
var consumers_tic = (remain_height/count_consumers)
$(consumers).each(function() {
$(this).height(Math.round((consumers_tic * $(this).data('consume_multiplicator'))*1000)/1000+'%')
})
}
})
};
$(function() {
flex()
$(self).resize(flex)
})
}(jQuery))
​
I might be missing something in your question, but see if this is what you are looking for. Pure CSS solution that works in all browsers down to IE7.
http://jsfiddle.net/nyHgM/1/
This is my suggestion (pure css)... Tested on IE7+, Chrome & FF http://jsfiddle.net/victmo/hKGUe/
HTML
<div id='header'></div>
<div id='col0'></div>
<div id='col1'></div>
<div id='col2'></div>
<div id='footer'></div>
CSS
div{
position:absolute;
}
#header{
top:0px;
left:0px;
right:0px;
height:3em;
}
#footer{
bottom:0px;
left:0px;
right:0px;
height:2em;
}
#col0,
#col1,
#col2{
top:3em; /* header height */
bottom:2em; /* footer height */
width:33.33%;
}
#col0{ left:0%; width:30%; } /* left = 0 */
#col1{ left:30%; width:40%; } /* left = 0 + 30 */
#col2{ left:70%; width:30%; } /* left = 30 + 40 */
/* Colors */
#header{ background:#bbb; }
#col0{ background:#ccc; }
#col1{ background:#ddd; }
#col2{ background:#eee; }
#footer{ background:#aaa; }
​

CSS - Change sub class background image on hover

I have the following code for a selectable tab:
<div class="tabOff">
<div class="lightCorner TL"></div><div class="lightCorner TR"></div>
<div class="cornerBoxInner">
<p>My Tab</p>
</div>
</div>
On the mouse over of tabOff I have the following CSS for changing the background colour:
.tabOff:hover
{
background:#AAA;
color:#CDEB8B;
}
Is there any way I can change my classes "lightCorner TL" and "lightCorner TR" to use a different background image, without JavaScript, when tabOff is hovered
This is the current CSS for lightCorner TL and TR:
.lightCorner
{
background:url(../Images/LightCorner.gif) no-repeat;
}
.TL
{
top:0px;
left:0px;
background-position:0px 0px;
}
.TR
{
top:0px;
right:0px;
background-position:-13px 0px;
}
I have never tried it, but I would start with:
.tabOff:hover .lightCorner {
}
Don´t know if that's correct usage though.
.tabOff:hover div.lightCorner {
background:url(../Images/LightCorner.gif) no-repeat;
}
.tabOff:hover div.TL {
top:0px;
left:0px;
background-position:0px 0px;
}
.tabOff:hover div.TR {
top:0px;
right:0px;
background-position:-13px 0px;
}

Resources