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

Use max-content in calc

Imagine I have a <div> with some <p> in it:

#myDiv {
  width: max-content;
  border-style: dashed;
}
<div id="myDiv">
    <p>This is my text</p>
</div>

But I don’t like the fact that the text is so close to the border, so I tried this:

#myDiv {
  width: calc(max-content + 2px);
  border-style: dashed;
}
<div id="myDiv">
  <p>This is my text</p>
</div>

But (as you can see) it didn’t work.

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


So my question is if there’s a way to create a <div> with a width of

Two pixels more than the content in it

since what I tried

calc(max-content + 2px)

didn’t work.

Thank you in advance.

>Solution :

Unfortunately you can’t use the max-content keyword with calc()

My solution to your problem would be to use padding:

#myDiv {
  width: max-content;
  padding-inline: 2px;
  border-style: dashed;
}
<div id="myDiv">
  <p>This is my text</p>
</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