Change the DIV background-color permanently - html

How can I change the background color of a div tag permanently once it is clicked.
Tried the following code :
div {
min-height: 50px;
background-color: yellow;
border-radius: 4px 4px 0 0;
padding: 10px;
display: flex;
align-items: center;
&:active {
background-color: red;
}
}
But with this the background color is restored to previous one after the click is released.
Is there a way to do this with SCSS and html alone?
<div class="div">
<p>Hello</p>
</div>

If you want pure CSS/SCSS approach, you can do it using transition delay.
First of all you set transition of 0s, and a delay of some higher value (like 100000s). Now when you active the element, change the transition to 0s without delay so that it changes it's color immediately, now since there is a huge delay it becomes almost permanent. (but after the delay time that you set is over, it would become back to it's initial state)
div {
min-height: 50px;
background-color: yellow;
border-radius: 4px 4px 0 0;
padding: 10px;
display: flex;
align-items: center;
transition: background-color 0s 100000s;
&:active {
background-color: red;
transition: background-color 0s;
}
}
div {
min-height: 50px;
background-color: yellow;
border-radius: 4px 4px 0 0;
padding: 10px;
display: flex;
align-items: center;
transition: background-color 0s 100000s;
}
div:active {
background-color: red;
transition: background-color 0s;
}
<div></div>

HTML
<div class="div" id="myDiv">
<p>Hello</p>
</div>
CSS
div {
min-height: 50px;
background-color: yellow;
border-radius: 4px 4px 0 0;
padding: 10px;
display: flex;
align-items: center;
}
JS
let myDiv=document.getElementById("myDiv");
myDiv.addEventListener('click',()=>{
myDiv.style.background='red';
})

Related

Burger Toggle only One Bar Changes on Hover

I'm currently working on a navbar for what's going to be my website, but I'm having problems with the toggle burger icon.
I would like for to have all three bars of the toggle button to change at the same time while hovering over it, but only of them changes at a time when I hover over it.
Relevant Code
HTML
<div class='toggle-button' onClick={() => setOpen(!open)}>
<div class='bar' />
<div class='bar' />
<div class='bar' />
</div>
CSS
.toggle-button {
border: 1px solid grey;
border-radius: 5px;
cursor: pointer;
display: inline-block;
height: fit-content;
transition: all 0.5s ease;
}
.toggle-button:hover {
background-color: black;
}
.toggle-button .bar {
background-color: grey;
border-radius: 2.5px;
margin: 4px;
height: 4px;
width: 25px;
}
.toggle-button .bar:hover {
background-color: black;
}
That's because the selector you used is used for inheritance.
That, and your :hover should be on the .toggle-button class, otherwise the style will change when you hover on the bar and not the full div.
Original:
.toggle-button .bar:hover {
background-color: black;
}
What you need:
.toggle-button:hover > .bar {
background-color: gray;
}
Also, I changed the color to gray so the bars won't blend with the rest of the button.
If you need more information I'm happy to explain.
If I understand correctly what you are trying to accomplish is to change to color of the 3 lines .bar on hover.
You will need to do it as follows:
.toggle-button {
border: 1px solid grey;
border-radius: 5px;
cursor: pointer;
display: inline-block;
height: fit-content;
transition: all 0.5s ease;
}
.toggle-button .bar {
background-color: grey;
border-radius: 2.5px;
margin: 4px;
height: 4px;
width: 25px;
}
.toggle-button:hover .bar {
background-color: black;
}

Expands background on a circle shape

I'm trying to do a circle that expands-change color from the center, and i want it to expand it with border-radius: 50%, you'll understand what i'm talking about if you watch the example i made
Checkout the sample i made for better understanding
Thanks for any help
You could run a transition over an inset box-shadow, like so
div {
width: 300px;
height: 300px;
display: flex;
color: #FFF;
justify-content: center;
align-items: center;
border-radius: 50%;
background: #03BF60;
cursor: pointer;
transition: box-shadow .75s 0s, color .5s 0s;
box-shadow: inset 0 0 0 0 #DCEDC8;
}
div p {
color: inherit;
text-align: center;
}
div:hover {
color: #444;
box-shadow: inset 0 0 0 150px #DCEDC8;
}
<div>
<p>
Responsive design. I get this certificate by
learning HTML, CSS, web design, media query plus
animations using keyframes, declaring variables
on css and a lot of CSS components.
</p>
</div>
You could to use Keyframes...
<div class="shape">
<div class="to-animate animate"></div>
</div>
.shape {
border-radius: 50%;
width: 200px;
height: 200px;
background: red;
display: flex;
align-items: center;
justify-content: center;
overflow: hidden;
}
.to-animate {
width: 0;
height: 0;
background: blue;
}
.animate {
animation: my-animation 1s forwards;
}
#keyframes my-animation {
to {
width:200px;
height: 200px;
}
}
https://jsfiddle.net/hernangiraldo89/ba3ne675/
Keyframes are a powerful tool, here its documentation:
https://developer.mozilla.org/en-US/docs/Web/CSS/#keyframes

Css Transition is not working as intended

I could really use some help in my css code.
I'm trying to make my <h1> change color and shape using the transition property.
I want the shape and color to change slowly while I hover over the headline,
but currently only the color is affected, and the shape changes independently.
my code is as follows :
html :
<h1 class="box">Abcdefg</h1>
css :
.box {
background: #2db34a;
margin: 1px auto;
transition: background 1s linear;
border-radius: 0.3%;
color: white;
cursor: pointer;
height: 95px;
line-height: 95px;
text-align: center;
width: 400px;
}
.box:hover {
background: #ff7b29;
border-radius: 50%;
}
thanks.
You just need to add border-radius to your transition
.box {
background: #2db34a;
margin: 1px auto;
transition: background 1s linear, border-radius 1s linear;
border-radius: 0.3%;
color: white;
cursor: pointer;
height: 95px;
line-height: 95px;
text-align: center;
width: 400px;
}
.box:hover {
background: #ff7b29;
border-radius: 50%;
}
<h1 class="box">Abcdefg</h1>
You have the next line of code:
transition: background 1s linear;
The transition only works on the background right now. If you change background to all the transition will work on both background and border-radius, like this:
transition: all 1s linear;
Use all in the transition setting to affect both the border-radius and the background-color:
.box {
background: #2db34a;
margin: 1px auto;
transition: all 1s linear;
border-radius: 0.3%;
color: white;
cursor: pointer;
height: 95px;
line-height: 95px;
text-align: center;
width: 400px;
}
.box:hover {
background: #ff7b29;
border-radius: 50%;
}
<h1 class="box">Abcdefg</h1>
Transitions work only on properties that have numbers. That being said, the question is it should work for the border-radius as well. But the problem here is the browser is unable to find the initial state of the property. Just add border-radius: 0% and it should work.
HTML code:
<p> the code is :</p>
<h1 class="sheet">Abcdefg</h1>
CSS Code:
css code :
.sheet {
background: blue;
margin: 1px auto;
transition: background 5s linear , border-radius 5s ease-in-out ;
color: white;
cursor: pointer;
height: 95px;
line-height: 95px;
text-align: center;
width: 400px;
}
.sheet:hover {
background: red;
color:grey;
border-radius: 40%;
}

How can i make css animation

i have 2 images, and one background:
(red and brown are images)
(white is background)
When i hover on first or second image it will slide to left/right side. Like a doors in shop. Slide both in same time (no only one).
Can someone help me? Thanks.
I tried the accepted answer but it's a bit buggy (in firefox more so) if you put your cursor over the center and it bounces the sides back and forth and doesn't open. Personally I'd do something more like this.
CODEPEN TO PLAY WITH
#stage {
width: 20rem;
height: 15rem;
background: green;
border: black 1px solid;
margin: 0 auto;
position: relative;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
}
span {
color: white;
font-size: 150%;
}
#left-curtain, #right-curtain {
position: absolute;
top: 0;
bottom: 0;
width: 10rem;
-webkit-transition: all 1s ease;
transition: all 1s ease;
display: flex;
align-items: center;
justify-content: center;
}
#left-curtain {
background: red;
left: 0;
}
#right-curtain {
background: blue;
right: 0;
}
#stage:hover #left-curtain {
left: -10rem;
}
#stage:hover #right-curtain {
right: -10rem;
}
<div id="stage">
<span>PEEK - A - BOO!</span>
<div id="left-curtain">Mouse</div>
<div id="right-curtain">Over</div>
</div>
Here is my quick and dirty solution. It's not perfect and perhaps someone else can fine tune it - but it works. Javascript/jQuery might allow you to come up with a more complete solution.
Hope this helps.
.l,
.r {
-webkit-transition: width .35s ease-in-out;
transition: width .35s ease-in-out;
width: 200px;
height: 100px;
}
.container {
width: 400px;
}
.l {
background-color: red;
float: left;
}
.r {
background-color: brown;
float: right;
}
.container:hover div {
width: 150px;
}
<div class='container'>
<div class='l'>
</div>
<div class='r'>
</div>
</div>

Close CSS dropdown menu onclick

I'm very new to CSS and HTML combination. I'm trying to make use of following code for dropdown menu. But when mouse is moved away from dropdown menu, it gets closed. I would like to close it onclick outside the dropdown menu. Can anyone suggest me a fix in CSS to achieve this? JSFiddle for me code is at following location Fiddle link. Your help will be much appreciated. HTML looks like this.
<div id="main">
<div class="wrapper">
<div class="content">
<content>
<div>
<ul>
<li>Lorem ipsum dolor</li>
<li>Consectetur adipisicing</li>
<li>Reprehenderit</li>
<li>Commodo consequat</li>
</ul>
</div>
</content>
</div>
<div class="parent">Drop Down Parent 1</div>
</div>
And CSS looks like this
#main {
margin: 30px 0 50px 0;
font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
}
#main .wrapper {
display: inline-block;
width: 180px;
margin: 0 10px 0 0;
height: 20px;
position: relative;
}
#main .parent {
height: 100%;
width: 100%;
display: block;
cursor: pointer;
line-height: 30px;
height: 30px;
border-radius: 5px;
background: #F9F9F9;
border: 1px solid #AAA;
border-bottom: 1px solid #777;
color: #282D31;
font-weight: bold;
z-index: 2;
position: relative;
-webkit-transition: border-radius .1s linear, background .1s linear, z-index 0s linear;
-webkit-transition-delay: .8s;
text-align: center;
}
#main .parent:hover, #main .content:hover ~ .parent {
background: #fff;
-webkit-transition-delay: 0s, 0s, 0s;
}
#main .content:hover ~ .parent {
border-bottom-left-radius: 0;
border-bottom-right-radius: 0;
z-index: 2;
}
#main .content {
position: absolute;
top: 0;
display: active;
z-index: 2;
height: 0;
width: 180px;
padding-top: 30px;
-webkit-transition: height .5s ease;
-webkit-transition-delay: .4s;
border: 1px solid #777;
border-radius: 5px;
box-shadow: 0 1px 2px rgba(0, 0, 0, .4);
}
#main .wrapper:active .content {
height: 123px;
z-index: 3;
-webkit-transition-delay: 0s;
}
#main .content div {
background: #fff;
margin: 0;
padding: 0;
overflow: hidden;
height: 100%;
border-bottom-left-radius: 5px;
border-bottom-right-radius: 5px;
}
#main .content:hover {
height: 123px;
z-index: 3;
-webkit-transition-delay: 0s;
}
I'm looking at something like this and will set new content to content tag. What I'm really looking at is following
1) Click on dropdown menu. Show the dropdown.
2) When hovered on it keep displaying. When hover outside dropdown keep showing it.
3) When clicked somewhere outside dropdown then close the dropdown.
I know I'm using hover selector so my behavior is like that. But I want to covert it to above behavior and I don't know how to do it.
<div id="main">
<div class="wrapper">
<div class="content">
<content>
</content>
</div>
<div class="parent">Drop Down Parent 1</div>
Your HTML is wrong. If you see, you have wrapped <li> inside the <a> tag. That's a basic issue you have. And there are other issues.
Change your existing HTML:
<a><li>...</li></a>
To the right form:
<li><a>...</a></li>
And then the click works. An <a> cannot contain an <li> inside it. So the click doesn't happen.