Vertical center of menu in a header - css

I am not able to center my menu in a header-bar.
I've tried display: table-cell and display: table but it doesn't work. The only thing I can do is a fixed margin-top, which I want to avoid.
<header>
<div class="container">
<div id="logo"></div>
<div id="mainnav">
<div class="clearfix">
<?php wp_nav_menu( array( 'theme_location' => 'header-menu', 'depth' => 1)); ?>
</di>
</div>
</div>
</header>
div#mainnav {
height: 120px;
width: 70%;
float: right;
background-color: red;
display:table;
}
div.menu {
background-color: yellow;
display:table-cell;
vertical-align:middle;
text-align:center; }
/* clearfix */
div.menu ul:after {
content: " "; /* Older browser do not support empty content */
visibility: hidden;
display: block;
height: 0;
clear: both;
}
I used cleafix to have a div.menu height (it was 0 before).

The centered (table-cell) div needs to be directly under the container (table) div.
You seem to have no .menu in the code, or maybe the PHP generates it - either way, it's nested inside. You might want to make the #mainnav itself as the clearfix, so that the children below it will be directly there.
<div id="mainnav" class="clearfix">
<?php wp_nav_menu( array( 'theme_location' => 'header-menu', 'depth' => 1)); ?>
</div>
Here's an example:
#mainnav {
height: 50px;
border: 1px solid #ccc;
display: table;
padding: 20px;
}
#mainnav .menu-item {
display: table-cell;
vertical-align: middle;
}
<div id="mainnav">
<div class="menu-item">My menu item</div>
</div>

try the code like this instead of display property. Is there any reason to use "display:table"?
UPDATED CSS
div.menu {
background-color: yellow;
display: table-cell;
vertical-align: middle;
text-align: center;
height: 120px;}

Change your css
div.menu {
background-color: yellow;
vertical-align: middle;
text-align: center;
margin: 30px 0;
padding: 0px 20px 0px 0px;
}
and
ul, ol {
margin-top: 0;
margin-bottom: 10px;
margin: 0 auto;
padding: 0px;
}
ul use default margin & padding if not set.

Related

How can you use flexbox method to center div(second child) inside another div (parent), but not let it be affected by (first) child? [duplicate]

This question already has answers here:
Center one and right/left align other flexbox element
(11 answers)
Closed 10 months ago.
In my case, flexbox method works very well for centering the navbar as the navbar has fixed positioning. What I don't want is the other div that's at the same level as the div I am having centered skew the div I want centered; I want it centered to the entire document, not just the area of the parent div that it is allowed.
body {
margin:0;
}
.navbar {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333333;
position: fixed;
top: 0;
width: 100%;
font-family:'Smooch Sans';
font-size:1.2em;
}
.navbar-item {
float: left;
}
.navbar a {
display: block;
color: white;
text-align: center;
padding: 6px 13px;
text-decoration: none;
margin:0.5rem;
border-radius:10px;
transition: 0.4s;
}
.navbar a:hover:not(.active) {
background-color: #111;
}
.navbar a.active:hover { background-color:#048f5c;
}
.active {
background-color: #04AA6D;
}
div.navbar-options {
position:relative;
display: flex;
align-items: center;
justify-content: center;
}
div#navbar-left-title {
color:white;
}
<!DOCTYPE html>
<html>
<head>
<style>
#import url('https://fonts.googleapis.com/css2?family=Smooch+Sans&display=swap');
</style>
</head>
<body>
<div class="navbar">
<div id="navbar-left-title" style="width:fit-contents;float:left;">The Website Name</div>
<div class="navbar-options">
<div class="navbar-item"><a class="active" href="#home">Home</a></div>
<div class="navbar-item">News</div>
<div class="navbar-item">Contact</div>
<div class="navbar-item">About</div>
</div>
</div>
<div style="padding:20px;margin-top:30px;background-color:#1abc9c;height:1500px;">
</div>
</body>
</html>
this is all of the code. Sorry all the styling clogs it up. So I don't want the div#navbar-left-title to skew the div.navbar-options off the absolute center, in conclusion. Haven't been able to find any solution!
link to the general idea off of tesla
A simple solution is to add position: absolute to #navbar-left-title as below.
body {
margin: 0;
}
.navbar {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333333;
position: fixed;
top: 0;
width: 100%;
font-family: 'Smooch Sans';
font-size: 1.2em;
}
.navbar-item {
float: left;
}
.navbar a {
display: block;
color: white;
text-align: center;
padding: 6px 13px;
text-decoration: none;
margin: 0.5rem;
border-radius: 10px;
transition: 0.4s;
}
.navbar a:hover:not(.active) {
background-color: #111;
}
.navbar a.active:hover {
background-color: #048f5c;
}
.active {
background-color: #04AA6D;
}
div.navbar-options {
position: relative;
display: flex;
align-items: center;
justify-content: center;
}
div#navbar-left-title {
color: white;
width: fit-contents;
float: left;
position: absolute;
}
<!DOCTYPE html>
<html>
<head>
<style>
#import url('https://fonts.googleapis.com/css2?family=Smooch+Sans&display=swap');
</style>
</head>
<body>
<div class="navbar">
<div id="navbar-left-title">The Website Name</div>
<div class="navbar-options">
<div class="navbar-item"><a class="active" href="#home">Home</a></div>
<div class="navbar-item">News</div>
<div class="navbar-item">Contact</div>
<div class="navbar-item">About</div>
</div>
</div>
<div style="padding:20px;margin-top:30px;background-color:#1abc9c;height:1500px;">
</div>
</body>
</html>
A more complicated solution would be to set up a grid with three columns such as
<navbar>
<div class="left">
...
</div>
<div class="center">
...
</div>
<div class="right">
...
</div>
</navbar>
and set it up so that left and right have the same width always, so the center element can fill the remaining space.

How to center buttons in navigation bar?

What should I do to align the first 3 buttons in the center of the bar?
#navbar{
list-style-type: none;
position: relative;
top: -4px;
padding: 0;
margin: 0;
overflow: hidden;
background-color: #212121;
}
In your navigation bar you can put these three in one separate div and then align it to center
Hello friends to center a that your
menu options (nav) you only need
to use the text-aling rule: center;
,why ? Because the menu list are
formed by '' li '' or '' ul '' and we
only apply this rule to list and
ready :), greetings from mexico;)
<html>
<head>
<style>
body{
margin: 0px;
}
.navbar{
height: 60px;
background-color: #bbb;
line-height: 60px; //this will center inline elements in your navbar
}
.container{
margin-left: 5%;
margin-right: 5%;
}
.btn{
background-color: white;
padding: 10px 20px;
width: 100px;
border: none;
border-radius: 2px;
display: inline;
}
.nav{
display: inline;
list-style-type: none;
}
.nav li{
display: inline;
}
</style>
</head>
<body>
<!--Navbar-->
<nav class="navbar">
<div class="container">
<ul class="nav">
<li><button class="btn">Button1</button></li>
<li><button class="btn">Button2</button></li>
<li><a class="btn"><a> Button</a></li>
</ul>
</div>
</nav>
</body>
The above code should work for you. I have shown buttons using anchor tag and also button tag

How would I horizontally center this ul inside this div?

JSFiddle
HTML
<div id="geometric-nav" class="left template-column cfix template-column-2" sort_key="nav">
<div class="geometric-nav-wrap cfix" style="
">
<ul id="geometric-nav-links" class="nav-links expandable">
<!-- page links are appended to this element -->
<li class="page page-896547 page-work" page_id="896547" nav-link="true" nav_link_tmpl="true" style="display: none;">
<div class="nav-link-container">
Gallery</div></li><li class="page page-896549 page-custom first-child" page_id="896549" nav-link="true" nav_link_tmpl="true">
<div class="nav-link-container">
About</div></li><li class="page page-896548 page-custom last-child" page_id="896548" nav-link="true" nav_link_tmpl="true">
<div class="nav-link-container">
Contact</div></li><li class="page page-896551 page-resume" page_id="896551" nav-link="true" nav_link_tmpl="true" style="display: none;">
<div class="nav-link-container">
Resume</div></li></ul>
</div> <!--#geometric-nav-wrap-->
</div>
CSS
#geometric-nav {
background: #222;
height: 40px;
width: 100%;
margin-top: 10px;
}
.template-column-2 #geometric-nav-links li {
margin-left: 15px;
float: left;
list-style: none;
}
This solution treats the li items as inline-block elements and uses the ul container to center the elements.
added:
text-align: center
to #geometric-nav
and
display: inline-block
to .template-column-2 #geometric-nav-links li
http://jsfiddle.net/gZB6Y/
google would have helped you ;)
You can use :table; for ul and reset margin to auto; it will shrink on it's content and will center:
.template-column-2 #geometric-nav-links {
display:table;
margin:auto;
}
DEMO
Or set display:inline-block to li and text-align:center to ul
DEMO 2
.template-column-2 #geometric-nav-links li {
margin-left: 15px;
display:inline-block;
list-style: none;
}
.template-column-2 #geometric-nav-links {
text-align:center;
}
Or set display:inline-block to ul and text-align:center to #geometric-nav.
DEMO 3
#geometric-nav {
background: #222;
height: 40px;
width: 100%;
margin-top: 10px;
text-align:center;
}
.template-column-2 #geometric-nav-links li {
margin-left: 15px;
float: left;
list-style: none;
}
.template-column-2 #geometric-nav-links {
display:inline-block;
}

equivalent tr of CSS?

How do you separate the menu bar from the body in a div, to place everything after contact below it, is there a corresponding code like a newline? I would really appreciate the help :) Thanks in advance
here's a link of picture shot:
CSS
/* because of the * default code it takes out all margin and padding */
* {
margin: 0px;
padding: 0px;
}
#container {
display: table;
}
#row {
display: table-row;
}
#left, #right, #middle {
display: table-cell;
}
#row {
display: table-row;
}
#left, #right, #middle {
display: table-cell;
}
body {
font-family: verdana;
font-size: 10px;
background-color: ABC;
padding: 50px;
margin: auto;
}
h1 {
text-align: center;
}
ul {
list-style: none;
margin: 0;
padding: 0;
}
li {
float: left;
position: relative;
}
li + li {
border-left: 1px solid #ccc;
}
a {
display: block;
padding: 7px 10px;
color: #222; /*changes the color of all item font color in menu bar */
background: #eee; /*changes the background color of Menu bar */
text-decoration: none;
}
a:hover {
color: #fff;
background: #666; /* changes hover bg color of any menu item being pointed*/
}
a:active {
color: #f2f75e;
background: #0090cf;
}
/* Child Menu Styles */
.level-two {
position: absolute;
top: 100%;
left: -9999px;
width: 100px;
}
li:hover .level-two {
left: 0;
}
.level-two li {
width: 100%;
border: 0;
border-top: 1px solid #ccc;
}
HTML
<h1>
<ul class="level-one">
<li> Home </li>
<li> Drops
<ul class="level-two">
<li> One </li>
<li> Two </li>
<li> Three </li>
</ul>
</li>
<li> Contact </li>
</ul>
</div>
<div id="container">
<div id="row">
<div id="left">
<h4>Left Col</h4>
<p>...</p>
</div>
<div id="middle">
<h4>Middle Col</h4>
<p>...</p>
</div>
<div id="right">
<h4>Right Col</h4>
<p>...</p>
</div>
</div>
</div>
</h1>
add clearfix class on both of .
DEMO
.clearfix{
clear:both;
}
DEMO1
One alternative to the clear property is to trigger a new block formatting context on the menu in order to contain the floats inside .level-one :
.level-one {
/* trigger block formatting context to contain floats. */
overflow: hidden;
}
Demo at http://jsfiddle.net/mrYdV/1/
Here is a list of other property/value pairs that trigger block formatting context
W3C specification
Bulletproof backwards-compatible version
There is a great answer with more details covering this method at How does the CSS Block Formatting Context work?
The clear property will do this for you. You can add it to your #container for example:
#container {
display: table;
clear:both;
}
Clear means something like:
clear all elements on both sides of this element

how to center css navigation bar

i am trying to center my css navigation bar but cant figure out why is not working, where am i doing wrong. i want it in the top center of the page.
i tried margin:0 auto but it wont work
here is my code:
<style>
ul {
list-style-type: none;
padding: 0;
overflow:hidden;
}
a:link,a:visited {
margin:0 auto;
display:block;
width: 120px;
font-weight:bold;
color:#FFFFFF;
text-align:center;
padding:4px;
text-decoration:none;
text-transform:uppercase;
}
a:hover, a:active {
background-color:#7A991A;
}
li {
float: left;
}
#menu {
background-color:#98bf21;
}
</style>
<div id="menu">
<header>
<ul>
<li>Home</li>
<li>News</li>
<li>Articles</li>
<li>Forum</li>
<li>Contact</li>
<li>About</li>
</ul>
</header>
</div>
Change your last two CSS rules to:
li {
display:inline-block;
}
#menu {
background-color:#98bf21;
text-align:center;
}
jsFiddle example
margin: 0 auto; only works on items that have defined widths. However, the way you currently have it written, each a tag is attempting to center, when you really want to center the ul. Here is a great way to center that when you don't know the width of your ul. Use these styles in your header, ul and li elements. Add styling to your a tags to suit.
header {
float: left;
width: 100%;
overflow: hidden;
position: relative;
}
ul {
float: left;
list-style: none;
position: relative;
left: 50%;
}
li {
display: block;
float: left;
position: relative;
right: 50%;
}
What's going on here is we're setting the header to full width, and then pushing the ul half way across the width of the browser. Then we push the li's back half the width of the ul, which now centers them on the page.
Here is a link with a very helpful tutorial about doing this very thing: http://matthewjamestaylor.com/blog/beautiful-css-centered-menus-no-hacks-full-cross-browser-support
Good luck!
Use the inline-block css magic :)
JSFiddle
li {
display: inline-block;
}
#menu {
background-color:#98bf21;
margin: 0 auto;
text-align: center;
width: 80%;
}
I had the same problem until I read this post and decided to center the text in the "nav".
So basically your ul should be in a nav so you can text-align: center the nav area.
nav {
width: 75%;
margin: auto
}
#menu {background-color: #98bf21}
.container{padding: 0.10em}
.cell-row {display:table; width:100%}
.cell {display: table-cell}
.cell-middle {vertical-align: middle}
a:link, a:visited {
font-weight: bold;
color: black;
text-align: center;
padding: 4px;
text-decoration: none;
text-transform: uppercase;
}
a:hover, a:active {background-color: #7A991A}
#media (max-width: 500px) {
.mobile {
display: block;
width: 100%;
text-align: center
}
nav {
width: 100%;
margin: auto
}
}
<nav>
<div id="menu" class="cell-row" style="text-align: center">
<div class="container cell cell-middle mobile">Home</div>
<div class="container cell cell-middle mobile">News</div>
<div class="container cell cell-middle mobile">Articles</div>
<div class="container cell cell-middle mobile">Forum</div>
<div class="container cell cell-middle mobile">Contact</div>
<div class="container cell cell-middle mobile">About</div>
</div>
</nav>
Add the following css:
ul {
margin: 0 auto;
display: inline-block;
}
This will make the ul fit its contents and then be centered in its container.

Resources