skip to Main Content

I tried writing some code for a webpage and it looked good on desktop view but I noticed that in mobile view, the right half of the page is whitespace. To fix this, I have tried to make the parent elements width ‘100%’ and the child element width to ‘auto’, as you can see from the code below.

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

li, a, button, input    {
    font-family: "Helvetica", sans-serif;
    text-decoration: none;
    font-size: 18px;
    color: azure;
    font-weight: 500;
}

header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20px 7%;
    background-color: cadetblue;
    width: 100%;
}

.logo   {
    cursor: pointer;
    width: 70px;
    height: 70px;
    margin-right: 100px;
}

.nav    {
    list-style: none;
}

.nav li {
    display: inline-block;
    padding: 0 20px;
}

.nav li a {
    transition: all 0.35s ease 0s;
}

.nav li a:hover     {
    color: greenyellow;
}

button  {
    background-color: greenyellow;
    color: azure;
    transition: all 0.3s ease 0s;
    padding: 10px 25px;
    border: none;
    border-radius: 50px;
    cursor: pointer;
}

button:hover    {
    background-color: darkseagreen;
    opacity: 0.8;
}

.box    {
    margin-left: 100px;
    margin-right: 20px;
    height: 40px;
    width: auto;
    padding: 10px 20px;
    background-color: azure;
    border-radius: 30px;
    box-shadow: 0 10px 25px rgba(112, 128, 144, 0.3);
    color: black;
}


.box input  {
    width: 0;
    outline: none;
    border: none;
    font-weight: 500;
    transition: all 0.3s ease 0s;
    background: transparent;
}

h1  {
    color: greenyellow;
    font-family: Helvetica, Arial, sans-serif;
    font-size: 150px;
    text-align: center;
    margin: 150px auto;
    width: auto;
}

p   {
    font-family: Helvetica, Arial, sans-serif;
    font-size: 40px;
    text-align: center;
    margin-bottom: 100px;
}

main    {
    background-color: blanchedalmond;
    padding: 100px;
    width: 100%;
}
<header>
    <img class="logo" src="logo.svg" alt="logo">
    <nav>
        <ul class="nav">
            <li><a href="#">Home</a></li>
            <li><a href="#">Services</a></li>
            <li><a href="#">Project</a></li>
        </ul>
    </nav>
    <input class="box" type="text" placeholder="search">
    <a href="#"><button>Search</button></a>
</header>
<main>
    <h1>Hello World</h1>
    <p>Our app will help Road desing engineers to manage traffic. 
        This app will use AI technology to plan roads and traffic lights, 
        so you won't have to wait in traffic.
    </p>
</main>

2

Answers


  1. CSS utilizes @media queries to adapt styles for various screen sizes. By defining the same CSS classes with different properties within these queries, you can tailor your styling specifically for smaller screens.

    @media screen and (max-width: 768px) {
        header {
            flex-direction: column;
            padding: 20px;
        }
        .nav li {
            padding: 10px 5px;
        }
        .box, .logo {
            margin-left: 0;
            margin-right: 0;
        }
        main {
            padding: 50px 20px;
        }
        h1 {
            font-size: 50px; /* Adjust based on your preference */
            margin: 50px auto;
        }
        p {
            font-size: 20px;
        }
    }
    
    Login or Signup to reply.
  2. Use media query css to fix responsive screen issues, have a look changed code.

    *   {
        box-sizing: border-box;
        margin: 0;
        padding: 0;
    }
    
    li, a, button, input    {
        font-family: "Helvetica", sans-serif;
        text-decoration: none;
        font-size: 18px;
        color: azure;
        font-weight: 500;
    }
    
    header {
        display: flex;
        justify-content: space-between;
        flex-wrap:wrap;
        align-items: center;
        padding: 20px 7%;
        background-color: cadetblue;
        width: 100%;
            align-items: center;
    }
    
    .logo   {
        cursor: pointer;
        width: 70px;
        height: 70px;
        border: 1px solid #fff;
    }
    
    .nav    {
        list-style: none;
    }
    
    .nav li {
        display: inline-block;
        padding: 0 20px;
    }
    
    .nav li a {
        transition: all 0.35s ease 0s;
    }
    
    .nav li a:hover     {
        color: greenyellow;
    }
    
    button  {
        background-color: greenyellow;
        color: azure;
        transition: all 0.3s ease 0s;
        padding: 10px 25px;
        border: none;
        border-radius: 50px;
        cursor: pointer;
    }
    
    button:hover    {
        background-color: darkseagreen;
        opacity: 0.8;
    }
    
    .search    {    
        padding: 10px 20px;    
    }
    
    
    .search input  {
        padding: 10px;
        background-color: azure;
        border-radius: 30px;
        box-shadow: 0 10px 25px rgba(112, 128, 144, 0.3);
        color: black;
        margin-right:10px;
    }
    
    h1  {
        color: greenyellow;
        font-family: Helvetica, Arial, sans-serif;
        font-size: 150px;
        text-align: center;
        margin: 150px auto;
        width: auto;
    }
    
    p   {
        font-family: Helvetica, Arial, sans-serif;
        font-size: 40px;
        text-align: center;
        margin-bottom: 100px;
    }
    
    main    {
        background-color: blanchedalmond;
        padding: 100px;
        width: 100%;
    }
    
    @media only screen and (max-width: 767px) {
    main {padding: 20px;}
    h1{font-size: 50px; margin: 20px 0;}
    p{font-size: 16px;}
    }
    <header>
        <img class="logo" src="logo.svg" alt="logo">
        <nav>
            <ul class="nav">
                <li><a href="#">Home</a></li>
                <li><a href="#">Services</a></li>
                <li><a href="#">Project</a></li>
            </ul>
        </nav>
        <div class="search">
        <input class="box" type="text" placeholder="search">
        <button>Search</button>
        </div>
    </header>
    <main>
        <h1>Hello World</h1>
        <p>Our app will help Road desing engineers to manage traffic. 
            This app will use AI technology to plan roads and traffic lights, 
            so you won't have to wait in traffic.
        </p>
    </main>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search