I wanna do a simple trick with only using html and css. I only changed to replace the buttons but only first one worked. And Do I have to use Js İf I want to do that like second one ? or Is there a solution without Js ? and I want to know what cause it. İs it about HTML render ? and Can I implement another way to fix that( buttons are down of container ) with small trick except using Js…
First One (it’s working…)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equi="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
body {
height: 100vh;
}
.container {
margin: 50px auto;
width: 500px;
height: 500px;
border: 1px solid black;
}
.content {
width: 100px;
height: 150px;
background-color: aqua;
animation: animasyon 1.5s linear alternate infinite;
}
.start:focus~.container .content {
animation-play-state: running;
}
.stop:focus~.container .content {
animation-play-state: paused;
}
@keyframes animasyon {
from {
width: 100px;
}
to {
width: 500px;
}
}
</style>
</head>
<body>
<button class="start">Start</button>
<button class="stop">Stop</button>
<div class="container">
<div class="content"></div>
</div>
</body>
</html>
Second One (it’s not working…)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
body {
height: 100vh;
}
.container {
margin: 50px auto;
width: 500px;
height: 500px;
border: 1px solid black;
}
.content {
width: 100px;
height: 150px;
background-color: aqua;
animation: animasyon 1.5s linear alternate infinite;
}
.start:focus~.container .content {
animation-play-state: running;
}
.stop:focus~.container .content {
animation-play-state: paused;
}
@keyframes animasyon {
from {
width: 100px;
}
to {
width: 500px;
}
}
</style>
</head>
<body>
<div class="container">
<div class="content"></div>
</div>
<button class="start">Start</button>
<button class="stop">Stop</button>
</body>
</html>
4
Answers
The general sibling selector does not select a sibling that is BEFORE the element, only after. Your buttons are AFTER the sibling in the one that does not work.
If you want to do with it after, you would need to use a parent and use
:has()
The sibling selector does not select a sibling that is BEFORE the element, only after. So need to Keep the order in HTML, but can change the order using CSS of flex.
As mentionned by everyone, your issue is about the structure.
You may use labels and hidden radio buttons instead.
Keep the radios ahead in the flow, and put the link labels anywhere you need within the document, they will trigger the radios.
read about the for attribute to understand how it works:
possible example hit start to run animation and stop for pause.
Placing the buttons after
.container
will invalidate the general sibling combinator (~
). How about creating an envelope and place the buttons within that? That way the.container
will still be a sibling of the buttons.Something like (adapted a bit for better display in the snippet run box):