首页 > 解决方案 > 将绝对定位的 HTML 元素保持在其位置

问题描述

在此处输入图像描述

red arrow具有的跨度标签position: absolute按预期位于右下角。当我开始在content容器中输入文本时,箭头开始飘走。

content高度设置为auto100%没有解决问题。

无论元素的高度如何,我们如何将 span 标签red arrow固定在右下角content

https://codesandbox.io/s/gracious-shockley-dcp59?file=/src/App.js

HTML

<div className="App">
      <div className="container">
        <div className="content">
          <div suppressContentEditableWarning={true} contentEditable="true">
            Test
          </div>
        </div>
        <span className="helper"></span>
      </div>

      <div className="container">
        <div className="content">
          <div suppressContentEditableWarning={true} contentEditable="true">
            <p>
              Lorem Ipsum is simply dummy text of the printing and typesetting
              industry. Lorem Ipsum has been the industry's standard dummy text
              ever since the 1500s, when an unknown printer took a galley of
              type and scrambled it to make a type specimen book. It has
              survived not only five centuries, but also the leap into
              electronic typesetting, remaining essentially unchanged. It was
              popularised in the 1960s with the release of Letraset sheets
              containing Lorem Ipsum passages, and more recently with desktop
              publishing software like Aldus PageMaker including versions of
              Lorem Ipsum.
            </p>
          </div>
        </div>
        <span className="helper"></span>
      </div>
    </div>

样式.css

.container {
  overflow: auto;
  border: 1px solid #000;
  width: 150px;
  height: 100px;
  float: left;
  transform: translate(0px, 10px);
  margin-right: 20px;
}

.content {
  padding: 15px;
  background-color: #ddd;
}

.helper::before {
  content: "";
  position: absolute;
  right: 3px;
  bottom: 3px;
  width: 5px;
  height: 5px;
  border-right: 4px solid red;
  border-bottom: 4px solid red;
}

标签: htmlcss

解决方案


为了解决这个问题,我将滚动行为定义移动到.content并分配position: relative;.container.

.container {
  position: relative; /* new */
  overflow: hidden; /* changed */
  border: 1px solid #000;
  width: 150px;
  height: 100px;
  float: left;
  transform: translate(0px, 10px);
  margin-right: 20px;
}

.content {
  padding: 15px;
  background-color: #ddd;
  width: inherit; /* new */
  height: inherit; /* new */
  box-sizing: border-box; /* new */
  overflow-y: scroll; /* new */
}

.helper {
  position: absolute;
  right: 3px;
  bottom: 3px;
  width: 5px;
  height: 5px;
  border-right: 4px solid red;
  border-bottom: 4px solid red;
}
<div class="App">
  <div class="container">
    <div class="content">
      <div suppressContentEditableWarning={true} contentEditable="true">
        Test
      </div>
    </div>
    <span class="helper"></span>
  </div>

  <div class="container">
    <div class="content">
      <div suppressContentEditableWarning={true} contentEditable="true">
        Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
        survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
        publishing software like Aldus PageMaker including versions of Lorem Ipsum.
      </div>
    </div>
    <span class="helper"></span>
  </div>
</div>

注意:我已更改classNameclass使代码段工作。我没有对 HTML 结构进行任何更改。


推荐阅读