I'm trying to make a button that changes two things with CSS. First, it will make text appear, and second it will make an image on top of the text disappear. I've tried a number of things, but I just can't get it to work. Any help would be greatly appreciated.
the Css is
p {
color:#591865;
text-align:center;
opacity:0;
}
p:target {
color:# a1a100;
opacity:1;
-webkit-transition: opacity 1s linear;
}
#image {
opacity: 1;
background-image:url(images/01.jpg);
height: 786px;
width:1024;
}
#image:target {
opacity:0;
}
and the html is
<nav>
One
Two
Three
Four
</nav>
<div id="image"><img src="images/01.jpg"/></div>
<div>
<p id="one"><img src="graphics/filler.png" width="281" height="128" onClick="javascript:playVideo1();"/></p>
<p id="two"><video src="images/01.m4v" poster="images/01.jpg" autoplay controls></video></p>
<p id="three">Number Three</p>
<p id="four">Number Four</p>
</div>
</div>
thanks for any input
This isn't possible, as it's currently written. To affect two disparate elements in this manner, would require JavaScript.
However, if you're able to reorder your HTML (and are willing to have support from only the more modern browsers), then it can be implemented with the following HTML:
<nav>
One
Two
Three
Four
</nav>
<div>
<p id="one">
<img src="graphics/filler.png" width="281" height="128" onclick="javascript:playVideo1();"/>
</p>
<p id="two">
<video src="images/01.m4v" poster="images/01.jpg" autoplay controls></video>
</p>
<p id="three">
Number Three
</p>
<p id="four">
Number Four
</p>
<div id="image">
<img src="images/01.jpg"/>
</div>
</div>
And the additional CSS selector:
p:target ~ #image {
opacity:0;
}
JS Fiddle demo.
If JavaScript's an option, then the following works:
var links = document.querySelectorAll('nav > a'),
image = document.getElementById('image');
for (var i=0,len=links.length; i<len; i++){
links[i].onclick = function(e){
image.style.opacity = '0';
};
}
JS Fiddle demo.
References:
CSS:
E ~ F, the general sibling combinator.
JavaScript:
getElementById().
querySelectorAll().
element.style.
You cant make two ids in a single href because a # href makes the browser focus on the object your linking to so if you point 2 objects the browser cant focus them both...
for example: imagine you put two hrefs to input text, both will be focused? when you type will it enter the same on both?
you can use javascript:
One
<script type="text/javascript">
function toggleVisible( textId, imageId ) {
$( "#" + textId ).show().
$( "#" + imageId ).hide();
}
</script>
of course you'll have to include jquery:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
I was trying to accomplish a similar thing, i.e. using css only to show\hide items using anchors, and the target selector...
I don't have the time to create an example specific to this question, but i'm sure you can figure it out... ;)
here's the fiddle...
(http://jsfiddle.net/pB72f/)
Something like this? (I am using jQuery here but the same idea can be easily applied to regular javascript)
One
<script type="text/javascript">
function toggleVisible( textId, imageId ) {
$( "#" + textId ).show(); //It should be semicolon
$( "#" + imageId ).hide();
}
</script>
You can't put 2 id's into the url, just as david Thomas has said, but there is a complex css solution which requires no javascript. In your proposal you have four a tags, therefore you have 4 individual states that the page can be in. wrap all 4 of your p tags, inside the parent div in 4 nested span tags and id each span tag with an id for each individual state
<nav>
One
Two
Three
Four
</nav>
<span id="oneimage1">
<span id="twoimage1">
<span id="oneimage2">
<span id="twoimage2">
<div id="image1"><img src="images/01.jpg"/></div>
<div id="image2"><img src="images/01.jpg"/></div>
<div>
<p id="one">Number One</p>
<p id="two">Number ~Two</p>
</div>
</span>
</span>
</span>
</span>
Css then would look something like this
span#oneimage1:target #image1 {Properties: values; go here }
span#oneimage1:target #one {Properties: values; go here }
span#oneimage2:target #image2 {Properties: values; go here }
span#oneimage2:target #one {Properties: values; go here }
span#twoimage1:target #image1 {Properties: values; go here }
span#twoimage1:target #two {Properties: values; go here }
span#twoimage2:target #image2 {Properties: values; go here }
span#twoimage2:target #two {Properties: values; go here }
This works because you are targeting a parent of all of the elements you wish to
control from the url. Because you need four id's and you can only place one id
on a single element, you therefore need to nest 4 span elements wrapped around,
the elements whose css you wish to control.
All the Best. TJE
Related
Is there any way to apply the same css var in a consecutive way? Or to get the current css property value and then appending a new one? I need this to apply multiple transforms on some nested elements.
Working example needs 2 separate var for parent and child
<style>
p {
transform: scale(var(--scale, 1)) scale(var(--parentScale, 1)) ;
}
</style>
<div style="--parentScale: 0.8">
<p style="--scale: -1;">Hello</p>
</div>
But I would like to write it like this (not working unfortunately inner --scale gets replaced by the parent --scale leaving the 'Hello' unsized)
<style>
p {
transform: scale(var(--scale, 1));
}
</style>
<div style="--scale: 0.8">
<p style="--scale: -1;">Hello</p>
</div>
Is there any workaround for this? If possibile without js
A --scale variable is defined in the div but it's never used on the div.
This snippet sets both div and p to have a transform each with its relevant --scale
div,
p {
transform: scale(var(--scale, 1));
}
<div style="--scale: 0.8">
<p style="--scale: -1;">Hello</p>
</div>
UPDATE: there is a clarification from the comments that it's each individual child, not the parent div, that needs to have that 0.8 scaling.
This snippet therefore introduces a second variable, --childscale, which is initially set to 1 and is combined with the --scale. Any div that does not set it is unaffected. For the div where you are looking for the flipping over -childscale is set to -1.
p {
transform: scale(calc(var(--scale) * var(--childscale)), 1);
}
<div style="--scale: 0.8; --childscale: 1;">
<p style="--childscale: -1;">Hello</p>
<p>Hello</p>
</div>
I'm in a situation where the number of elements showed is variable, and I need a strange solution which I'm not able to achieve, I even doubt if it's achievable only with css.
I need to select the last-child if my number of elements is odd, and the last 2 child if the number of elements is even.
I've been trying with nth-last-child, :not(:nth-last-child()), odd and even, but never got a good solution.
Anyone has any idea/advice about this issue a part of adding a class "odd" like on html tables?
Thanks a lot in advance!
Here is one way...
.wrap div:last-child,
.wrap div:nth-last-of-type(-n+2):not(:nth-child(even)) {
color: red;
}
<div class="wrap">
<div>Odd</div>
<div>Even</div>
<div>Odd</div>
<div>Even</div>
<div>Odd</div>
<div>Even</div>
</div>
<hr>
<div class="wrap">
<div>Odd</div>
<div>Even</div>
<div>Odd</div>
<div>Even</div>
<div>Odd</div>
</div>
You can use CSS like so:
li:last-child:nth-child(odd) {
/* Last child AND odd */
background: red;
}
li:nth-last-child(2):nth-child(odd),
li:last-child:nth-child(even) {
/* Before last child AND odd */
/* Last child AND even */
background: green;
}
Demo: https://jsfiddle.net/hw0ehrhy/
Absolutely it can be done, with pure CSS. See the complete code below (odd child, last child red; even childs, last 2 childs green)
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('#but1').click(function(){
var count = $('p').length;
if (count%2!=0) {$('div>p:last-child').css('background','red');}
else {$('div>p:last-child').css('background','green');alert(count);
$('div>p:nth-last-child(2)').css('background','green');
}
});
});
</script>
</head>
<body>
<button id=but1>Click</button>
<div>
<p>This is one. </p>
<p> This is two. </p>
<p> This is three. </p>
<p> This is four. </p>
<p> This is five. </p>
<p> This is six. </p>
</div>
</body>
</html>
Enjoy, the coding ;)
My HTML code looks as follows:
<div class="row">
<div class="col-md-6 col-sm-6 col-xs-6">
<section class="widget index">
<header>
<h4>
<i class="fa fa-bars"></i> Status word <small> </small>
</h4>
</header>
<div class="body">
- Output A: <div class="dash_data_A"></div>
- Output B: <div class="dash_data_B"></div>
- Output C: <div class="dash_data_C"></div>
The display on the website looks as follows:
- Output A:
false
- Output B:
true
- Output C:
false
First wish: The output value should be on the same line (avoid line break), like this:
- Output A: false
- Output B: true
- Output C: false
Second wish: The output value should change the font color of false (red) and true (green).
Do I have to implement that in the css-file? Or in the js? Or even both? What do you recommend?
By default, a div is a block level element, which means it takes up the entire width and causes elements to continue on the next line, under it...which is what you're seeing. So to fix that, you need to change the display type of the divs that need to be inline OR use a different tag that is inline by default, such as span.
.dash_data_A,
.dash_data_B,
.dash_data_C {
display: inline-block;
}
To handle the color part, I would apply a class depending on what the result is, like this:
<div class="dash_data_A false"></div>
<div class="dash_data_B true"></div>
<div class="dash_data_C false"></div>
And then the CSS:
.true {
color: green;}
.false {
color: red;}
Add the following CSS:
.dash_data_A, .dash_data_B, .dash_data_C, .title {
float: left;
}
And then wrap the "output"-stuff in a div as well.
A quick JSfiddle, it's not perfect, but it functions. You should make it perfect yourself :)
You can also simplify the code too.
<div class="dash_data false"></div>
<div class="dash_data true"></div>
<div class="dash_data false"></div>
.dash_data {
display: inline;
//float: left;
}
1. one option using only css is
CSS
.dash_data_A, .dash_data_B, .dash_data_C, .title {
float: left;
}
.false{
color:red;
}
.true{
color:Green;
}
DEMO FIDDLE
2. second option
if the true,false values are generated dynamically use jquery
FIDDLE JS DEMO
JQUERY
var option = "";
$(function () {
$('.option').each(function () {
option="";
option = $(this).html();
alert(option);
if (option.trim() == 'true') {
$(this).addClass('true');
} else {
$(this).addClass('false');
}
});
});
I want to style the following CSS classes; is there any short styling technique for this?
.test-0 { }
.test-2 { }
.test-3 { }
/* etc. */
I am looking for something like:
.test-%d% { }
I want to dynamically create many test-* classes with different numbers and common styles.
Update
here is my actual situation
<input type="button" value="click" class="button_class" />
<h1 class="ui-widget-header">Question - 1 </h1>
<div class="ui-widget-content">
<div id="form_container-0">
<div class="placeholder">Add your form fields here</div>
<div style="clear: both;"></div>
</div>
</div>
When user click the above button then same structure will clone and append to the end of the form
so the form will be as
<h1 class="ui-widget-header">Question - 1 </h1>
<div class="ui-widget-content">
<div id="form_container-0">
<div class="placeholder">Add your form fields here</div>
</div>
<div id="form_container-1">
<div class="placeholder">Add your form fields here</div>
</div>
</div>
the css class form_container-[%d] will be created dynamically by jquery.
so i want to add style to this class.
also it would be great if you share optimised code for cloning the structure with
different ID.
Please do let me know if you still have doubt.
thanks
You can use an attribute selector.
div[class*='test-'] {...}
I think #Ed W have the right solution BUT I have an extra idea while is not straight forward is shorter than what you have. And will help to make different testing that is waht I think you want... fiddel http://jsfiddle.net/ncubica/2sj9W/
css
.test-1,
.test-2,
.test-3,
.test-4,
.test-5{
color:#F60;
display:block;
}
.test-5{
color:blue
}
html
<span class="test-1">One</span>
<span class="test-2">Two</span>
<span class="test-3">Three</span>
<span class="test-4">Four</span>
<span class="test-5">Five</span>
span five will be in blue color... so you can override the class you want to test and play with it.
Also you can use selectors like
HTML
<div>
<span>I'm pink</span>
<span>I'm pink</span>
<span>I'm pink</span>
<span>I'm pink</span>
<span class="test-1">I'm red</span>
</div>
CSS
div > span{
color:pink;
display:block;
}
div > span.test-1{
color:red;
}
and the last span will be red. I hope this help.
My two cents...
I've got a page with a repeater and a bunch of documents that should be hidden to start and then shown with a plus sign next to each.
My question is - do I have to assign unique ID to each document DIV to make it be able to toggle hidden and shown?
What's the most code-efficient way to handle this?
Here is a quick example:
http://jsfiddle.net/aaamU/
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title></title>
</head>
<body>
<div id="repeater">
<div class="document">
<div class="title">Document 1</div>
<div class="button">+</div>
</div>
<div class="document">
<div class="title">Document 2</div>
<div class="button">+</div>
</div>
<div class="document">
<div class="title">Document 3</div>
<div class="button">+</div>
</div>
</div>
</body>
</html>
CSS
#repeater .document
{
height: 20px;
border: 1px solid black;
width: 200px;
padding: 10px;
}
.document .title
{
float:left;
}
.document .button
{
float:right;
}
JS
$(document).ready(function(){
$(".title").hide();
$(".button a").click(function(event){
$(this).parents(".document").children(".title").toggle();
event.preventDefault;
});
});
Here is a Fork with the sliding version:
http://jsfiddle.net/W5QkY/1/
You don't have to assign an ID, you can use their position in the document to identify the correct element.
For example, you have something like this:
<div id="documents">
<div> ... </div>
<div> ... </div>
<div> ... </div>
</div>
You can use jquery like so to trigger individual elements:
$('#documents > div').eq(0).show();
Where the number passed to the eq() method will return the div at that index.
no you dont have to assign them all a different Id. There are many ways to select multiple dom elements with one selector expression
you have a few options
1) you can assign them all the same class and then do $('.className').show()/.hide()
2) you can select them by a css selector related to the page's layout i.e $('#mainContent img').hide() will hide all images inside of a container (prob a div) with id mainContent
You could easily avoid unique id:s on the html tags by using jQuery's traversing capabilities:
<div class="frame">
[Document title] +
<div>[document contents, links or whatever go here]</div>
</div>
And the jQuery magic:
$(function() {
$('.frame a').click(function() {
var $t = $(this);
if ($t.html()=='+')
{
$t.html('-').next('div').show();
} else {
$t.html('+').next('div').hide();
}
});
});
You could obviously switch the .show()/.hide() calls to some animation of your choice.