Centering the text inside the navigation box - html

I'm working on a navigation menu and right now I'm trying to center the text both horizontally and vertically inside the boxes.
Here is how my navigation looks like right now:
Here is how I want it to be:
This is my code (I'm using Umbraco):
#inherits Umbraco.Web.Mvc.UmbracoTemplatePage
#{ var home = CurrentPage.Site(); }
#if (home.Children.Any())
{
// Get the first page in the children
var naviLevel = home.Children.First().Level;
// Add in level for a CSS hook
<ul class="meny level-#naviLevel">
// For each child page under the home node
#foreach (var childPage in home.Children)
{
if (childPage.Children.Any())
{
<li class="dropdown has-child #(childPage.IsAncestorOrSelf(CurrentPage) ? "selected" : null)">
#if (childPage.DocumentTypeAlias == "Huvudmeny")
{
<span>#childPage.Name</span>
#childPages(childPage.Children)
}
else
{
#childPage.Name
}
#helper childPages(dynamic pages)
{
// Ensure that we have a collection of pages
if (pages.Any())
{
// Get the first page in pages and get the level
var naviLevel = pages.First().Level;
// Add in level for a CSS hook
<ul class="meny dropdown-menu sublevel level-#(naviLevel)">
#foreach (var page in pages)
{
<li>
#page.Name
//if the current page has any children
#if (page.Children.Any())
{
// Call our helper to display the children
#childPages(page.Children)
}
</li>
}
</ul>
}
}
</li>
}
else
{
<li class="#(childPage.IsAncestorOrSelf(CurrentPage) ? "selected" : null)">
#childPage.Name
</li>
}
}
</ul>
}
css:
nav {
z-index: 999;
background: rgba(0, 0, 0, 0.95);
text-align: right;
left: 0px;
right: 0px;
display: block;
position: relative;
transition: height 300ms ease-in-out;
text-align:center;
}
nav > ul {
padding: 5px;
position: relative;
display: inline-table;
width: 100%;
}
nav > ul li {
position: relative;
font-family: "Lato", sans-serif;
}
.nav a {
display: inline-block;
}
nav > ul li a,
nav > ul li span {
cursor: pointer;
display: inline-block;
padding: 40px;
padding-top: 5px;
font-size: 20px;
font-weight: 500;
color: #000000;
border-right: 1px solid #000000;
}
nav > ul li a:hover,
nav > ul li span:hover {
color: #0066FF;
}
nav > ul li span {
cursor: default;
}
nav > ul li:after {
content: "";
clear: both;
display: block;
}
nav > ul li.selected > a,
nav > ul li.selected span {
color: #000000;
font-weight: 700;
}
nav li > ul {
position: absolute;
}
nav li > ul li {
float: none;
display: block;
position: relative;
}
nav li > ul li a {
font-size: 17px;
padding: 15px 15px;
font-weight: 500;
color: rgba(255, 255, 255, 0.8);
}
nav li > ul li a:hover {
color: #fff;
}
nav li > ul > li > ul {
left: 100%;
top: 0;
display: none;
}
nav {
margin: 0 auto;
background: none;
width: 100%;
}
nav > ul {
padding-right: 0;
width: auto;
text-align: center;
}
nav > ul li {
float: left;
}
nav > ul li a,
nav > ul li span {
font-size: 15px;
}
nav > ul li:last-child a {
padding-right: 0;
}
nav li > ul {
left: 0;
top: 75%;
display: none;
white-space: nowrap;
height: auto;
margin-bottom: 0;
margin-left: 10px;
text-align: left;
background: rgba(0, 0, 0, 0.8);
padding: 5px 8px 5px 0;
}
nav li > ul li a {
padding: 8px 15px;
font-size: 15px;
color: rgba(255, 255, 255, 0.8);
}

You should remove the padding-top and decrease the padding from the
nav > ul li a, nav > ul li span.
nav > ul li a, nav > ul li span {
cursor: pointer;
display: inline-block;
padding: 14px; /* decrease padding */
/* padding-top: 5px; */
font-size: 20px;
font-weight: 500;
color: #000000;
border-right: 1px solid #000000;
}

You can use this css to make layout as per reference image(you need to change height and font-size and color as per your design)
nav > ul li {
background: #fff none repeat scroll 0 0;
color: #000;
font-size: 18px;
margin: 0;
text-align: center;
padding: 0 15px;
height: 30px;
display: -webkit-box;
display: -webkit-flex;
display: -moz-box;
display: -ms-flexbox;
display: flex;
-webkit-flex-align: center;
-ms-flex-align: center;
-webkit-align-items: center;
align-items: center;
justify-content: center;
}

Related

content overlapping with flexbox header - how to avoid

CSS styles for my menu bar
* {
margin: 0;
padding: 0;
}
body {
background: white;
font-family: 'Helvetica Neue', Helvetica;
font-weight: 400;
font-size: 1.05em;
line-height: 1.6rem;
color: white;
font-size: 1.2em;
}
header {
position: fixed;
background: #d777ea; /*light purple*/
width: 100%;
display: block;
}
header > nav > ul {
display: flex;
margin: 0;
padding: 0;
justify-content: flex-start;
list-style: none;
flex-wrap: wrap;
}
header > nav > ul > li {
margin: 0;
flex: 0 1 auto;
position: relative;
transition: 0.2s;
cursor: pointer;
}
header > nav > ul > li:hover {
background: #aa64e0; /*dark purple*/
}
header > nav > ul > li > ul {
/* dropdown */
position: absolute;
background: #76a1e8; /*light blue*/
display: none;
list-style-type: none;
margin: 0;
padding: 0;
}
header > nav > ul > li:hover > ul {
/* dropdown */
display: block;
width: 220px;
}
header > nav > ul > li > a {
color: white;
display: flex;
font-size: 1.5rem;
text-decoration: none;
padding: 1rem 1.5rem;
letter-spacing: 1px;
}
header > nav > ul > li > ul > li {
display: block;
}
header > nav > ul > li > ul > li > a {
text-decoration: none;
color: white;
display: flex;
letter-spacing: 1px;
cursor: pointer;
padding: .25rem 1.5rem;
}
header > nav > ul > li > ul > li:hover {
background: blue;
}
header > nav > ul > li > a > i {
margin-left: 5px;
}
input {
padding: .25rem;
width: 100px;
}
input:invalid {
border: 2px solid red;
}
header > nav > ul > li > ul > li > input:invalid + button {
background: red;
cursor: not-allowed;
}
button {
padding: .25rem;
}
HTML header and an image
<header>
<nav>
<ul>
<li>Teams<i class="fas fa-caret-down"></i>
<ul>
<li><a class="preset-id" data-schoolid=13318>Huron</a> </li>
<li><a class="preset-id" data-schoolid=99999>Pioneer</a></li>
<li>
<input type="number" min="1" max="100000" class="custom-id-value" placeholder="School ID #" required>
<button class="custom-id"> Go </button>
</a>
</li>
</ul>
</li>
<li><a class="show-calendar">Calendar</a></li>
<li><a class="show-athletes">Athletes</a></li>
</ul>
</nav>
</header>
<main>
<image src="https://www.w3schools.com/w3css/img_lights.jpg"></image>
</main>
When it shows up,
The content is on top of the header bar. Is there some way to avoid this? and have the content displayed below? When the dropdown is activated, I don't want the content below it to move though.
JsFiddle: https://jsfiddle.net/g870xy3d/36/
You just need to add a margin top in the main tag with the height of the header. Also add top:0 to the header:
header{
top:0;
position: fixed;
background: #d777ea; /*light purple*/
width: 100%;
display: block;
}
main{
margin-top: 57px;
}

h1 and nav element refuse to properly line up

I've looked through several questions regarding this already; however, none of them seem to solve or cover this problem.
What is essentially happening is that the h1 and nav element end up side by side but aren't properly lining up. I've tried multiple display and float settings on each but none are working.
The CSS itself is relatively clean, perhaps flex is not working properly or i am not using it properly? I'm not sure whats causing the problem here help is greatly appreciated!
/* Mobile CSS */
#navbar h1 {
display: block;
padding-top: 1.4rem;
text-align: center;
font-weight: 400;
}
#hbmenu {
display: block;
position:absolute; top:1.8rem; left:2rem;
background-color: transparent;
border: none;
}
.iconbar {
display: block;
margin-top: 5px;
height: 3px;
border-radius: 3px;
width: 30px;
background-color: #888;
}
#hbmenu:hover .iconbar {
background-color: #fff
}
#navbar > nav {
display: flex;
flex-direction: column;
background-color: white;
transform: 300ms all ease;
}
#navbar > nav > ul {
margin: 0 auto;
flex: 1;
text-align: center;
list-style-type: none;
}
#navbar > nav > ul > li {
border-bottom: 1px solid rgba(51,51,51,0.4);
}
#navbar > nav > ul > li > a {/*border bottom none?*/
display: block;
padding: 1.2rem 0;
text-decoration: none;
transition: 250ms all ease;
}
/* Medium screens or Desktop screens */
#media all and (min-width: 600px) {
#hbmenu {
display: none;
}
#navbar h1 {
display: inline-block;
padding-left: 2rem;
}
#navbar > nav {
display: inline-block;
padding-top: 2.1rem;
background-color: #333333;
}
#navbar > nav > ul {
padding-left: 1.8rem;
}
#navbar > nav > ul > li {
display: inline-block;
}
#navbar > nav > ul > li > a {
padding: 0 0.5rem;
margin: 0 0.5rem;
color: #9D9D9D;
border-top: 1.5px solid transparent;
border-bottom: 1.5px solid transparent;
transition: 0.5s;
}
#navbar > nav > ul > li > a:hover {
border-bottom: 1.5px solid #fff;
color: #fff;
transition: 0.5s;
}
}
/* Color & Font Stuff */
#navbar > nav > ul > li > a {
font-family: "Helvetica Neue";
font-weight: 600;
}
.highlight {
color: #9D9D9D;
transition: 0.5s;
}
.highlight:hover {
color: #ffffff;
transition: 0.5s;
}
<header>
<div id="navbar">
<button type="button" id="hbmenu">
<span class="iconbar"></span>
<span class="iconbar"></span>
<span class="iconbar"></span>
</button>
<h1>
<span class="highlight">Gatsby</span>
</h1>
<nav>
<ul>
<li>
Home
</li>
<li>
Test
</li>
<li>
About
</li>
</ul>
</nav>
</div>
</header>
Removing the h1 element allows to me properly add padding to the top of the nav element. While, having both only allows me to properly pad the h1. I'm sure padding isn't the best way to vertically center either but the problem remains.
UPDATE:
misalignment picture
I quickly marked up the screenshot which hopefully clarifies the problem. The red line on the bottom demonstrates how the text currently lines up. While the red line on the top demonstrates how the li elements should be vertically centered.
h1, nav element views
As this shows perhaps the bigger problem is how its currently setup as the elements don't "snap" if you will.
display: inline-block; elements can be vertically center aligned by using vertical-align: middle;, just add it to both h1 and nav
/* Mobile CSS */
#navbar h1 {
display: block;
text-align: center;
font-weight: 400;
}
#hbmenu {
display: block;
position:absolute; top:1.8rem; left:2rem;
background-color: transparent;
border: none;
}
.iconbar {
display: block;
margin-top: 5px;
height: 3px;
border-radius: 3px;
width: 30px;
background-color: #888;
}
#hbmenu:hover .iconbar {
background-color: #fff
}
#navbar > nav {
display: flex;
flex-direction: column;
background-color: white;
transform: 300ms all ease;
}
#navbar > nav > ul {
margin: 0 auto;
flex: 1;
text-align: center;
list-style-type: none;
}
#navbar > nav > ul > li {
border-bottom: 1px solid rgba(51,51,51,0.4);
}
#navbar > nav > ul > li > a {/*border bottom none?*/
display: block;
padding: 1.2rem 0;
text-decoration: none;
transition: 250ms all ease;
}
/* Medium screens or Desktop screens */
#media all and (min-width: 600px) {
#hbmenu {
display: none;
}
#navbar h1 {
display: inline-block;
padding-left: 2rem;
vertical-align: middle;
}
#navbar > nav {
display: inline-block;
vertical-align: middle;
background-color: #333333;
}
#navbar > nav > ul {
padding-left: 1.8rem;
}
#navbar > nav > ul > li {
float: left;
}
#navbar > nav > ul > li > a {
padding: 0 0.5rem;
margin: 0 0.5rem;
color: #9D9D9D;
border-top: 1.5px solid transparent;
border-bottom: 1.5px solid transparent;
transition: 0.5s;
}
#navbar > nav > ul > li > a:hover {
border-bottom: 1.5px solid #fff;
color: #fff;
transition: 0.5s;
}
}
/* Color & Font Stuff */
#navbar > nav > ul > li > a {
font-family: "Helvetica Neue";
font-weight: 600;
}
.highlight {
color: #9D9D9D;
transition: 0.5s;
}
.highlight:hover {
color: #ffffff;
transition: 0.5s;
}
<header>
<div id="navbar">
<button type="button" id="hbmenu">
<span class="iconbar"></span>
<span class="iconbar"></span>
<span class="iconbar"></span>
</button>
<h1>
<span class="highlight">Gatsby</span>
</h1>
<nav>
<ul>
<li>
Home
</li>
<li>
Test
</li>
<li>
About
</li>
</ul>
</nav>
</div>
</header>
You can try to use flexbox for your problem. Check the codepen sample. I think this was your requirement
.header-container {
display: flex;
max-width: 600px;
align-items: center;
}
h1, nav {
flex-grow: 1;
}
Check the codepen.

Css div center alignment not working

I have tried to make horizontal menu in center of page but 'div align=center' not working properly.
.nav ul {
display: block;
list-style-type: none;
margin: 0;
position: absolute;
}
.nav li {
display: inline-block;
float: left;
}
.nav li a {
display: block;
min-width: 140px;
height: 50px;
text-align: center;
line-height: 50px;
font-family: "Helvetica
Neue", Helvetica, Arial, sans-serif; color: #fff; background: #2f3036; text-decoration: none; } .nav li:hover ul a { background: #f3f3f3; color: #2f3036; height: 40px; line-height: 40px; } .nav div ul {display:block;} .nav li ul { display: none; } .nav
li ul li {
display: block;
float: none;
}
.nav li ul li a {
width: auto;
min-width: 100px;
padding: 0 20px;
}
.nav ul > li:hover ul {
display: block;
visibility: visible;
}
.nav ul li:hover > ul {
display: block;
}
<div class="nav">
<ul>
<li>Home
</li>
<li>
About
<ul>
<li>A1
</li>
<li>A2
</li>
</ul>
</li>
</ul>
</div>
sample code
I have tried to make horizontal menu in center of page but 'div align=center' not working properly.
Remove position:absolute; from your ul and add these minor changes
.nav{
width: 100%;
}
.nav ul {
display:block;
list-style-type:none;
margin: 0px auto;
width: 50%;
}
Here's the solved fiddle
Remove position:absolute;. It doesn't take any margin or padding values.
.nav ul {
list-style-type:none;
margin:0 auto;
display:table;
}
Here solved fiddle link
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
li {
float: left;
}
li a, .dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover, .dropdown:hover .dropbtn {
background-color: #19c589;
}
li.dropdown {
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {background-color: #f1f1f1}
.dropdown:hover .dropdown-content {
display: block;
}
<ul>
<li><a class="active" href="#home">Home</a></li>
<li class="dropdown">
About
<div class="dropdown-content">
A1
A2
A3
</div>
</li>
<li>New</li>
</ul>
Use as the below:
.nav {
margin:0 auto;
}
please remove the ul position: absolute; style other styling left as it is. These are the additional changes you have to do.
.nav {
display: -webkit-box;
}
.nav ul {
margin: 0 auto;
}

Display inline li with equal distance between content

I have a question about how to display in ul list with fixed width, content with equal distance. Problem that I'll apply content in different languages, so text will jump out of ul tag if I write fixed sizes.
I'm trying to use display us a table, but li element has different content padding.
And solution must be without JS(only HTML and CSS).
<nav id="primary_nav_wrap">
<ul>
<li>Грузы<span>&#9660</span></li>
<li>Транспорт<span>&#9660</span></li>
<li>Услуги и цены<span>&#9660</span></li>
<li>Зона надежности<span>&#9660</span></li>
<li>Каталог</li>
<li>Форум<span>&#9660</span></li>
<li>Полезное<span>&#9660</span></li>
</ul>
</nav>
.
#primary_nav_wrap {
width: 730px;
height: 51px;
background: #f5f5f5;
}
#primary_nav_wrap ul {
list-style: none;
width: 712px;
margin: 0;
padding: 0;
}
#primary_nav_wrap ul a {
display: block;
color: #333;
text-decoration: none;
font-size: 14px;
line-height: 32px;
text-align: center;
vertical-align: middle;
}
#primary_nav_wrap ul a span {
font-size: 8px;
color: #ababab;
padding-left: 3px;
}
#primary_nav_wrap ul li {
margin: 0;
padding: 0;
background: #f5f5f5;
}
#primary_nav_wrap ul li:hover {
background: #1e88e5;
}
#primary_nav_wrap > ul {
display: table;
table-layout: initial;
}
#primary_nav_wrap > ul > li {
height: 51px;
display: table-cell;
margin: 0 auto;
}
#primary_nav_wrap > ul > li:hover {
background: #e7e7e7
}
#primary_nav_wrap > ul > li > a {
font-size: 15px;
color: #16568e;
height: 41px;
padding-top: 10px;
}
here is code in jsfiddle jsfiddle
You can use flexbox:
#primary_nav_wrap > ul {
display: flex;
}
#primary_nav_wrap > ul > li {
flex-grow: 1;
}
var de = [ 'Frachten', 'Transport', 'Leistungen und Preise', 'Sicherheitszone', 'Katalog', 'Forum', 'Nützlich' ];
var ru = [ 'Грузы', 'Транспорт', 'Услуги и цены', 'Зона надежности', 'Каталог', 'Форум', 'Полезное' ];
var lang = 0;
$("#de").click(function () {
lang = de;
console.log("de");
dispLang();
});
$("#ru").click(function () {
lang = ru;
console.log("ru");
dispLang();
});
function dispLang () {
var titleName;
for (var i = 0; i < 7; i++) {
titleName = lang[i];
$("p[id=title_" + i + "]").append(titleName);
}
};
body {
background-color: white;
}
#primary_nav_wrap {
width: 712px;
height: 51px;
position: absolute;
top: 40px;
left: 20px;
z-index: 0;
background: #f5f5f5;
}
#primary_nav_wrap > ul {
list-style: none;
width: 712px;
margin: 0;
padding: 0;
display: flex;
}
#primary_nav_wrap > ul > li {
margin: 0;
padding: 0;
background: #f5f5f5;
height: 51px;
flex-grow: 1;
}
#primary_nav_wrap > ul > li:hover {
background: #e7e7e7
}
#primary_nav_wrap > ul > li > a {
display: block;
text-decoration: none;
line-height: 32px;
text-align: center;
font-size: 15px;
color: #16568e;
height: 41px;
padding-top: 10px;
}
#primary_nav_wrap > ul > li > a > p {
margin: 0;
}
#primary_nav_wrap > ul > li > a > span {
font-size: 8px;
color: #ababab;
padding-left: 3px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav id="primary_nav_wrap">
<ul>
<li><p id="title_0"></p><span>&#9660</span></li>
<li><p id="title_1"></p><span>&#9660</span></li>
<li><p id="title_2"></p><span>&#9660</span></li>
<li><p id="title_3"></p><span>&#9660</span></li>
<li><p id="title_4"></p></li>
<li><p id="title_5"></p><span>&#9660</span></li>
<li><p id="title_6"></p><span>&#9660</span></li>
</ul>
</nav>
<button type="submit" id="de">de</button>
<button type="submit" id="ru">ru</button>

Center a horizontal CSS menu

I have a CSS menu using the following CSS.
What is the best way to center the whole menu on the page?
I have tried using another <div> outside <nav> and setting margins but its just aligning left all the time.
nav {
margin: 0 auto;
text-align: center;
border:1px solid black;
}
nav ul ul {
display: none;
}
nav ul li:hover > ul {
display: block;
}
nav ul {
list-style: none;
}
nav ul li {
float: left;
}
nav ul li:hover a {
color: #000000;
}
nav ul li a {
display: block;
padding: 10px 15px;
color: #000000;
text-decoration: none;
}
nav ul ul {
border-radius: 0px;
padding: 0;
position: absolute;
}
nav ul ul li {
float: none;
border-top: 1px solid #000000;
border-bottom: 1px solid #000000;
position: relative;
}
nav ul ul li a {
color: #000000;
}
nav ul ul li a:hover {
color: #666666;
}
nav ul ul ul {
position: absolute;
top:0;
}
jsfiddle : http://jsfiddle.net/njuVm/
You can center the navigation bar by using the following CSS rules:
nav {
margin: 0 auto;
text-align: center;
border:1px solid black;
}
nav ul ul {
display: none;
}
nav ul li:hover > ul {
display: block;
}
nav ul {
list-style: none;
margin: 0; /* << add this */
padding: 0; /* << add this */
display: inline-block; /* << add this */
vertical-align: top; /* << add this */
}
nav ul li {
float: left;
margin: 0; /* << add this */
padding: 0; /* << add this */
}
nav ul li:hover a {
color: #000000;
}
nav ul li a {
display: block;
padding: 10px 15px;
color: #000000;
text-decoration: none;
background-color: pink; /* optional... */
}
nav ul ul {
border-radius: 0px;
padding: 0;
position: absolute;
}
nav ul ul li {
float: none;
border-top: 1px solid #000000;
border-bottom: 1px solid #000000;
position: relative;
}
nav ul ul li a {
color: #000000;
}
nav ul ul li a:hover {
color: #666666;
}
nav ul ul ul {
position: absolute;
top:0;
}
See demo at: http://jsfiddle.net/audetwebdesign/DP6Ax/
The key is to set display: inline-block for nav ul, which will allow your text-align: center rule to take effect.
Make sure to zero out margins and paddings on the ul and li elements. Everything else that you did was more or less right, so you should be good.
Instead of floating the li, you can display them as inline-blocks.
Then, they will be centered relatively to the ul because of text-align: center.
Since the ul is as wide as the nav by default, the li will look like centered relatively to the nav.
nav {
text-align: center;
border: 1px solid black;
}
nav ul {
list-style: none;
padding: 0;
margin: 0;
}
nav > ul > li {
display: inline-block;
}
nav a {
display: block;
padding: 10px 15px;
color: #000000;
text-decoration: none;
}
nav li:hover > ul {
display: block;
}
nav > ul ul {
display: none;
position: absolute;
}
nav > ul ul > li {
border-bottom: 1px solid #000000;
}
nav > ul ul a:hover {
color: #666666;
}
<nav>
<ul>
<li>Add Contact</li>
<li>View Contact</li>
<li>Tickets
<ul>
<li><a>TEST1</a></li>
<li><a>TEST2</a></li>
</ul>
</li>
<li>Invoices</li>
<li>Itemised Calls</li>
</ul>
</nav>
First, when you float the ul's you have to clear the float by adding clear div:
HTML :
<div class="clear"></div>
CSS :
.clear{
clear:both;
}
And for centring the menu you should specify a width of the ul as in example and randomly I have set the width to 560px :
nav ul {
list-style: none;
width : 560px;
margin: 0 auto;
}
Take a Look:
http://jsfiddle.net/njuVm/6/