* {
box-sizing: border-box;
}
h1 {
text-align: center;
margin-bottom: 20px;
}
div {
width: 100%;
}
p {
width: 90%;
height: 200%;
margin: 20px;
color: black;
padding: 10px;
}
#media (min-width: 992px) {
.col-lg-1,
.col-lg-2,
.col-lg-3,
.col-lg-4,
.col-lg-5,
.col-lg-6,
.col-lg-7,
.col-lg-8,
.col-lg-9,
.col-lg-10,
.col-lg-11,
.col-lg-12 {
float: left;
position: relative;
border: 1px solid black;
margin: auto;
}
.col-lg-1 {
width: 8.33%;
}
.col-lg-2 {
width: 16.66%;
}
.col-lg-3 {
width: 25%;
}
.col-lg-4 {
width: 33.33%;
}
.col-lg-5 {
width: 41.66%;
}
.col-lg-6 {
width: 50%;
}
.col-lg-7 {
width: 58.33%;
}
.col-lg-8 {
width: 66.66%;
}
.col-lg-9 {
width: 74.99%;
}
.col-lg-10 {
width: 83.33%;
}
.col-lg-11 {
width: 91.66%;
}
.col-lg-12 {
width: 100%;
}
}
#heading {
position: absolute;
left: 358px;
top: 0;
background-color: #A52A2A;
width: 20.2%;
height: 12.2%;
text-align: center;
border: 1px solid black;
}
</style>
<h1>Our Menu</h1>
<div class="row">
<div class="col-lg-4">
<section id="heading">Heading</section>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dicta beatae voluptatibus veniam placeat iure unde assumenda porro neque voluptate esse sit magnam facilis labore odit, provident a ea! Nulla, minima.Lorem ipsum dolor sit amet, consectetur
adipisicing elit. Eius nemo vitae, cupiditate odio magnam reprehenderit esse eum reiciendis repellendus incidunt sequi! Autem, laudantium, accusamus. Doloribus tempora alias minima laborum, provident!</p>
</div>
<div class="col-lg-4">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dicta beatae voluptatibus veniam placeat iure unde assumenda porro neque voluptate esse sit magnam facilis labore odit, provident a ea! Nulla, minima.Lorem ipsum dolor sit amet, consectetur
adipisicing elit. Eius nemo vitae, cupiditate odio magnam reprehenderit esse eum reiciendis repellendus incidunt sequi! Autem, laudantium, accusamus. Doloribus tempora alias minima laborum, provident!</p>
</div>
<div class="col-lg-4">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dicta beatae voluptatibus veniam placeat iure unde assumenda porro neque voluptate esse sit magnam facilis labore odit, provident a ea! Nulla, minima.Lorem ipsum dolor sit amet, consectetur
adipisicing elit. Eius nemo vitae, cupiditate odio magnam reprehenderit esse eum reiciendis repellendus incidunt sequi! Autem, laudantium, accusamus. Doloribus tempora alias minima laborum, provident!</p>
</div>
</div>
I am trying to create a responsive webpapge:
I have 3 divs , I need each of them to have a heading at the top right corner. What I want to do is when I increase the width of the browser from 992px to full screen, the div size and position should change accordingly along with its headings.
I also need gaps between the divs, I have used relative and absolute positioning but that doesn't seem to work.
With my code, the heading doesnt resize when I change the browser width plus it also gets out of the div (see picture).
Please help!
P.s I have added heading on only the first div for checking purposes.
you have to set the parent div position to relative position:relative because the element with the absolute position always fallows the first parent that has the relative position
see your example here
You don't need to use position for the heading. You can use float. And one more thing you make the ID heading to class. Coz ID use for only unique identifier. SO make a class. Check your css section on media query I use for the gap .col-lg-4 { width: 32.33%; margin:5px; }
Just an instant answer for your question. Thanks, Any Question ask me in comment.
* {
box-sizing: border-box;
}
h1 {
text-align: center;
margin-bottom: 20px;
}
p {
width: 90%;
margin: 20px;
color: black;
padding: 10px;
}
#media (min-width: 992px) {
.col-lg-1,
.col-lg-2,
.col-lg-3,
.col-lg-4,
.col-lg-5,
.col-lg-6,
.col-lg-7,
.col-lg-8,
.col-lg-9,
.col-lg-10,
.col-lg-11,
.col-lg-12 {
float: left;
border: 1px solid black;
}
.col-lg-1 {
width: 8.33%;
}
.col-lg-2 {
width: 16.66%;
}
.col-lg-3 {
width: 25%;
}
.col-lg-4 {
width: 32.33%;
margin:5px;
}
.col-lg-5 {
width: 41.66%;
}
.col-lg-6 {
width: 50%;
}
.col-lg-7 {
width: 58.33%;
}
.col-lg-8 {
width: 66.66%;
}
.col-lg-9 {
width: 74.99%;
}
.col-lg-10 {
width: 83.33%;
}
.col-lg-11 {
width: 91.66%;
}
.col-lg-12 {
width: 100%;
}
}
.heading {
float:right;
background-color: #A52A2A;
width: 20.2%;
height: 12.2%;
text-align: center;
border: 1px solid black;
}
<h1>Our Menu</h1>
<div class="row">
<div class="col-lg-4">
<section class="heading">Heading</section>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dicta beatae voluptatibus veniam placeat iure unde assumenda porro neque voluptate esse sit magnam facilis labore odit, provident a ea! Nulla, minima.Lorem ipsum dolor sit amet, consectetur
adipisicing elit. Eius nemo vitae, cupiditate odio magnam reprehenderit esse eum reiciendis repellendus incidunt sequi! Autem, laudantium, accusamus. Doloribus tempora alias minima laborum, provident!</p>
</div>
<div class="col-lg-4">
<section class="heading">Heading</section>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dicta beatae voluptatibus veniam placeat iure unde assumenda porro neque voluptate esse sit magnam facilis labore odit, provident a ea! Nulla, minima.Lorem ipsum dolor sit amet, consectetur
adipisicing elit. Eius nemo vitae, cupiditate odio magnam reprehenderit esse eum reiciendis repellendus incidunt sequi! Autem, laudantium, accusamus. Doloribus tempora alias minima laborum, provident!</p>
</div>
<div class="col-lg-4">
<section class="heading">Heading</section>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dicta beatae voluptatibus veniam placeat iure unde assumenda porro neque voluptate esse sit magnam facilis labore odit, provident a ea! Nulla, minima.Lorem ipsum dolor sit amet, consectetur
adipisicing elit. Eius nemo vitae, cupiditate odio magnam reprehenderit esse eum reiciendis repellendus incidunt sequi! Autem, laudantium, accusamus. Doloribus tempora alias minima laborum, provident!</p>
</div>
</div>
Ok, some points here about your header:
It's a header, so use <h1, h2, h3...> tags.
If it has absolute position, and you said to it to be 358px from left, it will be positioned exactly 358px from the left, so that is why is get out of the div.
To put the header on the right, I would use float: right instead.
Of course, adding floating to the header, you have to add floating to the text below.
If you use a width of 20%, it will not occupe the total (if you want the header to fit the entire parent's width, you have to use 100%.
Use media queries with #media (min-width: 992px) to make those changes you said to bigger resolutions.
Proposed solution
Inside media query, you only have to override those elements that you want to change (in your case, just the width).
The change we want is that when screen is bigger than 992px, the header's width be 100% so:
Here is the general CSS rule:
#heading {
background-color: #A52A2A;
border: 1px solid black;
float: right;
height: 12.2%;
position: relative;
text-align: center;
width: 20%;
}
And here we have an especific rule...
#media (min-width: 992px) {
#heading {
width: 100%;
}
}
Imagine you want to change also the background-color, all you have to do is to add the property inside the rule in the media query....
Related
I'm trying to do an assignment, I couldn't solve it for 2 days.
flexbox, grid, bootstrap will not be used
in the first case,
Up to 992 pixels, 3 boxes will appear on the evenly spaced centered page.
in the second case,
In the tablet view (between 768px and 991px, inclusively), the first 2 sections should be in the first row and be of equal size. The 3rd section should be in the second row and take up the entire row by itself.
in the third case,
In the mobile view (equal to or less than 767px), each section should take up the entire row.
I couldn't get 3 boxes to appear centered on mobile.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Two Column Design</title>
<style>
* {
box-sizing: border-box;
}
#k1 {
background-color: blueviolet;
}
#k2 {
background-color: rgb(58, 245, 98);
}
#k3 {
background-color: rgb(179, 38, 38);
}
.column {
float: left;
width: 31%;
border: 2px solid black;
margin: 15px;
}
/* Clear floats after the columns */
.row {
clear: both;
}
.baslik1 {
font-family: Georgia, 'Times New Roman', Times, serif;
font-weight: bold;
background-color: palevioletred;
border: 1px solid black;
padding: 5px 20px 5px 20px;
display: inline;
float: right;
}
.baslik2 {
font-family: Georgia, 'Times New Roman', Times, serif;
font-weight: bold;
background-color: red;
border: 1px solid black;
padding: 5px 20px 5px 20px;
display: inline;
float: right;
}
.baslik3 {
font-family: Georgia, 'Times New Roman', Times, serif;
font-weight: bold;
background-color: rgb(217, 243, 166);
border: 1px solid black;
padding: 5px 20px 5px 20px;
display: inline;
float: right;
}
p {
clear: both;
padding: 10px;
}
#media only screen and (max-width: 992px) {
#k1 {
width: 29%;
}
#k2 {
width: 29%;
}
#k3 {
width: 29%;
}
}
#media (min-width: 768px) and (max-width: 991px) {
#k1 {
width: 46%;
}
#k2 {
width: 46%;
}
#k3 {
width: 95%;
}
}
</style>
</head>
<body>
<h1>Three Column Design</h1>
<div class="row">
<div id="k1" class="column">
<div class="baslik1">Chicken</div>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quia distinctio aliquid cupiditate perferendis fuga, sit quasi alias vero sunt non, ratione earum dolores nihil! Consequuntur pariatur totam incidunt soluta expedita.r sit amet, consectetur adipisicing elit. Eius nemo vitae, cupiditate odio magnam reprehenderit esse eum reiciendis repellendus incidunt sequi! Autem, laudantium, accusamus. Doloribus tempora alias minima</p></div>
<div id="k2" class="column">
<div class="baslik2">Beef</div>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quia distinctio aliquid cupiditate perferendis fuga, sit quasi alias vero sunt non, ratione earum dolores nihil! Consequuntur pariatur totam incidunt soluta expedita.r sit amet, consectetur adipisicing elit. Eius nemo vitae, cupiditate odio magnam reprehenderit esse eum reiciendis repellendus incidunt sequi! Autem, laudantium, accusamus. Doloribus tempora alias minima</p></div>
<div id="k3" class="column">
<div class="baslik3">Sushi</div>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quia distinctio aliquid cupiditate perferendis fuga, sit quasi alias vero sunt non, ratione earum dolores nihil! Consequuntur pariatur totam incidunt soluta expedita.r sit amet, consectetur adipisicing elit. Eius nemo vitae, cupiditate odio magnam reprehenderit esse eum reiciendis repellendus incidunt sequi! Autem, laudantium, accusamus. Doloribus tempora alias minima</p></div>
</div>
</body>
</html>
Try this:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Two Column Design</title>
<style>
* {
box-sizing: border-box;
}
#k1 {
background-color: blueviolet;
}
#k2 {
background-color: rgb(58, 245, 98);
}
#k3 {
background-color: rgb(179, 38, 38);
}
.column {
float: left;
width: 31%;
border: 2px solid black;
margin: 15px;
}
/* Clear floats after the columns */
.row {
clear: both;
}
.baslik1 {
font-family: Georgia, 'Times New Roman', Times, serif;
font-weight: bold;
background-color: palevioletred;
border: 1px solid black;
padding: 5px 20px 5px 20px;
display: inline;
float: right;
}
.baslik2 {
font-family: Georgia, 'Times New Roman', Times, serif;
font-weight: bold;
background-color: red;
border: 1px solid black;
padding: 5px 20px 5px 20px;
display: inline;
float: right;
}
.baslik3 {
font-family: Georgia, 'Times New Roman', Times, serif;
font-weight: bold;
background-color: rgb(217, 243, 166);
border: 1px solid black;
padding: 5px 20px 5px 20px;
display: inline;
float: right;
}
p {
clear: both;
padding: 10px;
}
#k1, #k2, #k3 {
width: 98%;
margin: 1%;
}
#media (min-width: 768px) and (max-width: 991px) {
#k1, #k2 {
width: 46%;
margin: 2%;
}
#k3 {
width: 96%;
margin: 2%;
}
}
#media only screen and (min-width: 991px) {
#k1, #k2, #k3 {
width: 29%;
margin: 2.16%;
}
}
</style>
</head>
<body>
<h1>Three Column Design</h1>
<div class="row">
<div id="k1" class="column">
<div class="baslik1">Chicken</div>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quia distinctio aliquid cupiditate perferendis fuga, sit quasi alias vero sunt non, ratione earum dolores nihil! Consequuntur pariatur totam incidunt soluta expedita.r sit amet, consectetur adipisicing elit. Eius nemo vitae, cupiditate odio magnam reprehenderit esse eum reiciendis repellendus incidunt sequi! Autem, laudantium, accusamus. Doloribus tempora alias minima</p></div>
<div id="k2" class="column">
<div class="baslik2">Beef</div>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quia distinctio aliquid cupiditate perferendis fuga, sit quasi alias vero sunt non, ratione earum dolores nihil! Consequuntur pariatur totam incidunt soluta expedita.r sit amet, consectetur adipisicing elit. Eius nemo vitae, cupiditate odio magnam reprehenderit esse eum reiciendis repellendus incidunt sequi! Autem, laudantium, accusamus. Doloribus tempora alias minima</p></div>
<div id="k3" class="column">
<div class="baslik3">Sushi</div>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quia distinctio aliquid cupiditate perferendis fuga, sit quasi alias vero sunt non, ratione earum dolores nihil! Consequuntur pariatur totam incidunt soluta expedita.r sit amet, consectetur adipisicing elit. Eius nemo vitae, cupiditate odio magnam reprehenderit esse eum reiciendis repellendus incidunt sequi! Autem, laudantium, accusamus. Doloribus tempora alias minima</p></div>
</div>
</body>
</html>
#media (max-width: 767px) {
#k1, #k2, #k3 {
width: 95%;
}
Here's another version with no need for floats or the .row div as we've used display: inline-block. It's also an example of how to use calc to set widths. I've also tidied your CSS up a bit.
Good luck with your assignment!
*,
*::before,
*::after {
box-sizing: border-box;
}
#k1 {
background-color: blueviolet;
}
#k2 {
background-color: rgb(58, 245, 98);
}
#k3 {
background-color: rgb(179, 38, 38);
}
.column {
display: inline-block;
width: calc(31% - 15px);
border: 2px solid black;
margin: 15px;
}
.baslik {
font-family: Georgia, 'Times New Roman', Times, serif;
font-weight: bold;
border: 1px solid black;
padding: 5px 20px 5px 20px;
display: inline;
float: right;
}
.baslik1 {
background-color: palevioletred;
}
.baslik2 {
background-color: red;
}
.baslik3 {
background-color: rgb(217, 243, 166);
}
p {
clear: both;
padding: 10px;
}
#media only screen and (min-width: 768px) and (max-width: 991px) {
#k1,
#k2 {
width: calc(50% - 33px);
}
#k3 {
width: auto;
}
}
#media only screen and (max-width: 767px) {
#k1,
#k2,
#k3 {
width: auto;
}
}
<h1>Three Column Design</h1>
<div id="k1" class="column">
<div class="baslik baslik1">Chicken</div>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quia distinctio aliquid cupiditate perferendis fuga, sit quasi alias vero sunt non, ratione earum dolores nihil! Consequuntur pariatur totam incidunt soluta expedita.r sit amet, consectetur adipisicing
elit. Eius nemo vitae, cupiditate odio magnam reprehenderit esse eum reiciendis repellendus incidunt sequi! Autem, laudantium, accusamus. Doloribus tempora alias minima</p>
</div>
<div id="k2" class="column">
<div class="baslik baslik2">Beef</div>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quia distinctio aliquid cupiditate perferendis fuga, sit quasi alias vero sunt non, ratione earum dolores nihil! Consequuntur pariatur totam incidunt soluta expedita.r sit amet, consectetur adipisicing
elit. Eius nemo vitae, cupiditate odio magnam reprehenderit esse eum reiciendis repellendus incidunt sequi! Autem, laudantium, accusamus. Doloribus tempora alias minima</p>
</div>
<div id="k3" class="column">
<div class="baslik baslik3">Sushi</div>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quia distinctio aliquid cupiditate perferendis fuga, sit quasi alias vero sunt non, ratione earum dolores nihil! Consequuntur pariatur totam incidunt soluta expedita.r sit amet, consectetur adipisicing
elit. Eius nemo vitae, cupiditate odio magnam reprehenderit esse eum reiciendis repellendus incidunt sequi! Autem, laudantium, accusamus. Doloribus tempora alias minima</p>
</div>
I am trying to make my image cover the whole div which is 50% of the parent div. I used object-fit:cover but it's still not working. The problem is as I reduce the width of window the image also shortens.
In (1) the width is full
The 2nd picture is 900px window size.
* {
box-sizing: border-box;
}
body {
background: yellow;
}
section {
background: red;
widtth: 100%;
min-height: 700px;
}
article {
background: green;
width: 50%;
min-height: 700px;
padding: 100px;
float: left;
}
picture {
float: left;
width: 50%;
min-height: 700px;
}
img {
width: 100%;
height: 100%;
object-fit: cover;
}
section::after {
content: "";
clear: both;
display: block;
}
<body>
<section class="about">
<article>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Consequatur expedita laudantium, ea eos fugiat dolores laboriosam voluptas illo deleniti pariatur ratione nobis perferendis in consectetur rerum ipsa debitis quis numquam! Lorem ipsum dolor
sit amet consectetur adipisicing elit. Veniam unde placeat ratione magnam tempore velit accusamus ipsam quaerat aspernatur maiores?</p>
</article>
<picture>
<img src="https://www.loveinartsz.com/wp-content/uploads/2021/03/b04803919effa1914ae6754d8bee30fb.jpg" alt="">
</picture>
</section>
</body>
I have linked the code pen link below for reference.
https://codepen.io/YASH_KR18/pen/LYObNrB
Simple solution, add display:flex to its parent element which is picture will make it work.
* {
box-sizing: border-box;
}
body {
background: yellow;
}
section {
background: red;
width: 100%;
min-height: 700px;
}
article {
background: green;
width: 50%;
min-height: 700px;
padding: 100px;
float: left;
}
picture {
float: left;
width: 50%;
min-height: 700px;
display:flex
}
img {
max-width: 100%;
max-height: 100%;
object-fit: cover;
}
section::after {
content: "";
clear: both;
display: block;
}
<body>
<section class="about">
<article>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Consequatur expedita laudantium, ea eos fugiat dolores laboriosam voluptas illo deleniti pariatur ratione nobis perferendis in consectetur rerum ipsa debitis quis numquam! Lorem ipsum dolor
sit amet consectetur adipisicing elit. Veniam unde placeat ratione magnam tempore velit accusamus ipsam quaerat aspernatur maiores?</p>
</article>
<picture>
<img src="https://www.loveinartsz.com/wp-content/uploads/2021/03/b04803919effa1914ae6754d8bee30fb.jpg" alt="">
</picture>
</section>
</body>
Because you are using the extra picture tag for that
I am avoiding that tag because I see no need of that in this whole code. If you want to use the picture tag vary badly we have to think of something else. Here take a look:
HTML
<body>
<section class="about">
<article>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Consequatur expedita laudantium, ea eos fugiat dolores laboriosam voluptas illo deleniti pariatur ratione nobis perferendis in consectetur rerum ipsa debitis quis numquam! Lorem ipsum dolor sit amet consectetur adipisicing elit. Veniam unde placeat ratione magnam tempore velit accusamus ipsam quaerat aspernatur maiores?</p>
</article>
<img src="https://www.loveinartsz.com/wp-content/uploads/2021/03/b04803919effa1914ae6754d8bee30fb.jpg" alt="">
</section>
</body>
CSS
*{
box-sizing:border-box;
}
body{
background:yellow;
}
section{
background:red;
widtth:100%;
min-height:700px;
}
article{
background:green;
width:50%;
min-height:700px;
padding:100px;
float:left;
}
img{
float:left;
width:50%;
min-height:700px;
}
section::after{
content:"";
clear:both;
display:block;
}
Floating elements (removing them from the normal flow of the html structure) is the source of a lot of problems. If you would use a flexbox or gridbox for the parent element then there would be no need to float the children to position them next to eachother and then I believe your problem is solved. Well that is if the snippet below does what you're after at least! If not I might not understand your question yet.
/* Colors for visibilty */
body{ background-color: yellow;}
article{ background-color: green; padding: 100px;}
picture{ background-color: red; }
/* The problem fix*/
section
{
display: grid;
grid-template-columns: 50% 50%;
width: 80%;
max-width: 1100px;
min-height: 700px;
margin: auto;
}
picture img{
object-position: center;
object-fit: cover;
width: 100%;
height: 100%;
}
<section>
<article>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Consequatur expedita laudantium, ea eos fugiat dolores laboriosam voluptas illo deleniti pariatur ratione nobis perferendis in consectetur rerum ipsa debitis quis numquam! Lorem ipsum dolor sit amet consectetur adipisicing elit. Veniam unde placeat ratione magnam tempore velit accusamus ipsam quaerat aspernatur maiores?</p>
</article>
<picture>
<img src="https://www.loveinartsz.com/wp-content/uploads/2021/03/b04803919effa1914ae6754d8bee30fb.jpg" alt="">
</picture>
</section>
When we set a block level element height to 100% it will take the full height min-height: 100%; of the page or at least the full height of its parent, however what is strange for me as I'm currently learning CSS is that when I shrink the height of the browser window
Here is a few seconds video to show what I mean. Video
As you can already see from the video, when I make the height of the browser very short and then scroll down I see that the content section no longer takes the full height until I make the browser height a little bit longer, however the sidebar and the profile sections don't do that since their width are a less than the content section.
So the question is: How to make the div to keep its full height when the browser height shrinks and then scroll down as you can see from the video?
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html {
height: 90%;
}
body {
position: relative;
overflow-y: scroll;
height: 100%;
}
h2 {
margin-bottom: 2em;
text-align: center;
}
p {
text-align: center;
}
.sidebar {
position: absolute;
width: 15%;
left: 0;
top: 0;
background-color: darkblue;
color: white;
padding: 2em;
margin: 2em;
min-height: 100%;
}
.content {
position: absolute;
width: 60%;
left: calc(15% + 2em);
top: 0;
background-color: green;
color: white;
padding: 2em;
margin: 2em;
min-height: 100%;
}
.profile {
position: absolute;
width: 15%;
top: 0;
left: calc(75% + 4em);
padding: 2em;
margin: 2em;
background-color: firebrick;
color: black;
min-height: 100%;
}
button {
display: block;
margin-top: 2em;
}
<div class="sidebar">
<h2>Sidebar</h2>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Laboriosam, voluptatibus tenetur. Et facere provident voluptatem praesentium illum explicabo vel architecto eum repellat facilis. Eum repudiandae nobis ad aliquid aut. Ut.</p>
</div>
<div class="content">
<h2>Content</h2>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Laboriosam, voluptatibus tenetur. Et facere provident voluptatem praesentium illum explicabo vel architecto eum repellat facilis. Eum repudiandae nobis ad aliquid aut. Ut.</p>
</div>
<div class="profile">
<h2>Profile</h2>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Laboriosam, voluptatibus tenetur. Et facere provident voluptatem praesentium illum explicabo vel architecto eum repellat facilis. Eum repudiandae nobis ad aliquid aut. Ut.</p>
</div>
My code on codepen: Code
CSS-Grid solution
One easy way is to use CSS-Grid. I cut your code down to the part that is actually "usefull" and removed all unecessary or non-working code.
display: grid activates CSS-Grid
grid-template-columns: 15% auto 15% creates your 3 column layout while auto occupies the remaining space.
min-height: 100vh sizes the boxes to be at least as heigh as the viewport.
body {
display: grid;
grid-template-columns: 15% auto 15%;
grid-gap: 2em;
padding: 2em;
min-height: 100vh;
}
/* original CSS cut down */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
h2 {
margin-bottom: 2em;
text-align: center;
}
p {
text-align: center;
}
.sidebar,
.content,
.profile {
padding: 2em;
}
.sidebar {
background-color: darkblue;
color: white;
}
.content {
background-color: green;
color: white;
}
.profile {
background-color: firebrick;
color: black;
}
<div class="sidebar">
<h2>Sidebar</h2>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Laboriosam, voluptatibus tenetur. Et facere provident voluptatem praesentium illum explicabo vel architecto eum repellat facilis. Eum repudiandae nobis ad aliquid aut. Ut.</p>
</div>
<div class="content">
<h2>Content</h2>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Laboriosam, voluptatibus tenetur. Et facere provident voluptatem praesentium illum explicabo vel architecto eum repellat facilis. Eum repudiandae nobis ad aliquid aut. Ut.</p>
</div>
<div class="profile">
<h2>Profile</h2>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Laboriosam, voluptatibus tenetur. Et facere provident voluptatem praesentium illum explicabo vel architecto eum repellat facilis. Eum repudiandae nobis ad aliquid aut. Ut.</p>
</div>
Flexbox solution:
To use Flexbox you have to use display: flex. Same as the CSS-Grid solution you also give the body a min-height: 100vh to fill up at least the entire viewport.
To get your intended 3 column layout, you have to define your left and right side as width: 15%.
To have the middle column fill up the remaining space, you just need to add: flex-grow: 1;
body {
display: flex;
gap: 2em;
padding: 2em;
min-height: 100vh;
}
.sidebar,
.profile {
width: 15%;
}
.content {
flex-grow: 1;
}
/* original CSS cut down */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
h2 {
margin-bottom: 2em;
text-align: center;
}
p {
text-align: center;
}
.sidebar,
.content,
.profile {
padding: 2em;
}
.sidebar {
background-color: darkblue;
color: white;
}
.content {
background-color: green;
color: white;
}
.profile {
background-color: firebrick;
color: black;
}
<div class="sidebar">
<h2>Sidebar</h2>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Laboriosam, voluptatibus tenetur. Et facere provident voluptatem praesentium illum explicabo vel architecto eum repellat facilis. Eum repudiandae nobis ad aliquid aut. Ut.</p>
</div>
<div class="content">
<h2>Content</h2>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Laboriosam, voluptatibus tenetur. Et facere provident voluptatem praesentium illum explicabo vel architecto eum repellat facilis. Eum repudiandae nobis ad aliquid aut. Ut.</p>
</div>
<div class="profile">
<h2>Profile</h2>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Laboriosam, voluptatibus tenetur. Et facere provident voluptatem praesentium illum explicabo vel architecto eum repellat facilis. Eum repudiandae nobis ad aliquid aut. Ut.</p>
</div>
I'd like to create 2 columns inline. The first column has fixed width of 200px and second column should take up the rest of the screen. The second column should autoscroll on the x-axis. I've been trying to use flex but I'm very confused by the markup. Can someone please explain what I'm doing wrong.
Path: html
<section className="container">
<div className="container-name">1</div>
<div className="container-time">
<div className="element">
222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222999999
</div>
</div>
</section>
Path: CSS
.container {
flex: 0 0 auto;
}
.container-name {
width: 200px;
border: 1px dotted;
}
.container-time {
flex: 0 0 auto;
border: 1px solid;
}
.element {
display: flex;
overflow-x: auto;
}
You can get the desired output using:
max-width on the container
display: inline-flex; on the container
max-width on the container-time
.container {
max-width: 100%;
display: inline-flex;
}
.container-name {
width: 200px;
border: 1px solid red;
}
.container-time {
flex-grow: 1;
max-width: calc(100% - 200px);
border: 1px solid green;
}
.element {
display: flex;
overflow-x: auto;
}
<section class="container">
<div class="container-name">1</div>
<div class="container-time">
<div class="element">
222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222
</div>
</div>
</section>
Hope this solution will help you!
* {
margin: 0;
padding: 0;
outline: 0;
box-sizing: border-box;
}
.container {
position: relative;
width: 100%;
height: 100vh;
display: flex;
min-height: 100vh;
}
.container .split {
position: relative;
width: 100%;
height: 100%;
}
.container .split__left {
max-width: 400px;
background-color: coral;
}
.container .split__right {
padding: 2rem;
overflow-x: scroll;
background-color: pink;
}
.container .split__right p {
white-space: nowrap;
}
<div class="container">
<div class="split split__left"></div>
<div class="split split__right">
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Nihil repellat sapiente assumenda. Ex fugit at amet sed libero eaque. Debitis obcaecati aperiam hic possimus unde. Voluptatibus cumque illo dolores architecto.</p>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Nihil repellat sapiente assumenda. Ex fugit at amet sed libero eaque. Debitis obcaecati aperiam hic possimus unde. Voluptatibus cumque illo dolores architecto.</p>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Nihil repellat sapiente assumenda. Ex fugit at amet sed libero eaque. Debitis obcaecati aperiam hic possimus unde. Voluptatibus cumque illo dolores architecto.</p>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Nihil repellat sapiente assumenda. Ex fugit at amet sed libero eaque. Debitis obcaecati aperiam hic possimus unde. Voluptatibus cumque illo dolores architecto.</p>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Nihil repellat sapiente assumenda. Ex fugit at amet sed libero eaque. Debitis obcaecati aperiam hic possimus unde. Voluptatibus cumque illo dolores architecto.</p>
</div>
</div>
[NOTE] see this in bigger screen.
In the following code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Grid View</title>
<style>
* {
box-sizing: border-box;
}
html {
font-size: 1vw;
}
body {
padding: 0 0;
margin: 0;
background-color: violet;
font-size: 1.1rem;
}
.container {
width: 80%;
margin: 0 auto;
}
header {
background-color: rgb(133, 76, 76);
border: .01rem solid rgb(133, 76, 76);
margin-bottom: 4%;
position: fixed;
top: 0;
left: 10%;
right: 10%;
padding: 0;
}
header h1 {
text-align: center;
}
.row {
background-color: blueviolet;
margin-bottom: 4%;
}
.row::after {
content: "";
clear: both;
display: table;
}
[class*="col-"] {
float: left;
padding: 15px;
background-color: whitesmoke;
}
.col-1 {
width: 8.33%;
}
.col-2 {
width: 16.66%;
}
.col-3 {
width: 25%;
}
.col-4 {
width: 33.33%;
}
.col-5 {
width: 41.66%;
}
.col-6 {
width: 50%;
}
.col-7 {
width: 58.33%;
}
.col-8 {
width: 66.66%;
}
.col-9 {
width: 75%;
}
.col-10 {
width: 83.33%;
}
.col-11 {
width: 91.66%;
}
.col-12 {
width: 100%;
}
div#three div.col-3 {
width: 24%;
margin-right: 1.33%;
}
div#three div.col-3:last-of-type {
margin-right: 0;
}
#media only screen and (max-width: 780px) {
html {
font-size: 2.5vw;
}
body {
padding-top: 17%;
}
}
#media only screen and (max-width: 480px) {
html {
font-size: 3vw;
}
body {
padding-top: 17%;
}
}
</style>
</head>
<body>
<div class="container">
<div id="one" class="row">
<div class="col-12">
<p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Nesciunt cupiditate, ullam, illum magnam quidem ipsam laudantium dicta autem fugit cumque laboriosam et neque consequuntur beatae? Molestiae sed id ipsam distinctio molestias asperiores,
labore officia! Maxime dolor illo, beatae cumque rerum sint corrupti possimus sed labore.</p>
</div>
</div>
<div id="three" class="row">
<div class="col-3">
<p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Nesciunt cupiditate, ullam, illum magnam quidem ipsam laudantium dicta autem fugit cumque laboriosam et neque consequuntur beatae? Molestiae sed id ipsam distinctio molestias asperiores,
labore officia! Maxime dolor illo, beatae cumque rerum sint corrupti possimus sed labore.</p>
</div>
<div class="col-3">
<p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Nesciunt cupiditate, ullam, illum magnam quidem ipsam laudantium dicta autem fugit cumque laboriosam et neque consequuntur beatae? Molestiae sed id ipsam distinctio molestias asperiores,
labore officia! Maxime dolor illo, beatae cumque rerum sint corrupti possimus sed labore.</p>
</div>
<div class="col-3">
<p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Nesciunt cupiditate, ullam, illum magnam quidem ipsam laudantium dicta autem fugit cumque laboriosam et neque consequuntur beatae? Molestiae sed id ipsam distinctio molestias asperiores,
labore officia! Maxime dolor illo, beatae cumque rerum sint corrupti possimus sed labore.</p>
</div>
<div class="col-3">
<p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Nesciunt cupiditate, ullam, illum magnam quidem ipsam laudantium dicta autem fugit cumque laboriosam et neque consequuntur beatae? Molestiae sed id ipsam distinctio molestias asperiores,
labore officia! Maxime dolor illo, beatae cumque rerum sint corrupti possimus sed labore.</p>
</div>
</div>
</div>
</body>
</html>
The web page breaks at around 470px (checked through device toolbar using chrome inspect). This happens as the text inside the 4 floated divs in the second row is not responsive and gets out of the div at 470px. This is only happening when the text is inside a floated element, when I make the divs block element again, the text becomes responsive.
Can someone tell me why this happens ?
Hope this help you. Added below mentioned CSS
div#three div.col-3{
width: 49%;
margin: 0 0.5%;
margin-bottom: 5px;}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Grid View</title>
<style>
* {
box-sizing: border-box;
}
html {
font-size: 1vw;
}
body {
padding: 0 0;
margin: 0;
background-color: violet;
font-size: 1.1rem;
}
.container {
width: 80%;
margin: 0 auto;
}
header {
background-color: rgb(133, 76, 76);
border: .01rem solid rgb(133, 76, 76);
margin-bottom: 4%;
position: fixed;
top: 0;
left: 10%;
right: 10%;
padding: 0;
}
header h1 {
text-align: center;
}
.row {
background-color: blueviolet;
margin-bottom: 4%;
}
.row::after {
content: "";
clear: both;
display: table;
}
[class*="col-"] {
float: left;
padding: 15px;
background-color: whitesmoke;
}
.col-1 {
width: 8.33%;
}
.col-2 {
width: 16.66%;
}
.col-3 {
width: 25%;
}
.col-4 {
width: 33.33%;
}
.col-5 {
width: 41.66%;
}
.col-6 {
width: 50%;
}
.col-7 {
width: 58.33%;
}
.col-8 {
width: 66.66%;
}
.col-9 {
width: 75%;
}
.col-10 {
width: 83.33%;
}
.col-11 {
width: 91.66%;
}
.col-12 {
width: 100%;
}
div#three div.col-3 {
width: 24%;
margin-right: 1.33%;
}
div#three div.col-3:last-of-type {
margin-right: 0;
}
#media only screen and (max-width: 780px) {
html {
font-size: 2.5vw;
}
body {
padding-top: 17%;
}
div#three div.col-3 {
width: 49%;
margin: 0 0.5%;
margin-bottom: 5px;
}
}
#media only screen and (max-width: 480px) {
html {
font-size: 3vw;
}
body {
padding-top: 17%;
}
}
#media only screen and (max-width: 380px) {
div#three div.col-3 {
width: 99%;
margin: 0 0.5%;
margin-bottom: 5px;
}
}
</style>
</head>
<body>
<div class="container">
<div id="one" class="row">
<div class="col-12">
<p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Nesciunt cupiditate, ullam, illum magnam quidem ipsam laudantium dicta autem fugit cumque laboriosam et neque consequuntur beatae? Molestiae sed id ipsam distinctio molestias asperiores,
labore officia! Maxime dolor illo, beatae cumque rerum sint corrupti possimus sed labore.</p>
</div>
</div>
<div id="three" class="row">
<div class="col-3">
<p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Nesciunt cupiditate, ullam, illum magnam quidem ipsam laudantium dicta autem fugit cumque laboriosam et neque consequuntur beatae? Molestiae sed id ipsam distinctio molestias asperiores,
labore officia! Maxime dolor illo, beatae cumque rerum sint corrupti possimus sed labore.</p>
</div>
<div class="col-3">
<p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Nesciunt cupiditate, ullam, illum magnam quidem ipsam laudantium dicta autem fugit cumque laboriosam et neque consequuntur beatae? Molestiae sed id ipsam distinctio molestias asperiores,
labore officia! Maxime dolor illo, beatae cumque rerum sint corrupti possimus sed labore.</p>
</div>
<div class="col-3">
<p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Nesciunt cupiditate, ullam, illum magnam quidem ipsam laudantium dicta autem fugit cumque laboriosam et neque consequuntur beatae? Molestiae sed id ipsam distinctio molestias asperiores,
labore officia! Maxime dolor illo, beatae cumque rerum sint corrupti possimus sed labore.</p>
</div>
<div class="col-3">
<p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Nesciunt cupiditate, ullam, illum magnam quidem ipsam laudantium dicta autem fugit cumque laboriosam et neque consequuntur beatae? Molestiae sed id ipsam distinctio molestias asperiores,
labore officia! Maxime dolor illo, beatae cumque rerum sint corrupti possimus sed labore.</p>
</div>
</div>
</div>
</body>
</html>
A single CSS rule can fix this issue without altering breakpoints or existing source code -
div#three div.col-3{
overflow-wrap: break-word;
}
This does not take best practices in terms of readability for the end user into consideration but does fix the bug you're experiencing. Further tweaking with padding on .col-3 divs for mobile devices should help.
Changing the font-size to a 'vw' value of text inside floating elements resolves the issue. But i still can't figure out why.