【问题标题】:Aligning a <p> tag in top right corner of <textarea>?在 <textarea> 的右上角对齐 <p> 标签?
【发布时间】:2016-11-27 10:01:22
【问题描述】:

我的页面上有一个小文本区域。有了它,我就有了一个&lt;p&gt; 标签,上面写着“HTML”。我想要做的是将 &lt;p&gt; 标记与 textarea 的右上角对齐,这样用户就会知道这个 textarea 显示 HTML 代码。但是,我所有的尝试都将&lt;p&gt;标签放在了页面的右上角。

<div id="mycode">
   <p id="htmltag">HTML</p>
   <textarea id="htmlcode" style="background-color: #1e1e1e; color: #ffff66;">
      This is my HTML code
   </textarea>
</div>

我现在也愿意使用 Javascript,但不会使用 Jquery。

【问题讨论】:

标签: html css


【解决方案1】:

做起来很简单。只需将您的包装器设置为相对定位,然后您就可以将您的标签绝对定位在包装器内。

正如@Optimae 所建议的,这里有一些 CSS 也可以防止 html 代码被您的标签所掩盖。

#mycode {
  position: relative;
  display: inline-block; /* Allow horizontal resize */
  background: #fff;
  border: 1px solid rgb(169, 169, 169);
}

#htmltag {
  position: absolute;
  top: 0;
  right: 0;
  padding: 0;
  margin: 0;
}

#htmlcode {
  border: 0;
  padding: 0;
  width: 100%;
  max-width: 100%;
  height: calc(100% - 1.2em);
  margin-top: 1.2em; /* prevents the code from going behind the label */
  outline: none; /* Prevents blue outline when focused */
}
<div id="mycode">
   <p id="htmltag">HTML</p>
   <textarea id="htmlcode">
      This is my HTML code
      
      With some more
      text
      to 
      force 
      scrolling
   </textarea>
</div>

【讨论】:

  • 虽然这是解决方案,但将 HTML 文本放在 textarea 之外并设置为 0 边距看起来相同对 OP 来说不是更有益吗?这样,用户可以在到达行尾时充分利用textarea,而无需 HTML 文本覆盖他们的代码。
  • 未能将用户可调整大小的textareas 考虑在内。
  • 我必须承认这是一个非常好的解决方案,尽管它更像是做我需要的事情的捷径。但是,有没有办法在 textarea 上方的 p 标签上做这样的事情,同时仍然防止代码落后于标签?
  • @IamGuest,仅使用 CSS,我不相信有。使用一些 JavaScript,可以在正确的位置插入换行符以防止出现问题。
  • @Gerrit0 当然有可能。其他有效的解决方案处理得很好。
【解决方案2】:

你需要在p标签上设置position:absolute, right and top。然后删除浏览器特定的边距和填充。要使其适应文本框大小,请使用 javascript 动态设置宽度。 DEMO

document.getElementById('mycode').style.width = document.getElementById('htmlcode').getBoundingClientRect().width + 'px';
#mycode {
  position: relative;
  padding:0;
}
#htmltag {
  position: absolute;
  right: 0;
  top:0;
  color: red;
  z-index:1;
  padding:0;
  margin:0;
}
<div id="mycode">
     <p id="htmltag">HTML</p>
     <textarea id="htmlcode" style="background-color: #1e1e1e; color: #ffff66;">
      This is my HTML code
   </textarea>
</div>

【讨论】:

  • 我不明白为什么你会使用 Javascript 来解决这个问题,因为你可以使用更多的 CSS 并拥有正确处理页面大小调整的东西。
  • 这没关系,但是当用户调整文本框的大小时,p 标签不再保留在右上角,这看起来很奇怪。
【解决方案3】:

你需要一个相对的父母作为绝对孩子的参考。

这个父级应该把label(你用p)和textarea放在一起并缩小它。

基本上可以这样:

#mycode {
  position: relative;
  /* be reference for positionning clidren*/
  display: table;
  /* shrinks/expands with content could be also inline-block/table/flex*/
}
#htmltag {
  margin: 0;
  right: 1.2em;
  /* stay away from scrollbar*/
  position: absolute;
  color: gray;
}
textarea {
  background-color: #1e1e1e;
  color: #ffff66;
  padding-top: 1.2em;/* clear some space to show the label*/
<div id="mycode">
  <label for="htmlcode" id="htmltag">HTML</label>
  <textarea id="htmlcode" placeholder="HTML">
    This is my HTML code, resize me boxe
  </textarea>
</div>

【讨论】:

  • @IamGuest 不,它没有。为此,您需要 javascript。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-03
  • 2012-08-26
  • 1970-01-01
  • 2013-05-02
  • 1970-01-01
  • 2017-11-28
  • 2010-12-14
相关资源
最近更新 更多