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.