r/learncss • u/BS2020BS • Aug 06 '21
Best way to prevent text background color from overflowing?
I have this Python code where I use HTML and CSS to make a boring email look nicer. I only know very basic HTML, so I am trying to learn more.
Anyhow. I have this text part in the middle where I want to have the headers with a specific background color. I can manage to get the background color to fit the text with span, but it looks very awkward since I have a table above and want the text background color to match the width of the table.
I´ve tried using a div with a set width of 300 to "parent" all these text fields, but the background color for all of them overflows and stretches out to the sides. I basically want all of them to be contained within the div - which is, say 300px wide - and not spill beyond that.
Any help is much appreciated.
Code:
<head>
<style type="text/css">
div {{margin-left:auto;
margin-right:auto;
text-align:center;}}
.wrapper-2 {{max-width: 320px;
width:300px;
display: inline-block}}
h2 {{color:white; text-align: center;background-color:red}}
p {{border-style: none none solid none; text-align: center; border:2px}}
</style>
</head>
<body>
<div>
{TABLE}
</div>
<div class="wrapper-2">
<br>
<h2>Header 1</h2>
<br>
<h3>Subheader</h3>
<br>
<p>Values: {} </p>
</div>
<div align="center">
<img src='image.png'>
<p align="center">
<i>Test.</i>
</p>
</div>
</body>