Cannot align inline-block image and nav buttons - html

For some reason or another my navigation buttons are being pushed down by the image. I can solve the issue by floating the image but that does not interest me.
CSS:
.wrapped {
position: relative;
width: 70%;
margin: 0 auto;
}
.header {
width: 70%;
margin: 0 auto;
}
.headernav {
width: 70%;
margin: 0 auto;
height: 120px;
}
.logo, .logo img {
display: inline-block;
margin-right: 30px;
}
.nav {
padding-left: 30px;
display: inline-block;
height: 120px;
line-height: 120px;
vertical-align: middle;
}
.navbutton {
padding: 5px 30px;
display: inline-block;
}
#fitness, #wcontrol, #recipes, #supplementation {
text-align: center;
height: 36px;
line-height: 36px;
font-weight: bold;
}
HTML:
<div id="wrapperheader">
<div class="headernav">
<div class="logo"><img src="http://protein.guru/images/PGlogo.png" alt="Protein.guru Logo" class="logo"></div>
<div class="nav">
<div class="navbutton"><div id="recipes">Recipes</div></div>
<div class="navbutton"><div id="fitness">Fitness</div></div>
<div class="navbutton"><div id="wcontrol">Weight Control</div></div>
<div class="navbutton"><div id="supplementation">Supplementation</div></div>
</div>
</div>
</div>
If anyone knows why this is happening or of a simple solution, please let me know. Sorry if its an obvious answer.. I'm been staring at the code on and off for a day and it's just not clicking.

You could simplify your HTML & CSS to remove div soup, by moving the logo into the nav bar:
Usually the logo would be wrapped with an anchor tag, so that clicking on it will return the user to the home page. So it becomes a nav element.
HTML:
<div class="nav">
<div class="navlogo"><img src="http://protein.guru/images/PGlogo.png" alt="Protein.guru Logo" class="logo"></div>
<div class="navbutton"><span id="recipes">Recipes</span></div>
<div class="navbutton"><span id="fitness">Fitness</span></div>
<div class="navbutton"><span id="wcontrol">Weight Control</span></div>
<div class="navbutton"><span id="supplementation">Supplementation</span></div>
</div>
The HTML above uses spans, but you can also use <... class="navbutton" id="recipes">Recipes</...>, as shown below.
Optimized HTML: Generally people create UL LI tag stacks for nav bars.
<ul class="nav">
<li class="navlogo"><img src="http://protein.guru/images/PGlogo.png" alt="Protein.guru Logo" class="logo"></li>
<li class="navbutton" id="recipes">Recipes</li>
<li class="navbutton" id="fitness">Fitness</li>
<li class="navbutton" id="wcontrol">Weight Control</li>
<li class="navbutton" id="supplementation">Supplementation</li>
</ul>
Then cleaning up the CSS, will remove problems with applying duplicate right margins & paddings to both the .logo & .logo .img tags. In your example, there is also a left margin on the nav element, which was creating extra margin space between the logo & the 1st link.
In this CSS example below, I've also thrown in a vertical-align:middle; property, so that the nav links center vertically next to a larger image logo. See this jsfiddle.
CSS:
.nav {
height: 120px;
line-height: 120px;
margin: 0 auto;
vertical-align: middle;
width: 70%;
}
.nav li {
display: inline-block;
list-style-type: none; /* removes bullets */
}
.nav .logo,
.nav .navlogo,
.nav .navbutton {
display: inline-block;
}
.nav .logo {
vertical-align: middle;
}
.nav .navbutton {
font-weight: bold;
height: 36px;
line-height: 36px;
padding: 5px 0 0 30px;
text-align: center;
}

If you wants to keep width: 70% of your .wrapped container, you have to reduce padding of your nav menu and size of your image.
See this fiddle
(It works on large size of the window)
Otherelse, you can put a fixed width of the .wrapped container but still have to consider padding of your nav menu and width of the image to make it fits.
.navbutton {
padding: 5px 25px;
display: inline-block;
}

Related

Line up Title inside navbar

Trying to get a title to appear inline between the Logo and the navbar links. I'm trying to learn without bootstrap first and ideally without flexbox (want to know the basics first).
I've been trying 'display: inline' and different 'position' values in different spots but I'm getting lost.
#header-img {
margin-left: 20px;
margin-top: 10px;
height: 100px;
width: 100px;
float: left;
}
#nav-bar {
text-align: right;
padding: 20px;
background-color: #A7A7A9;
}
li {
display: inline;
list-style: none;
}
ul {
top: 5px;
left: 10px;
}
.nav-link {
width: auto;
height: 50px;
margin-left: 40px;
margin-top: 25px;
display: inline-block;
color: #453F3C;
font-size: 20px;
font-weight: 500;
letter-spacing: .9px;
text-decoration: none;
}
<header id="header">
<div class="logo">
<img id="header-img" alt="company logo" src="https://preview.ibb.co/jabJYd/rocket_1976107_1280.png">
</div>
<h1>Title</h1>
<nav id="nav-bar">
<ul>
<li><a class="nav-link" href="#our-services">Our Services</a></li>
<li><a class="nav-link" href="#reviews">Reviews</a></li>
<li><a class="nav-link" href="#locations">Locations</a></li>
</ul>
</nav>
</header>
to learn more about css positioning use the following link : https://www.w3schools.com/Css/css_positioning.asp and to learn more about positioning read this article : https://www.w3schools.com/Css/css_inline-block.asp
You also need to look at using percentages for your widths.
In your code, the navbar and the title are the two parent elements which need to be positioned and assigned widths in percentages for responsiveness. like this;
#nav-bar {
text-align: right;
background-color: #A7A7A9;
width: 79%;
float: right;
margin-top: 12px;
display: inline-block;
}
For the title :
h1{
display: inline-block;
width: 18%;
min-width: 77px;
float: left;
}
For the ul element in the navbar :
ul {
top: 0px;
left: 0px;
padding-left: 0px;
}
finally for the .navlinks :
.navlink{
width: auto;
margin-right: 7%;
display: inline-block;
color: #453F3C;
font-size: 19px;
font-weight: 500;
letter-spacing: .9px;
text-decoration: none;
}
I am new to development as well but I think what you are trying to do is the following.
HTML:
<nav id="nav-bar">
<ul>
<div class="new_div"><h1>Title</h1></div>
<li><a class="nav-link" href="#our-services">Our Services</a></li>
<li><a class="nav-link" href="#reviews">Reviews</a></li>
<li><a class="nav-link" href="#locations">Locations</a></li>
</ul>
</nav>
</header>
CSS:
.new_div{
float: left;
}
I just added the title as a new div inside the navigation menu and float it left. If there is a logo in between then you can add it in the navigation list and float it left.
You could use
header * {
display: inline;
}
to put everything inside <header> </header> inline.
If you plan to use the header tag elsewhere use a class or an id.
header.top-bar * {
display: inline;
}
To keep all with the same background:
header.top-bar {
width: 100%;
background-color: #A7A7A9;
}
Hope it helps you!
.header h1{position:absolute; left:50%; transform:translateX(-50%); }
Now it's centered between the logo and navigation. By setting the top property you can vertically move the element. Make sure you parent element's position set to relative.

Get 2 Divs in one box?

I was wondering how I would get 2 divs in one div
html {
margin: auto;
width: 960px;
height: auto;
}
#navbar {
height: 90px;
background-color: #080808 !important;
display: block;
}
.logo {
padding-left: 31px;
height: 90px !important;
width: 90px !important;
}
.navitems li,
.navitems ul {
list-style-type: none;
display: inline-block;
}
.navitems {
float: right;
}
<div id="navbar">
<div class="logo">
<img src="images/logo.png" alt="" width="90px" height="90px">
</div>
<div class="navitems">
<li>
<ul>Home</ul>
<ul>Contact Us</ul>
<ul>About</ul>
</li>
</div>
</div>
It's probably better to use flex.
Set "display: flex" and "justify-content: space-between" on the parent element (navigator). I also changed the image to just have a background color that stands out.
html {
margin: auto;
width: 960px;
height: auto;
}
#navbar {
height: 90px;
background-color: #080808 !important;
display: flex;
justify-content: space-between;
}
img {
background-color: #0ff;
}
.logo {
padding-left: 31px;
height: 90px !important;
width: 90px !important;
}
.navitems li,
.navitems ul {
list-style-type: none;
display: inline-block;
}
<div id="navbar">
<div class="logo">
<img width="90px" height="90px">
</div>
<div class="navitems">
<li>
<ul>Home</ul>
<ul>Contact Us</ul>
<ul>About</ul>
</li>
</div>
</div>
More on flex properties:
https://css-tricks.com/snippets/css/a-guide-to-flexbox/
If using float, the floating items must be placed before "regular" (non-floating) items in the DOM tree. In this instance, you would want to move your <div class="navitems"> before <div class="logo">. Also, you might want to permutate your <ul> and <li> tags. <ul> contains <li>s, not the other way around. ;)
The 2 divs are in 1 div only but the fact is float property does not changes the flow .Floated elements remain a part of the flow of the web page unlike absolute and fixed positioning so place the div class="navitems" above the div class="logo".Moreover interchange the ul and li you have used them incorrectly conceptually although it doesnt make any difference visually
First, use display: inline-block; on those two DIVs. Also, use vertical-align: middle; on both to align them vertically centered to their container.
But there a mistake in your code: ul and li should be used the other way round, the lis are inside the ul . And also note that list-style-type: none; is only assigned to the ul, and display: inline-block; only to the li elements.
#navbar {
height: 90px;
background-color: #080808 !important;
display: block;
}
.logo {
display: inline-block;
padding-left: 31px;
height: 90px !important;
width: 90px !important;
vertical-align: middle;
}
.navitems {
display: inline-block;
vertical-align: middle;
}
.navitems ul {
list-style-type: none;
}
.navitems li {
display: inline-block;
margin-right: 4em;
}
<div id="navbar">
<div class="logo">
<img src="http://placehold.it/90x90/fb4" alt="" width="90px" height="90px">
</div>
<div class="navitems">
<ul>
<li>Home</li>
<li>Contact Us</li>
<li>About</li>
</ul>
</div>
</div>

CSS menu bar moving out of background image

i'm working on a project for school and we have to make a webpage. Since this is my first time working on something like this i have a little problem. The problem is when i make my screen smaller in width, my menu bar moves out of my background image. You can see it here: https://r0590903.webontwerp.khleuven.be/website/html/
My html:
nav {
background: url(Images/HeaderDepot.jpg);
height: 469px;
width: 100%;
background-repeat: no-repeat;
background-size: 100%;
margin-top: -50px;
display: inline-block;
}
ul {
list-style: none;
padding: 0px;
margin: 0px;
}
ul li {
display: block;
position: relative;
float: left;
}
li ul {
display: none;
}
ul li a {
display: block;
padding: 7px;
text-decoration: none;
white-space: nowrap;
color: #000;
background-color: #FFF;
opacity: 0.75;
margin-right: 10px;
margin-top: 430px;
}
<div class="header-container">
<header class="wrapper clearfix">
<nav class="nav">
EN
NL
<ul class="ul">
<li class="active">Home</li>
<li>Reserveren</li>
<li>Galerij</li>
</ul>
</nav>
</header>
</div>
I know my aside isn't right yet when you make the page smaller, but my main problem now is the menu bar.
Any ideas to fix this? Thanks in advance!
Remove background-size:100% from your nav tag.

Menu appears in reverse order

I've been trying to build a navbar for my website using Foundation. However, after I've tried the items in my menu bar are now appearing in reverse order. On the right hand side, rather than saying "portfolio about contact" it says "contact about portfolio". Any ideas?
HTML:
<div id="header-container">
<div id="header" class="row">
<nav class="nav-bar">
<ul class="left">
<li data-slide="1" class="andrewgu">andrewgu</li>
</ul>
<ul class="right">
<li data-slide="2" class="portfolio">portfolio</li>
<li data-slide="3" class="about">about</li>
<li data-slide="4" class="contact">contact</li>
</ul>
</nav>
</div><!--end header-->
</div><!--end header-container-->
CSS:
div#header ul{
height: 128px;
list-style-type: none;
}
div#header ul li {
background-color: #003264;
text-align: center;
height: 128px;
line-height: 128px;
transition: background-color 1s;
-webkit-transition: background-color 1s;
display: inline;
padding: 0;
margin: 0;
}
li.andrewgu {
width: 200px;
font-size: 60px;
}
li.portfolio {
float: right;
font-size: 30px;
width: 140px;
}
li.about {
float: right;
font-size: 30px;
width: 110px;
}
li.contact {
float: right;
font-size: 30px;
width: 130px;
}
Website: http://andrewgu12.kodingen.com/
Any help would be appreciated, thanks!
cahnge float:right; to float:left
li.portfolio {
float: left;
font-size: 30px;
width: 140px;
}
li.about {
float: left;
font-size: 30px;
width: 110px;
}
li.contact {
float: left;
font-size: 30px;
width: 130px;
}
and add float:right to ul
div#header ul{float:right;}
This is happening because the first list item floats to the right border. The second list item doesn't gets the space between the first item and the right border, and hence comes before the first item and so on.
In order to overcome this problem, as the best practice is to float the ul to the right, and li to the left. This will resolve the problem.
Always remember if you put 'float' in it will reverse the order of the menu, so write float value in tag or to main it's the arrangement as default.

navigation menu last item appearing and dissapearing on resizing

I have the below nav menu and whenever i resize using the ctrl+right click the last item FAQ appears and dissapears..not to mention it also breaks my entire site background represented by 2 images.
whats wrong and how to make it stay the same on resizing? cheers!
html:
<div class="nav">
<ul>
<li class='active '><a href='#'><span>Home</span></a></li>
<li><a href='#'><span>about us</span></a></li>
<li><a href='#'><span>our errand ladies</span></a></li>
<li><a href='#'><span>schedule an errand</span></a></li>
<li><a href='#'><span>contact us</span></a></li>
<li><a href='#'><span>faq</span></a></li>
</ul>
</div>
css:
.nav {
width: 100%;
height: 63px;
overflow: hidden;
}
.nav ul {
margin: 0;
padding: 0;
list-style-type: none;
width: auto;
position: relative;
display: block;
height: 63px;
text-transform: uppercase;
font-size: 21px;
background: transparent url('images/nav-bg-repeat.png') repeat-x top left;
font-family: Helvetica,Arial,Verdana,sans-serif;
}
.nav li {
display: block;
float: left;
margin: 0;
padding: 0;
}
.nav li a {
display: block;
float: left;
text-decoration: none;
padding:0 30px;
height: 63px;
line-height: 63px;
vertical-align: middle;
background: transparent url('images/divider.png') no-repeat top right;
}
.nav li a:hover {
background: transparent url('images/nav-hover.png') repeat-x top right;
}
.nav li a span {
color: #000;
font-weight: bold;
}
You have overflow: hidden; set on your .nav element and no width defined.
By default, the .nav is set to 100% width, as it's a block element. It will automatically span the width of the parent it's currently sitting in.
When you resize the window down to a size that cannot fit the links, they fall outside of the .nav and are hidden from view.
You can set a fixed width to your .nav (or parent container) to prevent it from collapsing in width.
.nav {
width: 960px;
}
Or if you still want it to collapse, but still show the nav links, you can remove overflow: hidden; and the elements will appear (however, they will not be inline with each other.
for the second problem set the
body{
background-repeat:no-repeat;
}
and it disappears as overflow is hidden; set it to none
.nav{
width:1000px;
overflow:none;
..
}
the way i see is you are trying to put the list elements inline so you can try this code and modify the looks:
<ul id="navlist">
<li id="active">Home</li>
<li>Here</li>
<li>There</li>
<li>Faq</li>
<li>Contact</li>
</ul>
</div>
#navlist li
{
display: inline;
list-style-type: none;
padding-right: 20px;
}