Center li of nav bar vertically [duplicate] - html

This question already has answers here:
Flexbox: center horizontally and vertically
(14 answers)
How can I center text (horizontally and vertically) inside a div block?
(27 answers)
Closed 10 months ago.
As you can see the menu points are not centered vertically like the social media points, could someone please tell me how to do that, it's the first page I am working on. I would also like to scale down the space between the social media links, would be very grateful if someone could help me!
.menu-link {
font-family: 'Montserrat', sans-serif;
font-weight: 500;
font-size: 22px;
color: #262a2b;
text-decoration: none;
height: 45px;
}
#social-media {
width: 100px;
height: 100px;
margin: 0;
padding: 0;
}
.menu-link:hover {
color: #0088a9;
}
.active-menu-links {
color: white;
}
#normal-header {
position: fixed;
display: flex;
flex-wrap: wrap;
justify-content: space-between;
align-items: center;
width: 100%;
margin: auto;
font-size: 19px;
min-height: 100px;
}
#header-img {
height: 110px;
padding-top: 20%;
padding-bottom: 20%;
margin-left: 20%;
}
#nav-bar {}
#nav-bar ul {
list-style: none;
display: flex;
flex-flow: row;
}
#nav-bar li {
padding: 10px;
margin: 12px;
}
#nav-bar ul,
a {
text-decoration: none;
}
<nav id="nav-bar">
<ul>
<li class="nav-link" id="menu-item"> Home </li>
<li class="nav-link" id="menu-item"> About </li>
<li class="nav-link" id="menu-item"> Roadmap </li>
<li class="nav-link" id="menu-item"> Ecosystem </li>
<li class="nav-link" id="menu-item"> Team
<li>
<li>
<a class="nav-link" href="xxx"><img id="social-media" src="xxx" alt="Instagram"></a>
</li>
<li>
<a class="nav-link" href="xxx"><img id="social-media" src="xxx" alt="Twitter"></a>
</li>
<li>
<a class="nav-link" href="xxx"><img id="social-media" src="xxx" alt="Discord"></a>
</li>
-->
</ul>
</nav>
I hope someone can help me. Thanks in advance!

So, I can't identify your problem because the snippet not working well as missing of files
But you can try to add align-items: center; to the #nav-bar ul selector
Or you can add display: flex align-items: center; to the #nav-bar li

You have several options:
Set line-height property of li or li a to the overall height
Set vertical-align: middle; (this doesn't always work for me)
Use paddings and margins (not recommended)
Set display: flex; and align-items: center to your li tag, but it can potentially break your layout

Since you made a snippet I tried and yes, you need line-height.✌️
To explain: If you use line-height with the same height as it's element. The two will match and make a easy menu button. Some side padding and/or margin, and you are done.
.menu-link {
font-family: 'Montserrat', sans-serif;
font-weight: 500;
font-size: 22px;
color: #262a2b;
text-decoration: none;
height: 45px;
line-height: 45px;
}
#social-media {
width: 100px;
height: 100px;
margin: 0;
padding: 0;
}
.menu-link:hover {
color: #0088a9;
}
.active-menu-links {
color: white;
}
#normal-header {
position: fixed;
display: flex;
flex-wrap: wrap;
justify-content: space-between;
align-items: center;
width: 100%;
margin: auto;
font-size: 19px;
min-height: 100px;
}
#header-img {
height: 110px;
padding-top: 20%;
padding-bottom: 20%;
margin-left: 20%;
}
#nav-bar {}
#nav-bar ul {
list-style: none;
display: flex;
flex-flow: row;
}
#nav-bar li {
padding: 10px;
margin: 12px;
}
#nav-bar ul,
a {
text-decoration: none;
}
<nav id="nav-bar">
<ul>
<li class="nav-link" id="menu-item"> Home </li>
<li class="nav-link" id="menu-item"> About </li>
<li class="nav-link" id="menu-item"> Roadmap </li>
<li class="nav-link" id="menu-item"> Ecosystem </li>
<li class="nav-link" id="menu-item"> Team
<li>
<li>
<a class="nav-link" href="xxx"><img id="social-media" src="xxx" alt="Instagram"></a>
</li>
<li>
<a class="nav-link" href="xxx"><img id="social-media" src="xxx" alt="Twitter"></a>
</li>
<li>
<a class="nav-link" href="xxx"><img id="social-media" src="xxx" alt="Discord"></a>
</li>
-->
</ul>
</nav>

Related

Removing line breaks in navbar

I am creating a navbar with a logo on the left side on the navbar and the links on the right side of the navbar. Although I have been successful, there are some unwanted line breaks in the text on the right side of the navbar. How do I get rid of the line breaks in the texts "How it works" and "Available Products"? I am not using Bootstrap and I do not want to use it.
* {
padding: 0;
margin: 0;
font-family: "Roboto", sans-serif;
}
ul {
list-style-type: none;
overflow: hidden;
}
li {
float: left;
}
.container {
align-items: center;
justify-content: center;
display: flex;
}
img {
max-width: 100%;
width: 72.5%;
height: auto;
}
.logo {
flex-basis: 75%;
margin-top: 10px;
margin-left: 10px;
}
.nav-link {
display: block;
text-align: center;
text-decoration: none;
font-size: 20px;
padding-right: 20px;
}
<header id="header">
<nav id="nav-bar">
<div>
<ul>
<div class="container">
<div class="logo">
<li>
<img
id="header-img"
src="https://i.ibb.co/5Mcnrcm/N-logo.png"
style="vertical-align: middle"
/>
</li>
</div>
<li><a class="nav-link" href="#f">Features</a></li>
<li><a class="nav-link" href="#h">How It Works</a></li>
<li><a class="nav-link" href="#a">Available Products</a></li>
</div>
</ul>
</div>
</nav>
</header>
It can be done by applying CSS white-space: nowrap; to e.g. .nav-link as shown below:
* {
padding: 0;
margin: 0;
font-family: "Roboto", sans-serif;
}
ul {
list-style-type: none;
overflow: hidden;
}
li {
float: left;
}
.container {
align-items: center;
justify-content: center;
display: flex;
}
img {
max-width: 100%;
width: 72.5%;
height: auto;
}
.logo {
flex-basis: 75%;
margin-top: 10px;
margin-left: 10px;
}
.nav-link {
display: block;
text-align: center;
text-decoration: none;
font-size: 20px;
padding-right: 20px;
white-space: nowrap;
}
<header id="header">
<nav id="nav-bar">
<div>
<ul>
<div class="container">
<div class="logo">
<li>
<img
id="header-img"
src="https://i.ibb.co/5Mcnrcm/N-logo.png"
style="vertical-align: middle"
/>
</li>
</div>
<li><a class="nav-link" href="#f">Features</a></li>
<li><a class="nav-link" href="#h">How It Works</a></li>
<li><a class="nav-link" href="#a">Available Products</a></li>
</div>
</ul>
</div>
</nav>
</header>

How do I center my nav bar to the screen? [duplicate]

This question already has answers here:
How do I center floated elements?
(12 answers)
Flexbox: center horizontally and vertically
(14 answers)
Closed 2 years ago.
I have a problem with my Nav Bar. I need it to be centered on the screen, so it is in the middle of everything. But my Nav Bar will only be on the left side of my screen.
I tried to use Margin to fix the issue, but then it will not be responsive to the rest, so that does not work.
Here is my code for the Nav Bar:
HTML:
<nav>
<ul>
<li>
<a href="BlogTest.html">
Home
</a>
</li>
<li>
<a href="BlogTest.html">
About Me
</a>
</li>
<li>
<a href="BlogTest.html">
Contact Me
</a>
</li>
<li>
<a href="BlogTest.html">
Blog
</a>
</li>
</ul>
</nav>
Ignore the "href", they will be sorted afterwards
the CSS:
*{
margin: 0%;
padding: 0%;
background-color: rgb(53, 53, 53);
text-decoration: none;
}
nav{
width: 100%;
height: 100px;
text-align: center;
display: inline-block;
}
ul{
text-align: center;
}
ul li{
list-style: none;
display: inline-block;
float: left;
line-height: 100px;
}
ul li a{
font-family: 'PT Sans', sans-serif;
color: white;
font-size: 200%;
padding: 0px 20px;
display: block;
display: inline-block;}
ul li a:hover {
color: red;
}
I did read some of the others answered questions but without any luck from them, hope you can help me once again!
Just add display: inline-block; to the CSS rule for ul to make it only as wide as its contents and therefore also to allow the text-align: center for nav to have an effect:
* {
margin: 0%;
padding: 0%;
background-color: rgb(53, 53, 53);
text-decoration: none;
}
nav {
width: 100%;
height: 100px;
text-align: center;
display: inline-block;
}
ul {
display: inline-block;
text-align: center;
}
ul li {
list-style: none;
display: inline-block;
float: left;
line-height: 100px;
}
ul li a {
font-family: 'PT Sans', sans-serif;
color: white;
font-size: 200%;
padding: 0px 20px;
display: block;
display: inline-block;
}
ul li a:hover {
color: red;
}
<nav>
<ul>
<li>
<a href="BlogTest.html">
Home
</a>
</li>
<li>
<a href="BlogTest.html">
About Me
</a>
</li>
<li>
<a href="BlogTest.html">
Contact Me
</a>
</li>
<li>
<a href="BlogTest.html">
Blog
</a>
</li>
</ul>
</nav>
Try putting the nav in center tags.
Like this:
<center>
<nav>
<ul>
<li>
<a href="BlogTest.html">
Home
</a>
</li>
<li>
<a href="BlogTest.html">
About Me
</a>
</li>
<li>
<a href="BlogTest.html">
Contact Me
</a>
</li>
<li>
<a href="BlogTest.html">
Blog
</a>
</li>
</ul>
</nav>
</center>
Sorry if this doesn't help. I couldn't fully understand your problem.
Try this. Don't use <center> tag, it's obsolete.
html:
<header>
<nav>
<ul>
<li>Menu</li>
</ul>
</nav>
</header>
css:
header { width: 100%; }
nav { width: 980px; margin: 0 auto; display: block; }
margin: 0 auto; will center the element. just make sure that it has a width, otherwise this will not work.
try this =)
* {
margin: 0;
padding: 0;
background-color: rgb(53, 53, 53);
text-decoration: none;
}
nav {
position: absolute;
width: 100%;
height: 100px;
text-align: center;
display: block;
margin: auto;
border: 2px solid white;
}
ul {
display: inline-block;
text-align: center;
width: max-content;
}
ul li {
list-style: none;
display: inline-block;
float: left;
line-height: 100px;
margin: auto;
}
ul li a {
font-family: 'PT Sans', sans-serif;
color: white;
font-size: 100%;
padding: 0px 10px;
display: inline-block;
}
ul li a:hover {
color: red;
}
<nav>
<ul>
<li>
<a href="BlogTest.html">
Home
</a>
</li>
<li>
<a href="BlogTest.html">
About Me
</a>
</li>
<li>
<a href="BlogTest.html">
Contact Me
</a>
</li>
<li>
<a href="BlogTest.html">
Blog
</a>
</li>
</ul>
</nav>

shift an element of nav bar to left most side

I have the following Nav bar. I want to shift the Login/Register Component of it on the very left most of the nav bar but can't find a way to do it. What might be the possible solution, I have tried the padding-left:0 thing on the login li element but this does not seem to work. Any help in this would be appreciated
HTML:
body {
margin: 0;
background: #222;
font-weight: 300;
background-image: url('bg.jpeg');
}
header {
background: #d9c2ac;
position: relative;
}
header::after {
content: '';
display: table;
clear: both;
}
nav {
float: left;
}
nav ul {
margin: 0;
padding: 0;
list-style: none;
}
nav li {
display: inline-block;
margin-left: 70px;
padding-top: 30px;
position: relative;
}
nav ul li a {
color: #444;
text-decoration: none;
text-transform: uppercase;
font-size: 14px;
font-weight: bold;
}
nav a:hover {}
nav a::before {
content: '';
display: block;
height: 5px;
width: 0%;
background-color: #444;
transition: all ease-in-out 500ms;
}
nav a:hover::before {
width: 100%;
}
form .form {
float: right;
}
.search-bar {
float: right;
}
ul li:last-child {
position: absolute;
right: 0;
bottom: 0;
margin: 15px;
margin-bottom: 0px;
padding: 0;
}
<body>
<header>
<div class="container" id="#home">
<nav>
<ul>
<li id="login"> Login/Register </li>
<li> Home </li>
<li> About </li>
<li> Services </li>
<li> Products </li>
<li> Contact Us </li>
<li>
<form class="form"> <input type="text" name="Search" placeholder="Search"> </form>
</li>
</ul>
</nav>
</div>
</header>
You can use pseudo css to align item.
nav li:first-child {
margin-left: 0;
}
you can check here
Use rm-auto to set the margin right to 0
.navbar-tan
{
background-color:tan;
}
<html>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<nav class="navbar navbar-expand-sm navbar-tan" >
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Login/Register</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Services</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Products</a>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#">Contact Us</a>
</li>
</ul>
<ul class="navbar-nav">
<li class="nav-item">
<input type="text" name="" placeholder="Search" class="form-control">
</li>
</ul>
</nav>
Try to make use of Flexbox here...and use margin utilities to align the first and last item...
...apply margin-right:auto to the first element to align it left most corner
...and apply margin-left:auto to the last element to align it right most corner..
I have also changed your some css and also removed all the float values. Also no need to use position:absolute here for the search-box.
Stack Snippet
body {
margin: 0;
background: #222;
font-weight: 300;
background-image: url('bg.jpeg');
}
header {
background: #d9c2ac;
position: relative;
}
nav ul {
margin: 0;
list-style: none;
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: center;
padding: 20px 0;
}
nav li {
position: relative;
margin: 0 10px;
}
nav ul li a {
color: #444;
text-decoration: none;
text-transform: uppercase;
font-size: 12px;
font-family: verdana;
}
nav a:hover {}
nav a::before {
content: '';
display: block;
height: 5px;
width: 0%;
background-color: #444;
transition: all ease-in-out 500ms;
}
nav a:hover::before {
width: 100%;
}
nav ul li:first-child {
margin-right: auto;
}
nav ul li:last-child {
margin-left: auto;
}
<header>
<div class="container" id="#home">
<nav>
<ul>
<li id="login"> Login/Register </li>
<li> Home </li>
<li> About </li>
<li>
<form class="form"> <input type="text" name="Search" placeholder="Search"> </form>
</li>
</ul>
</nav>
</div>
</header>

Image affecting alignment in nav bar [duplicate]

This question already has answers here:
My inline-block elements are not lining up properly
(5 answers)
Closed 5 years ago.
I'm trying to make a nav bar with an image and I'm having trouble with the image affecting the alignment of the other elements in the nav bar. I can't get the links to be flush with the very top of the page. If I remove the image there is no issue.
.logo {
max-height: 80px;
border-radius: 80px;
}
.link-group-wrapper {
display: inline;
margin: 0;
padding: 0;
}
.link-wrapper {
list-style: none;
text-align: center;
font-size: 1.2em;
background-color: #0b215c;
width: 120px;
display: inline-block;
height: 90px;
line-height: 90px;
}
.link {
color: white;
display: block;
text-decoration: none;
}
<div class="navbar-wrapper">
<img class="logo" src="https://image.ibb.co/cVNZww/logo.jpg">
<ul class="link-group-wrapper">
<li class="link-wrapper">
<a class="link" href="">Roster</a>
</li>
<li class="link-wrapper">
<a class="link" href="">Roster</a>
</li>
<li class="link-wrapper">
<a class="link" href="">Roster</a>
</li>
</ul>
</div>
Easy fix ! Just put the image html element after your nav bar and add a float style to it (float: left;) with CSS
.logo {
max-height: 80px;
border-radius: 80px;
}
.link-group-wrapper {
display: inline;
margin: 0;
padding: 0;
}
.link-wrapper {
list-style: none;
text-align: center;
font-size: 1.2em;
background-color: #0b215c;
width: 120px;
display: inline-block;
height: 90px;
line-height: 90px;
}
.link {
color: white;
display: block;
text-decoration: none;
}
<div class="navbar-wrapper">
<ul class="link-group-wrapper">
<li class="link-wrapper">
<a class="link" href="">Roster</a>
</li>
<li class="link-wrapper">
<a class="link" href="">Roster</a>
</li>
<li class="link-wrapper">
<a class="link" href="">Roster</a>
</li>
</ul>
<img class="logo" style="float: left; "src="https://image.ibb.co/cVNZww/logo.jpg">
</div>

Tightly pack a grid of list items in HTML

For part of a site I'm making, I'm looking to have a grid of square objects, and have them pack together tightly so there's no spaces.
Here is what I have made:
But here's what I want it to look like:
So far I've only been doing this by padding and adding margins, and then by vertically aligning each list item. But I want it to go one step further than vertical alignment, I want each item to fit directly underneath the one above it.
I'm sure there's a very different, better approach than the one I've taken, which would be great too!
Here's what I have done:
HTML:
<header class="results">
<ul class="container">
<li>
<a id="name">Temp</a>
<a id="position">Temp</a>
</li>
<li>
<a id="name">Temp</a>
<a id="position">Temp</a>
<a id="position">Temp</a>
</li>
<li>
<a id="name">Temp</a>
<a id="position">Temp</a>
</li>
<li>
<a id="name">Temp</a>
<a id="position">Temp</a>
</li>
<li>
<a id="name">Temp</a>
<a id="position">Temp</a>
</li>
<li>
<a id="name">Temp</a>
<a id="position">Temp</a>
</li>
</ul>
</header>
CSS:
body {
margin: 0;
}
.page {
background: #fff;
}
header.results {
max-width: 100%;
}
header.results .container {
padding: 1em 0 2em;
margin: 0px auto 3px;
line-height: 1;
}
header.results .container li {
width: 30%;
display: inline-block;
padding: 2em 2em 0.75em;
margin: 0px auto 3px;
background: rgb(240,240,240);
vertical-align: top;
}
header.results .container li #name {
text-align: center;
display: block;
margin-top: 0.5em;
font-weight: 500;
}
header.results .container li #position {
text-align: center;
display: block;
margin-top: 0.5em;
font-weight: 250;
font-size: 85%;
}
If you're not supporting older browsers (IE 8 & 9), you could implement this with CSS columns, as shown here.
For full browser support, I'd go with the jQuery masonry plugin.