Vertical align nav in header along with the title - html

I cannot seem to get the navigation to vertical-align: middle; along with the title. Any idea how to fix this issue?
https://jsfiddle.net/ozvmkeaq/
header,
nav {
height: 80px;
}
header {
background-color: #E74C3C;
vertical-align: middle;
}
header nav {
float: right;
list-style: center;
text-transform: uppercase;
color: #ECF0F1;
vertical-align: middle;
}
nav li {
display: inline;
padding: 10px;
}
nav a {
display: inline-block;
text-decoration: none;
}
.title {
float: left;
}
.title h1 a {
color: #ECF0F1;
text-decoration: none;
padding: 15px;
}
<header>
<div class="title">
<h1> HTTP</h1>
</div>
<nav>
<ul>
<li class="active">Home</li>
<li>Test</li>
<li>Test</li>
<li>Test</li>
</ul>
</nav>
</header>

By adding the following CSS, the links vertically center.
ul { margin: 0; height: 100%; display: table; width: 100%;}
nav ul li { height: 100%; vertical-align: middle; display: table-cell;}
I've also updated your fiddle: https://jsfiddle.net/ozvmkeaq/2/
Basically, you need the combination of table and vertical-align properties to tell the browser you want the content in the middle.
As as an alternative, you can also use flex-box.
display: flex;
align-items: center;

You need to set vertical-align:middle to the list items themselves. I would also set it to the links directly to compensate for any list item padding etc.
For example:
nav ul li, nav ul li a{vertical-align:middle;}
This is because vertical-align sets the position based on the parent element, so you need to set it to the child element (different from properties such as text-align where you can set the style to the parent element to affect the elements within).
See more on vertical align at the W3C vertical-align property guide.
Or Try it here.

Try this
header {
background-color: #E74C3C;
height: 5em;
position: relative;
}
header * {
padding: 0;
margin: 0;
}
header a {
text-decoration: none;
}
header nav, header .title {
display: block;
height: 100%;
}
header .title {
position: absolute;
top: 0;
left: 0;
}
header .title h1 {
position: relative;
top: 0.70em;
}
header .title a {
color: #ECF0F1;
margin: 0 0.5em;
}
header nav {
position: absolute;
right: 0;
top: 0;
padding: 0 0.5em;
}
header ul {
list-style-type: none;
list-style-position: inside;
position: relative;
top: 2em;
right: 0;
}
header ul li {
display: inline-block;
padding-right: 0.25em;
}

update this 2 style and it should do the trick.
nav ul
{
padding: 0px;
margin: 0px;
}
nav li
{
display:inline-block;
padding-right:10px;
padding-left:10px;
line-height: 80px;
background-color: green;
}

Related

How do I arrange the navbar so that it is spaced out evenly?

I have tried to use the margin and padding tags, but I can never get it right because screen sizes change, and that makes the arrangements change. Also tried using flex
I am trying to make a navigation bar (top) in HTML and CSS, but I want to make all the tags uniform in spacing so it looks nicer.
Right now it looks like this:
Here is my HTML code:
<div class="header">
<div class="container">
<h1 class="logo"></h1>
<div class="nav">
<ul>
<li>Home</li>
<li>History</li>
<li>Where to Eat</li>
<li>Places to visit</li>
<li>Beauties of Nature</li>
<li>How to navigate Seoul</li>
</ul>
</div>
</div>
</div>
CSS:
.container {
width: 80%;
margin: 0 auto;
}
.header {
background: #293380;
}
.header::after {
content: '';
display: table;
clear: both;
}
.logo {
float: left;
padding: 10px 0;
}
.nav {
float: right;
}
.nav ul {
display: flex;
flex-direction: row;
margin: 0;
padding: 0;
list-style: none;
}
.nav li {
flex: 1;
display: inline-block;
margin-left: 70px;
padding-top: 23px;
position: relative;
}
.nav a {
color: #F0EFF7;
text-decoration: none;
text-transform: uppercase;
font-size: 14px;
}
.nav a:hover {
color: #953D60;
}
.nav a::before {
content: '';
display: block;
height: 5px;
background-color: #F7D9F1;
position: absolute;
top: 0;
width: 0%;
in your css file, under .nav ul, write justify-content: space-between;, which spaces all the elements evenly.
I really recommend you look further into justify-content, since its the main way to space out the content the way you want. Really good tool

Text inside li navigation bar on two lines in Chrome/ Safari but one line in Firefox

I'm not sure why, but past a certain font size the text inside my navigation bar shows up on two lines. The box size isn't being updated for some reason in Chrome and Safari but still works fine in Firefox.
Firefox
Chrome
What would be the difference between these web browsers that would have such an effect on my code?
<nav id="topTab">
<ul>
<li>page1</li>
<li>page2</li>
<li>page3</li>
</ul>
<div>
<h1>
<b href="http://localhost:8000/home.html" title="Home">Example1</b></h1>
</div>
</nav>
CSS:
#media only screen and (min-width : 1024px) {
a {
background: #fcfcfc;
color: #000;
text-align: center;
text-decoration: none;
font-family: 'Gloria Hallelujah';
}
#topTab{
position:relative;
}
nav#topTab {
float: left;
width: 100%;
overflow: hidden;
}
nav#topTab ul {
float: left;
clear: left;
position: relative;
list-style: none;
margin: 0;
padding: 0;
left: 50%;
}
nav#topTab ul li {
display: block;
float: left;
list-style: none;
margin: 0;
padding: 0;
position: relative;
right: 50%;
}
nav#topTab ul li a {
display: block;
padding: 0 5% 0 5%;
margin: 0 15% 0 3%;
font-size: 2.2em;
}
nav#topTab ul li a:hover {
background: #000;
color: #fff;
}
h1 {
position: relative;
text-align: center;
top: 20%;
}
h1 b {
font-size: 2.3em;
text-decoration: none;
color: black;
font-weight: bold;
font-family: 'Caveat Brush';
}
}
Your unordered list is floated. Floating an element removes it from the "natural" flow of the document and as a consequence, your text is trying to adjust to this "unnatural" flow.
You have to clear your floats to restore the flow again. This can be done by adding an element with clear: both style attached to it. In this case, you would add clear both to your div wrapping the heading tag.
div {clear: both}

<span> and <ul> vertical alignment

I have a <span> element followed by a horizontal <ul> element. Why is there a left and bottom margin on the <ul>? How can I remove this space?
jsfiddle
HTML
<span>AAAAA</span>
<ul>
<li>BBBBB</li>
<li>CCCC</li>
</ul>
CSS
body {
background-color: #000;
margin: 0;
padding: 0;
}
span {
background-color: #f00;
}
ul {
background-color: #0f0;
display: inline-block;
list-style: none;
margin: 0;
padding: 0;
}
ul > li {
float: left;
padding-right: 1em;
}
Check this out!
Added inline-block to the span and vertical-align too.
The span has a visual margin / space because it is not inline-block and the ul next to it but is inline-block
Is this what you expected? Please let me know your feedback on this. Thanks!
body {
background-color: #000;
margin: 0;
padding: 0;
}
span {
background-color: #f00;
display: inline-block;
vertical-align: top;
}
ul {
background-color: #0f0;
display: inline-block;
vertical-align: top;
list-style: none;
margin: 0;
padding: 0;
}
ul > li {
float: left;
padding-right: 1em;
}
<span>AAAAA</span>
<ul>
<li>BBBBB</li>
<li>CCCC</li>
</ul>
As the other users already stated, display: inline-block; should do the trick, but your fiddle seems like you already got that right. To find out where unexpected margins, borders or other css stuff come from, I rely on firebug (Firefox Plugin for web developers).
while you are using display inline-block give font-size: 0; to the parent element.
Try this code...
body {
background-color: #000;
margin: 0;
padding: 0;
font-size: 0;
}
span {
background-color: #f00;
font-size: 14px;
display: inline-block;
vertical-align: top;
}
ul {
background-color: #0f0;
display: inline-block;
list-style: none;
margin: 0;
padding: 0;
}
ul > li {
float: left;
padding-right: 1em;
font-size: 14px;
}

How to make top navigation vertically center with the logo?

I am trying to make the top menu vertically center without assigning value like margin-top: 50px; because some of my friends say this is not the ideal approach.
/* Nav Section */
.nav {
width: 100%;
padding: 0;
margin: 0;
}
.nav-contain {
width: 1100px;
margin: 0 auto;
padding: 0;
overflow: hidden;
}
.logo {
z-index: 10;
display: inline-block;
background: #2980B9;
padding: 65px 50px 35px 45px;
font-size: 36px;
line-height: 42px;
color: #fff;
text-align: center;
}
.logo a {
color: #FFFFFF;
text-decoration: none;
}
#medical {
display: block;
text-transform: uppercase;
}
.menu {
padding: 0;
margin: 0;
float: right;
display: table-cell;
position: relative;
}
.menu a {
text-decoration: none;
color: #505050;
font-weight: bold;
}
.menu ul {
padding: 0;
margin: 0;
float: left;
top: 50%;
}
.menu ul ul {
padding: 0;
margin: 0;
}
.menu ul li {
float: left;
display: block;
margin-left: 45px;
}
.menu ul ul {
position: absolute;
left: -999px;
}
.menu ul li:hover ul {
left: auto;
}
.menu ul li ul li {
margin-left: 0;
float: none;
margin-top: 15px;
}
<div class="nav">
<div class="nav-contain">
<div class="logo">
<span id="medical">Medical</span><span id="company"> Company</span>
</div>
<!-- Logo -->
<div class="menu">
<ul>
<li>Home</li>
<li>About
<ul class="dropdown">
<li>Sample</li>
<li>Sample</li>
</ul>
</li>
<li>Gallery</li>
<li>Prices</li>
<li>Contact</li>
</ul>
</div>
<!-- Menu -->
</div>
<!-- Nav Contain -->
</div>
<!-- Nav -->
Remove float:right on .menu, and set both .logo and .menu to this:
.logo, .menu {
display: inline-block;
vertical-align: middle;
}
If you need .menu to stay on far right side, also add this:
.nav-contain {
text-align: justify;
}
.nav-contain:after{
content: '';
display: inline-block;
width: 100%;
}
How it works:
Set text-align: justify; will line up the two inner inline blocks to the left and right edges of the container.
Create an invisible 100% width element by using :after or :before pseudo-element stretching the box to occupy the entire space of the container. Otherwise inline element occupies only the space bounded by the tags that define the inline element.
One easy way to center here is to use Flexbox:
.nav-contain {
/* what is already there */
display: flex;
align-items: center;
}
Beware of browser support (check caniuse.com to see if the compatibility level is acceptable to you).
This is superior to the margin-top solution as it ensures that you won't have to manually change that 50px each time the size of the image or anything else in the navbar changes.
Try:
.menu > ul > li {
min-height:50px;
display: table;
}
.menu > ul > li > a {
display: table-cell;
vertical-align: middle;
}
http://jsfiddle.net/rawat/4h05rq2s/
Since your navbar remains the same height the whole time, I suggest you give the .nav-contain the following code:
.nav-contain {
width: 1100px;
margin: 0 auto;
line-height: 184px;
padding: 0;
overflow: hidden;
}
Note the line-height.
This will, once you smaller the available width of your device, result in a probably not so nice looking huge navigation bar. For this, I suggest media queries.

centered horizontal navigation bar css

I know there are a lot of answers with this subject but it still doesn't work somehow with my code.
Every time I try to center the text with text-align center it doesn't work unless I remove float: left, display: inline or display: inline-block. But then I have a vertical centered navigation bar...
Can anyone help me with this?
This is my HTML code:
<div class="nav">
<ul>
<li>Home</li>
<li>About</li>
<li>Moviekids festivals</li>
</ul>
</div>
and here is the CSS code:
*{
margin: 0px;
padding: 0px;
}
body {
text-align: center;
font-family: arial;
background-color: white;
}
#wrapper{
width: 960px;
margin: 0 auto;
height: 100%;
text-align: left;
background-color: #1d1d1b;
}
.nav {
background-image:url("img/nav.jpg");
width: 100%;
height: 50px;
display: inline-block;
}
.nav ul {
list-style-type: none;
}
.nav li {
display: inline;
text-align: center;
}
.nav a {
text-decoration: none;
display: inline-block;
color: #1d1d1b;
padding: 10px;
}
You need to set the text-align on the parent element like so:
.nav ul {
list-style-type: none;
text-align: center;
}
.nav li {
display: inline-block;
}
See Demo
I just edited your css to acheive what you need
.nav ul {
list-style-type: none;
text-align:center;
}
.nav li {
display:inline-block;
text-align: center;
padding: 10px;
}
.nav a {
text-decoration: none;
display: block;
color: #1d1d1b;
}
DEMO
Always avoid using body text-align try using instead for wrapper.
Make sure you have mentioned <!DOCTYPE html> on the top of your HTML code.
Otherwise your code seems perfect.