Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
171 views
in Technique[技术] by (71.8m points)

javascript - make div position fixed depending of its height using css

Good day.

Please consider next simple code:

                <div className="box-and-clock">
                    
                        {isBoxShown && (
                            <div className="box">
                                {value}
                            </div>
                        )}
                        
                        <img
                            onMouseOver={() => setIsBoxShown(true)}
                            onMouseOut={() => setIsBoxShown(false)}
                            src={clockIcon}/>
                </div>

So I've got following layout (I've cut some redundant data)

enter image description here

When user points at the clock icon he sees some data. The problem is when there is more data in the black box the arrow (which is done by pseudoelements) doesn't point to clock icon anymore. Something like this

enter image description here

I need to make that blackbox to be on certain position from the clock icon without depending on its height.

I've tried display: table to the parent div and display: table-cell to the child, but with no luck. any advices?

My css

.box {
    width: 200px;
    min-height: 130px;
    height: auto;
    background: black;
    border-radius: 2px;
    position: absolute;
    top: 320px;
    right: 228px;
}

.box-and-clock {
display: flex;
flex-direction: column;
}
question from:https://stackoverflow.com/questions/65829232/make-div-position-fixed-depending-of-its-height-using-css

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you can set parent element with css position: relative; to let children identity its position, so children element can use position: absolute; to place itself relative to its parent's position.


.box {
  width: 200px;
  min-height: 130px;
  /* height: auto; */
  background: black;
  border-radius: 2px;
  /* top: 320px; */
  /* right: 228px; */

  position: absolute;
  top: 0px;
  transform: translateY(-100%); /* offset element */
}

.box-and-clock {
  display: flex;
  flex-direction: column;

  position: relative; /* you need this! */
}

ps, for more detailed concept of css position, you may keyword search: css position property


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...