保持一个绝对定位的 HTML 元素在它的位置

斯拉维斯

在此处输入图片说明

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

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

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

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>

样式文件

.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;
}
共通者

为了解决这个问题,我将滚动行为定义移动到.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 结构进行任何更改。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

绝对定位一个兄弟姐妹时如何保留兄弟姐妹元素的位置?

CSS位置绝对值-下一个定位元素是body吗?

在绝对定位元素的顶部定位一个元素反应原生

如何同时给一个元素绝对定位和粘性定位?

将绝对定位的元素水平居中放置在另一个元素的中心之下

Javascript更改html元素的绝对位置

Html href 到另一个页面的特定位置

CSS 掩码图像,是否可以在掩码“顶部”显示一个元素(没有绝对定位)?

如何使div填充高度直到下一个绝对定位的元素

在另一个绝对定位的元素上方应用可拖动容器

在一个元素上的绝对和相对位置?

html select 元素,它的选项取决于另一个选择

html中的Java脚本数组排序问题,它首先显示最后一个元素

在列表的指定位置插入一个新元素

为什么改变 html 中绝对定位元素的代码位置会改变其屏幕位置?

Javascript /根据另一个元素的位置定位一个元素

(C) 在数组中查找一个元素并只打印一次它的位置

如何在 CSS 中相对于另一个元素的位置引用 html 元素位置?

将新元素从 XML 附加到 HTML,然后使用一个函数/回调定位新元素

纯 CSS:垂直和水平居中一个绝对定位元素,其宽度取决于子元素

选择绝对最后一个元素

当位置从绝对位置更改为固定位置时如何使元素保持原位

为什么将绝对定位的元素的同级设置为position:relative,使其高于前一个?

将一个元素放置在另一个元素上时,我是否可以正确使用绝对定位?

为什么我不能在 td 或 tr 标签中定位一个没有 html 中的 table 标签的元素?

单击其上方的另一个元素时,Html 不会转到 A href 位置

定位特定元素,如果它只包含另一个带有 CSS 的元素

如何在一个元素上创建一个事件切换 HTML 类,同时从其他元素中删除它?

使第二个div出现在第一个div之上,而没有绝对位置或更改html