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

Time:02-05

I am not allowed to use bootstrap for this.

Here is my HTML
<section >
        <div >
            <h2>Blog</h2>
                <div >
                    <div >
                        <div >
                            <div >
                                <img src="myimage.jpg" />
                                <div >
                                    <p>DATE</p>
                                    <h3>Title</h3>
                                        <p>Lorem ipsum</p>
                                            <button >Read More</button>
                            </div>
                        </div>
                        <div >
                            <img src="myimage.jpg" />
                                <div >
                                    <p>Date</p>
                                    <h3>Title</h3>
                                        <p>Lorem ipsum dolor</p>
                                            <button >Read More</button>
                            </div>
                        </div>
                    </div>
                    <div >
                        <img src="myimage.jpg" />
                            <div >
                                <p>Date</p>
                                <h3>Title</h3>
                                    <p>Lorem ipsum dolor sit</p>
                                        <button >Read More</button>
                            </div>
                    </div>
</div>
                <div >
                    <img src="myimage.jpg" />
                    <div >
                        <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't center my code inside the wrapper so I used "padding-left: 175px" and I think this is incorrect. I tried to utilize justify-content: center; but it did not work. What am I doing wrong?

CodePudding user response:

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

CodePudding user response:

Html code will look like this

<section >
  <div >
    <h2>Blog</h2>
    <div >
      <div >
        <img src="myimage.jpg" />
        <div >
          <p>DATE</p>
          <h3>Title</h3>
          <p>Lorem ipsum</p>
          <button >Read More</button>
        </div>
      </div>
      <div >
        <img src="myimage.jpg" />
        <div >
          <p>Date</p>
          <h3>Title</h3>
          <p>Lorem ipsum dolor</p>
          <button >Read More</button>
        </div>
      </div>
      <div >
        <img src="myimage.jpg" />
        <div >
          <p>Date</p>
          <h3>Title</h3>
          <p>Lorem ipsum dolor sit</p>
          <button >Read More</button>
        </div>
      </div>
      <div >
        <img src="myimage.jpg" />
        <div >
          <h3>About Me</h3>
          <p>Lorem ipsum dolor.</p>
        </div>
      </div>
    </div>
  </div>
</section>

CSS code will look like this

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

.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;
}
  •  Tags:  
  • Related