I would like to have a span within the .btn rotate 180 deg when I click the .btn. However, the span also moved to the right and went down after it was rotated. Could anyone help me explain why it moved like that.
I tried to transform: translate(-21px, 1px) after rotation then the span will moved the right place but I believe there will be another better way to fix it. I also tried transform-origin: 50% 50% but it doesn't work either.
$('.btn').on('click', function(){
$(this).toggleClass('open');
})
#import "https://fonts.googleapis.com/css?family=Raleway";
body {
background-color: #ddd;
font-family: "Raleway", "Microsoft JhengHei", Arial, sans-serif;
color: #7a7b7c;
}
.btn {
width: 80px;
height: 80px;
background-color: #6F7272;
border-radius: 50%;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
box-shadow: 0 0 2rem #babbbc;
position: absolute;
}
.btn span {
width: 40px;
height: 2px;
background-color: #ffffff;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
position: absolute;
}
.btn::before, .btn::after {
content: "";
width: 40px;
height: 2px;
background-color: #ffffff;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
position: absolute;
}
.btn::before {
margin-top: -7px;
}
.btn::after {
margin-top: 7px;
}
.open {
transition: 0.3s ease-in-out;
background-color: #6F7272;
}
.open span {
transition: .3s ease-in-out;
transform: rotate(180deg);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="btn">
<span></span>
</div>
If you remove transform: translate(-50%, -50%); and just keep left:26% and top: 49% It works fine.
To your question why it is happening. Because you have moved it using left and top position and again you are trying to neutralise it by translate. which is moving the center of animation to different position.
$('.btn').on('click', function(){
$(this).toggleClass('open');
})
#import "https://fonts.googleapis.com/css?family=Raleway";
body {
background-color: #ddd;
font-family: "Raleway", "Microsoft JhengHei", Arial, sans-serif;
color: #7a7b7c;
}
.btn {
width: 80px;
height: 80px;
background-color: #6F7272;
border-radius: 50%;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
box-shadow: 0 0 2rem #babbbc;
position: absolute;
}
.btn span {
width: 40px;
height: 2px;
background-color: #ffffff;
top: 49%;
left: 26%;
// transform: translate(-50%, -50%);
position: absolute;
}
.btn::before, .btn::after {
content: "";
width: 40px;
height: 2px;
background-color: #ffffff;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
position: absolute;
}
.btn::before {
margin-top: -7px;
}
.btn::after {
margin-top: 7px;
}
.open {
transition: 0.3s ease-in-out;
background-color: #6F7272;
}
.open span {
transition: .3s ease-in-out;
transform: rotate(180deg);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="btn">
<span></span>
</div>
Your approach isn't working as expected, because after click event, you are transforming the X & Y values of your span using translate property and then rotating it. This causes it to shift it's location when transformed.
You used position: absolute to place your span in middle. this can be also be achieved via other CSS properties like display: table or display :flex
Check out the below suggestion - most of the code is all yours with just slight change.
$('.btn').on('click', function() {
$(this).toggleClass('open');
})
#import "https://fonts.googleapis.com/css?family=Raleway";
body {
background-color: #ddd;
font-family: "Raleway", "Microsoft JhengHei", Arial, sans-serif;
color: #7a7b7c;
}
.btn {
width: 80px;
height: 80px;
background-color: #6F7272;
border-radius: 50%;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
box-shadow: 0 0 2rem #babbbc;
position: absolute;
display: flex;
justify-content: center;
align-items: center;
}
.btn span {
width: 40px;
height: 2px;
background-color: #ffffff;
/* top: 50%;
left: 50%;
transform: translate(-50%, -50%);
position: absolute; */
}
.btn::before,
.btn::after {
content: "";
width: 40px;
height: 2px;
background-color: #ffffff;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
position: absolute;
}
.btn::before {
margin-top: -7px;
}
.btn::after {
margin-top: 7px;
}
.open {
transition: 0.3s ease-in-out;
background-color: #6F7272;
}
.open span {
transition: .3s ease-in-out;
transform: rotate(180deg);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="btn">
<span></span>
</div>
Update this style
.open span {
transition: .3s ease-in-out;
transform: rotate(180deg) translate(20px, 1px );
}
$('.btn').on('click', function(){
$(this).toggleClass('open');
})
#import "https://fonts.googleapis.com/css?family=Raleway";
body {
background-color: #ddd;
font-family: "Raleway", "Microsoft JhengHei", Arial, sans-serif;
color: #7a7b7c;
}
.btn {
width: 80px;
height: 80px;
background-color: #6F7272;
border-radius: 50%;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
box-shadow: 0 0 2rem #babbbc;
position: absolute;
}
.btn span {
width: 40px;
height: 2px;
background-color: #ffffff;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
position: absolute;
}
.btn::before, .btn::after {
content: "";
width: 40px;
height: 2px;
background-color: #ffffff;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
position: absolute;
}
.btn::before {
margin-top: -7px;
}
.btn::after {
margin-top: 7px;
}
.open {
transition: 0.3s ease-in-out;
background-color: #6F7272;
}
.open span {
transition: .3s ease-in-out;
transform: rotate(180deg) translate(20px, 1px );
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="btn">
<span></span>
</div>
Check if this is what you are trying to achieve.
Here, I have added a single line to the css transform-origin: 10px;. Everything else is the same. The change in JS is to remove the add class as soon as the animation carried out. Otherwise, you will need to click double times on the button to see the animation again.
$('.btn').on('click', function(){
$(this).addClass('open');
setTimeout(function() {
$('.btn').removeClass('open');
}, 300);
})
body {
background-color: #ddd;
font-family: "Raleway", "Microsoft JhengHei", Arial, sans-serif;
color: #7a7b7c;
}
.btn {
width: 80px;
height: 80px;
background-color: #6F7272;
border-radius: 50%;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
box-shadow: 0 0 2rem #babbbc;
position: absolute;
}
.btn span {
width: 40px;
height: 2px;
background-color: #ffffff;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
position: absolute;
}
.btn::before, .btn::after {
content: "";
width: 40px;
height: 2px;
background-color: #ffffff;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
position: absolute;
}
.btn::before {
margin-top: -7px;
}
.btn::after {
margin-top: 7px;
}
.open {
transition: 0.3s ease-in-out;
background-color: #6F7272;
}
.open span {
transition: .3s ease-in-out;
transform: rotate(180deg);
transform-origin: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="btn">
<span></span>
</div>
Related
I am quite new to html and css, and have started using transitions. I watched Kevin Powell's video on it, and all his demonstrations had the transition applied when transitioning out (for instance when he stopped hovering on the element). However in my example, the transition works when the logo moves to the left, but instantly teleports back. Why is this, and what have I done wrong?
body {
background: #121212;
color: #FFFFFF;
font-family: Helvetica, Arial, sans-serif;
}
nav {
background: #212121;
width: 100%;
height: 80px;
position: absolute;
top: 0;
left: 0;
}
nav:hover .logobg {
left: 0;
transform: translateX(0);
transition-duration: 1000ms;
transition-timing-function: ease-in-out;
}
.navtext {
left: 50%;
transform: translateX(-50%);
position: absolute;
font-size: 30px;
}
.logo {
top: 0%;
font-size: 65px;
position: absolute;
left: 50%;
transform: translate(-50%, -50%);
top: 50%;
text-decoration: none;
color: #FFF;
}
.logobg {
width: 150px;
height: 100%;
position: absolute;
left: 50%;
transform: translateX(-50%);
}
.navelements {
position: absolute;
left: 50%;
transform: translateX(-50%);
background: red;
height: 100%;
}
.navelements:hover {
background: #303030;
transition-duration: 300ms;
}
.navbarline {
background: linear-gradient(90deg, rgba(221, 16, 247, 1) 0%, rgba(0, 212, 255, 1) 100%);
width: 100%;
height: 4px;
position: absolute;
top: 80px;
left: 0;
}
<nav>
<a href="index.html">
<div class="logobg">
<div class="logo">
AI
</div>
</div>
</a>
<a href="blank.html">
<div class="">
</div>
</a>
</nav>
<div class="navbarline"></div>
you should add this rule:
nav .logobg {
left: 50%;
transform: translateX(0);
transition-duration: 1000ms;
transition-timing-function: ease-in-out;
}
So when mouse leave the element it should apply the transition you defined that overrides the :hover rule.
Lety has a solid answer but I thought I would elaborate on my comment earlier. So I had the right idea just wrong placement. I moved .logobg up above nav:hover .logobg and added your duration and timing to .logobg. It now transitions smoothly back and forth.
body {
background: #121212;
color: #FFFFFF;
font-family: Helvetica, Arial, sans-serif;
}
nav {
background: #212121;
width: 100%;
height: 80px;
position: absolute;
top: 0;
left: 0;
}
.logobg {
width: 150px;
height: 100%;
position: absolute;
left: 50%;
transform: translateX(-50%);
transition-duration: 1000ms;
transition-timing-function: ease-in-out;
}
nav:hover .logobg {
left: 0;
transform: translateX(0);
}
.navtext {
left: 50%;
transform: translateX(-50%);
position: absolute;
font-size: 30px;
}
.logo {
top: 0%;
font-size: 65px;
position: absolute;
left: 50%;
transform: translate(-50%, -50%);
top: 50%;
text-decoration: none;
color: #FFF;
}
.navelements {
position: absolute;
left: 50%;
transform: translateX(-50%);
background: red;
height: 100%;
}
.navelements:hover {
background: #303030;
transition-duration: 300ms;
}
.navbarline {
background: linear-gradient(90deg, rgba(221, 16, 247, 1) 0%, rgba(0, 212, 255, 1) 100%);
width: 100%;
height: 4px;
position: absolute;
top: 80px;
left: 0;
}
<nav>
<a href="index.html">
<div class="logobg">
<div class="logo">
AI
</div>
</div>
</a>
<a href="blank.html">
<div class="">
</div>
</a>
</nav>
<div class="navbarline"></div>
CSS transitions reverse once the "hover" event stops. Here's a way to do it with javascript.
I'm adding an "id" with the styling that you require. An id's css will always override that of a class, which is why this works.
The javascript:
const nav = document.querySelector("nav");
const logobg = document.querySelector(".logobg");
nav.addEventListener("mouseover", () => {
logobg.setAttribute("id", "move");
})
The css:
body{
background: #121212;
color: #FFFFFF;
font-family: Helvetica, Arial, sans-serif;
}
nav{
background: #212121;
width: 100%;
height: 80px;
position: absolute;
top: 0;
left: 0;
}
#move {
left: 0;
transform: translateX(0);
transition-duration: 1000ms;
transition-timing-function: ease-in-out;
}
.navtext{
left: 50%;
transform: translateX(-50%);
position: absolute;
font-size: 30px;
}
.logo{
top: 0%;
font-size: 65px;
position: absolute;
left: 50%;
transform: translate(-50%,-50%);
top: 50%;
text-decoration: none;
color: #FFF;
}
.logobg{
width: 150px;
height: 100%;
position: absolute;
left: 50%;
transform: translateX(-50%);
}
.navelements{
position: absolute;
left: 50%;
transform: translateX(-50%);
background: red;
height: 100%;
}
.navelements:hover{
background: #303030;
transition-duration: 300ms;
}
.navbarline {
background: linear-gradient(90deg, rgba(221,16,247,1) 0%, rgba(0,212,255,1) 100%);
width: 100%;
height: 4px;
position: absolute;
top: 80px;
left: 0;
}
I was searching for a way to make some kind of toggle button and I found one that is exactly how I wanted but this example is using the toggle on an input field and I want to use it on a div. I tried several things changing the CSS classes but for some reason I can't get it working properly.
This is the example CSS I am using:
.switch {
position: relative;
display: inline-block;
width: 90px;
height: 34px;
}
.switch input {display:none;}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ca2222;
-webkit-transition: .4s;
transition: .4s;
border-radius: 34px;
}
.slider:before {
position: absolute;
content: "";
height: 26px;
width: 26px;
left: 4px;
bottom: 4px;
background-color: white;
-webkit-transition: .4s;
transition: .4s;
border-radius: 50%;
}
input:checked + .slider {
background-color: #2ab934;
}
input:focus + .slider {
box-shadow: 0 0 1px #2196F3;
}
input:checked + .slider:before {
-webkit-transform: translateX(26px);
-ms-transform: translateX(26px);
transform: translateX(55px);
}
/*------ ADDED CSS ---------*/
.slider:after
{
content:'OFF';
color: white;
display: block;
position: absolute;
transform: translate(-50%,-50%);
top: 50%;
left: 50%;
font-size: 10px;
font-family: Verdana, sans-serif;
}
input:checked + .slider:after
{
content:'ON';
}
/*--------- END --------*/
This is the example HTML:
<label class="switch"><input type="checkbox" id="togBtn"><div class="slider round"></div></label>
When I use it like this everything is working fine but my HTML and the div I want to toggle is like this:
<div class="facetwp-facet facetwp-facet-aanbieding facetwp-type-checkboxes" data-name="aanbieding" data-type="checkboxes">
<div class="facetwp-checkbox" data-value="ja">Ja</div>
</div>
I made changes to the example CSS to make it work on the div I want to toggle but I'm a bit lost here on how to get this working.
Maybe it is very simple but I'm lost so if somebody could help me out a little bit that would be great!
I added checked class to slider div instead of :checked the input checkbox.
So some changes in CSS code.
.slider.checked{...} instead of input:checked + .slider {...}
.slider:focus{...} instead of input:focus + .slider {...}
.slider.checked:before {...} instead of input:checked + .slider:before {...}
Try the Demo in Jquery
Using Jquery for clicking the slider div to toggle class checked.
$(".slider").click(function(){
$(this).toggleClass("checked");
});
.switch {
position: relative;
display: inline-block;
width: 90px;
height: 34px;
}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ca2222;
-webkit-transition: .4s;
transition: .4s;
border-radius: 34px;
}
.slider:before {
position: absolute;
content: "";
height: 26px;
width: 26px;
left: 4px;
bottom: 4px;
background-color: white;
-webkit-transition: .4s;
transition: .4s;
border-radius: 50%;
}
.slider.checked{
background-color: #2ab934;
}
.slider:focus{
box-shadow: 0 0 1px #2196F3;
}
.slider.checked:before {
-webkit-transform: translateX(26px);
-ms-transform: translateX(26px);
transform: translateX(55px);
}
/*------ ADDED CSS ---------*/
.slider:after
{
content:'OFF';
color: white;
display: block;
position: absolute;
transform: translate(-50%,-50%);
top: 50%;
left: 50%;
font-size: 10px;
font-family: Verdana, sans-serif;
}
.slider.checked:after
{
content:'ON';
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="switch">
<div class="slider round"></div>
</div>
Try the Demo in Javascript
Using Javascript for clicking the slider div to toggle class checked.
function sliding(obj){
obj.classList.toggle("checked");
}
.switch {
position: relative;
display: inline-block;
width: 90px;
height: 34px;
}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ca2222;
-webkit-transition: .4s;
transition: .4s;
border-radius: 34px;
}
.slider:before {
position: absolute;
content: "";
height: 26px;
width: 26px;
left: 4px;
bottom: 4px;
background-color: white;
-webkit-transition: .4s;
transition: .4s;
border-radius: 50%;
}
.slider.checked{
background-color: #2ab934;
}
.slider:focus{
box-shadow: 0 0 1px #2196F3;
}
.slider.checked:before {
-webkit-transform: translateX(26px);
-ms-transform: translateX(26px);
transform: translateX(55px);
}
/*------ ADDED CSS ---------*/
.slider:after
{
content:'OFF';
color: white;
display: block;
position: absolute;
transform: translate(-50%,-50%);
top: 50%;
left: 50%;
font-size: 10px;
font-family: Verdana, sans-serif;
}
.slider.checked:after
{
content:'ON';
}
<div class="switch">
<div onclick="sliding(this);" class="slider round"></div>
</div>
I have the following view icon for articles:
.viewIcon {
display: inline-block;
width: 1em;
height: 1em;
background: #888;
position: relative;
border-radius: 65% 0;
-webkit-transform: rotate(45deg);
-ms-transform: rotate(45deg);
transform: rotate(45deg);
top: 5px;
}
.viewIcon:before,
.viewIcon:after {
content: "";
position: absolute;
display: block;
top: 50%;
left: 50%;
border-radius: 100%;
}
.viewIcon:before {
height: .5em;
width: .5em;
background: #fff;
margin-top: -.25em;
margin-left: -.25em;
}
.viewIcon:after {
height: .25em;
width: .25em;
background: #888;
margin-top: -.1em;
margin-left: -.11em;
}
.activeArticle {
transform: scale(1.5);
}
<div class="viewIcon"></div>
<br/><br/>
<div class="viewIcon activeArticle"></div>
As you can see the ".activeArticle" rotates the icon around 45 degrees.
Why is this happening? Am I missing something in the pseudo elements?
How can I fix it/How can I scale it without rotation? (transform/rotate will scale the icon back to the original size)
You are resetting your transform when you specify scale for activeArticle - use this:
.activeArticle {
transform: rotate(45deg) scale(1.5);
}
Demo below:
.viewIcon {
display: inline-block;
width: 1em;
height: 1em;
background: #888;
position: relative;
border-radius: 65% 0;
-webkit-transform: rotate(45deg);
-ms-transform: rotate(45deg);
transform: rotate(45deg);
top: 5px;
}
.viewIcon:before,
.viewIcon:after {
content: "";
position: absolute;
display: block;
top: 50%;
left: 50%;
border-radius: 100%;
}
.viewIcon:before {
height: .5em;
width: .5em;
background: #fff;
margin-top: -.25em;
margin-left: -.25em;
}
.viewIcon:after {
height: .25em;
width: .25em;
background: #888;
margin-top: -.1em;
margin-left: -.11em;
}
.activeArticle {
transform: rotate(45deg) scale(1.5);
}
<div class="viewIcon"></div>
<br/>
<br/>
<div class="viewIcon activeArticle"></div>
Use rotate() & scale() transform property combined, just like this:
.activeArticle {
transform: scale(1.5) rotate(45deg);
}
.viewIcon {
display: inline-block;
width: 1em;
height: 1em;
background: #888;
position: relative;
border-radius: 65% 0;
-webkit-transform: rotate(45deg);
-ms-transform: rotate(45deg);
transform: rotate(45deg);
top: 5px;
}
.viewIcon:before,
.viewIcon:after {
content: "";
position: absolute;
display: block;
top: 50%;
left: 50%;
border-radius: 100%;
}
.viewIcon:before {
height: .5em;
width: .5em;
background: #fff;
margin-top: -.25em;
margin-left: -.25em;
}
.viewIcon:after {
height: .25em;
width: .25em;
background: #888;
margin-top: -.1em;
margin-left: -.11em;
}
.activeArticle {
transform: scale(1.5) rotate(45deg);
}
<div class="viewIcon"></div>
<br/><br/>
<div class="viewIcon activeArticle"></div>
Hope this helps!
I have created this animation but it is not smooth. When you hover over the blue circle, a multicolored circle opens up but the opening is shaky, not 100% smooth. Can the animation be smoothened and how?
#container {
border: 1px solid black;
width: 600px;
height: 600px;
position: relative;
}
#circle {
margin: 0;
padding: 0;
border: 1px solid black;
width: 50px;
height: 50px;
border-radius: 50%;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
overflow: hidden;
transition: width 0.2s, height 0.2s;
}
#circle a {
margin: 0;
display: block;
padding: 0;
width: 250px;
height: 220px;
position: absolute;
top: 50%;
left: 50%;
transform-origin: 0 0;
}
#circle a:hover {
opacity: 0.5;
cursor: pointer;
}
#trap1 {
background-color: green;
transform: rotate(0deg) skewX(30deg);
}
#trap2 {
background-color: yellow;
transform: rotate(60deg) skewX(30deg);
}
#trap3 {
background-color: red;
transform: rotate(120deg) skewX(30deg);
}
#trap4 {
background-color: blue;
transform: rotate(180deg) skewX(30deg);
}
#trap5 {
background-color: orange;
transform: rotate(240deg) skewX(30deg);
}
#trap6 {
background-color: purple;
transform: rotate(300deg) skewX(30deg);
}
#hide {
position: absolute;
top: 50%;
left: 50%;
z-index: 1;
transform: translate(-50%, -50%);
width: 50px;
height: 50px;
border-radius: 50%;
background-color: cyan;
}
#circle:hover {
width: 500px;
height: 500px;
}
<div id="container">
<div id="circle">
<div id="hide"></div>
<a id="trap1"></a>
<a id="trap2"></a>
<a id="trap3"></a>
<a id="trap4"></a>
<a id="trap5"></a>
<a id="trap6"></a>
</div>
</div>
Reason: (no links/source to back-up, it is just an educated guess)
I've run into similar a case in the past and what I've managed to find out is that the shake happens due to (I believe) a sub-pixel rendering issue.
When height and width are transitioned, the updates to the element seem to happen pixel by pixel. For example, in the below snippet there are two div elements whose height and width are being transitioned (first one increases by 3px over 5s while second increases by 5px). The key thing to note here is that for the first div there are three visible steps while there are five steps for the second one (meaning they increase pixel by pixel).
div{
display: inline-block;
height: 100px;
width: 100px;
background: red;
border: 1px solid;
margin: 10px;
transition: all 5s linear;
}
div:nth-child(1):hover{
height: 103px;
width: 103px;
}
div:nth-child(2):hover{
height: 105px;
width: 105px;
}
<div></div>
<div></div>
Now you would ask me how does this have any connection with the shake. The connection is that the height and width increase pixel by pixel but the translate(-50%, -50%) means that the no. of px by which to translate the element is sometimes in fractions and it seems like some corrections happen during the actual transition to overcome these fractional values.
Solution: (or a work-around)
Instead of using translate(-50%, -50%) trick for horizontal + vertical centering, if we directly position the element by providing the top and left in pixels, you'd see that there is no shake. Based on my understanding, this is because browsers transition all 4 properties (height, width, top and left) pixel by pixel and hence there are no fractional values that cause the correction.
(Tested on the latest Chrome + Windows 10.)
#container {
border: 1px solid black;
width: 600px;
height: 600px;
position: relative;
}
#circle {
margin: 0;
padding: 0;
border: 1px solid black;
width: 50px;
height: 50px;
border-radius: 50%;
position: absolute;
top: 275px;
left: 275px;
overflow: hidden;
transition: all 0.2s;
}
#circle a {
margin: 0;
display: block;
padding: 0;
width: 250px;
height: 220px;
position: absolute;
top: 50%;
left: 50%;
transform-origin: 0 0;
}
#circle a:hover {
opacity: 0.5;
cursor: pointer;
}
#trap1 {
background-color: green;
transform: rotate(0deg) skewX(30deg);
}
#trap2 {
background-color: yellow;
transform: rotate(60deg) skewX(30deg);
}
#trap3 {
background-color: red;
transform: rotate(120deg) skewX(30deg);
}
#trap4 {
background-color: blue;
transform: rotate(180deg) skewX(30deg);
}
#trap5 {
background-color: orange;
transform: rotate(240deg) skewX(30deg);
}
#trap6 {
background-color: purple;
transform: rotate(300deg) skewX(30deg);
}
#hide {
position: absolute;
top: 50%;
left: 50%;
z-index: 1;
transform: translate(-50%, -50%);
width: 50px;
height: 50px;
border-radius: 50%;
background-color: cyan;
}
#circle:hover {
width: 500px;
height: 500px;
left: 50px;
top: 50px;
}
<div id="container">
<div id="circle">
<div id="hide"></div>
<a id="trap1"></a>
<a id="trap2"></a>
<a id="trap3"></a>
<a id="trap4"></a>
<a id="trap5"></a>
<a id="trap6"></a>
</div>
</div>
I would be interested to know if you can make a modal pop-up window just using HTML and CSS without JQuery.
Does anyone know such a simple example?
Thanks in advance!
A modal with only HTML and CSS no jQuery or javascript
body {
color: #333333;
font-family: 'Helvetica', arial;
height: 80em;
}
.wrap {
padding: 40px;
text-align: center;
}
hr {
clear: both;
margin-top: 40px;
margin-bottom: 40px;
border: 0;
border-top: 1px solid #aaaaaa;
}
h1 {
font-size: 30px;
margin-bottom: 40px;
}
p {
margin-bottom: 20px;
}
.btn {
background: #428bca;
border: #357ebd solid 1px;
border-radius: 3px;
color: #fff;
display: inline-block;
font-size: 14px;
padding: 8px 15px;
text-decoration: none;
text-align: center;
min-width: 60px;
position: relative;
transition: color .1s ease;
/* top: 40em;*/
}
.btn:hover {
background: #357ebd;
}
.btn.btn-big {
font-size: 18px;
padding: 15px 20px;
min-width: 100px;
}
.btn-close {
color: #aaaaaa;
font-size: 30px;
text-decoration: none;
position: absolute;
right: 5px;
top: 0;
}
.btn-close:hover {
color: #919191;
}
.modal:before {
content: "";
display: none;
background: rgba(0, 0, 0, 0.6);
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
z-index: 10;
}
.modal:target:before {
display: block;
}
.modal:target .modal-dialog {
-webkit-transform: translate(0, 0);
-ms-transform: translate(0, 0);
transform: translate(0, 0);
top: 20%;
}
.modal-dialog {
background: #fefefe;
border: #333333 solid 1px;
border-radius: 5px;
margin-left: -200px;
position: fixed;
left: 50%;
top: -100%;
z-index: 11;
width: 360px;
-webkit-transform: translate(0, -500%);
-ms-transform: translate(0, -500%);
transform: translate(0, -500%);
-webkit-transition: -webkit-transform 0.3s ease-out;
-moz-transition: -moz-transform 0.3s ease-out;
-o-transition: -o-transform 0.3s ease-out;
transition: transform 0.3s ease-out;
}
.modal-body {
padding: 20px;
}
.modal-header,
.modal-footer {
padding: 10px 20px;
}
.modal-header {
border-bottom: #eeeeee solid 1px;
}
.modal-header h2 {
font-size: 20px;
}
.modal-footer {
border-top: #eeeeee solid 1px;
text-align: right;
}
/*ADDED TO STOP SCROLLING TO TOP*/
#close {
display: none;
}
<div class="wrap">
<h1>Modal - Pure CSS (no Javascript)</h1>
<hr />
Modal!
</div>
<!-- Modal -->
<div class="modal" id="modal-one" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-header">
<h2>Modal in CSS?</h2>
× <!--CHANGED TO "#close"-->
</div>
<div class="modal-body">
<p>One modal example here! :D</p>
</div>
<div class="modal-footer">
Nice! <!--CHANGED TO "#close"-->
</div>
</div>
</div>
</div>
<!-- /Modal -->
Yes, it is possible.
You can use the :target pseudo selector to show a modal.
Here's an example including some transitions as well (code combined from shehary's codepen example and http://tympanus.net/Development/ModalWindowEffects):
body {
background-color: #e74c3c;
}
a {
background-color: #c0392b;
color: #fff;
text-align: center;
width: 200px;
height: 50px;
line-height: 50px;
text-decoration: none;
border-radius: 2px;
}
a:hover {
background-color: #a5281b;
}
body > a {
display: block;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
.md-modal {
position: fixed;
top: 50%;
left: 50%;
width: 50%;
max-width: 630px;
min-width: 320px;
height: auto;
z-index: 2000;
visibility: hidden;
transform: translateX(-50%) translateY(-50%);
}
.md-modal:target {
visibility: visible;
}
.md-overlay {
position: fixed;
width: 100%;
height: 100%;
visibility: hidden;
top: 0;
left: 0;
z-index: 1000;
opacity: 0;
background: rgba(143,27,15,0.8);
transition: all 0.3s;
}
.md-modal:target ~ .md-overlay {
opacity: 1;
visibility: visible;
}
/* Content styles */
.md-content {
color: #fff;
background: #e74c3c;
position: relative;
border-radius: 3px;
margin: 0 auto;
}
.md-content h3 {
margin: 0;
padding: 0.4em;
text-align: center;
font-size: 2.4em;
font-weight: 300;
opacity: 0.8;
background: rgba(0,0,0,0.1);
border-radius: 3px 3px 0 0;
}
.md-content > div {
padding: 15px 40px 30px;
margin: 0;
font-weight: 300;
font-size: 1.15em;
}
.md-content > div p {
margin: 0;
padding: 10px 0;
}
.md-content > div ul {
margin: 0;
padding: 0 0 30px 20px;
}
.md-content > div ul li {
padding: 5px 0;
}
.md-content a {
display: block;
margin: 0 auto;
font-size: 0.8em;
}
/* Effect */
.md-modal .md-content {
-webkit-transform: scale(0.7);
-moz-transform: scale(0.7);
-ms-transform: scale(0.7);
transform: scale(0.7);
opacity: 0;
-webkit-transition: all 0.3s;
-moz-transition: all 0.3s;
transition: all 0.3s;
}
.md-modal:target .md-content {
-webkit-transform: scale(1);
-moz-transform: scale(1);
-ms-transform: scale(1);
transform: scale(1);
opacity: 1;
}
MODAL
<div class="md-modal" id="modal">
<div class="md-content">
<h3>Modal Dialog</h3>
<div>
<p>This is a modal window.</p>
<a class="md-close" href="#">Close me!</a>
</div>
</div>
</div>
<div class="md-overlay"></div>
If you don't feel comfortable using visibility: hidden you can use display: none instead. But you better remove the transitions in that case (visibility can be used in a transition, display can't).