I can't seem to find a great way of sizing my logo to fit within the header, I am trying to get a result with the logo on the left side of the header and the naviagtion on the right side. I've tried this and it seems to work but I don't know if there's a better way to approach this. If anyone could give some tips it would be greatly appreciated.
My CSS and HTML
a {
text-align: center;
color: #232323;
text-decoration: none;
transition: .1s;
}
button {
border: none;
outline: none;
color: #f3f3f3;
cursor: pointer;
transition: .3s;
}
header {
display: flex;
align-items: center;
justify-content: space-between;
position: fixed;
top: 0;
padding: 1em 2em;
max-height: 20%;
width: 100%;
z-index: 2;
background-color: #ffffff;
}
.logo-wrapper {
display: inline-block;
overflow: hidden;
}
.logo-wrapper img {
height: auto;
width: auto;
max-width: 150px;
}
nav {
display: inline-block;
}
.nav-item {
display: inline-block;
margin: 0 1.5em;
}
.get-started {
background-color: #f27649;
padding: 1em 2em;
border-radius: 3em;
}
.get-started:hover {
filter: brightness(85%);
}
<header>
<div class="logo-wrapper">
<a href="/">
<img src="images/devchat.png" alt="DevTalk Logo">
</a>
</div>
<nav>
<ul>
<li class="nav-item">Learning</li>
<li class="nav-item">About</li>
<li class="nav-item">Blog</li>
<li class="nav-item">Explore</li>
<li class="nav-item">Contact</li>
<li class="nav-item">Login</li>
<li class="nav-item"><button class="get-started" href="#">Get Started <i class="fas fa-angle-right"></i></button></li>
</ul>
</nav>
</header>
Related
I am trying to make a product landing page for freecodecamp, my navbar (the first thing I have to do) isn't coming out well. What is wrong with my code?
Also I want to put a image behind the navbar.
It's a bit frustrating coding, how do you learn with all these gimmicky little things going the wrong way? I find I have to have someone here to make sure the code is explained, but sincerely I don't have that luxury.
<div class="nav-bar-wrapper">
<nav id="nav-bar">
<ul>
<li><a class="nav-link" href="#">link1</a></li>
<li><a class="nav-link" href="#">link2</a></li>
<li><a class="nav-link" href="#">link3</a></li>
<li><a class="nav-link" href="#">link4</a></li>
</ul>
</nav>
</div>
html{
width: 100%;
height: auto;
}
.nav-bar{
width: 100%;
height: 7vh;
background-color: #555;
}
#nav-bar a.active {
background-color: #04AA6D;
}
.nav-link{
text-decoration: none;
padding: 3px;
display: inline-block;
color: coral;
width: 25%;
}
.ul, li{
display:inline-block;
}
ul{
display: inline;
text-align: center;
padding:10px;
border: 3px solid;
font-size: 20px;
text-align: justify;
}
li{
margin: auto;
display: inline;
}
.nav-link:hover{
color: green;
}
In place of your css, try this:
ul {
display: flex;
align-items: stretch;
justify-content: space-between;
width: 100%;
margin: 0;
padding: 0;
}
li {
border: 3px solid;
display: block;
flex: 1 1 auto;
list-style-type: none;
text-align: center;
}
fiddle:
https://jsfiddle.net/L039vgsf/1/
This is because of you set for all item width of 25%. You want to be all the same size in full of width of screen. But all of it has margin, padding, border and etc so you don't extualy has 100% space, you have maybe 98%. So if you change it to 24% or 23% will be like that.
.nav-link{
width: 23%;
}
A better practice is to use flex.For flex you just need to set:
html{
width: 100%;
height: auto;
}
.nav-bar{
width: 100%;
height: 7vh;
background-color: #555;
}
#nav-bar a.active {
background-color: #04AA6D;
}
.nav-link{
text-decoration: none;
padding: 3px;
display: inline-block;
color: coral;
width: 100%;
}
.ul, li{
display:inline-block;
}
ul{
display: flex;
text-align: center;
padding:10px;
border: 3px solid;
font-size: 20px;
text-align: justify;
}
li{
margin: auto;
display: inline;
}
.nav-link:hover{
color: green;
}
<div class="nav-bar-wrapper">
<nav id="nav-bar">
<ul>
<li><a class="nav-link" href="#">link1</a></li>
<li><a class="nav-link" href="#">link2</a></li>
<li><a class="nav-link" href="#">link3</a></li>
<li><a class="nav-link" href="#">link4</a></li>
</ul>
</nav>
</div>
This question already has answers here:
Does UL have default margin or padding [duplicate]
(2 answers)
Closed 7 months ago.
For some reason, my nav bar is slightly set to the right. I've tried configuring everything to do with position but it doesn't seem to be working. I'm not sure if it's a CSS property or I just messed up with the configuration but it's a few pixels off-center no matter what. It might not be visible instantly (in the image) but I checked it with a virtual ruler and it is off.
header {
margin: 0 auto;
width: 100%;
height: 70px;
display: flex;
align-items: center;
justify-content: space-around;
background: transparent;
position: fixed;
top: 0;
transition: 0.3s;
z-index: 5;
}
.nav-show {
opacity: 0;
}
header:hover {
opacity: 1;
background: var(--main-header-background);
}
.nav-bar {
list-style: none;
position: relative;
display: inline-flex;
}
a.nav-link {
margin: 2px;
padding: 5px 10px;
text-decoration: none;
color: var(--main-fonts-color);
font-family: var(--main-font-family);
cursor: pointer;
text-transform: uppercase;
}
.active {
background: var(--main-decor-color);
}
.nav-link:hover {
color: #000000;
background: var(--main-decor-color);
}
<header>
<nav>
<ul class="nav-bar">
<div class="bg"></div>
<li>
<a class="nav-link" href=""></a>
</li>
<li>
<a class="nav-link" href=""></a>
</li>
<li><a class="nav-link" href="">Test</a></li>
<li><a class="nav-link" href="">Test</a></li>
</ul>
</nav>
</header>
Just as a note it's about 50px off based on what I see in photoshop.
Add these properties to your CSS
* {
margin: 0;
padding: 0;
}
Sometimes the browser will apply its default margin and padding to the elements, which happened in your case, where the header has an unusual left margin. Thus, we set margin and padding of every element to 0.
* {
margin: 0;
padding: 0;
}
header {
margin: 0 auto;
width: 100%;
height: 70px;
display: flex;
align-items: center;
justify-content: space-around;
background: transparent;
position: fixed;
top: 0;
transition: 0.3s;
z-index: 5;
}
.nav-show {
opacity: 0;
}
header:hover {
opacity: 1;
background: var(--main-header-background);
}
.nav-bar {
list-style: none;
position: relative;
display: inline-flex;
}
a.nav-link {
margin: 2px;
padding: 5px 10px;
text-decoration: none;
color: var(--main-fonts-color);
font-family: var(--main-font-family);
cursor: pointer;
text-transform: uppercase;
}
.active {
background: var(--main-decor-color);
}
.nav-link:hover {
color: #000000;
background: var(--main-decor-color);
}
<header>
<nav>
<ul class="nav-bar">
<div class="bg"></div>
<li>
<a class="nav-link" href="">Test</a>
</li>
<li>
<a class="nav-link" href="">Test</a>
</li>
<li><a class="nav-link" href="">Test</a></li>
<li><a class="nav-link" href="">Test</a></li>
</ul>
</nav>
</header>
nav {
display:block;
padding:10px;
margin:5px 55px 5px 55px;
}
I have an unordered linked list. I'm trying to shift one of the items in the navigation all the way to the right (Order) as if it had text-align: right;. I tried using float: right; and text-align: right;, but none of them seemed to work. If I set the margin-left to a really high number (such as 100px) it does shift to the right, but if I resize my window then I can't see it anymore or it's not on the right side of the page. Here is the HTML:
nav {
position: fixed;
}
.navigation-links-no-style a {
text-decoration: none;
position: relative;
margin: 15px;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
}
li {
float: left;
}
.navigation-links li {
padding-top: 1.3em;
}
.navbar {
overflow: hidden;
position: fixed;
top: 0;
width: 100%;
border-bottom: solid 1px black;
background: white;
padding-left: 5em;
}
.navbar a {
float: left;
display: block;
text-align: center;
padding: 14px 16px;
text-decoration: none;
margin-left: 20px;
color: black;
font-size: 14pt;
}
.order {
color: #FFFFFF !important;
background: #1419e2;
text-decoration: none;
padding: 8px;
border-radius: 5px;
margin-top: 15px;
}
<div class="navbar">
<a class="glacier-hills" href="glacier_hills.html">
<img src="Images/Glacier-Hills-Logo.svg" alt="" width="182" height="90">
</a>
<ul class="navigation-links">
<div class="navigation-links-no-style">
<li>
<a class="menu" href="menu.html">Menu</a>
</li>
<li>
<a class="location" href="location.html">Hours and Location</a>
</li>
</div>
<li>
<a class="order" href="order.html">Order</a>
</li>
</ul>
</div>
Any help would be greatly appreciated. Thanks!
Assuming you're looking to move your .order element, you'll want to apply the float: right rule to the parent (<li>) element. I've added a class to this, .order-container, to make this easier to achieve in the following example.
Note also that once you float to the right, it will be off the screen by default. You'll want to set a negative margin-right to circumvent this. I've gone with margin-right: -10em in the following, to match the offset from the image on the left.
Ultimately, you may wish to consider using a framework to achieve responsive design, ensuring that the offset is correct regardless of screen size.
nav {
position: fixed;
}
.navigation-links-no-style a {
text-decoration: none;
position: relative;
margin: 15px;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
}
li {
float: left;
}
.navigation-links li {
padding-top: 1.3em;
}
.navbar {
overflow: hidden;
position: fixed;
top: 0;
width: 100%;
border-bottom: solid 1px black;
background: white;
padding-left: 5em;
}
.navbar a {
float: left;
display: block;
text-align: center;
padding: 14px 16px;
text-decoration: none;
margin-left: 20px;
color: black;
font-size: 14pt;
}
.order {
color: #FFFFFF !important;
background: #1419e2;
text-decoration: none;
padding: 8px;
border-radius: 5px;
margin-top: 15px;
float: right;
}
.order-container {
float: right;
margin-right: 10em;
}
<div class="navbar">
<a class="glacier-hills" href="glacier_hills.html">
<img src="Images/Glacier-Hills-Logo.svg" alt="" width="182" height="90">
</a>
<ul class="navigation-links">
<div class="navigation-links-no-style">
<li>
<a class="menu" href="menu.html">Menu</a>
</li>
<li>
<a class="location" href="location.html">Hours and Location</a>
</li>
</div>
<li class="order-container">
<a class="order" href="order.html">Order</a>
</li>
</ul>
</div>
MDN still advises that <div> is not a valid child of <ul>. Furthermore float adds a whole heap of side effects by removing the items from the natural flow of the document. To modernize this we can make use of display:flex
/*Normalise body*/
body {
margin:0;
}
/*Set flex on the nabar top position logo and links*/
.navbar {
display: flex;
}
/*Ad a maring to the logo link*/
.navbar > a:first-of-type {
margin-left: 5em;
}
nav {
position: fixed;
}
.navigation-links-no-style a {
text-decoration: none;
}
.navbar ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
width: 100%;
/*Ad flex to the nav link element*/
display: flex;
/*Vertically center the links*/
align-items:center;
}
/*Push the last element right but give it a little margin to the right*/
.navbar ul>li:last-of-type {
margin-left: auto;
margin-right:1em;
}
.navigation-links li {
padding-top: 1.3em;
}
.navbar {
overflow: hidden;
position: fixed;
top: 0;
width: 100%;
border-bottom: solid 1px black;
background: white;
}
.navbar a {
display: block;
text-align: center;
padding: 14px 16px;
text-decoration: none;
margin-left: 20px;
color: black;
font-size: 14pt;
}
.order {
color: #FFFFFF !important;
background: #1419e2;
text-decoration: none;
padding: 8px;
border-radius: 5px;
margin-top: 15px;
}
<div class="navbar">
<a class="glacier-hills" href="glacier_hills.html">
<img src="Images/Glacier-Hills-Logo.svg" alt="" width="182" height="90">
</a>
<ul class="navigation-links">
<li>
<a class="menu" href="menu.html">Menu</a>
</li>
<li>
<a class="location" href="location.html">Hours and Location</a>
</li>
<li>
<a class="order" href="order.html">Order</a>
</li>
</ul>
</div>
You should use media queries for making navbar responsive.
a {
text-decoration: none;
color: black;
}
.navbar {
width: 100%;
overflow: hidden;
position: fixed;
top: 0;
display: flex;
justify-content: space-between;
border-bottom: solid 1px black;
}
.div-links {
display: flex;
align-items: center;
width: 70%;
}
.nav-links {
width: 100%;
display: flex;
justify-content: end;
align-items: center;
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
}
.nav-links li {
padding: 2rem;
}
.nav-items {
width: 30%;
display: flex;
justify-content: space-around;
}
.order {
overflow: hidden;
color: #ffffff !important;
background: #1419e2;
text-decoration: none;
padding: 0.8rem;
border-radius: 5px;
}
<div class="navbar">
<a href="glacier_hills.html">
<img
src="Images/Glacier-Hills-Logo.svg"
alt=""
width="182"
height="90"
/>
</a>
<div class="div-links">
<ul class="nav-links">
<div class="nav-items">
<li>
<a class="menu" href="menu.html">Menu</a>
</li>
<li>
<a class="location" href="location.html">Hours and Location</a>
</li>
</div>
<li class="btn">
<a class="order" href="order.html">Order</a>
</li>
</ul>
</div>
</div>
I'm trying to give my self some extra space to work with at the bottom of the site by adding bottom-padding: 50px; to the body in CSS but for some reason it does not add anything. I've seen people do this to make the page to a scrollable length but for me it literally does nothing no matter what.
This is the code so far:
body {
margin: 0px;
padding-bottom: 500px;
}
header {
height: 60px;
position: fixed;
top: 0;
left: 0;
right: 0;
background-color: white;
}
.nav-list {
display: flex;
justify-content: flex-end;
align-items: center;
}
.nav-item {
list-style: none;
margin-right: 25px;
}
.nav-item a {
font-family: poppins, arial;
text-decoration: none;
color: black;
}
.nav-item:first-child {
margin-right: auto;
font-size: 16px;
}
.project-button {
height: 40px;
width: 100px;
border: none;
border-radius: 20px;
background-color: #86c232;
}
.projects-link {
font-size: 16px;
}
<body>
<header>
<nav>
<ul class="nav-list">
<li class="nav-item">
<strong> Tiam Nazari </strong>
</li>
<li class="nav-item">
Home
</li>
<li class="nav-item">
Contact
</li>
<li class="nav-item">
<button class="project-button">
<a class="projects-link" href="">Projects</a>
</button>
</li>
</ul>
</nav>
</header>
</body>
The padding is working, you just don't have any content. Height controls the actual height of the element (basically the distance from the outermost edges of the container) whereas padding controls the distance between the content and the edges.
If you want space, add a static height and then remove it once all of your content is in. If you still want white space vertically or horizontally once the content is in, then use padding.
body {
margin: 0px;
}
section.first {
height: 800px;
background-color: orange;
padding-top: 4em;
}
section:not(.first) {
background-color: pink;
height: 600px;
}
header {
height: 60px;
position: fixed;
top: 0;
left: 0;
right: 0;
background-color: white;
}
.nav-list {
display: flex;
justify-content: flex-end;
align-items: center;
}
.nav-item {
list-style: none;
margin-right: 25px;
}
.nav-item a {
font-family: poppins, arial;
text-decoration: none;
color: black;
}
.nav-item:first-child {
margin-right: auto;
font-size: 16px;
}
.project-button {
height: 40px;
width: 100px;
border: none;
border-radius: 20px;
background-color: #86c232;
}
.projects-link {
font-size: 16px;
}
<body>
<header>
<nav>
<ul class="nav-list">
<li class="nav-item">
<strong> Tiam Nazari </strong>
</li>
<li class="nav-item">
Home
</li>
<li class="nav-item">
Contact
</li>
<li class="nav-item">
<button class="project-button">
<a class="projects-link" href="">Projects</a>
</button>
</li>
</ul>
</nav>
</header>
<section class="first">section 1</section>
<section>section 2</section>
</body>
It is working for me. Use background-color: red; to see how the body grows. Make sure you're connecting the documents correctly.
I have a navbar that is not re-sizing correctly when my logo image gets shrunk. Here is a link to the Codepen page for the complete code:
https://codepen.io/gabor-szekely/pen/JeMqQz
Basically, I am trying to re-size the "Sino Medical" logo image in the top left corner to 80% of it's original size, but when I do so, the entire navbar is not shrinking along with it, and is therefore too tall.
Can anyone help?
Here is the HTML:
<div class="navWrapper">
<nav class="flex-wrapper">
<div class="top-row-logo">
<img class="logo-img" src="https://img1.wsimg.com/isteam/ip/7bf494f1-7493-4549-b272-842b0d021577/logo/345e441d-0495-4f5b-bd62-f6413ac9b7a5.png/:/rs=h:71" alt="Sino Medical Institute">
</div>
<div class="top-row-links">
<ul>
<li>
Home
</li>
<li>
About Us
</li>
<li>
Services
</li>
<li>
Register
</li>
<li>
Contact Us
</li>
<li>
FAQ
</li>
</ul>
</div>
<div class="login-links">
<ul>
<li>
Login
</li>
</ul>
</div>
</nav>
</div>
And here is the relevant CSS:
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
}
li {
float: left;
}
li a {
text-align: center;
padding: 0 1.5em;
color: #333;
}
.navWrapper {
position: fixed;
top: 0;
left: 0;
width: 100%;
}
.flex-wrapper {
display: flex;
flex-flow: row nowrap;
background-color: white;
border-bottom: 1px solid #c8c8dc;
}
.top-row-logo {
flex: 1;
}
.logo-img {
margin-left: 3.2rem;
height: 80%; /* THIS IS THE ISSUE! */
}
.top-row-links, .login-links {
margin: auto 0;
}
.top-row-links {
flex: 1.5;
margin-right: 3.2rem;
}
.login-links {
margin-right: 4rem;
}
Thanks!
You could set the .top-row-logo to align-self: center instead of having it set to stretch which it inherits from the parent elements align-items declaration and set the .logo-img to display: block to get rid of the white space below the image.
In regards to setting the height of the element as a percentage value, this isn't possible unless you explicitly set the height of the imgelement's containing block. So for your case, you could do something like below:
.top-row-logo {
flex: 1;
align-self: center;
height: calc(71px * 0.8);
}
.logo-img {
margin-left: 3.2rem;
height: 100%;
display: block;
}
Or, if you want to be more dynamic you could use some javascript to set the height so even if the height of the image is over 71px it will always render at 80% of its original value.
See below for a demo:
// get the img
let img = document.querySelector(".logo-img");
// retrieve it's height
let imgCSS = window.getComputedStyle(img);
let imgHeight = imgCSS.getPropertyValue("height");
imgHeight = parseInt(imgHeight);
// set the height to 80% of it's original value
let newHeight = imgHeight * 0.8;
// set the height of img element to the new height
img.style.height = newHeight + "px";
*, *::before, *::after {
box-sizing: border-box;
}
body {
font-family: "Open Sans";
margin: 0;
padding: 0;
font-size: 0.8em;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
}
li {
float: left;
}
li a {
text-decoration: none;
padding: 0 1.5em;
color: #333;
transition: color 400ms ease;
}
li a:hover {
color: #6dacd5;
}
.navWrapper {
position: fixed;
top: 0;
left: 0;
z-index: 3;
width: 100%;
}
.flex-wrapper {
display: flex;
flex-flow: row nowrap;
background-color: white;
border-bottom: 1px solid #c8c8dc;
}
.top-row-logo {
flex: 1;
align-self: center;
}
.logo-img {
margin-left: 3.2rem;
display: block;
}
.top-row-links, .login-links {
margin: auto 0;
}
.top-row-links {
flex: 1.5;
margin-right: 3.2rem;
}
.login-links {
float: right;
margin-right: 4rem;
}
.login-button {
display: inline-block;
color: #008fe1;
font-size: 0.9em;
font-family: "Roboto", sans-serif;
font-weight: 700;
text-align: center;
text-decoration: none;
text-transform: uppercase;
background-color: white;
padding: .8em 1.6em;
border: 2px solid #008fe1;
border-radius: 4px;
cursor: pointer;
transition: all 400ms ease;
}
.login-button:hover {
background-color: #008fe1;
color: #fff;
}
<div class="navWrapper">
<nav id="flex-wrapper" class="flex-wrapper">
<div class="top-row-logo" id="top-row-logo">
<img class="logo-img" src="https://img1.wsimg.com/isteam/ip/7bf494f1-7493-4549-b272-842b0d021577/logo/345e441d-0495-4f5b-bd62-f6413ac9b7a5.png/:/rs=h:71" alt="Sino Medical Institute">
</div>
<div class="top-row-links">
<ul class="navbar">
<li>
Home
</li>
<li>
About Us
</li>
<li>
Services
</li>
<li>
<a href="#">
Register
</a>
</li>
<li>
Contact Us
</li>
<li>
FAQ
</li>
</ul>
</div>
<div class="login-links">
<ul>
<li>
Login
</li>
</ul>
</div>
</nav>
</div>
use auto height...
.navWrapper {
position: fixed;
top: 0;
left: 0;
z-index: 3;
width: 100%;
height:auto;
}