How can I prevent the text from overflowing the background image when the page width is decreased, as shown?
I would like the text to always remain relative to the background image.
Here is my HTML code:
<section class="sec2">
<h1 class="h1two">A 44-letter alphabet.</h1>
<h1 class="h1three">A phonetically consistent language.</h1>
<h1 class="h1four">A determined teacher you can count on.</h1>
</section>
Here is my CSS code:
h1 {
position: relative;
font-family: "LiberationSansRegular";
font-style: normal;
font-weight: 600;
font-size: 55px;
}
.h1two {
top: 15%;
}
.h1three {
top: 30%;
}
.h1four {
top: 45%;
}
.sec2 {
height: 30vw;
background-image: url(pictures/output-onlinepngtools.png);
background-repeat: no-repeat;
background-size: cover;
background-position: center;
word-break: keep-all;
}
2
Answers
You need to change the size depending on the screen size:
Documentation: https://www.w3schools.com/css/css_rwd_mediaqueries.asp
Remove the default margin from the
h1
tags.Then add
line-height: calc(30vw / 3);
this will keep the text fit to the image height up until any line breaks into 2 lines.You may want to add the
line-height
in the@media
queryTry the following code bellow:
Result