User Interface in a Navbar - html

I am a beginner in HTML/CSS, So I am facing a problem while trying to integrate the user interface on the Navbar like this: .
I don't know how to style it, which tags should I use and how to put icon of user...
/* Add a background color to the top navigation */
.topnav {
background-color: #e61a26;
overflow: hidden;
position: relative;
top: 0;
width: 100%;
}
/* Style the links inside the navigation bar */
.topnav a {
float: left;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
/* Change the color of links on hover */
.topnav a:hover {
background-color: #ddd;
color: black;
}
/* Add a color to the active/current link */
.topnav a.active {
background-color: #f1f3f2;
color: #000;
}
/* Add a gray right border to all a items, except the last item (last-child) */
a {
border-right: 1px solid #fff;
}
a:last-child {
border-right: none;
}
<p>Welcome <strong>Username></strong></p>
<p>logout</p>
<div class="topnav">
<a class="active" href="/WWW/home.php">Home</a>
Shortcode
Work Order
</div>

I tried to solve this with flexbox: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
There are also some special cases like the border on the last, which is invisible. For this use the :last-child selector to remove it from the last item. And to style the link to look like a normal text, use text-decoration: none; For the user image you can use the img tag. After you set your custom width and height use and border-radius: 50%; to make it automaticlly a circle.
Just study the flexbox guide and you'll see it isn't that hard. Hope this helps.
body {
margin: 0;
}
.navbar {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
background-color: red;
}
.navbar__left,
.navbar__right {
display: flex;
flex-direction: row;
align-items: center;
}
.navbar__item {
padding: 20px;
color: white;
border-right: 1px solid white;
text-decoration: none;
}
.navbar__item:last-child {
border-right: none;
}
.navbar__item--active {
background-color: white;
color: black;
}
.navbar__user-image {
width: 45px;
height: 45px;
border-radius: 50%;
}
.navbar__user-image,
.navbar__user-logout {
margin-right: 15px;
}
<nav class="navbar">
<div class="navbar__left">
Home
About us
Contact
</div>
<div class="navbar__right">
<img class="navbar__user-image" src="https://www.telegraph.co.uk/content/dam/news/2016/08/23/106598324PandawaveNEWS_trans_NvBQzQNjv4Bqeo_i_u9APj8RuoebjoAHt0k9u7HhRJvuo-ZLenGRumA.jpg?imwidth=450" alt="user img">
<span class="navbar__user-logout">user logout</span>
</div>
</nav>

So after a few tries I succeeded it's really simple here is my code now :
/* Add a background color to the top navigation */
.topnav {
background-color: #e61a26;
overflow: hidden;
position: relative;
top: 0;
width: 100%;
}
/* Style the links inside the navigation bar */
.topnav a {
float: left;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
/* Change the color of links on hover */
.topnav a:hover {
background-color: #ddd;
color: black;
}
/* Add a color to the active/current link */
.topnav a.active {
background-color: #f1f3f2;
color: #000;
}
/* Add a gray right border to all a items, except the last item (last-child) */
a {
border-right: 1px solid #fff;
}
.workorder_link {
border-right: none;
}
a:last-child {
border-right: none;
}
<nav class="navbar">
<div class="topnav">
Home
Shortcode
Work Order
Logout
<a style="float: right; text-decoration: none;">Welcome <strong>Username</strong><img src="img/user.png" style="margin-bottom: 5px; margin-left: 5px; width: 20px; height: 20px;"></a>
</div>
</nav>

Related

How do you make a navbar that covers the entire top-screen?

I'm trying to make a website, and I want a navbar to cover the entire screen, like this:
[1]: https://i.stack.imgur.com/X6zyc.png
I put the following code:
/* Add a black background color to the top navigation */
.topnav {
overflow: hidden;
padding: 5px 10px;
color: black;
}
/* Style the links inside the navigation bar */
.topnav a {
font-family: 'Roboto', sans-serif;
float: left;
color: black;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
background-color: white;
}
/* Change the color of links on hover */
.topnav a:hover {
color: #ff726f;
}
/* Add a color to the active/current link */
.topnav a.active {
color: white;
}
<div class="topnav">
<a class="#" href="#">yes</a>
Home
Commands
Community Server
</div>
i've been searching for this a lot, but still couldn't find a clear answer. could someone please help me?
Your nav is spanning complete area which you can see in below snippet as the red background .
If you want nav links to span in both ends you can use display: flex; property with justify-content:space-between;
/* Add a black background color to the top navigation */
.topnav {
display: flex;
justify-content:space-between;
overflow: hidden;
padding: 5px 10px;
color: black;
background-color: red;
}
/* Style the links inside the navigation bar */
.topnav a {
font-family: 'Roboto', sans-serif;
float: left;
color: black;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
background-color: white;
}
/* Change the color of links on hover */
.topnav a:hover {
color: #ff726f;
}
/* Add a color to the active/current link */
.topnav a.active {
color: white;
}
<div class="topnav">
<a class="#" href="#">yes</a>
Home
Commands
Community Server
</div>

Navigation Drop-Down Menu not working Properly

I am trying to create one link, Buying Tips, in my navigation menu as a drop down menu. I am running into a few problems and nothing I do seems to fix these issues.
For some reason when I scroll over Buying Tips, the first option of my drop down menu overlaps the navigation menu. I am not sure why this is happening and how to correct this.
I would like the drop down arrow to show but not sure how to code this in CSS.
I would like the navigation links to be equally spaced out and Buying Tips is all on one line. (please view image to see problem) I tried changing the width of the navbar and change the font sizes of navigation links but that does not help.
Printscreen of navigation issues on webpage
.navbar {
width: 100%;
background-color: black;
overflow: auto;
}
.navbar a {
float: left;
padding: 5px;
color: white;
text-align: center;
text-decoration: none;
font-size: 20px;
width: 24%;
}
.navbar a:hover {
color: #FFA500
}
.dropdown {
float: left;
overflow: hidden;
}
.dropdown .BuyTip {
float: left;
color: white;
text-align: center;
text-decoration: none;
font-size: 20px;
}
.dropdown-content {
display: none;
position: absolute;
min-width: 200px;
}
.dropdown-content a {
float: none;
color: black;
display: block;
text-align: left;
font-size: 10px;
}
.dropdown:hover .dropdown-content {
display: block;
}
#media screen and (max-width: 480px) {
.navbar a {
float: none;
display: block;
width: 100%;
}
}
<div class="navbar">
Chade's Bicycle Company
<div class="dropdown">
<a class="BuyTip">Buying Tips</a>
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
Choosing The Correct Bike
Setting A Budget
Test-Ride Before Buying
Choosing The Correct Size
The Essential Accessories
</div>
</div>
Company Calendar
Contact Us
</div>
I've changed your code to achieve the desired result.
As for the questions you've asked:
Since you've made the element bearing the .dropdown class absolutely positioned it will stick to the top since it has no relative parent.
I've used a CSS border property to create an arrow. You can customize it as you wish. (even replace it with the caret class you've used originally, as long as you position it appropriately.)
You can control the spacing between the navigation links using CSS margin or padding properties. read-more about padding and margin.
.navbar {
width: 100%;
background-color: black;
overflow: auto;
font-size: 0;
}
.navbar li {
display: inline-block;
margin: 0 10px;
}
.navbar li a {
display: block;
padding: 5px;
color: white;
text-align: center;
text-decoration: none;
font-size: 20px;
}
.navbar a:hover {
color: #FFA500
}
.dropdown>a {
position: relative;
padding-right: 10px;
}
.dropdown>a:after {
content: "";
display: block;
border-width: 6px;
border-style: solid;
border-right: 6px solid transparent;
border-color: red transparent transparent;
width: 0;
height: 0;
position: absolute;
right: -14px;
top: 14px;
}
.dropdown-content {
display: none;
position: absolute;
padding: 10px 0;
}
.navbar li .dropdown-content a {
color: black;
display: block;
text-align: left;
font-size: 10px;
}
.dropdown:hover .dropdown-content {
display: block;
}
#media screen and (max-width: 800px) {
.navbar li a {
font-size: 16px;
}
.dropdown>a:after {
border-width: 5px;
border-right: 5px solid transparent;
right: -10px;
top: 12px;
}
}
<div class="navbar">
<ul>
<li>Chade's Bicycle Company</li>
<li class="dropdown">
<a>Buying Tips</a>
<div class="dropdown-content">
Choosing The Correct Bike
Setting A Budget
Test-Ride Before Buying
Choosing The Correct Size
The Essential Accessories
</div>
</li>
<li>Company Calendar</li>
<li>Contact Us </li>
</ul>
</div>

Responsive Banner CSS/HTML

I am trying to make my header/banner (which will eventually be input into a SharePoint masterpage) responsive. I have a set height for the header, where usually if I am making anything responsive, I set the height to auto.
I am using a flex to contain the info within the banner. When I toggle the device emulation and make the window smaller, the headers push right and eventually are hidden along with the image on the right.
I want to make it so that when the viewport shrinks small enough, all of the headers are contained in something along the lines of an accordion or create my own button(dropdown) with the same styling as an accordion. Would I need to utilize :before or :after.
Something like this (containing all of the headers/anchors when in a small viewport):
Here is my original snippet & Fiddle:
body {
font-family: Arial, Helvetica, sans-serif;
}
.armylogo{
float: left;
justify-content: center;
height: 95%;
bottom: 0;
width: auto;
vertical-align: baseline;
}
.armylogo2{
float: right;
justify-content: center;
height: 95%;
bottom: 0;
width: auto;
vertical-align: baseline;
}
.navbar {
overflow: hidden;
background-color: #104723;
height: 94px;
width: 100%;
text-align: right;
float: left;
display: flex;
align-items: center;
justify-content: space-between;
flex-wrap: wrap;
}
.navbar a {
float: left;
font-size: 16px;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
vertical-align: center;
}
.dropdown {
float: left;
overflow: hidden;
vertical-align: center;
}
.dropdown .dropbtn {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
font-family: inherit;
margin: 0;
}
.navbar a:hover, .dropdown:hover .dropbtn {
background-color: #104723;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: #ddd;
}
.dropdown:hover .dropdown-content {
display: block;
}
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<div class="navbar">
<img src="https://upload.wikimedia.org/wikipedia/commons/1/17/Military_service_mark_of_the_United_States_Army.png" class="armylogo"/>
Home
News
Test 1
Test 2
Test 3
<div class="dropdown">
<button class="dropbtn">Dropdown
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</div>
<img src="https://upload.wikimedia.org/wikipedia/commons/1/17/Military_service_mark_of_the_United_States_Army.png" class="armylogo2"/>
</div>
HERE IS THE UPDATED VERSION FROM ANSWER (STILL NOT WORKING)
.navbar {
/* overflow: hidden; */ /* not needed */
background-color: #104723;
height: 94px;
display: flex;
align-items: center;
justify-content: space-between;
flex-wrap: wrap; /* possibly leave out to keep both logos visible for narrow views */
}
.armylogo,
.armylogo2 {
height: 95%;
/* bottom: 0; they should just sit in space */
padding: 0 2px; /* keep logo from touching edge */
width: auto;
}
.navbar a {
font-size: 16px;
color: #fff; /* use one type of color values */
padding: 14px 16px;
text-decoration: none;
}
body {
font-family: Arial, Helvetica, sans-serif;
}
#media only screen and (max-width: 600px) {
.newDiv {
width:50%;
display:flex;
flex-wrap:wrap;
}
}
.navbar a {
font-size: 16px;
color: #fff; /* use one type of color values */
padding: 14px 16px;
text-decoration: none;
}
.navbar a {
float: left;
font-size: 16px;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
vertical-align: center;
}
.dropdown {
float: left;
overflow: hidden;
vertical-align: center;
}
.dropdown .dropbtn {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
font-family: inherit;
margin: 0;
}
.navbar a:hover, .dropdown:hover .dropbtn {
background-color: #104723;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: #ddd;
}
.dropdown:hover .dropdown-content {
display: block;
}
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<div class="navbar">
<img src="https://upload.wikimedia.org/wikipedia/commons/1/17/Military_service_mark_of_the_United_States_Army.png" class="armylogo"/>
<div class="newDiv">
Home
News
Test 1
Test 2
Test 3
<div class="dropdown">
<button class="dropbtn">Dropdown
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</div>
</div>
<img src="https://upload.wikimedia.org/wikipedia/commons/1/17/Military_service_mark_of_the_United_States_Army.png" class="armylogo2"/>
</div>
You can wrap this part of your component in a div and then you can give this properties to that div in media query.
Home
News
Test 1
Test 2
Test 3
<div class="dropdown">
<button class="dropbtn">Dropdown
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
Also for your a elements and .dropdown you should give width:%50 in media query then they will be 3 rows and 2 columns in total. Of course you can also change font sizes or other featerus in media query.
#media only screen and (max-width: 600px) {
.newDiv {
width:100%;
display:flex;
flex-wrap:wrap;
}
}
Agree with #EvrenK about the approach. One thing to note about the ::before and ::after pseudo-elements is that you can't attach events. So really a styled button would be the best approach, with the best semantics too.
A little off topic, but noticed a few general things about your sample. One practice that might help is grouping your selectors, also you have some bonus declarations floating around. Flexbox should pretty much buy you everything you'll need for this nav. So no floating.
CSS tends to be kinda verbose and carving away the extra stuff generally makes it a lot more readable/less headache inducing.
.navbar {
/* overflow: hidden; */ /* not needed */
background-color: #104723;
height: 94px;
display: flex;
align-items: center;
justify-content: space-between;
flex-wrap: wrap; /* possibly leave out to keep both logos visible for narrow views */
}
.armylogo,
.armylogo2 {
height: 95%;
/* bottom: 0; they should just sit in space */
padding: 0 2px; /* keep logo from touching edge */
width: auto;
}
.navbar a {
font-size: 16px;
color: #fff; /* use one type of color values */
padding: 14px 16px;
text-decoration: none;
}

Navbar menu with large dropdown

I need to create menu like shown in this screenshot:
So as you can, the cursor hover opens large sub menu with two sub sections. Will be glad for any similar examples to my issues. Thanks for your answers!
Here is a very simple example to get you started. It needs more styling of course and more content, but this should give you all the tools to have a dropdown on hover in your menu
HTML:
<header>
<a href="/url">
Hover to see dropdown
</a>
<div>
<section></section>
<section></section>
</div>
</header>
CSS:
header {
position: fixed;
top: 0;
left: 0;
width: 100vw;
height: 50px;
}
header > a {
padding: 0 2em;
height: 50px;
display: grid;
place-content: center;
}
header > div {
display: hidden;
background-color: white;
}
a:hover + div {
display: inherit;
}
Something like this should work. It utilizes the :hover attribute in the navbar tab 'Dropdown' to reveal more content (in this case it'll be just some links).
HTML:
<div class="navbar">
<div class="dropdown">
<button class="dropbtn">Dropdown
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</div>
</div>
CSS:
.navbar {
overflow: hidden;
background-color: #333;
}
.dropdown {
float: left;
overflow: hidden;
}
.dropdown .dropbtn {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
font-family: inherit;
margin: 0;
}
.navbar a:hover, .dropdown:hover .dropbtn {
background-color: red;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: #ddd;
}
.dropdown:hover .dropdown-content {
display: block;
}
You can find more information on the W3School website for more!

how do i position my nav element under my header element?

I want my navigation to be under my 'Acme Web Design' header whenever I view it on a mobile device. All my elements are positioned in the correct place for a laptop screen but when I check if it is responsive, they don't position at the place where I want them to be.
Here is what my header looks like in a responsive view.
This is the HTML and CSS file i used.
.headerdiv {
display: flex;
justify-content: space-between;
align-items: flex-end;
}
/* Header */
header{
background-color: #35424a;
border-bottom: 2px solid #ff6600;
color: white;
padding-top: 30px;
min-height: 70px;
}
nav {
float: right;
margin-bottom: 30px ;
}
nav a {
color: white;
text-decoration: none;
padding: 10px;
}
<header>
<div class="container">
<div class="headerdiv">
<h1>Acme Web Design</h1>
<nav>
HOME
ABOUT
SERVICES
</nav>
</div>
</div>
</header>
This is what I want my header to look like
I guess this is what you were trying to achieve. Here is the working Codepen Link
body {
font-family: sans-serif;
}
.headerdiv {
display: flex;
justify-content: space-between;
flex-direction: column;
align-items: center;
}
.headerdiv h1 {
margin-bottom: 20px;
font-size: 32px;
font-weight: bold;
}
.headerdiv h1 span {
color: #e7491c;
}
/* Header */
header {
background-color: #35424a;
border-bottom: 2px solid #ff6600;
color: white;
padding-top: 30px;
min-height: 70px;
}
nav {
margin-bottom: 30px;
}
nav a {
color: white;
text-decoration: none;
padding: 10px;
font-size: 20px;
font-weight: bold;
}
nav a.active {
color: #e7491c;
}
<header>
<div class="container">
<div class="headerdiv">
<h1><span>Acme</span> Web Design</h1>
<nav>
HOME
ABOUT
SERVICES
</nav>
</div>
</div>
</header>
Change your flex settings for the container as follows (especially flex-direction: column;), use text-align: center for the child elements of .headerdiv to center them and delete the floats to include all elements in the parent element/background
Oh, and put those extra rules in a media query to leave your desktop view untouched - the snippet below only shows the mobile view, no media queries (since you didn't have any in your code)
headerdiv {
display: flex;
flex-direction: column;
}
.headerdiv>* {
text-align: center;
}
/* Header */
header {
background-color: #35424a;
border-bottom: 2px solid #ff6600;
color: white;
padding-top: 30px;
min-height: 70px;
}
nav {
margin-bottom: 30px;
}
nav a {
color: white;
text-decoration: none;
padding: 10px;
}
<body>
<header>
<div class="container">
<div class="headerdiv">
<h1>Acme Web Design</h1>
<nav>
HOME
ABOUT
SERVICES
</nav>
</div>
</div>
</header>
Change your css property of class .headerdiv and remove nav class.
.headerdiv {
text-align: center;
margin-bottom: 30px;
}
/* Header */
header {
background-color: #35424a;
border-bottom: 2px solid #ff6600;
color: white;
padding-top: 30px;
min-height: 70px;
}
nav a {
color: white;
text-decoration: none;
padding: 10px;
}
try this.. you will get same result in any device.
if you want to increase the size of the menu u can do that using font-size..
headerdiv {
display: flex;
justify-content: space-between;
align-items: flex-end;
}
/* Header */
header{
background-color: #35424a;
border-bottom: 2px solid #ff6600;
color: white;
padding-top: 8px;
min-height: 70px;
}
nav {
/* float: right; */
text-align: center;
margin-bottom: 30px ;
}
nav a {
color: white;
text-decoration: none;
padding: 10px;
}
<header>
<div class="container">
<div class="headerdiv">
<div>
<h1 style="text-align: center;">Acme Web Design</h1>
</div>
<div>
<nav>
HOME
ABOUT
SERVICES
</nav>
</div>
</div>
</div>
</header>