Is there a way to create this layout using CSS? - css

I have two sections: a hero and an about section. There is a container that I am using but for the about section, the image on the left will go outside of the container line, while the content on the right will stay within the container. I have attached a photo of what I am trying to accomplish.
.container {
max-width: 1280px;
margin: 0 auto;
}
#about {
width: 100%;
display: grid;
grid-template-columns: repeat(2, 1fr);
}
.img img {
width: 100%;
height: 100%;
object-fit: cover;
display: block;
}
.content {
background-color: #1A969F;
padding: 2rem 8rem 2rem 3rem;
}
<section id="about">
<div class="img">
<img src="about-img.jpg" alt="">
</div>
<div class="content">
<h2>Heading</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sapiente, vitae! Quam, ipsam quidem autem numquam ipsa atque debitis similique pariatur aliquid, quibusdam labore mollitia dolorum temporibus et magni, commodi blanditiis.</p>
</div>
</section>

from comments:
yes, with grid you are missing 2 columns. – G-Cyrillus
So we would use two extra grid columns for the about section? Would we still be able to use the container class for the other sections and navbar? - Webdev1995
i made you an example below of the idea of 2 extra columns (but on the parent ;) ) that #about can span through – G-Cyrillus
extra column shoul be used from .container where you did set the max-width inside the grid via minmax() instead max-width on .container.
here is an idea:
.container {
margin: 0 auto;
display: grid;
grid-template-columns: 1fr minmax(auto, 1280px) 1fr
}
section {
grid-column: 2;
background: #bee
}
#about {
grid-column: 1 /span 3;
width: 100%;
display: grid;
grid-template-columns: repeat(2, 1fr);
}
.img img {
width: 100%;
height: 100%;
object-fit: cover;
display: block;
background: #eee
}
.content {
background-color: #1A969F;
padding: 2rem 8rem 2rem 3rem;
}
<div class="container">
<section>whatever of 1280px max-width</section>
<section id="about">
<div class="img">
<img src="about-img.jpg" alt="about illustration">
</div>
<div class="content">
<h2>Heading</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sapiente, vitae! Quam, ipsam quidem autem numquam ipsa atque debitis similique pariatur aliquid, quibusdam labore mollitia dolorum temporibus et magni, commodi blanditiis.</p>
</div>
</section>
<section>whatever of 1280px max-width</section>
</div>

Related

Setting width of div element in a flex container [duplicate]

This question already has an answer here:
Why is a flex item limited to parent size?
(1 answer)
Closed 4 months ago.
So I have a flex container with 2 children. And I want to assign a fixed width to the first child. But If I set width: 200px, for some reason it does not work.
Here's my code:
.carousel {
background-color: #087f5b;
color: white;
padding: 32px;
width: 800px;
display: flex;
gap: 88px;
position: relative;
}
.img-container {
width: 200px;
height: 200px;
border: 1px solid;
}
<div class="carousel">
<div class="img-container"> </div>
<blockquote>
<p class="testimonial-text">
Lorem ipsum dolor sit amet consectetur adipisicing elit. Ipsum,
sapiente labore! Accusantium, voluptas omnis dicta facere, porro
mollitia minus ad ut debitis consequuntur.
</p>
</blockquote>
</div>
However, if I use min-width instead of just width, it works alright.
I also found out that if I delete some text from the blockquote, then it works fine.
.carousel {
background-color: #087f5b;
color: white;
padding: 32px;
width: 800px;
display: flex;
gap: 88px;
position: relative;
}
.img-container {
width: 200px;
height: 200px;
border: 1px solid;
}
.carousel blockquote {
flex: 1;
}
<div class="carousel">
<div class="img-container"> </div>
<blockquote>
<p class="testimonial-text">
Lorem ipsum dolor sit amet consectetur adipisicing elit. Ipsum,
sapiente labore! Accusantium, voluptas omnis dicta facere, porro
mollitia minus ad ut debitis consequuntur.
</p>
</blockquote>
</div>

cant float two items inside of a grid element

I have a grid with a bunch of items. One of the items contains an image and some text. I want to float the text next to the image but can't get it to work...
See code below:
<div id="blog-post-item" class="blog-post">
<h1>Some amazing blogpost title about something super cool!</h1>
<div class="blog-post-content">
<div class="content-flex-container clearfix">
<img src="Data/Content/food_post5.jpg">
<p> Lorem ipsum dolor sit amet consectetur adipisicing elit. Ratione, repellendus natus! Corporis, architecto laboriosam natus doloremque corrupti sunt a incidunt! Eligendi soluta beatae neque reprehenderit ipsa perspiciatis architecto accusantium incidunt.</p>
</div>
</div>
</div>
body {
display: grid;
background: tomato;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: minmax(max-content, max-content);
column-gap: 20px;
row-gap: 20px;
}
.blog-post {
background: #fff;
border: 5px solid #fff;
border-radius: 10px;
display: grid;
grid-template-rows: minmax(auto, max-content) 1fr;
height: 100%;
grid-column: 2/-1;
width: 100%;
}
.blog-post h1 {
grid-row: 1;
grid-column: 1/-1;
}
.blog-post-content {
grid-row: 2;
}
.content-flex-container {
height: 100%;
width: 100%;
display: block;
}
.content-flex-container img {
height: 100%;
float: left;
}
.content-flex-container p {
float: left;
}
But this is the result i get:
as you can see the text is below the image not to the right of the image in the big white open space..
If more information is needed please let me know so i can clarify.

Is there something in flexbox so that when the children of the container overflow the container they go to another line?

I have a parent container:
.flex-container {
padding: 0;
margin: 0;
border: 2px solid silver;
display: flex;
}
inside it I have 2 divs, which have the same size and are always on the same line.
.flex-item {
background: tomato;
height: 100px;
min-width:300px;
margin: 10px;
}
each child div has these properties.
the min-width of each div is 300px, so when the screen is very small and the divs overflow in width to the parent container, I would like the div that exceeds the width of the parent to be displayed below the other.
I think the solution would be done with a mediaquery, but maybe there is another more optimal solution from flexbox and that is why I have asked this question(for example with flex-wrap I achieve that the div that exceeds the width of the parent, is put underneath, but it is no longer on the same line when the resolution is large). Thank you very much
.flex-container {
padding: 0;
margin: 0;
border: 2px solid silver;
display: flex;
}
this is my full code:
<div class="flex-container ">
<div class="flex-item">Lorem ipsum dolor sit amet consectetur adipisicing elit. Aliquam expedita excepturi dolor iusto et architecto, repellat fugit quisquam reprehenderit recusandae alias ut, magni quis cumque earum at laborum, consectetur voluptas.</div>
<div class="flex-item">Lorem ipsum dolor sit amet consectetur adipisicing elit. Aliquam expedita excepturi dolor iusto et architecto, repellat fugit quisquam reprehenderit recusandae alias ut, magni quis cumque earum at laborum, consectetur voluptas.</div>
</div>
https://jsfiddle.net/2x8yLhu0/
.flex-container {
padding: 0;
margin: 0;
border: 2px solid silver;
display: flex;
flex-wrap: wrap;
}
.flex-item {
background: tomato;
height: 100px;
min-width:300px;
margin: 10px;
flex: 0;
}
You can add the flex-wrap:wrap in the container class.
but in this case, you should add it in flex-item class;
Add flex-wrap:wrap; to .flex-container.
Then you probably want to add a max-width to .flex-item or else the boxes will forever be on top of each other as they expand the full width.
.flex-container {
padding: 0;
margin: 0;
border: 2px solid silver;
display: flex;
flex-wrap:wrap; /* Add this so they go on top of each other */
justify-content: space-evenly; /* Center boxes, evenly spaced out in div */
}
.flex-item {
background: tomato;
height: 100px;
min-width:300px;
max-width:500px; /* Add this so they don't stretch full width */
margin: 10px;
}

Positioning divs inside wrapper, one of them must have y-scrollbar when content overflows

I'm trying to accomplish the following layout, but I'm having trouble with the description box height definition. I'm trying to avoid javascript.
Right now I have both the wrapper and the title bar working as they should, the title and description divs being nested inside the wrapper div:
#wrapper{
position: fixed;
right: 0;
width: calc(100vw - 1.51 * 95vh - 5vh);
top: calc(40px + 2.5vh + 2.5vh);
height: calc(100vh - 40px - 40px);
}
#title{
width: 100%;
height: auto;
top: calc(2.5vh + 40px + 2.5vh + 5vh);
}
What about the description div? Can anyone point me in the right direction?
Thanks!
Flexbox can do that.
.container {
height: 100vh;
display: flex;
flex-direction: column;
}
header {
background: lightblue;
}
.content {
flex: 1;
overflow-y: auto;
background: orange;
}
.spacer {
height: 2000px;
/* for demo purposes */
}
<div class="container">
<header>Lorem ipsum dolor sit amet consectetur adipisicing elit. Quos tenetur magnam labore laboriosam dolores, fugit ipsum quibusdam, aperiam totam itaque soluta debitis cumque provident repudiandae.</header>
<div class="content">
<div class="spacer"></div>
</div>
</div>

Responsive layout in which left column may move to the right

Say we have this responsive design with 3 columns (all 3 are dynamic contents so we don't know their heights)
For desktop:
For tablet (left column moves to the right)
What's the best way we can achieve this? (I don't mind using flexbox or other modern css specs)
You can make the elements make float: left on desktop with with 1/3 width each, then on tablet/mobile give them 50% width and make 1 and 3 float: right.
HTML:
<div class="b1">
1
</div>
<div class="b2">
2
</div>
<div class="b3">
3
</div>
CSS:
div {
float: left;
width: calc(100%/3);
box-sizing: border-box;
}
#media (max-width: 600px) {
div {
width: 50%;
}
.b1,
.b3 {
float: right;
}
}
Working JSFiddle
div{
border: 1px solid #333;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
padding: 20px;
float: left;
}
.div1{
background : yellow;
width: 25%;
}
.div2{
background : red;
width: 50%;
min-height: 400px;
}
.div3{
background : purple;
width: 25%;
}
#media (max-width: 768px){
.div1, .div3{
clear: right;
float: right;
width: 30%;
}
.div2{
width: 70%;
}
}
<div class="div1">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Illo reiciendis sapiente cumque optio incidunt, dolore impedit officiis ut tempore. Pariatur commodi perspiciatis ducimus laudantium atque sed eveniet explicabo animi ipsam!</div>
<div class="div2">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Illo reiciendis sapiente cumque optio incidunt, dolore impedit officiis ut tempore. Pariatur commodi perspiciatis ducimus laudantium atque sed eveniet explicabo animi ipsam!</div>
<div class="div3">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Illo reiciendis sapiente cumque optio incidunt, dolore impedit officiis ut tempore. Pariatur commodi perspiciatis ducimus laudantium atque sed eveniet explicabo animi ipsam!</div>
https://jsfiddle.net/yuywh5gv/
Try this, it might help. You can edit CSS as your wish
HTML code:
<div class="wrapper">
<div class="left"> 1 </div>
<div class="middle">2 </div>
<div class="right"> 3 </div>
</div>
CSS code:
.wrapper div {float:left;margin-right:.5%;text-align:center;}
.wrapper div:last-child {margin-right:0;}
.left {background: yellow; width:33%}
.middle {background: green; width:33%}
.right {background: blue; width:33%}
# Phone only screen and (max-width:320px) # Tablet only screen and (min-width:321px) and (max-width:768px) # Desktop only screen and (min-width:769px)
<div class="b1 col" >1</div>
<div class="b2 con">2</div>
<div class="b3 col">3</div>
add style bellow ::
div {
border: 2px solid black;
padding: 20px;
float: left;
box-sizing: border-box;
}
.con{
width: 66.66%;
}
div.col {
width: 16.5%;
}
.b1 {
height: 50px;
border-color: #ee8;
}
.b2 {
height: 200px;
border-color: #fcc;
}
.b3 {
height: 80px;
border-color: #caf;
}
#media screen and (min-width:321px) and (max-width:768px) {
div.col {
width: 33.33%;
}
.b1,
.b3 {
float: right;
}
}
DEMO

Resources