Nav element won't fully float to the right - css

/* Generic Styles */
body {
background: #ffffff;
color: #222222;
font: 1em;
}
.container {
margin: auto;
width: 90%;
}
img {
max-width: 100%;
}
/* Layout Styles */
header {
background-image: url(../images/rocket.png);
background-repeat: no-repeat;
background-size: contain;
background-position: left;
background-color: #003151;
}
header img {
float: left;
margin-top: 30px;
}
header h3 {
clear: both;
}
nav {
float: right;
width: 100%;
font-size: 1.250em;
font-weight: bold;
text-transform: uppercase;
margin-top: 62px;
}
nav ul {
list-style: none;
}
nav ul li {
float: left;
display: block;
margin-right: 04%;
}
nav ul li:last-child {
margin-right: 0;
}
nav ul li a {
color: #ffffff;
text-decoration: none;
}
<header>
<div class="container">
<img src="images/logo.png" alt="Logo" />
<nav>
<ul>
<li>Home</li>
<li>Portfolio</li>
<li>Services</li>
<li>About</li>
<li>Blog</li>
<li>FAQS</li>
<li>Contact</li>
</ul>
</nav>
<h3>BlahBlahBlahH3</h3>
<p>BlahBlahBlah</p>
Get in touch
</div>
<!-- End Container -->
</header>
<!-- End Header -->
Despite my best efforts it's as if the nav element has some kind of padding or margin (which it doesn't). If I set the width to 100% to stop the nav spilling onto two lines it then jumps beneath the logo instead of floating to the right on the same line as the logo. The code may be a little messy now from a lot of trial and error but can anyone explain why if I don't set the nav element to width 100% that the nav spills onto two lines or why if it is set to 100% it won't stay floated to the right?

Heyo,
It looks like your % margin is flubbing things up. If you're willing to part ways with that (suggested if you're going to be in such tight spaces where 4% will be very tiny), try this:
nav {
float: right;
font-size: 1.250em;
font-weight: bold;
text-transform: uppercase;
}
nav ul {
list-style: none;
float: right;
}
nav ul li {
float: left;
display: block;
margin-right: 20px;
}
Your nav is rather large, so at around the 900px breakpoint, you'll want to either reduce the font-size, or shift it all to float:left, so that when it goes below the logo it looks more natural.

Remove width: 100% from your nav.
Remove padding and margin from ul (added by browser as default)
Reduce font-size of nav items - if its too big of course the nav will wrap!
Note that your container has width of 90% - as the nav is within this, it won't reach the far right edge.
/* Generic Styles */
body {
background: #ffffff;
color: #222222;
font: 1em;
}
.container {
margin: auto;
width: 90%;
}
img {
max-width: 100%;
}
/* Layout Styles */
header {
background-image: url(../images/rocket.png);
background-repeat: no-repeat;
background-size: contain;
background-position: left;
background-color: #003151;
}
header img {
float: left;
margin-top: 30px;
}
header h3 {
clear: both;
}
nav {
float: right;
font-size: 1.250em;
font-weight: bold;
text-transform: uppercase;
margin-top: 62px;
}
nav ul {
list-style: none;
margin: 0;
padding: 0;
}
nav ul li {
float: left;
display: block;
margin-right: 14px;
}
nav ul li:last-child {
margin-right: 0;
}
nav ul li a {
color: #ffffff;
text-decoration: none;
font-size: 14px;
}
<header>
<div class="container">
<img src="images/logo.png" alt="Logo" />
<nav>
<ul>
<li>Home</li>
<li>Portfolio</li>
<li>Services</li>
<li>About</li>
<li>Blog</li>
<li>FAQS</li>
<li>Contact</li>
</ul>
</nav>
<h3>BlahBlahBlahH3</h3>
<p>BlahBlahBlah</p>
Get in touch
</div>
<!-- End Container -->
</header>
<!-- End Header -->

Related

Trying to create a web page. Issue with centering text on nav bar

I am using pixel perfect to try to duplicate a web page for learning purposes. I am having a really hard time trying to create this. For starters, my text will not stay centered on the nav bar. I think it may be an issue with my header. I might have combined the header and nav by accident. I'm not sure how to fix it.
The page I am trying to copy
My html:
<body>
<div id="wrapper">
<header>
<div id="bluebox"><h1>Main Title Here</h1></div>
<div id="outerbox"></div>
<div id="navbox"></div>
</header>
<div class="navigation-bar">
<nav>
<ul>
<li>Home</li>
<li>About</li>
<li>Portfolio</li>
<li>Contact</li>
</ul>
</nav>
</div>
My css:
/* general */
*{margin: 0; padding: 0;}
body {font-size: 100%; line-height: 1.5em; font-family: 'arial'; }
article, aside, figure, footer, header, main, menu, nav, section, video {display: block;}
/* wrapper */
#wrapper {width: 1024px; margin: 0 auto; }
/* header */
#wrapper header {height: 50px; padding-top: 128px; padding-right: 0px;
padding-bottom: 0px; padding-left: 0px;}
#wrapper h1 {font-family: "arial"; color: #FFF; top: 70px; position: absolute; padding-left: 195px; font-size: 2.75em;}
#bluebox{
background-color: #23b6eb;
width: 1020px;
height: 140px;
position: absolute;
top: 30px;
}
#outerbox{
background-color: #5d5a5a;
width: 1020px;
height: 30px;
position: absolute;
top: 0px;
}
/* navigation */
#navbox{
background-color: #BBB;
width: 1020px;
height: 40px;
position: absolute;
top: 170px;
}
.navigation-bar {display: inline-block; font-weight: bold; font-size: 1.2em; vertical-align: center; text-align: center;}
nav ul li span, nav ul li a {text-decoration: none; color: #000; }
Same one with the right way. I have changed the following:
Completely get rid of the position: absolute.
Replace all the height using line-height.
Get rid of unnecessary tags, like outer-box, etc.
Use semantic tags, wherever possible.
/* general */
* {
margin: 0;
padding: 0;
}
body {
font-size: 100%;
line-height: 1.5em;
font-family: 'arial';
}
article, aside, figure, footer, header, main, menu, nav, section, video {
display: block;
}
/* wrapper */
#wrapper {
width: 1024px;
margin: 0 auto;
padding: 20px 0;
background-color: #5d5a5a;
}
/* header */
#wrapper header {
background-color: #23b6eb;
}
#wrapper header h1 {
font-family: "arial";
color: #fff;
font-size: 2.75em;
text-align: center;
line-height: 2.5;
}
/* navigation */
nav ul {
text-align: center;
background-color: #ccc;
}
nav ul li {
display: inline-block;
padding: 0 25px;
}
nav ul li a {
text-decoration: none;
color: #000;
}
<div id="wrapper">
<header>
<h1>Main Title Here</h1>
</header>
<nav>
<ul>
<li>Home</li>
<li>About</li>
<li>Portfolio</li>
<li>Contact</li>
</ul>
</nav>
</div>
Preview

#media rule - force new line in NavBar and display previously right aligned links in this line but center aligned

I have a NavBar with my name left aligned(green background color), and then links to other pages which are right aligned(no background color). When re-sizing to less than 640px I need to move the right aligned links to a new line, and center all NavBar content. I cannot get the links to move to a second line.
HTML:
/* menu bar */
header{
background-color: #ffffff;
height: 60px;
margin: 0;
padding:0;
}
ul{
list-style-type: none;
margin: 0;
padding: 0;
display:block;
}
/* align right */
li{
float:right;
}
/*link formatting*/
li a{
display:block;
padding: 8px;
color:black;
text-align: center;
padding:10px 16px;
text-decoration: none;
font-weight: bold;
}
/* name with background color*/
li:last-child{
font-size: 34px;
background-color: #4aaaa5;
position:absolute;
float:left;
}
#media screen and (max-width: 640px) {
li:last-child{
font-size: 34px;
background-color: #4aaaa5;
position:absolute;
width: 100%;
text-align: center;
top: 0px;
}
}
<ul>
<li><a id="bottomlinks"href="index.html">About</a></li>
<li><a id="bottomlinks"href="portfolio.html">Portfolio</a></li>
<li><a id="bottomlinks"href="contact.html">Contact</a></li>
<li> Mark Ring</li>
</ul>
Here is a basic demo of what it looks like you're attempting to achieve. As you can see I've simplified the HTML and CSS a bit.
Hope it helps!
body {
margin: 0;
}
ul,
li {
margin: 0;
padding: 0;
list-style: none;
}
header {
text-align: center;
overflow: hidden; /* clearfix */
}
.brand {
display: block;
background-color: #4AAAA5;
line-height: 60px;
}
#media ( min-width: 640px ) {
header {
text-align: left;
height: 60px;
}
.nav {
float: right;
}
.nav li {
float: left;
line-height: 60px;
}
.brand {
display: inline-block;
padding: 0 1rem;
}
}
<header>
<a class="brand" href="#">Brand</a>
<ul class="nav">
<li>About</li>
<li>Portfolio</li>
<li>Contact</li>
</ul>
</header>
In your code you were absolute positioning the brand element on top of the other links (couldn't see them) and didn't undo the float (which kept them from stacking vertically).

How do I add a logo into the nav bar in CSS?

Okay after a lot of googling I finally found out how to centre the menu links in the middle of the nav bar. After that I came across another problem...adding a logo in the navigation bar.
My problem is, the logo doesn't come into the navigation bar, it instead goes above the bar.
I would like to have the logo floating to the left in the nav bar.
I have tried a few things, including adding display: inline-block/inline to all the main elements, but no difference. Though I tried adding display: inline to the nav ul, but the background of the nav bar disappears (see second image) and I can't add a background via adding height and width.
P.s Excuse me If there are few amateur mistakes, I have only started to code a few months ago.
Thanks for your time!
How the nav bar looks now:
How the nav bar looks with display: inline;
Here is my HTML and CSS:
#logo {
height: 50px;
width: auto;
float: left;
}
nav ul {
list-style-type: none;
overflow: hidden;
margin: 0;
padding: 0;
background-color: #1a1a1a;
text-align: center;
border: 1px solid #e7e7e7;
display: inline-block;
width: 100%;
}
nav li {
display: inline-block;
}
nav a {
display: inline-block;
padding: 16px 15px;
text-decoration: none;
font-family: arial;
font-weight: bold;
color: white;
}
nav a:hover {
background-color: orange;
color: white;
}
<nav>
<img id="logo" src="https://67.media.tumblr.com/f607af5bc60d1b2837add83c70a2aa45/tumblr_inline_mrwv19q8fE1qz4rgp.gif"></img>
<ul>
<li>Game 1
</li>
<li>Game 2
</li>
<li>Game 3
</li>
</ul>
</nav>
Just insert it inside your ul.
#logo {
height: 50px;
width: auto;
float: left;
}
nav ul {
list-style-type: none;
overflow: hidden;
margin: 0;
padding: 0;
background-color: #1a1a1a;
text-align: center;
border: 1px solid #e7e7e7;
display: inline-block;
width: 100%;
}
nav li {
display: inline-block;
}
nav a {
display: inline-block;
padding: 16px 15px;
text-decoration: none;
font-family: arial;
font-weight: bold;
color: white;
}
nav a:hover {
background-color: orange;
color: white;
}
<nav>
<ul>
<img id="logo" src="https://67.media.tumblr.com/f607af5bc60d1b2837add83c70a2aa45/tumblr_inline_mrwv19q8fE1qz4rgp.gif"/>
<li>Game 1
</li>
<li>Game 2
</li>
<li>Game 3
</li>
</ul>
</nav>
The easiest way is to probably do a position:absolute
https://jsfiddle.net/fj1r6b1e/
#logo {
height: 50px;
width: auto;
position:absolute;
}
Also, img tag should be written like this <img src="..." /> instead of <img></img>
Give the logo img position: fixed. This will not affect the position/centering of the menu items.
https://jsfiddle.net/kjmm3du5/

Text not aligning center, left

Good Day
I am having a bad CSS day. I have 3-column div (horizontal, thats why I am using the float), but I want to align my text in the center, but to the left of the containing column div.
HTML:
<div id="footer">
<div class="footerColumn">
<ul>
<li><a>Contact Us</a></li>
<li><a>Home</a></li>
<li><a>Emergency Numbers</a></li>
</ul>
</div>
<div class="footerColumn">
<ul>
<li><a>Support</a></li>
<li><a>Privacy Policy</a></li>
<li><a>Terms of Use</a></li>
<li><a>Careers</a></li>
</ul>
</div>
<div class="footerColumn">
<ul>
<li><p><a>login</a></p></li>
<li><a>Sign Up</a></li>
</ul>
</div>
<div style="clear: both;"></div>
</div>
CSS:
#footer {
width: 960px;
margin: 0 auto;
padding: 10px;
background: url('/images/bg2.png') repeat-x center;
border-top: 6px solid #f3911f;
background: #1b1d21;
}
#footer div.footerColumn {
position: relative;
float: left;
width: 33%;
margin: 0 auto;
text-align: justify;
}
#footer div.footerColumn ul li {
}
#footer ul li a{
color: #fff;
font-family: Arial, sans-serif;
font-size: 14px;
text-decoration: none;
cursor: pointer;
}
#footer div.disclaimer {
color: #ccc;
font-family: verdana, Arial, sans-serif;
font-size: 12px;
}
SEE MY FIDDLE
Thank you
The reason why you are unable to center the <ul> element in your design is because the <li> elements are stretching the parent <ul> width to 100%, therefore using margin: 0 auto will not work.
Instead, I suggest that you use display: inline-block for your <ul> - allowing it to ONLY stretch as far as the width of inner contents (the child <li> elements), and not to the 100% width of the containing element.
As for the children <li>, you can set them to display as a block element, float them to the left and then clear the left float, therefore forcing each floated element to start on a new line (because you clear the left float for every element).
Simply implement the style suggested as follow:
#footer ul {
display: inline-block;
overflow: hidden;
}
#footer ul li {
display: block;
float: left;
clear: left;
}
#footer ul li a{
color: #fff;
font-family: Arial, sans-serif;
font-size: 14px;
text-decoration: none;
cursor: pointer;
}
Check the fiddle here ;) http://jsfiddle.net/teddyrised/DvXzB/48/
Use text-align:center in your footerColumn divs.
#footer div.footerColumn {
position: relative;
float: left;
width: 33%;
margin: 0 auto;
text-align: center;
}
#footer div.footerColumn {
position: relative;
float: left;
width: 33%;
margin: 0 auto;
text-align: center; /*new here*/
}
#footer div.footerColumn ul {
float: left; /*new here*/
}

CSS <ul> Navigation Bar Centering Issues

Take a look at this website I am working on: new.AudioManiacProductions.com
A screenshot from Dreamweaver showing the divs can be found here: new.AudioManiacProductions.com/images/stack.png
Notice how to navigation bar is not centered and the "home" has an intent on the left side.
I want the nav bar to stretch the whole span of it's parent container and be centered both vertically and horizontally.
Here's my CSS for the navigation section of the page, the nav list item, and the links.
#nav {
width: 100%;
height: 40px;
margin: 0 auto;
padding: 0px;
text-align: center;
color: #FFF;
list-style: none;
line-height: auto;
verticle-align: middle;
}
#nav li {
padding: 0px;
display: block;
width: 20%;
list-style-type: none;
float: left;
text-align: center;
overflow: hidden;
line-height: auto;
verticle-align: middle;
}
#nav li a {
font-size: 18px;
font-weight: 600px;
text-decoration: none;
font-weight:800;
color:#FFF;
}
#nav li a:hover, a:visited {
color: #00F;
}
And here is my HTML:
<div id="nav">
<ul>
<li>Home</li>
<li>About Us</li>
<li>Packages</li>
<li>Quote</li>
<li>Contact Us</li>
</ul>
</div>
Any help is greatly appreciated!
Browsers set padding-left by default on UL tags. Remove the padding and that should help out.
#nav ul {
padding-left: 0;
}
or just clear all padding:
#nav ul {
padding: 0;
}

Resources