CSS Center Navbar Elements [duplicate] - html

This question already has answers here:
How can I vertically center a div element for all browsers using CSS?
(48 answers)
Closed 2 years ago.
I know that this is probably asked many times already. I just want to center all items of my navbar vertically. I tried to set vertical-align: center; in my headercss tag.
I probably miss something:
<header>
<nav class="navbar">
<div class="branding">
<img src="pic/branding.png" alt="branding">
</div>
<div class="navbar-links">
<ul>
<li>Login </li>
</ul>
</div>
<div class="searchbox-container">
<input class="searchbar" type="text" placeholder="Search...">
<button class="search-button" type="submit"><img src="pic/searchicon.png" height="25px" width="25px" alt="search"></button>
</div>
</nav>
</header>
css:
*{
margin: 0;
padding: 0;
text-decoration: none;
list-style: none;
}
body{
font-family: sans-serif;
}
/* Navbar stuff */
header{
background-color: #424242/*#80aaff*/;
height: 60px;
}
.navbar{
width: 80%;
margin: auto;
}
.branding{
float: left;
}
.navbar-links{
float: right;
}
.navbar-links ul{
list-style: none;
}
.navbar-links ul li a{
text-decoration: none;
text-transform: uppercase;
color: white;
}

* {
margin: 0;
padding: 0;
text-decoration: none;
list-style: none;
}
body {
font-family: sans-serif;
}
/* Navbar stuff */
header {
background-color: #424242/*#80aaff*/
;
height: 60px;
display: flex;
align-items: center;
}
.navbar {
width: 80%;
margin: auto;
display: flex;
flex-flow: row nowrap;
align-items: center;
}
.branding {
margin-right:
}
.navbar-links {
margin-left: auto;
}
.navbar-links ul {
list-style: none;
}
.navbar-links ul li a {
text-decoration: none;
text-transform: uppercase;
color: white;
}
<header>
<nav class="navbar">
<div class="branding">
<img src="pic/branding.png" alt="branding">
</div>
<div class="navbar-links">
<ul>
<li>Login </li>
</ul>
<div>
<div class="searchbox-container">
<input class="searchbar" type="text" placeholder="Search...">
<button class="search-button" type="submit"><img src="pic/searchicon.png" height="25px" width="25px" alt="search"></button>
</div>
</div>
</div>
</nav>
</header>

Related

Add space between two divs

I'm trying to create a navbar with the two regions split with white space between. I've tried using float:right and justify-content: space-between, but I'm not sure why it is not doing anything. I would like for site-header-right to go on the right side and site-header-navbar to the left.
I did think of making margins between the two divs but that just seemed like an ugly fix, which may or may not cause responsiveness problems later. (If I'm wrong though about that, please tell me haha).
.site-header-navbar {
display: inline-block;
margin: auto;
}
nav ul {
display: inline-block;
list-style-type: none;
}
nav ul li {
display: inline-block;
margin-right: 20px;
}
nav ul li .sale {
color: #a62120;
}
.site-header-right {
display: inline-block;
margin: auto;
}
.small-container {
display: inline-block;
}
.search-btn {
display: inline-block;
margin-right: 20px;
text-transform: uppercase;
font-family: Arial, Helvetica, sans-serif;
color: #555;
font-size: 12px;
line-height: 1.1;
letter-spacing: 1px;
font-weight: 500;
}
.site-header-right a {
display: inline-block;
margin-right: 20px;
}
.site-navigation {
display: flex;
}
<html>
<body>
<div class="site-header-masthead">
<div class="container">
<div class="row">
<div class="col-12 navbar">
<div class="menu-hamburger"></div>
<div class="toast-logo">
<img src="images/logo.jpeg" width="110px">
</div>
<div class="site-navigation">
<nav class="site-header-navbar">
<ul class="site-nav">
<li>Women</li>
<li>Men</li>
<li>House&Home</li>
<li>Sale</li>
<li>Events</li>
<li>Magazine</li>
<li>Our Approach</li>
</ul>
</nav>
<div class="site-header-right">
<div class="small-container">
<button class="search-btn">Search</button>
<a href="#" class="site-header-wishlist">
<span>Saved</span>
<span>(0)</span>
</a>
Account
<a href="#" class="site-header-cart">
<span>Bag</span>
<span>(0)</span>
</a>
</div>
</div>
</body>
</html>
Add justify-content: space-between; to your flex container (.site-navigation) to move the two child elements to the far left and right. And erase margin: auto; for the two flex items! All other offsets are due to margins (also default margins top and bottom), so you might want to reset these to 0.
.site-header-navbar {
display: inline-block;
}
nav ul {
display: inline-block;
list-style-type: none;
}
nav ul li {
display: inline-block;
margin-right: 20px;
}
nav ul li .sale {
color: #a62120;
}
.site-header-right {
display: inline-block;
}
.small-container {
display: inline-block;
}
.search-btn {
display: inline-block;
margin-right: 20px;
text-transform: uppercase;
font-family: Arial, Helvetica, sans-serif;
color: #555;
font-size: 12px;
line-height: 1.1;
letter-spacing: 1px;
font-weight: 500;
}
.site-header-right a {
display: inline-block;
margin-right: 20px;
}
.site-navigation {
display: flex;
justify-content: space-between;
}
<div class="site-header-masthead">
<div class="container">
<div class="row">
<div class="col-12 navbar">
<div class="menu-hamburger"></div>
<div class="toast-logo">
<img src="images/logo.jpeg" width="110px">
</div>
<div class="site-navigation">
<nav class="site-header-navbar">
<ul class="site-nav">
<li>Women</li>
<li>Men</li>
<li>House&Home</li>
<li>Sale</li>
<li>Events</li>
<li>Magazine</li>
<li>Our Approach</li>
</ul>
</nav>
<div class="site-header-right">
<div class="small-container">
<button class="search-btn">Search</button>
<a href="#" class="site-header-wishlist">
<span>Saved</span>
<span>(0)</span>
</a>
Account
<a href="#" class="site-header-cart">
<span>Bag</span>
<span>(0)</span>
</a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
You can use &nbsp between your navbars
&nbsp

How do I center an unordered list? [duplicate]

This question already has answers here:
How to center an unordered list?
(8 answers)
How can I center <ul> <li> into a div?
(16 answers)
Closed 2 years ago.
I have a list and css.
However the ul goes to the left. I have tried many things but it either stays inline and floats to the left or it goes to the center as I want it to, but then it's not inline, they stack on one another..how can I have both an centered ordered list and inline?
.navbar {
display: table;
margin: 0 auto;
width: 100%;
}
.navbar ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: white;
}
.navbar ul li {
display: inline-block;
}
li a {
display: block;
color: black;
text-align: center;
padding: 12px 16px;
font-size: 25px;
text-decoration: none;
}
<div class="navbar">
<ul>
<li>The Stock</li>
<li>
<a> <img style="height: 50px; width: 50px" src="img/logo.svg" alt="logotype" /> </a>
</li>
<li>The Stock</li>
</ul>
</div>
To center the navbar ul add text-align: center;
.navbar {
display: table;
margin: 0 auto;
width: 100%;
}
.navbar ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: white;
text-align: center;
}
.navbar ul li {
display: inline-block;
}
li a {
display: block;
color: black;
text-align: center;
padding: 12px 16px;
font-size: 25px;
text-decoration: none;
}
<div class="navbar">
<ul>
<li>The Stock</li>
<li>
<a> <img style="height: 50px; width: 50px" src="img/logo.svg" alt="logotype" /> </a>
</li>
<li>The Stock</li>
</ul>
</div>
All you need is "text-align:center;" on your .navbar ul like below:
.navbar ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: white;
text-align: center;
}
<li> runs as a text element because of your css .navbar ul li { display: inline-block; }. therefor you can center it with text-align. In this case just add: .navbar { text-align: center; }
.navbar {
margin: 0 auto;
width: 100%;
text-align: center;
}
.navbar ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: white;
}
.navbar ul li {
display: inline-block;
}
li a {
display: block;
color: black;
text-align: center;
padding: 12px 16px;
font-size: 25px;
text-decoration: none;
}
<div class="navbar">
<ul>
<li>The Stock</li>
<li>
<a> <img style="height: 50px; width: 50px" src="img/logo.svg" alt="logotype" /> </a>
</li>
<li>The Stock</li>
</ul>
</div>
Just use display: flex; justify-content: center; for either <ul> tag or navbar div container.
.navbar {
display: table;
margin: 0 auto;
width: 100%;
}
.navbar ul {
display: flex;
justify-content: center;
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: white;
}
.navbar ul li {
display: inline-block;
}
li a {
display: block;
color: black;
text-align: center;
padding: 12px 16px;
font-size: 25px;
text-decoration: none;
}
<div class="navbar">
<ul>
<li>The Stock</li>
<li>
<a> <img style="height: 50px; width: 50px" src="img/logo.svg" alt="logotype" /> </a>
</li>
<li>The Stock</li>
</ul>
</div>
Just change your .navbar class like this
.navbar {
display: flex;
justify-content: center;
}

How do I position a button to the right of the page when its parent uses flex? [duplicate]

This question already has answers here:
In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?
(6 answers)
Closed 3 years ago.
I am building the top navigation of a page. To the left, I have a logo and directly beside it I have my primary navigation links. I have two buttons that I want to position all the way to the right of the page, but I can't figure out how to get them over there. When I am able to get the buttons to move at all, it's only about halfway through the page...which isn't correct.
The buttons should be all the way to the right, like on https://webflow.com/
Notes: I used flex center to vertically center my navigation items.
body {
font-family: 'Poppins', sans-serif;
}
.wrap {
margin: 0 auto;
width: 90%;
}
nav {
display: flex;
align-items: center;
height: 80px;
width: 1000px;
}
nav ul li {
display: inline;
list-style: none;
padding: 0 24px 0 24px;
}
nav a {
color: #353D49;
font-size: 14px;
font-weight: 500;
text-decoration: none;
}
nav a:hover {
color: #247BFA;
}
<div class="wrap">
<nav>
<img src="img/logo.svg" />
<ul class="navItems">
<li>Overview</li>
<li>Features</li>
<li>Blog</li>
<li>Learn</li>
</ul>
<div class="actionButtons">
<button class="primary">Request a demo</button>
</div><!--actionButtons-->
</nav>
</div><!--wrap-->
.actionButtons {margin-left: auto}
or
nav {justify-content: space-between}
cheers :)
You can position the element to the right of the flex parent using margin-left: auto. You can do the same for vertical positioning with margin-top: auto.
body {
font-family: 'Poppins', sans-serif;
}
.wrap {
margin: 0 auto;
width: 90%;
}
nav {
display: flex;
align-items: center;
height: 80px;
width: 1000px;
}
nav ul li {
display: inline;
list-style: none;
padding: 0 24px 0 24px;
}
nav a {
color: #353D49;
font-size: 14px;
font-weight: 500;
text-decoration: none;
}
nav a:hover {
color: #247BFA;
}
nav .actionButtons {
margin-left: auto;
}
<div class="wrap">
<nav>
<img src="img/logo.svg" />
<ul class="navItems">
<li>Overview</li>
<li>Features</li>
<li>Blog</li>
<li>Learn</li>
</ul>
<div class="actionButtons">
<button class="primary">Request a demo</button>
</div><!--actionButtons-->
</nav>
</div><!--wrap-->
Great case for margin-left: auto; though I would create a separate class. The goal is to make code reusable, so nesting it in your nav tag will make it unwieldy if you continue with this method throughout your project.
I created a separate class for .align-right and you can reuse this in other areas of your project , rather than being limited to .nav or applying it to all of your .actionButtons
body {
font-family: 'Poppins', sans-serif;
}
.wrap {
margin: 0 auto;
width: 90%;
}
nav {
display: flex;
align-items: center;
height: 80px;
width: 1000px;
}
nav ul li {
display: inline;
list-style: none;
padding: 0 24px 0 24px;
}
nav a {
color: #353D49;
font-size: 14px;
font-weight: 500;
text-decoration: none;
}
nav a:hover {
color: #247BFA;
}
.align-right {
margin-left: auto;
}
<div class="wrap">
<nav>
<img src="img/logo.svg" />
<ul class="navItems">
<li>Overview</li>
<li>Features</li>
<li>Blog</li>
<li>Learn</li>
</ul>
<div class="actionButtons align-right">
<button class="primary">Request a demo</button>
</div><!--actionButtons-->
</nav>
</div><!--wrap-->

How to put a input-text on the end of the Nav-bar?

nav {
background-color: #e1e1e1;
height: 200px;
}
nav ul li a {
color: #fff;
padding-right: 20px;
}
nav ul {
display: inline-flex;
list-style: none;
}
<nav>
<ul>
<li>Home</li>
<li>About</li>
<div class="col-sm-12 col-md-12 col-lg-3 my-1 divSearch">
<form>
<div>
<input placeholder="Search">
<button type="submit">
<img>
</button>
</div>
</form>
</div>
</ul>
</nav>
I need to place the input on the right side (at the end) of the nav. How can I do it?
I tried to put a float: right;, justify-content: flex-end; and align-content: flex-end, but it did not work.
Here is my solution.
Wrap the div .divSearch with li and add margin-left: auto;
Also change inline-flex to flex for the ul
nav {
background-color: #e1e1e1;
height: 200px;
}
nav ul li a {
color: #fff;
padding-right: 20px;
}
nav ul {
display: flex;
list-style: none;
}
.input-parent {
margin-left: auto;
}
<nav>
<ul>
<li>Home</li>
<li>About</li>
<li class="input-parent">
<div class="col-sm-12 col-md-12 col-lg-3 my-1 divSearch">
<form>
<div>
<input placeholder="Search">
<button type="submit">
<img>
</button>
</div>
</form>
</div>
</li>
</ul>
</nav>
Try this way...
body {
margin: 0;
font-family: Arial, Helvetica, sans-serif;
}
.topnav {
overflow: hidden;
background-color: #e9e9e9;
}
.topnav a {
float: left;
display: block;
color: black;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.topnav a:hover {
background-color: #ddd;
color: black;
}
.topnav a.active {
background-color: #2196F3;
color: white;
}
.topnav .search-container {
float: right;
}
.topnav input[type=text] {
padding: 6px;
margin-top: 8px;
font-size: 17px;
border: none;
}
.topnav .search-container button {
float: right;
padding: 6px;
margin-top: 8px;
margin-right: 16px;
background: #ddd;
font-size: 17px;
border: none;
cursor: pointer;
}
.topnav .search-container button:hover {
background: #ccc;
}
#media screen and (max-width: 600px) {
.topnav .search-container {
float: none;
}
.topnav a, .topnav input[type=text], .topnav .search-container button {
float: none;
display: block;
text-align: left;
width: 100%;
margin: 0;
padding: 14px;
}
.topnav input[type=text] {
border: 1px solid #ccc;
}
}
<body>
<div class="topnav">
<a class="active" href="#home">Home</a>
About
<div class="search-container">
<form action="/action_page.php">
<input type="text" placeholder="Search.." name="search">
<button type="submit">Submit</button>
</form>
</div>
</div>
<div style="padding-left:16px">
<h2>Responsive Input Field in Navbar</h2>
<p>Navigation bar with a search box and a submit button inside of it.</p>
<p>Resize the browser window to see the responsive effect.</p>
</div>
</body>
I have documented the changes in the CSS code
nav {
background-color: #e1e1e1;
height: 200px;
}
nav ul li a {
color: #fff;
padding-right: 20px;
}
nav ul {
display: flex; /* Instead of inline-flex */
list-style: none;
}
.divSearch {
margin-left: auto; /* Pushes it to the right */
}
<nav>
<ul>
<li>Home</li>
<li>About</li>
<div class="col-sm-12 col-md-12 col-lg-3 my-1 divSearch">
<form>
<div>
<input placeholder="Search">
<button type="submit">
<img>
</button>
</div>
</form>
</div>
</ul>
</nav>

Having Issues Making A Navigation Bar in Header

I am trying to make a Navigation Bar for the top of my page
.header {
width: 100%;
background-color: #2C2F33;
border-bottom: 2.5px #FF9F00 solid;
font-size: 15px;
}
#logo img {
float: left;
margin: 0;
padding: 20px;
width: 187.5px;
height: 63.75px;
}
.navbar {
display: flex;
justify-content: space-between;
}
.navbar,
li {
display: inline;
padding: 0 22.5px 0 22.5px;
}
.navbar,
li,
a {
text-decoration: none;
list-style-type: none;
}
.navbar,
li,
a:hover {
color: #FF9F00;
text-decoration: none;
list-style-type: none;
}
#dollarydoos,
#dsh {
color: #FF9F00;
}
#dosh {
color: #FFFFFF;
}
<div class="header">
<div id="logo">
<img src="./img/logo.png"></img>
</div>
<div class="navbar">
<div id="leftnavbar">
<ul>
<li><span id="dollarydoos">Dollarydoos:</span> <span id="dosh">1.00000000</span> <span id="dsh">DSH</span></li>
</ul>
</div>
<div id="rightnavbar">
<ul>
<li>Button1</li>
<li>Button2</li>
<li>Button3</li>
<li>Button4</li>
</ul>
</div>
</div>
</div>
I cant get the buttons and the dollardoos to have a small box that surrounds it (inside) and to align to the center of the logo and get listed in line in the center.
Like this:
How can I do this as I am not too experienced in HTML/CSS. I am a newbie trying to build his own site.
I made a flexbox of the header and removed display:inline for the navbar
.header {
width: 100%;
background-color: #2C2F33;
border-bottom: 2.5px #FF9F00 solid;
font-size: 15px;
display: flex;
align-items: center;
}
#logo img {
margin: 0;
padding: 20px;
width: 187.5px;
height: 63.75px;
}
.navbar {
display: flex;
justify-content: space-between;
margin-left: auto;
}
li {
display: inline;
padding: 0 22.5px 0 22.5px;
}
.navbar,
li,
a {
text-decoration: none;
list-style-type: none;
}
.navbar,
li,
a:hover {
color: #FF9F00;
text-decoration: none;
list-style-type: none;
}
#dollarydoos,
#dsh {
color: #FF9F00;
}
#dosh {
color: #FFFFFF;
}
<div class="header">
<div id="logo">
<img src="http://placehold.it/200x100">
</div>
<div class="navbar">
<div id="leftnavbar">
<ul>
<li><span id="dollarydoos">Dollarydoos:</span> <span id="dosh">1.00000000</span> <span id="dsh">DSH</span></li>
</ul>
</div>
<div id="rightnavbar">
<ul>
<li>Button1</li>
<li>Button2</li>
<li>Button3</li>
<li>Button4</li>
</ul>
</div>
</div>
</div>