"push up" background above content with dynamic height - html

I have a div with a background image. Inside of that div is an absolutely positioned div with text in it. At widths>650, The text is a rectangle floating inside of the div. At widths<=650, the text is anchored to the bottom of the div with 100% width.
When this happens, it sits on top of the bottom of the background image. I would like the background image to get "pushed up" so that you can see the bottom of the background image. I could give a negative offset to the background position, but the height of the content is dynamic, because the text wraps to fit the width of the viewport, so I won't know the height, so specifying a specific offset/margin is not an option.
Is there any way to accomplish this in pure CSS with the design that I have? I would like to avoid javascript, and to avoid redesigning the layout of the HTML (e.g., using an <img> instead of a background-image).
Pictures describing the situation below, code below that:
What I have:
What I want:
#container{
background-image:url("http://i.imgur.com/u4xZlez.png");
background-size:100%;
height:400px;
display:block;
position:relative;
width:100%;
max-width:650px;
border:1px solid red;
background-repeat:no-repeat;
}
#content{
position:absolute;
top:10%;
display:block;
background-color:white;
width:60%;
margin-left:10%;
border:1px solid black;
}
#media(max-width:650px){
#content{
bottom:0px;
top:auto;
width:100%;
margin-left:0px;
}
}
<div id="container">
<div id="content">
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna. Nunc viverra imperdiet enim. Fusce est.</p>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna. Nunc viverra imperdiet enim. Fusce est.</p>
</div>
</div>

In the media query, change the container display to flex.
Use justify content to move content to the bottom.
Add a pseudo, using order to move it to the beginning, ahead of content.
Set the background image of container to this pseudo instead, and align it to bottom
#container{
background-image:url("http://i.imgur.com/u4xZlez.png");
background-size:100%;
height:400px;
display:block;
position:relative;
width:100%;
max-width:650px;
border:1px solid red;
background-repeat:no-repeat;
}
#content{
position:absolute;
top:10%;
display:block;
background-color:white;
width:60%;
margin-left:10%;
border:1px solid black;
}
#media(max-width: 650px) {
#container {
display: flex;
flex-direction: column;
justify-content: flex-end;
background-size: 0%;
}
#container::before {
content: "";
/* background-image: url("http://i.imgur.com/u4xZlez.png"); */
background-image: inherit;
background-size: 100%;
background-position: center bottom;
width: 100%;
flex-grow: 1;
order: 1;
}
#content {
position: static;
bottom: 10px;
top: auto;
width: 100%;
margin-left: 0px;
order: 2;
}
}
<div id="container">
<div id="content">
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna. Nunc viverra imperdiet enim. Fusce est.</p>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna. Nunc viverra imperdiet enim. Fusce est.</p>
</div>
</div>

#container {
background-image: url("http://i.imgur.com/u4xZlez.png");
background-size: 100%;
height: 400px;
display: block;
position: relative;
width: 100%;
max-width: 650px;
border: 1px solid red;
background-repeat: no-repeat;
}
#content {
position: absolute;
top: 10%;
display: block;
background-color: white;
width: 60%;
margin-left: 10%;
border: 1px solid black;
}
#media(max-width:650px) {
#container {
background-image: none;
height: auto;
}
#content {
position: relative;
width: 100%;
margin-left: 0px;
}
#content:before{
content: '';
display: block;
background-image: url("http://i.imgur.com/u4xZlez.png");
background-repeat: no-repeat;
background-size: 100%;
padding-bottom: 61.54%; /* 400/650 */
}
}
<div id="container">
<div id="content">
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna. Nunc viverra imperdiet enim. Fusce est.</p>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna. Nunc viverra imperdiet enim. Fusce est.</p>
</div>
</div>

#container{
background-image:url("http://i.imgur.com/u4xZlez.png");
background-size:100%;
background-position: 0 -110px;
height:400px;
display:block;
position:relative;
width:100%;
max-width:650px;
border:1px solid red;
background-repeat:no-repeat;
}
#content{
position:absolute;
top:10%;
display:block;
background-color:white;
width:60%;
margin-left:10%;
border:1px solid black;
}
#media(max-width:650px){
#content{
bottom:0px;
top:auto;
width:100%;
margin-left:0px;
}
}
<div id="container">
<div id="content">
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna. Nunc viverra imperdiet enim. Fusce est.</p>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna. Nunc viverra imperdiet enim. Fusce est.</p>
</div>
</div>
Try using the background-position CSS property, which accepts px as well as % measurements. You will need to put it in a media query so it only applies on screens > 650px but I will leave that for you to do.

Here it is...
But image is no longer background.
#container {
overflow:hidden; /* if desired */
height:400px;
display:block;
position:relative;
width:100%;
max-width:650px;
border:1px solid red;
}
#content {
position:absolute;
top:10%;
display:block;
background-color:white;
width:60%;
margin-left:10%;
border:1px solid black;
}
#media(max-width:650px){
#content {
bottom:0px;
top:auto;
width:100%;
margin-left:0px;
}
/* This... */
#content img {
width:100%;
position:absolute;
bottom:100%;
}
}
<div id="container">
<div id="content">
<img src="http://i.imgur.com/u4xZlez.png" />
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna. Nunc viverra imperdiet enim. Fusce est.</p>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna. Nunc viverra imperdiet enim. Fusce est.</p>
</div>
</div>

Related

CSS not applying to footer <p> element [duplicate]

This question already has answers here:
CSS margin terror; Margin adds space outside parent element [duplicate]
(7 answers)
CSS Property Border-Color Not Working
(6 answers)
Closed 9 months ago.
I am having some trouble understanding why margin: 1rem is not applying to my footer element. When I modify the size, only the text content in the <p> for the article div and aside element are modified. There is no margin between the footer text and the background color on the top and bottom, only on the left and right. Could anyone tell me what's causing this? Thanks
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<style>
body {
background-color: pink;
}
section {
background-color: lightgray;
max-width: 1000px;
box-sizing: border-box;
}
.article {
background-color: lightyellow;
width: 70%;
float: left;
margin: 0;
}
aside {
background-color: lightgreen;
float: right;
float: none;
overflow: hidden;
margin: 0;
}
footer {
clear: both;
background-color: aqua;
display: block;
border: black 10px;
box-sizing: border-box;
}
p {
margin: 1rem;
}
</style>
</head>
<body>
<section>
<div class="article">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. In ac
eleifend ex, vitae bibendum tortor. Sed rutrum, orci quis venenatis
congue, justo orci volutpat justo, semper vestibulum mauris est mattis
mi. Duis tincidunt enim congue elit egestas, ut ultrices purus
vulputate. Curabitur gravida tellus vel ornare convallis. Nunc
</p>
</div>
<aside>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec auctor
aliquam massa. Pellentesque maximus tortor ac est ultricies, id
sodales ligula vehicula. Fusce dignissim risus ligula, a feugiat augue
</p>
</aside>
<footer>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam
malesuada dolor quis ante tempus, eget posuere massa egestas. Integer
feugiat tellus nibh. Vestibulum pellentesque quam eu hendrerit porta.
Suspendisse sagittis eros vitae urna convallis, sit amet venenati
</p>
</footer>
</section>
</body>
</html>
The margin is applied - your problem is just that you have declared a 10px border without declaring a border-style, so essentially it looks like the p-element's margin is overflowing, because there is an invisible border of 10px. Apply a border-style and you will see the margin:
body {
background-color: pink;
}
section {
background-color: lightgray;
max-width: 1000px;
box-sizing: border-box;
}
.article {
background-color: lightyellow;
width: 70%;
float: left;
margin: 0;
}
aside {
background-color: lightgreen;
float: right;
float: none;
overflow: hidden;
margin: 0;
}
footer {
background-color: aqua;
display: block;
border: black solid 10px;
box-sizing: border-box;
}
p {
margin: 1rem;
}
<section>
<div class="article">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. In ac eleifend ex, vitae bibendum tortor. Sed rutrum, orci quis venenatis congue, justo orci volutpat justo, semper vestibulum mauris est mattis mi. Duis tincidunt enim congue elit egestas, ut ultrices
purus vulputate. Curabitur gravida tellus vel ornare convallis. Nunc
</p>
</div>
<aside>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec auctor aliquam massa. Pellentesque maximus tortor ac est ultricies, id sodales ligula vehicula. Fusce dignissim risus ligula, a feugiat augue
</p>
</aside>
<footer>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam malesuada dolor quis ante tempus, eget posuere massa egestas. Integer feugiat tellus nibh. Vestibulum pellentesque quam eu hendrerit porta. Suspendisse sagittis eros vitae urna convallis,
sit amet venenati
</p>
</footer>
</section>
I believe that is margin collapse.
I can see the intent to set a black border on the parent element footer in the first place. But that probably isn't working, and border: black 10px solid; would do the trick. And the margin collapse would no longer occur in this case.
Please read following page to learn about the margin collapse.
What is Margin Collapse in CSS? And How to Avoid It
And, as the other answers pointed out, perhaps it is padding, not margin, that suits your purpose.
You need to give padding to footer
Changes I made
In BODY tag
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
IN FOOTER
padding: 2%;
You apply box-sizing then i think you like to use padding. I refactor your css a little bit.
body {
background-color: pink;
}
section {
background-color: lightgray;
max-width: 1000px;
box-sizing: border-box;
}
.article {
background-color: lightyellow;
width: 70%;
float: left;
margin: 0;
}
aside {
background-color: lightgreen;
float: right;
float: none;
overflow: hidden;
margin: 0;
}
footer {
background-color: aqua;
box-sizing: border-box;
padding: 10px;
}
p {
margin: 1rem;
}
<section>
<div class="article">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. In ac
eleifend ex, vitae bibendum tortor. Sed rutrum, orci quis venenatis
congue, justo orci volutpat justo, semper vestibulum mauris est mattis
mi. Duis tincidunt enim congue elit egestas, ut ultrices purus
vulputate. Curabitur gravida tellus vel ornare convallis. Nunc
</p>
</div>
<aside>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec auctor
aliquam massa. Pellentesque maximus tortor ac est ultricies, id
sodales ligula vehicula. Fusce dignissim risus ligula, a feugiat augue
</p>
</aside>
<footer>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam
malesuada dolor quis ante tempus, eget posuere massa egestas. Integer
feugiat tellus nibh. Vestibulum pellentesque quam eu hendrerit porta.
Suspendisse sagittis eros vitae urna convallis, sit amet venenati
</p>
</footer>
</section>

Overlapping image with footer - CSS

I have a design here that is part of a website footer.
https://ibb.co/mBRpx7Y
The website has a box kind of layout with a dark background. But in the footer, at the bottom right corner, there is an extra image pattern comes which will overlap the content section and also covert the background part.
Right now, I used CSS to achieve this by adding two background images. The image position is correct but the image pattern does not appear on the white container because it is not transparent. Is there any way to achieve this?
Here is the sample code of what I have right now:
body{
background-image: url(https://www.nicepng.com/png/full/10-102760_dot-background-png.png),url(https://images.unsplash.com/photo-1508615070457-7baeba4003ab?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1470&q=80) !important;
background-size: auto, cover!important;
background-position: right bottom, left top!important;
background-repeat: no-repeat!important;
background-attachment: scroll, scroll;
}
.container{
max-width: 1500px;
margin:0px auto;
}
.section1{
min-height:200px;
background: #b0f3ff;
padding:50px;
}
.section2{
min-height:150px;
background: #fff;
padding:50px;
}
<div class="container">
<div class="section1">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras et mi ante. Etiam gravida tincidunt magna vestibulum rutrum. Phasellus a libero vulputate, malesuada nunc non, efficitur leo. Fusce porttitor nisl in fringilla lacinia. Fusce a sem sagittis enim imperdiet pretium at a diam. Aliquam et lacus quis augue sodales venenatis eu id justo. Phasellus et blandit felis. Proin in purus lectus.
</div>
<div class="section2">
Terms and Conditions
Privacy Policy
©2021 All rights reserve
</div>
</div>
You can use pseudo element on body e.g.: body:after {}. Make it absolute to bottom right corner and add this dotted background to it instead of on body.
body {
background-image: url(https://images.unsplash.com/photo-1508615070457-7baeba4003ab?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1470&q=80);
background-size: cover;
background-position: left top;
background-repeat: no-repeat;
background-attachment: scroll;
position: relative;
margin: 0;
}
body:after {
content: '';
background-image: url(https://www.nicepng.com/png/full/10-102760_dot-background-png.png);
width: 380px;
height: 384px;
position: absolute;
bottom: 0;
right: 0;
}
.container {
max-width: 1500px;
margin: 0px auto;
}
.section1 {
min-height: 200px;
background: #b0f3ff;
padding: 50px;
}
.section2 {
min-height: 150px;
background: #fff;
padding: 50px;
}
<div class="container">
<div class="section1">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras et mi ante. Etiam gravida tincidunt magna vestibulum rutrum. Phasellus a libero vulputate, malesuada nunc non, efficitur leo. Fusce porttitor nisl in fringilla lacinia. Fusce a sem sagittis enim imperdiet pretium at a diam. Aliquam et lacus quis augue sodales venenatis eu id justo. Phasellus et blandit felis. Proin in purus lectus.
</div>
<div class="section2">
Terms and Conditions
Privacy Policy
©2021 All rights reserve
</div>
</div>
Is this the kind of effect you're looking for?
.container{
max-width: 1500px;
margin:0px auto;
position: relative;
}
.section1{
min-height:200px;
background: #b0f3ff;
padding:50px;
}
.section2{
min-height:150px;
background: #fff;
padding:50px;
}
.section3 {
background-image: url(https://www.nicepng.com/png/full/10-102760_dot-background-png.png),url(https://images.unsplash.com/photo-1508615070457-7baeba4003ab?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1470&q=80) !important;
background-size: auto, cover!important;
background-position: right bottom, left top!important;
background-repeat: no-repeat!important;
background-attachment: scroll, scroll;
width : 100%;
height : 100%;
z-index : 2;
opacity:0.5;
bottom:0;
top:0;
position: absolute;
}
<div class="container">
<div class="section3" ></div>
<div class="section1">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras et mi ante. Etiam gravida tincidunt magna vestibulum rutrum. Phasellus a libero vulputate, malesuada nunc non, efficitur leo. Fusce porttitor nisl in fringilla lacinia. Fusce a sem sagittis enim imperdiet pretium at a diam. Aliquam et lacus quis augue sodales venenatis eu id justo. Phasellus et blandit felis. Proin in purus lectus.
</div>
<div class="section2">
Terms and Conditions
Privacy Policy
©2021 All rights reserve
</div>
</div>

How to ignore a middle <div>?

I want to create a sticky background which starts to stick after the header is scrolled out of frame
so far this is my progress
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.header
{
width:100%;
background-color:black;
height:100px;
}
div.sticky {
position: -webkit-sticky;
position:sticky;
width:100%;
top: 0;
z-index: -1;
}
#contain
{
width: 50%;
background-color: yellow;
margin: auto;
padding-left: 100px;
padding-right: 100px;
height: 1000px;
clear:both;
}
</style>
</head>
<body>
<div class="header"></div>
<div class="sticky"><img src="https://images.all-free-download.com/images/graphicthumb/small_mouse_macro_515329.jpg" style="background-size: cover; flex-shrink: 0;min-width: 100%;min-height: 100%; "></div>
<div id="contain">
<h2>Scroll </h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus imperdiet, nulla et dictum interdum, nisi lorem egestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas nisl est, ultrices nec congue eget, auctor vitae massa. Fusce luctus vestibulum augue ut aliquet. Mauris ante ligula, facilisis sed ornare eu, lobortis in odio. Praesent convallis urna a lacus interdum ut
</p>
</div>
</body>
</html>
but the yellow div is below the image, whereas I need it to start below the black header
so is there anyway to "ignore" the middle image wrapper <div>?
(I'm a beginner so please provide the simplest possible solution)
Make its height 0:
.header {
background-color: black;
height: 100px;
}
div.sticky {
position: sticky;
top: 0;
z-index: -1;
height:0;
}
div.sticky img {
width: 100%;
}
#contain {
width: 50%;
background-color: yellow;
margin: auto;
padding:10px 100px;
height: 1000px;
}
<div class="header"></div>
<div class="sticky"><img src="https://images.all-free-download.com/images/graphicthumb/small_mouse_macro_515329.jpg"></div>
<div id="contain">
<h2>Scroll </h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus imperdiet, nulla et dictum interdum, nisi lorem egestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas nisl est, ultrices nec congue eget, auctor vitae massa. Fusce luctus
vestibulum augue ut aliquet. Mauris ante ligula, facilisis sed ornare eu, lobortis in odio. Praesent convallis urna a lacus interdum ut
</p>
</div>
Or consider a hack using float and shape-outside:
.header {
background-color: black;
height: 100px;
}
div.sticky {
position: sticky;
top: 0;
z-index: -1;
float:left;
width: 100%;
shape-outside:inset(50%);
}
div.sticky img {
width: 100%;
}
#contain {
background-color: yellow;
margin:0 20%;
padding:10px 100px;
box-sizing:border-box;
height: 1000px;
display:inline-block;
}
<div class="header"></div>
<div class="sticky"><img src="https://images.all-free-download.com/images/graphicthumb/small_mouse_macro_515329.jpg"></div>
<div id="contain">
<h2>Scroll </h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus imperdiet, nulla et dictum interdum, nisi lorem egestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas nisl est, ultrices nec congue eget, auctor vitae massa. Fusce luctus
vestibulum augue ut aliquet. Mauris ante ligula, facilisis sed ornare eu, lobortis in odio. Praesent convallis urna a lacus interdum ut
</p>
</div>

When page resizes content boxes aren't centred anymore

The content boxes look good when they're full size, but when the page shrinks they are no longer centred. I've tried messing around with a bunch of things but can't for the life of me figure out what's wrong.
I've only been learning to code for a few weeks to please be kind lol, however if you have any off topic tips they are also appreciated
* {margin: 0; padding: 0}
main {
margin: 35px 50px;
padding: 0px 15px;
}
.mainContent {
display:block;
height:auto;
width: 60%;
float:left;
background-color: rgba(29,1,52,1);
padding: 30px;
color: #01b3ff;
text-align:center;
}
.newsHolder {
display: block;
height:auto;
width: 30%;
float:right;
background-color: rgba(29,1,52,1);
padding: 20px 15px;
color: #01b3ff;
}
.newsHolder img {
float:left;
padding: 15px;
}
#media screen and (min-width: 1141px) {
.mainContent {
margin-right: 10px;
}
}
#media screen and (max-width: 1140px) {
.mainContent {
width: 100%;
margin-bottom: 40px;
clear:both;
}
.newsHolder {
width:100%;
clear:both;
}
}
<body>
<main>
<div class="mainContent">
<h1> Welcome to my website! </h1><br>
<br>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus quis laoreet eros. Sed tristique felis quis turpis aliquet, sed hendrerit massa tincidunt. Phasellus consequat finibus lectus id euismod. Sed orci nunc, tempus id condimentum vitae, pharetra ultrices nibh. Quisque erat ipsum, aliquam vitae pretium ac, consequat at nisi. Pellentesque vehicula, neque ac varius aliquam, lectus nisi sodales ante, bibendum lobortis justo metus nec sem. Duis a imperdiet lectus, ut interdum enim. Quisque eu commodo elit. Mauris massa lorem, ullamcorper luctus diam quis, lacinia lobortis arcu. Vestibulum facilisis feugiat nisl, eget tempus erat tempor sed.
</p>
</div>
<div class="newsHolder">
<h1 style="text-align: center;"> BREAKING NEWS </h1><br>
<br>
<img src="images\angryluke.png">
<h2> Lorem ipsum </h2>
<p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus quis laoreet eros. </p>
</div>
</main>
</body>
You have to read more about box-sizing, just add box-sizing: border-box; to all elements and it will fix your problem.
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
main {
margin: 35px 50px;
padding: 0px 15px;
}
.mainContent {
display:block;
height:auto;
width: 60%;
float:left;
background-color: rgba(29,1,52,1);
padding: 30px;
color: #01b3ff;
text-align:center;
}
.newsHolder {
display: block;
height:auto;
width: 30%;
float:right;
background-color: rgba(29,1,52,1);
padding: 20px 15px;
color: #01b3ff;
}
.newsHolder img {
float:left;
padding: 15px;
}
#media screen and (min-width: 1141px) {
.mainContent {
margin-right: 10px;
}
}
#media screen and (max-width: 1140px) {
.mainContent {
width: 100%;
margin-bottom: 40px;
clear:both;
}
.newsHolder {
width:100%;
clear:both;
}
}
<body>
<main>
<div class="mainContent">
<h1> Welcome to my website! </h1><br>
<br>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus quis laoreet eros. Sed tristique felis quis turpis aliquet, sed hendrerit massa tincidunt. Phasellus consequat finibus lectus id euismod. Sed orci nunc, tempus id condimentum vitae, pharetra
ultrices nibh. Quisque erat ipsum, aliquam vitae pretium ac, consequat at nisi. Pellentesque vehicula, neque ac varius aliquam, lectus nisi sodales ante, bibendum lobortis justo metus nec sem. Duis a imperdiet lectus, ut interdum enim.
Quisque eu commodo elit. Mauris massa lorem, ullamcorper luctus diam quis, lacinia lobortis arcu. Vestibulum facilisis feugiat nisl, eget tempus erat tempor sed.
</p>
</div>
<div class="newsHolder">
<h1 style="text-align: center;"> BREAKING NEWS </h1><br>
<br>
<img src="images\angryluke.png">
<h2> Lorem ipsum </h2>
<p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus quis laoreet eros. </p>
</div>
</main>
</body>
Here is a link where you could know more about it: https://developer.mozilla.org/en-US/docs/Web/CSS/box-sizing

vary box height based on the text length inside

I would like to wrap the content of varying length text within a box. With the below code, the width of the box is adjusted for smaller length text. But the height doesn't vary and text is not wrapped inside the box.
.chatbox {
border: 1px solid black;
border-radius: 3.5em/5em;
padding: 2%;
max-width: 60%;
float: left;
height: auto;
white-space: nowrap;
}
<div class="chatbox">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed justo arcu, aliquet quis interdum sed, molestie iaculis turpis. Morbi rutrum molestie mauris id gravida. Curabitur libero tortor, tincidunt at facilisis vitae, euismod id urna. Proin sit amet
facilisis est. Vivamus id rutrum eros, in tempus mauris. Nunc nec velit tempus, varius neque sit amet, varius mi. Nullam ullamcorper lacus arcu, eu commodo magna consectetur sit amet.
</div>
Try this
.chatbox{
border:1px solid black;
border-radius:3.5em/5em;
padding:2em;
max-width:60%;
float:left;
}
You don't need height:auto and word-wrap, which create the problem. I changed the padding to be compatible with border-radius.
Change white-space:nowrap to white-space:normal , hope this will help you
.chatbox {
border: 1px solid black;
border-radius: 3.5em/5em;
padding: 2%;
max-width: 60%;
float: left;
height: auto;
white-space: normal;
}
<div class="chatbox">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed justo arcu, aliquet quis interdum sed, molestie iaculis turpis. Morbi rutrum molestie mauris id gravida. Curabitur libero tortor, tincidunt at facilisis vitae, euismod id urna. Proin sit amet
facilisis est. Vivamus id rutrum eros, in tempus mauris. Nunc nec velit tempus, varius neque sit amet, varius mi. Nullam ullamcorper lacus arcu, eu commodo magna consectetur sit amet.
</div>
just replace your css with below css:
.chatbox{
border:1px solid black;
border-radius:3.5em/5em;
padding:2%;
max-width:60%;
float:left;
height:auto;
word-wrap: break-word;
}
Just remove white-space: nowrap it works this way.
.chatbox{
border:1px solid black;
border-radius:3.5em/5em;
padding:2%;
max-width:60%;
height:auto;
float: left;
}