mitas1c
mitas1c

Reputation: 335

CSS - Element moving out of parent width

I am creating blog app, currently I am at the process of design, however I have come to a problem.

I have a div called PostsContainer where all posts would be located. I have a component called Post. This componenet is made out of 4 divs, Post -> Post Header, Post Body, Post Footer.

However when I apply margin-left to Post, the post jumps out of the PostsContainer width. Anybody knows how to fix this ?

I am using Next.Js and I have found problems with it

These are my Css modules. Globals.css

    @import url('https://fonts.googleapis.com/css2?family=Roboto:wght@100;400;700&display=swap');

:root{
    --blueColour: rgb(53, 144, 219);
    --blue: #1FA2F1;
    --blueLight: #9BD1F9;
    --buttonHoverBg: #d4edff;
    --textColor: rgb(36, 36, 36);
}

*{
    box-sizing: border-box;
    padding: 0;
    margin: 0;

    font-family: 'Roboto';
    font-weight: 100;

    text-decoration: none;
}

html, body{
    background-color: whitesmoke;
    font-weight: 300;
}


#__next{
    height: 100%;
    min-height: 100vh;

    display: flex;
    flex-direction: column;

    justify-content: center;
    align-items: center;
  
}


.pageContent{

    padding: 65px 0px 10px 0px;

    height: 100%;
    min-height: 100vh;
    width: 50%;
  

    border-right: 1px solid rgb(175, 175, 175);
    border-left: 1px solid rgb(175, 175, 175);

}

Post.css

.Post{
    height: 130px;
    width: 100%;


    border-bottom: 1px solid grey;


    margin-left: 25px;



}

.PostHeader{
    display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
}
.PostHeader img{
    width: 50px;
    height: 50px;

    background-color: whitesmoke;
    border-radius: 50%;
}

And Home.css

    .NewPostContainer{

    width: 100%;
    height: 150px;

    border-bottom: 1px solid grey;
    padding-bottom: 10px;
}

.NewPostFormContainer{
    width: 100%;
    height: 80px;

    display: flex;
    flex-direction: row;
    flex-wrap: nowrap;

    justify-content: space-between;

    padding: 10px 10px 10px 10px;
}

.NewPostFormContainer img{
    padding: 2px 2px 2px 2px;
    width: 50px;
    height: 50px;

    border-radius: 50%;
    background-color: whitesmoke;

}
.NewPostFormContainer textarea{
    margin-left: 10px;
    padding: 10px;
    width: 100%;

    height: 80px;
    resize: none;

    border: none;
    outline: none;
    border-radius: 10px;
}


.NewPostToolbar{
    height: 60px;
    margin-top: 10px;

    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
}

.NewPostToolbar button{
    height: 35px;
    width: 60px;
    
    margin-right: 15px;

    border: none;
    border-radius: 15px;

    font-size: 18px;
    font-weight: 500;
    text-align: center;

    color: whitesmoke;
    background-color: var(--blue);
}

.NewPostToolbar button:hover{
    background-color: var(--blueLight);
    cursor: pointer;
}


.PostsContainer{
    width: 100%;
    height: 100%;
}

This is my Index.tsx and Post.tsx

Index.tsx

    import Head from 'next/head'
import Image from 'next/image'
import Post from '../components/Post';
import styles from '../styles/HomePage.module.css'


export default function Home() {

    return (
        <>
            <Head>
                <title>Home page</title>
                <meta name="description" content="Generated by create next app" />
                <meta name="viewport" content="width=device-width, initial-scale=1" />
                <link rel="icon" href="/favicon.ico" />
            </Head>

            <div className='pageContent'>
                <div className={styles.NewPostContainer} id="NewPostContainer">
                    <div className={styles.NewPostFormContainer} id="textAreaContainer">
                        <Image src='/images/user_icon.png' width="512" height="512" alt='User profile image'/>
                        <textarea id='newPostForm' />
                    </div>
                    <div className={styles.NewPostToolbar}>
                        <p></p>
                        <button>Post</button>

                    </div>

                </div>
                <div className='PostsContainer'>
                    <Post/>
                    <Post/>
                    <Post/>
                    <Post/>
                    <Post/>

 
        
                </div>
                
            </div>
        </>
    )
}

Post.tsx

    import React from 'react'
import Image from 'next/image'
import styles from '../styles/Post.module.css'

function Post() {
  return (
    <div className={styles.Post}>
        <div className={styles.PostHeader}>
            <Image src='/images/user_icon.png' width="512" height="512" alt='User profile image'/>
            <div>
                <h1>username</h1>
                <h2>10:22</h2>
            </div>
        </div>

        <div className={styles.PostContent}>
            Lorem ipsum sudo lomen duro lomen laros pam saudlo lumeno ite
        </div>

        <div className={styles.PostFooter}>

        </div>

    </div>
  )
}

export default Post

enter image description here

Upvotes: 1

Views: 82

Answers (2)

JMM
JMM

Reputation: 13

Instead of using margin-left on your post, try using padding on its parent, post-container. This should lock your inner divs inside of the parent container:

 <div class="post-container">    
      <div class="post">
        <div class="post-header"></div>
        <div class="post-body"></div>
        <div class="post-footer"></div>
      </div>
    </div>
    
    <style> 
    .post-container {
       width: 400px;
       height: 600px;
       padding: 1rem;
     }

    .post {
      height:100%;
      }

    .post, 
    .post-header, 
    .post-body, 
    .post-footer {
      width: 100%;
     }
    
    .post-header,
    .post-footer {
      height: 20%;
     }

     .post-body {
       height: 60%;
      }
     </style>

Upvotes: 1

Cloud 8.5
Cloud 8.5

Reputation: 51

The width of each .Post is set to be 100% of the width of its container. This means that after pushing it to the right with margin-left: 25px;, some of it will be hanging off. Here are a couple of ways to fix this:

  1. Set the width of the post to be the width of the container minus the 25px of margin with a CSS calculation: width: calc(100% - 25px).
  2. Set overflow: hidden on your .PostsContainer to hide any content that bleeds over the edge.

Upvotes: 2

Related Questions