Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Position a child div with it's right side aligned with the parent's left

I want to pop up a "help text box" to the right of an existing element.

I can’t see how I can position it so that the help text box’s right hand side aligns with the parent’s left hand side.

.thingy {
  border-width: 2px;
  border-color: grey;
  border-style: solid;
  margin-left: 50vw;
  position: relative;
  width: fit-content;
}

.thingy-help {
  color: red;
  z-index: 10;
  text-align: right;
  position: absolute;
  top: 0.5rem;
  right: 5rem; /* how to set this to be parent width? */
  border-width: 2px;
  border-color: grey;
  border-style: solid;
  border-radius: 5px;
}
<div class="root">
  <div class="thingy">
    <div class="thingy-help">
      This is a thingy, how about that?
    </div>
    A thingy
  </div>
</div>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You could use transform: translate(-100%) to translate it to the left by 100% of its parent, and use transform-origin: top right to set the origin to the top right point of the .thingy-help.

.thingy {
  border-width: 2px;
  border-color: grey;
  border-style: solid;
  margin-left: 50vw;
  position: relative;
  width: fit-content;
}

.thingy-help {
  color: red;
  z-index: 10;
  text-align: right;
  position: absolute;
  /* how to set this to be parent width? */
  transform-origin: top right;
  transform: translate(-100%);
  
  border-width: 2px;
  border-color: grey;
  border-style: solid;
  border-radius: 5px;
}
<div class="root">
  <div class="thingy">
    <div class="thingy-help">
      This is a thingy, how about that?
    </div>
    A very long thingy
  </div>
</div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading