0

FI have some text inside a div,

<div>
<p style="text-indent: 0">something</p>
</div>

some the text is inside the div

     |-----------------------|
     |                       |
     |something              |
     |                       |
     |                       |
     |-----------------------|

How can I get the offset from g to left side of the div? above should be something positive

and

    <div>
    <p style="text-indent: -100px">something</p>
    </div>

looks like

             |-----------------------|
             |                       |
   something |                       |
             |                       |
             |                       |
             |-----------------------|

How can I get the offset from g to left side of the div? Above should be something negative

garen96
  • 175
  • 1
  • 9

2 Answers2

0

You can just use position:relative to move the p from its current position to the left of your div:

<div>
  <p style="position:relative;left:-100px;">something</p>
</div>

Here's a simple example: https://jsfiddle.net/as1m860v/

Claire
  • 3,146
  • 6
  • 22
  • 37
0

The following modification can yield your desired result if your "something" is going to dynamic or have variable width.

HTML

<div>
    <p id="ToIndent">something</p>
</div>

Javascript

const dom = document.querySelector('#ToIndent');
dom.style.textIndent = `-${dom.offsetWidth}px`;

Alternatively;

HTML

<div>
    <p id="ToIndent" style="position: relative;" >something</p>
</div>

Javascript

const dom = document.querySelector('#ToIndent');
dom.style.left= `-${dom.offsetWidth}px`;
WillyMilimo
  • 447
  • 3
  • 12