As your child1 text length is variable you’ll need to use jQuery to do this. Here’s a jsfiddle I made with a potential solution. Basically you’re calculating the width of your child1 and then using that to set padding that centers child1 and also puts all p
elements on the same guideline.
<html>
<div id="parent">
<div id="child1" class="child"><p id="keyWidth">Variable text.
</p></div>
<div id="child2" class="child"><p>
Variable text padding-left is:
</p></div>
<div id="child3" class="child"><p>
Some much much much longer text here this text is so long
</p></div>
</div>
</html>
<style>
#parent {
display: block;
width: 50%;
margin: 0 auto;
padding: 2%;
background-color: yellow;
}
.child{
margin: 2%;
padding: 2%;
background-color: cyan;
}
p{
width: auto;
display: inline-block;
}
</style>
<script>
var keyWidth = $('#keyWidth').width();
var keyDivWidth = $('.child').width();
var keyPadding = keyDivWidth-keyWidth;
$('#child2 p').append(keyPadding);
$('p').css('padding-left', keyPadding/2 + "px");
</script>