javascript - CSS 基于元素高度?

标签 javascript html css

我有一个按钮,由于文本换行,它会根据屏幕大小更改其高度。

当屏幕尺寸足够大时,按钮非常适合:

enter image description here

但是当它变小时,按钮不再居中:

enter image description here

我尝试应用 @media 设置:

@media (max-width: 767px) {
    #footer-content input[type="button"],
    #footer-content button {
        margin-top: -22px;
    }
}

@media (min-width: 768px) {
    #footer-content input[type="button"],
    #footer-content button {
        margin-top: -10px;
    }
}

当换行发生时,它完美地工作:

enter image description here

但是当它不存在并且屏幕小于 767px 时,它会再次偏心:

enter image description here enter image description here

所以我希望无论如何都可以根据按钮的高度而不是屏幕的宽度来做 CSS,但是我一直在做的研究并没有太大帮助,所以,有没有办法通过 CSS 或我会被迫使用 javascript 吗?

最佳答案

尝试使用 flexbox。如果这实际上是一个输入类型范围,您甚至不需要指定 align-items: center; 来让它工作。 https://jsfiddle.net/vo4xcrm5/

CSS

.wrapper {
  display: flex;
  margin-top: 100px;
}

.range {
  width: 100%;
}

.button {
  max-width: 15%;
}

HTML

<div class="wrapper">
  <input class="range" type="range" />
  <span class="button">
    Some type of button-like thing
  </span>
</div>

关于javascript - CSS 基于元素高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43795851/

相关文章:

c# - 如何使用 c#.net 在电子邮件中将 Html 输入文件值作为附件发送

javascript - jQuery Waterfall 元素不按其 HTML 布局顺序排列

Android - CSS 垂直居中不起作用

css - 我需要字体大小 :0? 吗

javascript - 如何获取js当前所在文件夹?

javascript - 如何使用 javascript 使按钮标签动态化?

javascript - Tinymce编辑器: Create preset blocks of text to insert from toolbar

javascript - Node.js - "Cannot find module ' module.js'“

javascript - 类型错误 : string is not a function?

html - 为不支持的浏览器重现剪辑路径多边形?