Aligning navbar unordered list with flexbox - html

I am using flexbox to create a navbar - A Codepen here. I want a logo on the left and the links on the right of the navbar.
However I cannot workout how to use flex-end to good effect. My links in the unordered list do not appear to be affected by flexbox. Is there a special way to bind flexbox to a navbar?
My HTML:
<link rel="stylesheet" type="text/css" href="//fonts.googleapis.com/css?family=Raleway" />
<nav>
<!-- Primary Logo -->
<div id="logo">
<img src="http://www.shinetown.com.sg/shinetown/wp-content/uploads/2016/04/logo-dummy.png" alt="NewCircle">
</div>
<!-- Primary Navigation -->
<div id="navigation">
<ul>
<li class="current">Home
<li>Products
<li>Pricing Plans
<li>Help
</ul>
</div>
</nav>
and my Stylus code:
nav-height = 100px
primary-color = #FF5B4E
primary-text-color = #444
secondary-border-color = #EEE
body {
font-family: Raleway
}
nav, nav *
display: flex
align-items: center
width: 100%
nav
height: nav-height
border-bottom: 5px solid secondary-border-color
#logo
width: 300px
height: nav-height
border-right: 1px solid secondary-border-color
padding: 0 1em 0 1em
img
max-width: 100%
#navigation
height: 100%
justify-content: flex-end
ul
list-style-type: none
padding: 0
height: 100%
margin: 0
li
height: 100%
a
height: 100%
transition: color 0.4s ease
font-weight: bold
font-size: 13px
letter-spacing: 1px
text-transform: uppercase
text-align: center
text-decoration: none
color: primary-text-color
padding: 0 18px 0 18px
a:hover
color: primary-color
.current
> a
color: primary-color

flex-end won't solve it, margin-left: auto will.
nav, nav *
display: flex
align-items: center
#navigation
height: 100%
margin-left: auto
Also, in the rule you have nav *, which will make all nav's descendants to have 100% width, and the #navigation can't align right being full width.
Removing width: 100% from your nav, nav * rule will fix that.
Updated codepen
Stack snippet
body {
font-family: Raleway;
}
nav,
nav * {
display: flex;
align-items: center;
}
nav {
height: 100px;
border-bottom: 5px solid #eee;
}
nav #logo {
width: 300px;
height: 100px;
border-right: 1px solid #eee;
padding: 0 1em 0 1em;
}
nav #logo img {
max-width: 100%;
}
nav #navigation {
height: 100%;
margin-left: auto;
}
nav #navigation ul {
list-style-type: none;
padding: 0;
height: 100%;
margin: 0;
}
nav #navigation ul li {
height: 100%;
}
nav #navigation ul li a {
height: 100%;
transition: color 0.4s ease;
font-weight: bold;
font-size: 13px;
letter-spacing: 1px;
text-transform: uppercase;
text-align: center;
text-decoration: none;
color: #444;
padding: 0 18px 0 18px;
}
nav #navigation ul li a:hover {
color: #ff5b4e;
}
nav #navigation ul .current > a {
color: #ff5b4e;
}
<link rel="stylesheet" type="text/css" href="//fonts.googleapis.com/css?family=Raleway" />
<nav>
<!-- Primary Logo -->
<div id="logo">
<img src="http://www.shinetown.com.sg/shinetown/wp-content/uploads/2016/04/logo-dummy.png" alt="NewCircle">
</div>
<!-- Primary Navigation -->
<div id="navigation">
<ul>
<li class="current">Home
<li>Products
<li>Pricing Plans
<li>Help
</ul>
</div>
</nav>
Note, the justify-content: flex-end should be used on the flex container, not on flex items. When one want to set it on a flex item, use align-self: flex-end.
In this case the flex has row direction, so the align-self won't work as it applies on the cross axis, hence margin-left: auto will do the trick.

Related

How do i close the letter gap accross my navbar at the top

So there is the letter spacing/gap between each word and i want to minimize that, but when i try to do it the only thing that shrinks is the button itself.
.navbar {
position: fixed;
top: 0;
right: 7%;
z-index: 10;
display: flex;
text-align: center;
display: table;
}
.navbar ul {
flex: 1;
text-align: right;
padding: 40px;
background: transparent;
}
.navbar ul li {
display: inline-block;
list-style: none;
margin: 10px 30px;
background: transparent;
}
.navbar ul li {
display: inline-block;
list-style: none;
margin: 10px 30px;
background: transparent;
}
.navbar ul li a {
color: #6dffe7;
text-decoration: none;
letter-spacing: 1px;
font-size: 15px;
cursor: pointer;
}
body { background: black; }
<div class="page">
<nav class="navbar">
<ul>
<div class="download-btn">
<li>Home</li>
<li>About</li>
<li>Work</li>
<li>Contact</li>
<li> Download Resume</li>
</div>
</ul>
</nav>
</div>
Thank you for your help
I think you are trying to recreate the navbar as depicted in the image provided. As such, you need to following rules:
Horizontal navbar with a fixed height (I assumed it to be 100px).
All links except for the button should appear on the left and separated by some gap.
The "Download Resume" button needs to appear on the right side of your navbar.
There should be some padding at the left and right side of the navbar (I assumed this to be 2rem).
To achieve, this you need to set flex layout on your navbar, so that we can use the align-items: center to center the links vertically inside the navbar. We then need to set flex layout on the ul itself, and give it flex-direction: row with some gap: 2rem.
Now to place the button on the far right side of your navbar, you need to remove it from the ul inside the navbar, and place it as a sibling below it. And set justify-content: space-between on your navbar. This should move the links to far left, and the button to far right.
Additionally, we can style the visuals by giving navbar a background-color and color. We then inherit this color onto the anchor links a. We also set list-style: none and text-decoration: none on the ul and a respectively, to achieve the look of the image linked in the question.
*,
*::before,
*::after {
box-sizing: border-box;
margin: 0;
padding: 0;
}
.navbar {
padding: 0 2rem;
position: fixed;
top: 0;
width: 100%;
background-color: #12182b;
color: #6dffe7;
height: 100px;
display: flex;
align-items: center;
justify-content: space-between;
}
.navbar ul {
display: flex;
flex-direction: row;
align-items: center;
list-style: none;
gap: 2rem;
}
.navbar ul a {
font-size: 0.9em;
color: inherit;
text-decoration: none;
}
.navbar .btn {
font-size: 0.9em;
border: 2px solid #6dffe7;
padding: 0.6em;
text-decoration: none;
color: inherit;
font-weight: bold;
}
<div class="page">
<nav class="navbar">
<ul>
<li>Home</li>
<li>About</li>
<li>Work</li>
<li>Contact</li>
</ul>
Download Resume
</nav>
</div>
Looking at the image it seems like you want to adjust nav items spacing instead of letter spacing/gap.
You can try to adjust paddings/margins on navbar ul and the li child items.
Also, you have duplicate display prop on .navbar you can remove one.
.navbar {
background-color: #12182b;
position: fixed;
top: 0;
left: 0;
z-index: 1;
display: flex;
text-align: center;
}
.navbar ul {
flex: 1;
text-align: right;
padding: 20px;
background: transparent;}.navbar ul li {
display: inline-block;
list-style: none;
margin: 10px 20px;
background: transparent;
}
.navbar ul li {
display: inline-block;
list-style: none;
margin: 10px 20px;
background: transparent;
}
.navbar ul li a {
color: #6dffe7;
text-decoration: none;
letter-spacing: 1px;
font-size: 15px;
cursor: pointer;
}
<nav class="navbar">
<ul>
<div class="download-btn">
<li>Home</li>
<li>About</li>
<li>Work</li>
<li>Contact</li>
<li>
Download Resume </li>
</div>
</ul>
</nav>
it seems to me you use bootstrap if you do then you will to need to use !important after any styles in css

White space above the body when I use "height:100vh"

I've got a basic HTML document setup and I want the div to have the height of the view port. But as soon as I add height:100vh to the .main div there's a gap above the body and there's a vertical scroll bar
Here's my code.
HTML:
<div class="main">
<div class="container">
<div class="nav">
<ul>
<li>Work</li>
<li>About</li>
<li>Contact</li>
</ul>
</div>
<div class="main_text">
<h1>Hey there! I'm <span class="yellow">Gautham SK</span>, a digital designer & web developer.</h1>
</div>
</div> <!-- end container -->
</div> <!-- end main -->
CSS:
html, body {
height: 100%;
padding: 0;
margin: 0;
font-family: 'Montserrat', sans-serif;
font-weight: 400;
}
.container {
max-width: 1024px;
padding: 0 20px;
margin: 0 auto;
}
/* NAVIGATION */
.nav ul {
display: flex;
align-items: center;
justify-content: flex-end;
/* margin-top: 75px;*/
list-style-type: none;
}
.nav ul li {
margin-left: 40px;
list-style: none;
font-size: 16px;
}
.nav ul li a, .nav ul li a:visited {
color: #000;
padding: 5px;
text-decoration: none;
transition: 0.2s color;
}
.nav ul li a:hover {
color: #ffde00;
transition: 0.2s color;
}
.nav ul li a.active {
color: #000;
border-bottom: 5px solid #ffde00;
}
/* MAIN SECTION */
.main {
background: url("../images/bg.jpg") center center no-repeat;
background-size: cover;
}
.main, .main .container {
height: 100vh;
}
.main_text {
height: 100%;
text-align: center;
display: flex;
align-items: center;
justify-content: center;
}
.main_text h1 {
font-weight: 400;
font-size: 45px;
}
span.yellow {
color: #ffde00;
}
I've also got a normalize.css linked to this document. I tried to remove the padding and margin for all the elements but that doesn't work.
UPDATE
Yay! Found the solution. Here's what I did:
Removed the margin for the nav and it's children and replaced it with a top padding to get the desired look.
Changed the height of the main_text to 80%.
If you've got a better solution, let me know!
As a simple test, set padding: 1px on body. If the gap goes away read: http://www.sitepoint.com/web-foundations/collapsing-margins/

How to center nav in div

I am trying to center the navigation bar in the middle of the div body. I want the navigation bar to go from one side of the div to the other but have the list in the ul to be center in the middle of the div if that makes sense. I can't seem to figure it out even after trying online examples. Thanks
body {
margin: 0px;
padding: 0px;
background-color: #505050 ;
}
#body {
width: 75%;
margin: 0 auto;
position: center;
background-color: #C0C0C0;
height: 100%;
}
.nav {
}
.nav ul {
background-color: #CCCCCC;
width: 100%;
padding: 0;
text-align: center;
}
.nav li {
list-style: none;
font-family: Arial Black;
padding: 0px;
height:40px;
width: 120px;
line-height: 40px;
border: none;
float: left;
font-size: 1.3em;
background-color: #CCCCCC;
display:inline;
}
.nav a {
display: block;
color: black;
text-decoration: none;
width: 60px;
}
<div id="body">
<h2>Hello World!</h2>
<div class="nav">
<ul>
<li><a href="#">Home<a></li>
<li><a href="#">About<a></li>
<li><a href="#">News<a></li>
<li><a href="#">Contact<a></li>
</ul>
</div>
</div>
i attach fix here http://jsfiddle.net/o4716uo9/
use inline-block for li
background property should be setted in ul element, not li, in your case. Delete the float in nav li. Also, the a element it isn't closed correctly. Main changes:
.nav ul {
background-color: #cccccc;
text-align: center;
}
.nav ul li {
display: inline-block;
min-width: 120px;
[...]
}
I'll recommend you to take a look at the bootstrap framework. It could be interesting for you.
There are a couple things you can change to correct the issue:
1) Your <a> elements have a width of 60px. You can remove this.
2) You .nav li has a width of 120px. I would change this to 25% (If there are only going to be four navigational items).
http://jsfiddle.net/xLnz90ek/
Is that any closer to the desired effect.
Is this what you’re trying to do?
* { margin:0; padding:0 }
html {
background-color: #505050;
font-size: 4vw;
}
header {
width: 75%;
margin: 0 auto;
background-color: #C0C0C0;
}
nav {
background-color: #CCCCCC;
display: flex;
padding: 0.2rem 0;
}
nav a {
flex: 1 0 auto;
font-family: Arial Black;
font-size: 1rem;
background-color: #CCCCCC;
color: black;
text-decoration: none;
text-align: center;
margin: 0 0.3rem;
}
<header>
<h2>Hello World!</h2>
<nav>
Home
About
News
Contact
</nav>
</header>

Can't get rid of gap between images?

I have a question about positioning my website. As you can see in the IMG below, there is a gap between two of my images for some odd reason: http://puu.sh/6SWgu.png
I am trying to get rid of that gap, but I can't figure out why.
EDIT
(I don't know who deleted the other comment someone left, but I tried that one with a little bit of configuration and it worked.)
(Question is, will this new code hurt any process as I continue coding in my website?)
Here is the NEW content:
HTML:
<body>
<div id="page-wrap">
<div id="header">
<img src="images/header.png" />
</div>
<img src="images/navbar.png" />
<ul id="nav">
<li>Home</li>
<li>Forums</li>
<li>Members</li>
<li>Streams</li>
<li>Contact Us</li>
</ul>
<div vertical-align: top; ><img src="images/mainbody.png" /></div>
<div id="footer">
<p>©2014 Rythmn Designs<p>
</div>
CSS:
body
{
margin: 0px;
padding: 0px;
background: url("http://puu.sh/6RlKi.png")
}
#page-wrap
{
width: 1019px;
margin: 0 auto;
}
#header
{
width:100%;
text-align: center;
display: block;
}
#nav
{
height: 0.1px;
list-style: none;
padding-left: 0.1px;
text-align: center;
padding-bottom: 0px;
margin: -14px;
}
#nav li a
{
position:relative;
top: -12px;
display: block;
width: 100px;
float: left;
color: white;
font-size: 14.09px;
text-decoration: none;
font-family:"BankGothic Md BT"
}
#nav li a:hover, #nav li a:active
{
color: red;
}
#footer
{
background: #181818;
color: white;
padding: 20px 0 20px 0;
text-transform: uppercase;
border-top: 15px solid #828080;
text-align: center;
font-family:"BankGothic Md BT";
font-size: 12px;
}
For this you can set the navigation margin-bottom in negative or the main body image margin top as negative if your 0 margin or padding isn't working

Center responsive nav

i'm trying to center my nav in a responsive design. When the viewport is stretched to about 900px, the 3 nav titles look off center to the rest of the content on the page. Does anyone of a way to fix this?
HTML:
<header>
<div id="logo">
<a href="#">
<img src="images/logo_55x73.png" alt="StudioMed" />
</a>
</div>
<nav>
<ul>
<li>Work</li>
<li>Profile</li>
<li>Journal</li>
</ul>
</nav>
</header>
CSS:
header {
background-color: #fff;
border-bottom: solid 1px #e4e4e4;
}
#logo {
padding: 2.3em 1.1em 1.7em 1.1em;
text-align: center;
}
nav {
display: block;
}
nav ul {
text-align: center;
}
nav li {
font-size: 0.95em;
display: inline;
text-transform: uppercase;
letter-spacing: 0.06em;
padding: 0 1%;
}
nav a {
text-decoration: none;
display: inline-block;
width: 122px;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
#media only screen and (min-width:767px) {
header {
float: left;
overflow: auto;
width: 100%;
}
#logo {
float: left;
width: 10%;
}
nav ul li {
float: left;
width: 22.1354166666667%;
margin-top: 50px;
font-size: 1em;
}
}
Thanks in advance
Your UL may have some default padding on its left.
Try adding this to your CSS:
nav ul {
...
padding: 0px;
}
http://jsfiddle.net/cxNZZ/
EDIT:
The padding issue above did not seem to solve your issue.Here is a different theory:
Each LI is the same width, but the text inside each LI may not be. That could cause the menu to appear to be off center, even though the LIs are centered.
http://jsfiddle.net/d6hRg/1/
http://jsfiddle.net/d6hRg/2/
See what I mean?