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
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.
Use media query css to fix responsive screen issues, have a look changed code.