'Why my menu is merging with the input field ? I want it to be responsive
I am new to HTML CSS, can anyone tell me what am I doing wrong?
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
:root {
--pink: #ff6161;
--white: #fff;
--black: #292828;
--font: "Roboto", sans-serif;
}
header {
min-height: 100px;
max-width: 100%;
background-color: var(--pink);
display: flex;
justify-content: center;
align-items: center;
}
.menu {
max-width: 10rem;
display: flex;
justify-content: center;
align-items: center;
}
.menu-items {
list-style-type: none;
padding-right: 30px;
font-family: var(--font);
font-size: 20px;
}
.menu-items a {
text-decoration: none;
color: var(--black);
}
.search-bar {
width: 700px;
}
.inp {
width: 100%;
height: 2rem;
}
<header>
<div class="search-bar">
<input class="inp" type="text" />
</div>
<ul class="menu">
<i class="fa-solid fa-house"></i>
<li class="menu-items"><a href="#"> Home</a></li>
<i class="fa-solid fa-store"></i>
<li class="menu-items"><a href="#">Store</a></li>
<i class="fa-solid fa-headset"></i>
<li class="menu-items"><a href="#">Support</a></li>
</ul>
</header>
Solution 1:[1]
It's because you're using align items: center and justify-content: center in your header at the same time.
You're basically forcing items inside header to move to the center.
Solution 2:[2]
In your CSS, when you are modifying the header, you are putting all elements inside of it in the same position. Something you could to fix this is, give the header a relative position, and then manipulate each element inside the header individually.
Solution 3:[3]
Change your display for menu to contents:
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
:root {
--pink: #ff6161;
--white: #fff;
--black: #292828;
--font: "Roboto", sans-serif;
}
header {
min-height: 100px;
max-width: 100%;
background-color: var(--pink);
display: flex;
justify-content: center;
align-items: center;
}
.menu {
max-width: 10rem;
display: contents;
justify-content: center;
align-items: center;
}
.menu-items {
list-style-type: none;
padding-right: 30px;
font-family: var(--font);
font-size: 20px;
}
.menu-items a {
text-decoration: none;
color: var(--black);
}
.search-bar {
width: 700px;
}
.inp {
width: 100%;
height: 2rem;
}
<header>
<div class="search-bar">
<input class="inp" type="text" />
</div>
<ul class="menu">
<i class="fa-solid fa-house"></i>
<li class="menu-items"><a href="#"> Home</a></li>
<i class="fa-solid fa-store"></i>
<li class="menu-items"><a href="#">Store</a></li>
<i class="fa-solid fa-headset"></i>
<li class="menu-items"><a href="#">Support</a></li>
</ul>
</header>
Solution 4:[4]
The sum of the text widths of the menu Home Store Support will be greater than max-width: 10rem;
.menu {
display: flex;
justify-content: center;
align-items: center;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Lee Taylor |
| Solution 2 | lj_tang |
| Solution 3 | Lajos Arpad |
| Solution 4 | DrumRobot |
