Image in Navigation Bar Breaks Vertically Centered Text - html

I have this nav bar:
<div id="nav">
Portfolio
<img src="assets/zslogoblack.png">
Contact
</div>
Styled as so:
#nav {
position: fixed;
width: 100%;
}
#nav a {
text-align: center;
display: inline-block;
width: 33%;
}
#nav a:first-of-type,#nav a:last-of-type {
line-height: 60px;
}
#nav img {
height: 60px;
}
Here is the output
I need everything to be vertically aligned within a fixed navbar that is 60px tall, but for some reason, this is not happening. I thought the problem was caused by
line-height:60px;
being applied to the anchor that contained the image, hence the
#nav a:first-of-type,#nav a:last-of-type {
line-height: 60px;
}
But that did not fix the problem. Any help would be very much appreciated.

Try adding 'display:flex;' to '#nav':
#nav
display:flex;
align-items: center;
}
Here is a working example:
#nav {
position: fixed;
width: 100%;
display:flex;
align-items: center;
}
#nav a {
text-align: center;
display: inline-block;
width: 33%;
}
#nav a:first-of-type,#nav a:last-of-type {
line-height: 60px;
}
#nav img {
height: 60px;
}
<div id="nav">
Portfolio<img src="/" style="background:#000;width:50px;">Contact
</div>

Related

Center main menu headers in header of website

I have created a header with one div as a main menu of website and added into it some divs as main menu items. My question is: how to center vertically and horizontally inner divs which are headers of main menu placed in main menu items? I have tried many solutions like transformations and flex style of display but unfortunately nothing works. Why there is not something like Horizontal / VerticalAlignment from XAML in CSS?
body {
margin: 0;
}
header {
height: 50px;
background-color: lightgrey;
}
#main-menu {
margin: 0 auto;
width: 1000px;
height: inherit;
}
.main-menu-item {
float: left;
width: 100px;
height: inherit;
}
.main-menu-header {
display: block;
height: inherit;
}
.main-submenu {
width: inherit;
word-wrap: break-word;
/*display: none;*/
list-style: none;
margin-left: -40px;
margin-top: 0px;
}
<body>
<header>
<div id="main-menu">
<div class="main-menu-item" onmouseover="ChangeDisplayOfSelectedSubmenu(0, 'block')" onmouseout="ChangeDisplayOfSelectedSubmenu(0, 'none')">
<div class="main-menu-header">MenuItem1</div>
<ul class="main-submenu">
<li>SubMenuItem1</li>
<li>SubMenuItem2</li>
<li>SubMenuItem3</li>
</ul>
</div>
<div class="main-menu-item" onmouseover="ChangeDisplayOfSelectedSubmenu(1, 'block')" onmouseout="ChangeDisplayOfSelectedSubmenu(1, 'none')">
<div class="main-menu-header">MenuItem2</div>
<ul class="main-submenu">
<li>SubMenuItem1</li>
<li>SubMenuItem2</li>
<li>SubMenuItem3</li>
</ul>
</div>
<div class="main-menu-item" onmouseover="ChangeDisplayOfSelectedSubmenu(2, 'block')" onmouseout="ChangeDisplayOfSelectedSubmenu(2, 'none')">
<div class="main-menu-header">MenuItem3</div>
<ul class="main-submenu">
<li>SubMenuItem1</li>
<li>SubMenuItem2</li>
<li>SubMenuItem3</li>
</ul>
</div>
</div>
</header>
Use the padding top and padding bottom simply?
Try the CSS line-height Property Follor this : https://www.w3schools.com/cssref/pr_dim_line-height.asp
You can use a css old trick, use this:
header{
height:50px;
display:block;
}
#main-menu{
position:aboslute;
top:50px;
margin-top:-25px;
}
Change your CSS to the following -
body {
margin: 0;
}
header {
height: 50px;
background-color: lightgrey;
}
#main-menu {
height: inherit;
display: flex;
align-items: center;
align-content: center;
justify-content: center;
}
.main-menu-item {
float: left;
width: 100px;
/* height: 100%; */
text-align: center;
}
.main-menu-header {
display: block;
height: inherit;
}
.main-submenu {
width: inherit;
word-wrap: break-word;
/*display: none;*/
list-style: none;
margin-left: -40px;
margin-top: 0px;
}
Using flexbox property here to achieve the desired effect

Evenly spaced <li> inline with fixed image

I have the following elements. The image is fixed at 325X70px and is placed at the top left corner. I want the list items, evenly spaced, to fill the remainder of the width and be responsive to browser resize. I'm sure this is easy, but I can't seem to get it to work.
<div class="wrapper">
<div class="left">
<div class="image">Image Here</div>
</div>
<div class="menu">
<ul>
<li>X</li>
<li>Y</li>
<li>Z</li>
<li>A</li>
</ul>
</div>
</div>`
CSS
* {
padding: 0px;
margin: 0px;
}
.wrapper {
position: relative;
}
.left {
float: left;
}
.image {
min-width: 325px;
max-width: 325px;
height: 70px;
background-color: red;
}
.menu {
width: 100%;
height: 70px;
background-color: black;
}
.menu ul {
position: absolute;
width: 100%;
height: 70px;
display: table;
}
.menu li {
color: #FFF;
width: 25%;
text-align: center;
vertical-align: middle;
display: table-cell;
list-style-type: none;
}
You may not need the .left class, you might be able to just do that styling on the image itself, but regardless, what needs to happen here is that .left and .menu need to be side by side. To do that...
.left,
.menu {
display: inline-block;
vertical-align: middle;}
We know the image is always 325px wide, so let's set the parent container to match...
.left {
width:325px;}
And then we want .menu to be the entire width of the screen, minus that image/container, so can we do this...
.menu {
width: calc(100% - 325px);}
You'll still have to turn your li horizontal...
li {
display: inline-block;
vertical-align:middle;}

Centering a child of an absolute position div tag

Trying to position a horizontal menu over an image.
I have it positioned over the image as of now, but I can't seem to center the li elements to the middle of the screen.
Here is the HTML:
<div id="banner">
<img src="security-people-interior.jpg"/>
</div>
<div id="menu-outer">
<div id="menu">
<ul id="horizontal-list">
<li>About</li>
<li>Products</li>
<li>Monitoring</li>
<li>Testimonials</li>
<li>FAQ</li>
<li>Contact</li>
</ul>
</div>
</div>
The CSS3:
#banner {
display: block;
margin-left: auto;
margin-right: auto;
}
img, #banner {
width: 100%;
}
#menu-outer {
background: url(images/bar-bg.jpg) repeat-x;
z-index: 100;
position: absolute;
top: 25%;
width: 100%;
}
#menu {
????
}
ul#horizontal-list {
/*list-style: none;*/;
}
ul#horizontal-list li {
display: inline;
}
#menu a {
text-decoration: none;
margin: 10px;
float: left;
color: black;
padding: 2px 5px;
text-align: center;
white-space: nowrap;
}
#menu a:hover {
color: #fff;
background: red;
}
The only thing I am able to use is float in #menu{} in the css, but obviously float doesn't have centering.
CSS3 isn't my particular favourite to deal with, so any help would be greatful thanks.
Here are two options:
Set the display of the #horizontal-list element to inline-block and then use text-align: center on the parent element in order to center it.
Example Here
#menu {
text-align: center;
}
#horizontal-list {
display: inline-block;
}
Alternatively, using flexboxes:
Example Here
#menu {
display: flex;
justify-content: center;
}

Put list with position absolute into middle of div with CSS

I have small problem with some design.
I have this very simple html:
<div>
<ul>
<li></li>
<li></li>
<li></li>
</ul>
</div>
This is just a small part of a bigger widget. To make thsi widget work, I need at least this css:
div {
position: relative;
width: 400px;
height: 200px;
}
ul {
z-index: 99;
}
li {
display: block;
float: left;
width: 16px;
height: 16px;
}
Now I want to put list down into the midst of div. There is no problem with putting it down, but it is impossible for me to put it into middle. List can have any number of items.
JSfiddle: http://jsfiddle.net/MBxNU/1/
So far, I tried for example:
ul {
width: 100%;
display: block;
text-align: center;
}
But it didnt work and I have no clue why.
If you could give me some help, I'd appreciate it.
Your code with text-align: center doesn't work because you have floated items inside ul. You can use display: inline-block instead of float:
li {
display: inline-block;
width: 16px;
height: 16px;
background-color: red;
margin: 5px;
}
JSfiddle: http://jsfiddle.net/caprella/r2RjM/1/
Here you are ;)
div {
position: relative;
left: 20px;
top: 20px;
background-color: #EEE;
width: 400px;
text-align: center;
height: 200px;
}
ul {
padding: 0;
display: inline-block;
z-index: 99;
list-style: none inside;
}
li {
float: left;
width: 16px;
height: 16px;
background-color: red;
margin: 5px;
}
So the main idea is to set text-align: center for parent div and display: inline-block for ul, that's it )

Centering header (includes logo & navigation)

I'm working on the header of a website. I've looked around stackoverflow for instructions on how to center the header (includes logo & navigation bar).
I'm using Dreamweaver CC and when I click the preview button, it shows up on the browser centered, but the right has more white space than the left.
My current CSS:
.container {
width: 1000px;
margin-right: auto;
margin-left: auto;
text-align: center;
}
.header_left {
float: left;
width: 300px;
}
.navi {
float: right;
width: 600px;
}
.navi li {
list-style: none;
display: inline;
}
My Current HTML:
<body id="home">
<div id="header">
<div class="container">
<div class="header_left">
<img src="../images/bestfoodservicesweb_04.jpg" width="208" height="69"/>
</div>
<div class="header_right">
<ul class="navi">
<li><img src="../images/bestfoodservicesweb_07.jpg" width="88" height="56"/></li>
<li><img src="../images/bestfoodservicesweb_09.jpg" width="88" height="56"/></li>
<li><img src="../images/bestfoodservicesweb_11.jpg" width="88" height="56"></li>
<li><img src="../images/bestfoodservicesweb_13.jpg" width="88" height="56"></li>
</ul>
<div style="clear: both"></div>
</div>
</div>
EDIT: Sample of what it looks like
Trying to understand the problem. The header as a whole is centered. The elements inside have margin issues due to specifying width on the images and then giving the class a different width as well. You can remove the width in the class and it will push each floated element flush to the their specified sides. Then add margin to push them the distance you would like:
body, html {
width: 100%;
height: auto;
margin: 0;
}
.container {
background: #333;
width: 1000px;
margin: auto;
text-align: center;
}
.header_left {
float: left;
margin-left: 70px;
margin-top: 12px;
}
.navi {
float: right;
margin-right: 60px;
}
.navi li {
list-style: none;
display: inline;
}
http://jsfiddle.net/derekstory/zz2Dy/3/
text-align:center and float don't make good friends :)
test this : setting ul as inline-block element and not floatting: http://jsfiddle.net/zz2Dy/2/
.container {
width: 1000px;
margin-right: auto;
margin-left: auto;
text-align: center;
background:#333;
}
.header_left {
float: left;
}
.navi {
display:inline-block;
padding:0;
margin:0;
}
.navi li {
list-style: none;
display: inline;
}
The right header element has the property text-align: center, and it doesn't occupy the entire width of the element, so it ends up with more white space on the right. If you add to your fiddle the class:
.header_right {
text-align: right;
}
That should remove the white space on the right.
(if I understood your issue properly)
I believe this is what you are looking for:
.container {
margin: 0 auto;
text-align: center;
}
.header_left {
display: inline-block;
}
.header_right {
display: inline-block;
}
.navi {
display: inline-block;
padding: 0;
}
.navi li {
list-style: none;
display: inline;
}
Demo
Basically, I've removed floats and widths and padding, used display: inline-block;.
<img src="../images/bestfoodservicesweb_07.jpg" style=" display: block;
margin-left: auto;
margin-right: auto;"/>