How To Align Logo And Menu Items In One Line?
Solution 1:
Use flex box for inline block elements. This will also give you a lot more control over the children elements and their layout.
I altered your HTML a little, adding the center menu buttons in a parent div and separating unordered list into two unordered lists. This way we can use flex to justify their content with space-between
placing them on separate ends of the parent elements block.
On the menu display: flex
, we also add a flex
value for each child element, the logo, the menu buttons parent div between and the login button. By placing these elements into their own parent divs, we can control the width using flex. To center the elements vertically, if our default axis for display flex is set to row, we can use the align-items
property. This will align the items in that parent elements border vertically. align-items: center
The logo and login buttons parents both having a flex value of 0, which means they will take up only the space they need to show their content. The middle element, .between having a flex of 1 will mean it will take up the rest of the parent menus width. This will allow us to place a flex display on the UL elements and we can then set them on separate sides of the remaining sections width using justify-content
on the UL elements parent element .between.
Snippit best if viewed in full page view as you define the width of your wrapper to 1200px.
body {
background-color: #ffffff;
margin: 0 auto;
}
#wrapper {
width: 1200px;
margin: 0 auto;
background-color: #CDCDCD;
}
.top-menu {
margin-top: 40px;
background-color: #95E659;
}
.logo {
flex: 0;
}
.logoimg {
width: 40px;
height: 40px;
}
.search-container {
flex: 0;
display: inline-flex;
align-items: center;
justify-content: center;
}
.menu {
display: flex;
justify-content: space-between;
align-items: center;
padding-right: 2rem;
}
.between {
display: flex;
justify-content: space-between;
flex: 1;
}
.menudivul.left {
display: flex;
justify-content: flex-start;
}
.menudivul.right {
display: flex;
justify-content: flex-end;
}
.menuulli {
flex: auto;
display: inline;
list-style: none;
padding: 01rem;
}
.menuullia {
text-decoration: none;
color: #303030;
font: 14px lato;
}
<header><divid="wrapper"><divclass="top-menu clearfix"><divclass="menu"><divclass="logo"><ahref=""><imgsrc="https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-icon.png?v=c78bd457575a"alt=""></a></div><divclass="between"><ulclass="left"><li><ahref="">Women</a></li><li><ahref="">Men</a></li><li><ahref="">Kids</a></li><li><ahref="">Coming Soon</a></li><li><ahref="">About</a></li></ul><ulclass="right"><li><ahref=""><imgsrc="assets/images/user.png"alt="">Login</a></li><li><ahref=""><imgsrc="assets/images/bucket.png"alt="">Basket</a></li></ul></div><divclass="search-container"><formaction="/action_page.php"><buttontype="submit"><iclass="fa fa-search">search</i></button></form></div></div></div></div></header>
Solution 2:
Here's a start with flexbox.
https://css-tricks.com/snippets/css/a-guide-to-flexbox/
body {
background-color: #ffffff;
}
#wrapper {
max-width: 1200px;
margin-left: auto;
margin-right: auto;
}
.top-menu {
margin-top: 40px;
display: flex;
align-items: center;
justify-content: space-between;
}
.menu {
display: flex;
align-items: center;
}
.logo {
width: 40px;
height: 40px;
}
.menuulli {
display: inline;
list-style: none;
}
.menuullia {
text-decoration: none;
color: #303030;
font: 14px lato;
}
<divid="wrapper"><header><divclass="top-menu clearfix"><divclass="menu"><divclass="logo"><ahref=""><imgsrc="https://via.placeholder.com/50"alt=""></a></div><ul><li><ahref="">Women</a></li><li><ahref="">Men</a></li><li><ahref="">Kids</a></li><li><ahref="">Coming Soon</a></li><li><ahref="">About</a></li></ul></div><divclass="menu"><ul><li><ahref=""><imgsrc="https://via.placeholder.com/10"alt=""> Login</a></li><li><ahref=""><imgsrc="https://via.placeholder.com/10"alt=""> Basket</a></li></ul><divclass="search-container"><formaction="/action_page.php"><buttontype="submit"><iclass="fa fa-search">s</i></button></form></div></div></div></header>
Post a Comment for "How To Align Logo And Menu Items In One Line?"