【问题标题】:Set coordinates of element in DIV with js, css, html用js、css、html设置DIV中元素的坐标
【发布时间】:2017-03-08 16:29:20
【问题描述】:

我正在处理期刊问题的一些图形表示。我需要的是在其他 DIV 中使用简单的 DIV(或其他)显示文本块,与它们在问题页面上的组织方式完全相同。为此,我需要将 DIV 元素的坐标设置为精确数字,但与父 DIV 相关。有没有办法通过使用css或js来做到这一点?

【问题讨论】:

标签: javascript html css frontend web-frontend


【解决方案1】:

如果您将外部 div 设置为position: relative,您可以将内部 div 设置为position: absolute,并将其顶部、左侧、右侧和底部属性设置为您需要的像素。例如。

 .outer {
      position: relative;
    }

    .inner {
      position: absolute;
      top: 10px; //your coordinate
      left: 5px; //your coordinate
    }
<div class="outer">
      <div class="inner">Your content</div>
</div>

   

否则,您可以简单地在内部元素上使用填充。

【讨论】:

  • 是父DIV或窗口边缘的坐标(10px,5px)偏移吗?
  • 父div边缘的偏移量。
【解决方案2】:

如果您希望div 成为display: block;,您可以使用简单的margin-topmargin-left 来设置坐标。

假设(例如)您需要将div 的坐标设置为:

为此,请在 CSS 中设置 margin-left: 100pxmargin-top: 50px

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-09
    • 1970-01-01
    • 1970-01-01
    • 2011-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多