Sudarshan Kalebere Sudarshan Kalebere - 1 month ago 9
CSS Question

How to start description text after title by leaving same width for title

I have a title, and a description, the description always should start after title and continue next line by leaving the same width for title, it should look something like this img below enter image description here

Can any one help me with this please?

<div class="">
<p><span>Description :</span> <span>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. </span></p>
<p>Disponibilité : En stock </p>
</div>

Answer

There are two ways to do it with the HTML structure you provided.

Make it act like a table.

p {
  display: table; 
}
p span {
  display: table-cell;
}
p span:first-child {
  padding-right: 10px;
  white-space: nowrap;
}

Example - https://jsfiddle.net/0az97ekw/

Or by using float with overflow.

p span:first-child {
  float: left;
  padding-right: 10px;
}
p span + span {
  display: block;
  overflow: hidden;
}

Example - https://jsfiddle.net/0az97ekw/1/

Comments