html - 媒体查询中的转换在两个方向都不起作用

标签 html css css-transitions

我是 CSS 转换的新手,并试图弄清楚它是如何处理媒体查询的。

我创建了一个基本示例,其中希望我的按钮以平滑过渡向上并返回中心,但它仅以一种方式起作用:当它返回中心时不起作用。

这是一个真实的例子,可以用鼠标调整结果窗口的宽度来查看问题:
https://jsfiddle.net/hgaoe3zc/

这是我的 CSS 过渡:

body {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  justify-content: center;
  align-items: center;
}

button {
  top: calc(50% - 30px);
  transition: top 0.5s ease-in-out;
}

@media only screen and (max-width: 500px) {
  button {
    position: absolute;
    top: 6rem;
  }
}

任何想法都会非常感激:)

最佳答案

您需要设置position: absolute在两个州。

https://jsfiddle.net/yo4fedq6/

body {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  justify-content: center;
  align-items: center;
}

button {
  top: calc(50% - 30px);
  position: absolute; // HERE
  transition: top 0.5s ease-in-out;
}

@media only screen and (max-width: 500px) {
  button {
    top: 6rem;
  }
}
<button>Button</button>

关于html - 媒体查询中的转换在两个方向都不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62224558/

相关文章:

html - 在导航栏品牌附近添加灯光效果到背景

html - 使用 CSS 在边框中制作曲线

html - 使用链接显示/隐藏 div 相同的链接也适用于选项卡内容

html - 当内容较少时,如何将页脚放在视口(viewport)下方?

javascript - 如何使用 html 和 javascript 创建复制按钮?

javascript - 无法读取未定义的属性 'style' -- 未捕获的类型错误

javascript - 如何清除内容 div 并使用 javascript 重新出现

css - ui 选项卡和相对位置

internet-explorer-9 - 我需要CSS3过渡才能在IE9中工作

CSS:滚动幻灯片不重复