How do I prevent a parent element's width from affecting the child's width?

Viewed 1083

I've got a <div> as a parent and a <p> as a child. The <p> is centered in the <div> and has a max-width of 650px and the <div> has a width of 800px.

Before, I had <body> parenting the <p> and max-width worked as expected -- when there was a little text, the <p> would be pretty small. When there was a lot of text, the <p> width would go up to 650px and form a new line.
Now, with the <div> as a parent, the <p> seems to have an automatic max-width of half of the parent's width, which I don't want.

This code snippet includes a slider to adjust the parent's width. You'll notice that the <p> automatically changes size according to the parent, which is what I don't want. I want the <p> to have an independent width without explicitly setting it. I attempted to fix this using !important which had no effect (as expected, but it was worth a try).

document.querySelector("input").oninput = event => {
  document.querySelector("div").style.width = event.target.value + "px";
}
@import url("https://fonts.googleapis.com/css2?family=Cinzel&display=swap");
p {
  position: absolute;
  left: 50%;
  transform: translate(-50%, 0%);
  background-color: rgba(80, 80, 80, 0.75);
  text-align: center;
  font-size: 25px;
  max-width: 650px !important;
  width: auto !important;
}
div {
  position: absolute;
  background-color: #98765432;
  width: 1000px;
  height: 200px;
}
<div>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<input type="range" min="700" max="1000">
</div>

One solution I might try is using Javascript and <canvas> to calculate the width of the text I want to set and set the width that way. But, I was wondering if this could be done in CSS.

1 Answers

the important piece of info here is that any block level elements will automatically take the full-width of their parent (until they hit the max-width if a value is provided).

this means, if your parent (div) is 600px, and the child (p) has 650px max-width, it will still take up the full 600px because that's less than the max-width it has defined, and because it's block level, it wants to take up the full width

another useful thing to keep in mind is, if your layout starts to get complex with lots of css rules, take a second and do a simplified version first (like the examples I have below) then build on top of that

you can do it like this:

div {
  border: 1px dashed #ddd;
  text-align: center;
}

p {
  max-width: 650px;
  display: inline-block;
  border: 1px dashed #ddd;
}
<div>
  <p>
    text
  </p>
</div>


<div>
  <p>
    lots of text lots of text lots of text 
    lots of text lots of text lots of text 
    lots of text lots of text lots of text 
  </p>
</div>

or like this with flexbox

div {
  border: 1px dashed #ddd;
  display: flex;
  justify-content: center;
}

p {
  max-width: 650px;
  border: 1px dashed #ddd;
}
<div>
  <p>
    text
  </p>
</div>


<div>
  <p>
    lots of text lots of text lots of text 
    lots of text lots of text lots of text 
    lots of text lots of text lots of text 
  </p>
</div>

Related