Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two child divs one match another with variable width

Tags:

html

css

I have 2 child divs within a parent div. I need child1 to be always centered in the parent, and child2 to be positioned under child1 (being aligned to left side of child1).

Child1 contains variable length of text. How can this be done?

#parent {
  text-align: center;
}
<div id="parent">
  <div id="child1">Variable length text here</div>
  <div id="child2">Some text here</div>
</div>

Current child2 position

This is what I need

like image 264
woodo Avatar asked Mar 09 '26 20:03

woodo


1 Answers

Try the display:table and display:table-caption approach:

.parent {
  display: table;
  margin: auto;
}

.child2 {
  display: table-caption;
  caption-side: bottom;
  background: silver;
}
<div class="parent">
  <div class="child1">Variable length text here</div>
  <div class="child2">Some text here</div>
</div>

<br>

<div class="parent">
  <div class="child1">Variable length text</div>
  <div class="child2">Some text Some text Some text</div>
</div>
like image 131
Stickers Avatar answered Mar 12 '26 12:03

Stickers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!