How do I get my Divs to become responsive and stack ontop of one another?

Viewed 46

I am not allowed to use bootstrap for this.

Here is my HTML

<section class="blog">
        <div class="wrapper">
            <h2>Blog</h2>
                <div class="container">
                    <div class="sub-container">
                        <div class="top-sub-container">
                            <div class="box box-1">
                                <img src="myimage.jpg" />
                                <div class="box-text">
                                    <p>DATE</p>
                                    <h3>Title</h3>
                                        <p>Lorem ipsum</p>
                                            <button class="button">Read More</button>
                            </div>
                        </div>
                        <div class="box box-2">
                            <img src="myimage.jpg" />
                                <div class="box-text">
                                    <p>Date</p>
                                    <h3>Title</h3>
                                        <p>Lorem ipsum dolor</p>
                                            <button class="button">Read More</button>
                            </div>
                        </div>
                    </div>
                    <div class="box box-3">
                        <img src="myimage.jpg" />
                            <div class="box-text">
                                <p>Date</p>
                                <h3>Title</h3>
                                    <p>Lorem ipsum dolor sit</p>
                                        <button class="button">Read More</button>
                            </div>
                    </div>
</div>
                <div class="box box-4">
                    <img src="myimage.jpg" />
                    <div class="box-text4">
                        <h3>About Me</h3>
                            <p>Lorem ipsum dolor.</p>
                    </div>
                </div>
</div>

And then here is my CSS:

.container {
    display: flex;
    gap: 3.5rem;
    height: 30rem;
    width: 30rem;
    padding-left: 175px;
    padding-top: 50px;
    /* background: pink; */
    
}

.top-sub-container {
    flex: 1 1 auto;
    display: flex;
    gap: 3.5rem;
}

.sub-container {
    flex: 1 1 auto;
    display: flex;
    gap: 3.5rem;
    flex-direction: column;
}

.box {
    display: flex;
    flex: 1 1 auto;
    flex-direction: column;
    /* padding: 25px; */
}

.post-text {
    padding: 10px;
    background-color: white;
    color: #D64C31;
    width: 250px;
    height: 250px;
}

.box-text4 {
    background-color: #7C7D98;
    padding: 20px;
    color: white;
}


.box-text {
    padding: 20px;
    color: white;
    background-color: #4A455C
}

When I shrink my browser window, the content stays put instead of narrowing/stacking on top of each other.

If you need to see more of my code, please let me know. I'm very confused. I also can not center my code inside the wrapper so I used "padding-left: 175px". I think this is incorrect. I tried to utilize "justify-content: center;" but it did not work. What am I doing wrong?

2 Answers

I reproduced a minimal working example of this issue and then made changes to make it stack. I used the flex-wrap: wrap property on the top container, however, if you wanted to change the flex-direction when on a certain viewport, then you will need to add a CSS Media query (shown below).

You can find the example I made at this codepen.

If you wanted to use a media query, then the CSS for that would look something like:

@media screen and (max-width: 768px) {
    .container {
        flex-direction: column;
    }
}

Edit: I looked through your question history, and see you wanted to achieve a particular layout, my minimal example doesn't account for this, however, it'll be fairly easy to change to account for that

    .container {
      display: flex;
      flex-wrap:wrap;
      gap:3rem;

     justify-content: center;
     align-items: center;
    }
    
    .box-text {
        padding: 1rem; /*use rem and em for responsive css and adaptive sizing*/
        color: white;
        background-color: #4A455C
    }
    
    .box:last-child .box-text{
        background-color: #7C7D98;
        color: white;
    }
    <section class="blog">
      <div class="wrapper">
        <h2>Blog</h2>
        <div class="container">
          <div class="box box-1">
            <img src="myimage.jpg" />
            <div class="box-text">
              <p>DATE</p>
              <h3>Title</h3>
              <p>Lorem ipsum</p>
              <button class="button">Read More</button>
            </div>
          </div>
          <div class="box box-2">
            <img src="myimage.jpg" />
            <div class="box-text">
              <p>Date</p>
              <h3>Title</h3>
              <p>Lorem ipsum dolor</p>
              <button class="button">Read More</button>
            </div>
          </div>
          <div class="box box-3">
            <img src="myimage.jpg" />
            <div class="box-text">
              <p>Date</p>
              <h3>Title</h3>
              <p>Lorem ipsum dolor sit</p>
              <button class="button">Read More</button>
            </div>
          </div>
          <div class="box box-4">
            <img src="myimage.jpg" />
            <div class="box-text">
              <h3>About Me</h3>
              <p>Lorem ipsum dolor.</p>
            </div>
          </div>
        </div>
      </div>
    </section>

Related