1

I'm trying to have a word like "hello" then when you click on it, it fades out and a new word fades in.

Is there any way to do this only using HTML and CSS? If not, I guess I'm going to have to learn javascript and/or jquery.

brettwhiteman
  • 4,210
  • 2
  • 29
  • 38
Rzy
  • 63
  • 1
  • 5
  • maybe on hover, with click is a suicide, you have to use checkbox below the text and set the css of the text to change if the checkbox is checked – Alberto Sinigaglia Dec 29 '19 at 22:33
  • You should learn js and possibly jQuery. They're good things to know for web development. [CSS fadein fadout onclick](https://stackoverflow.com/questions/30696642/css-fadein-fadeout-onclick). The checkbox hack that @AlbertoSinigaglia suggested is described in the 2019 answer here: [Can I have an onclick effect in CSS?](https://stackoverflow.com/questions/13630229/can-i-have-an-onclick-effect-in-css) – lurker Dec 29 '19 at 22:34
  • 1
    Hi and welcome! I think that you can have more help if you write a clearer question title, take a look to 'How to ask' if you want: https://stackoverflow.com/help/how-to-ask Anyway, as @lurker said I think you'll need to learn javascript. – artberri Dec 29 '19 at 22:36

3 Answers3

0

Using transition can solve your problem. This may help you.

By the way, learning JS could be a real asset for web dev ;)

Shinobi San
  • 143
  • 4
0

Do You mean something like this?

<span>Hello</span> <span>World</span>
<style>
  span { transition: opacity 1s ease; }
  span + span,
  span:first-child:active { opacity: 0; } 
  span:first-child:active + span { opacity: 1; }
</style>

Updated according to comment:

<span>Hello</span> <span>World</span>
<style>
  span { transition: opacity 1s ease; position: absolute; top: 0; left: 0 }
  span + span,
  span:first-child:active { opacity: 0; } 
  span:first-child:active + span { opacity: 1; }
  span + span { pointer-events: none; }
</style>

https://codepen.io/Patu/pen/RwNZOoM

Patryk Falba
  • 417
  • 3
  • 15
0

You can play with selector and html elements trying to achieve wat you want, this is what i made.

I used css pseudo-classes ( here you can find a list of pseudo-classes with relative explanation )

the pseudo-classes are used for detect special states of the elements, like the :hover ( i think the most used ) pseudo-class that detects when the mouse is hover an element.

in this case i used

  • :focus: That selects the element that has the focus ( like when you click a link, in the moment that you click the link it get the focus same thing when you click and input, so if you click the input it gets the focus.)

  • :visited: Is used for detect the visited links, unfortunatelly this selector has a special behaviour becouse it can be used for violate the user privacy, so you cannot correctly styles othe elements based on the links that has been visited ( that is what i tried to do here )

<a href="#" id="first">
hello
</a>



<a href="#" id="second">
hello
</a>

<style type="text/css">
#first{
  text-decoration: none;
  color: black;
  opacity: 1;
  outline: none;
  transition: opacity 1s;
}

#first:visited{
 opacity: 0;
}

#first:focus{
  opacity: 0;
}
#second:focus{
  outline:none
}
#second{
  opacity: 0;
  text-decoration: none;
  color: black;
  transition: opacity 1s;
}

#first:focus + #second{
  opacity: 1;
}

#first:visited ~ #second{
  opacity: 1;
}

</style>

Unfortunatelly as this when the user click another element the previous text comes back

Mat.C
  • 1,379
  • 2
  • 21
  • 43
  • I'm sorry if I sound like a complete idiot, but are you able to explain :visited and :focus. I've never seen them before. – Rzy Dec 29 '19 at 23:23
  • Yes, i'm going to add this at the answer – Mat.C Dec 29 '19 at 23:27
  • Sorry for all the questions, but one last thing. Is there anyway i could have a background and when you clicked the background it would do the effect? I tried using a wrapper but that did not work. – Rzy Dec 30 '19 at 00:01