Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Smooth CSS transition using media queries

I'm trying to create a smooth transition for a div that's placed elsewhere when @media only screen and (max-width: #) changes. The idea is when the max-width reaches, let's say, 600 pixels, the div smoothly transitions to another position.

Here's the fiddle: https://jsfiddle.net/8nQ6v/399/

This is a similar fiddle, transition is controlled by a click on the button: http://jsfiddle.net/8nQ6v/2/

HTML

<div id="square" class="position">

CSS

#square {
   height: 100px;
   width: 100px;
   background-color: red;
}

.position {
    position: absolute;
    bottom: 20px;
    left: 20px;
}

@media only screen and (max-width: 600px){
    .position {
       position: static!important;
    }
}

With the current HTML & CSS it instantly hops to the other position. I'm wondering if there is someway to smoothly transition this.

like image 585
TitoMikani Avatar asked Nov 16 '15 18:11

TitoMikani


People also ask

How do you transition smoothly in CSS?

The transition-timing-function property can have the following values: ease - specifies a transition effect with a slow start, then fast, then end slowly (this is default) linear - specifies a transition effect with the same speed from start to end. ease-in - specifies a transition effect with a slow start.

Do media queries go in CSS?

Using media queries. Media queries are commonly associated with CSS, but they can be used in HTML and JavaScript as well. There are a few ways we can use media queries directly in HTML.

Which delay CSS property is used for transition effect?

The transition-delay property specifies when the transition effect will start. The transition-delay value is defined in seconds (s) or milliseconds (ms).

Should I use transition or animation CSS?

CSS transitions are generally best for simple from-to movements, while CSS animations are for more complex series of movements. It's easy to confuse CSS transitions and animations because they let you do similar things. Here are just a few examples: You can visualize property changes.


1 Answers

you can use css transition to animate

transition: ease all .5s;

and to animate don't change the position try changing the values

#square {
  height: 100px;
  width: 100px;
  background-color: red;
  transition: ease all .5s;

}
.position {
  position: absolute;
  bottom: 20px;
  left: 300px;
}
@media only screen and (max-width: 600px) {
  .position {
    bottom: 20px;
    left: 20px;
  }
}
<div id="square" class="position">
</div>
like image 175
shakee93 Avatar answered Oct 30 '22 03:10

shakee93