简体   繁体   English

CSS - 当页面宽度较小时,onhover 垂直下拉 div

[英]CSS - onhover pulls down div vertically when page width is small

I am working on a site where there is an info icon next to the logo in the center of the page.我在一个网站上工作,该网站在页面中央的徽标旁边有一个信息图标。 When hovering over the info icon, a text box displays.将鼠标悬停在信息图标上时,会显示一个文本框。 For some reason, when the screen width is shorter than around 1300px, the entire logo as well as the info box is pulled down vertically.出于某种原因,当屏幕宽度小于 1300px 左右时,整个徽标以及信息框会被垂直下拉。 Not sure what aspect of onhover could be causing this.不确定悬停的哪个方面可能导致此问题。

I tried to manually move down the info icon with media queries but that is not a good way of doing it.我尝试使用媒体查询手动下移信息图标,但这不是一个好方法。

Here is react component:这是反应组件:


function HomeContent() {
  return (
    <>
      <div className="center-div">
        <img src={tenGreenLogo} className="ten-green-logo" alt=""></img>
        <div className="info-div">
          <img src={infoIcon} alt="" className="info-icon"></img>
          <p className="hidden-info">
            The 10Green Score indicates the health of your environment using a
            number from 0 to 10. The higher the score, the healthier your
            environment.
            <br></br>
            <br></br>
            Factors that can reduce your score include unhealthy air quality
            readings and poor environmental monitoring.
          </p>
        </div>
      </div>
      <Globe />
    </>
  );
}

export default HomeContent;

and here is CSS for that component:这是该组件的 CSS:

.center-div {
  display: flex;
  justify-content: center;
  align-items: center;
  padding-top: 7rem;
  padding-bottom: 0rem;
  scroll-behavior: smooth;
}
.ten-green-logo {
  max-width: 40%;
  animation: transitionIn 1s;
  padding-right: 1rem;
}

.info-div {
  width: 2rem;
  margin-top: auto;
  margin-bottom: 0;
}

.info-icon {
  width: 2rem;
  animation: transitionIn 1s;
}

.hidden-info {
  display: none;
}

.info-icon:hover + .hidden-info {
  display: block;
  position: relative;
  background-color: white;
  padding: 0.6rem;
  border-radius: 5%;
  width: 20rem;
  font-size: 80%;
  right: 7.5rem;
  top: 10.5rem;
}

.info-icon:hover {
  position: relative;
  top: 10.6rem;
}

@keyframes transitionRight {
  from {
    transform: translateX(0rem);
  }
  to {
    transform: translateX(2rem);
  }
}

@keyframes transitionIn {
  from {
    opacity: 0;
    transform: translateY(4rem);
  }
  to {
    opacity: 1;
    transform: translateY(0rem);
  }
}

Any help would be greatly appreciated.任何帮助将不胜感激。

use this style, you have to use position absolute on hidden-info使用这种风格,你必须在隐藏信息上使用 position absolute

 .info-div { position: relative; }.hidden-info { display: none; position: absolute; background-color: white; padding: 0.6rem; border-radius: 5%; width: 20rem; font-size: 80%; right: 7.5rem; top: 10.5rem; }.info-icon:hover +.hidden-info { display: block; }

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

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