简体   繁体   English

ReactJS 内联式绝对顶部:“100vh”无法正常工作

[英]ReactJS inline-style absolute top: “100vh” not working correctly

I have a basic component inside of my App.js called Overlay.我的 App.js 中有一个名为 Overlay 的基本组件。 It's just a div and I am trying to position it using absolute positioning but when I try to set the top style from inline-styles, the component does not leave the view;它只是一个 div,我试图 position 它使用绝对定位但是当我尝试从内联样式设置顶部样式时,组件不会离开视图; however, when I set top using a separate stylesheet, it works fine.但是,当我使用单独的样式表设置顶部时,它可以正常工作。 I've included side-by-side pictures of inline vs. stylesheet and would like to know how to make the inline be the same as the stylesheet.我已经包含了内联与样式表的并排图片,并且想知道如何使内联与样式表相同。 Thanks in advance.提前致谢。

No-Top: No Top Styling无上衣:无上衣造型

Inline-Top: Inline Top Styling内联顶部:内联顶部样式

Stylesheet-Top: Stylesheet Top样式表顶部:样式表顶部

Code:代码:

Overlay.js Overlay.js

import React from "react";
import "./Overlay.css";

class Overlay extends React.Component {
  render() {
    return (
      <div
        className="Overlay" style={{ top: "calc(100vh - 60px)" }}
      ></div>
    );
  }
}

export default Overlay;

Overlay.css叠加层.css

.Overlay {
  height: calc(485px - 16px);
  width: calc(100vw - 16px);
  background-color: white;
  color: black;
  border-top-left-radius: 15px;
  border-top-right-radius: 15px;
  padding: 8px;
  position: absolute;
  /* top: calc(100vh - 60px); */
}

Edit: The issue seems to be that react's hot-reload does not update the inline styles correctly sometimes and needs to be reloaded manually in order to get applied.编辑:问题似乎是反应的热重载有时不能正确更新内联 styles,需要手动重新加载才能应用。 Seems to be an issue on React's end.似乎是 React 的一个问题。

The code works fine, just to show you, in code, I have added backgroundColor.该代码工作正常,只是为了向您展示,在代码中,我添加了背景颜色。

<div className="Overlay" style={{ top: "calc(100vh - 200px)" , backgroundColor: "yellow"}}></div

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM