Spreading out Centered Navigation Bar in HTML - html

Banners
I am looking to "spread out" my centered banner in HTML. I was able to actually spread out the text, but I want to make the links from the banner bigger, as they are currently on the size of my text. If any clarification is needed, please let me know.
body {
margin: 0;
font-family: Times New Roman;
}
.topnav {
overflow: hidden;
background-color: #0F5AEA;
display: flex;
justify-content: center;
justify-content: space-between;
}
.topnav a {
float: left;
color: #F2F2F2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.topnav a:hover {
background-color: #BBEAFA;
color: black;
}
.topnav a.active {
background-color: #6F16A5;
color: white;
}
<div class="topnav">
<a class="active" href="#home">Home</a>
News
Contact
About
</div>
<div style="padding-left:16px">
<h2>Top Navigation Example</h2>
<p>Some content..</p>
</div>

Simply add flex-grow: 1 to the navigation links. If you want all of them to be the same width, you can use flex: 1 which does the same as a combination of flex-grow: 1 and flex-basis: 0.
.topnav a {
flex-grow: 1; // or flex: 1;
float: left; // this does not have effect on flex items by the way
color: #F2F2F2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
If you want to learn more, there is no need to repeat already well documented:
https://developer.mozilla.org/en-US/docs/Web/CSS/flex-grow
https://developer.mozilla.org/en-US/docs/Web/CSS/flex-basis
Here is one of your options:
body {
margin: 0;
font-family: Times New Roman;
}
.topnav {
overflow: hidden;
background-color: #0F5AEA;
display: flex;
justify-content: center;
justify-content: space-between;
}
.topnav a {
flex: 1;
float: left;
color: #F2F2F2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.topnav a:hover {
background-color: #BBEAFA;
color: black;
}
.topnav a.active {
background-color: #6F16A5;
color: white;
}
<div class="topnav">
<a class="active" href="#home">Home</a>
News
Contact
About
</div>
<div style="padding-left:16px">
<h2>Top Navigation Example</h2>
<p>Some content..</p>
</div>

Just give it a max-width:25% and force width: 100%. Also you do not need float since you're using flex.
.topnav a {
max-width: 25%;
width: 100%;
color: #F2F2F2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
Check it live on Codepen here.

Related

Use multiple style tags in the A tag

Just so you know, I'm still learning HTML, and I'm not very knowledgeable.
So I'm trying to figure out how to add multiple styles to one of my buttons on my navbar
<div id="navbar">
Home
Skills
Interests
<a style="float:right" href="#about">About</a>
</div>
On the bottom line I want to include: (You will need to use the style sheet below for the HTML to format correctly its just above the last sentence.)
style="background-color: #04AA6D"
Into the the code so that the background is turned green
I have this working on the other buttons that aren't floating right
here's my main pages one, the button named "home" is green on the back and that's how I want the About pages one to be.
<div id="navbar">
<a style="background-color: #04AA6D" href="#home">Home</a>
Skills
Interests
<a style="float:right" href="#about">About</a>
</div>
I'm using this style sheet (Needed for the page to format correctly.)
<style>
body {
margin: 0;
background-color: #f1f1f1;
font-family: Arial, Helvetica, sans-serif;
}
#navbar {
background-color: #333;
position: fixed;
top: 0;
width: 100%;
display: block;
transition: top 0.3s;
}
#navbar a {
float: left;
display: block;
color: #f2f2f2;
text-align: center;
padding: 15px;
text-decoration: none;
font-size: 17px;
}
#navbar a:hover {
background-color: #ddd;
color: black;
}
li a:hover:not(.active) {
background-color: #111;
}
li {
float: left;
}
li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
</style>
I hope you understand what I mean, I'm horrible at explaining things 😂. Any Questions you need me to answer so that you can help please let me know
I tried connecting the two together like this
<a style="float:right" style="background-color: #04AA6D" href="#contact">Interests</a>
But that didn't work
body {
margin: 0;
background-color: #f1f1f1;
font-family: Arial, Helvetica, sans-serif;
}
#navbar {
background-color: #333;
position: fixed;
top: 0;
width: 100%;
display: block;
transition: top 0.3s;
}
.style1{
float:right !important;
}
.style2{
background: #04AA6D;
}
#navbar a {
float: left;
display: block;
color: #f2f2f2;
text-align: center;
padding: 15px;
text-decoration: none;
font-size: 17px;
}
#navbar a:hover {
background-color: #ddd;
color: black;
}
li a:hover:not(.active) {
background-color: #111;
}
li {
float: left;
}
li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
<div id="navbar">
<a style="background-color: #04AA6D" href="#home">Home</a>
Skills
Interests
<a class="style1 style2" href="#about">About</a>
</div>
You can implement the following approach:
Create classes for different CSS:
.style1{
float:right !important;
//i used '!important' because you are overriding other class
}
.style2{
background-color: #04AA6D
}
.style3{
//your 3rd extra css here
}
Then you can implement multiple css in following way:
<a class="style1 style2 style3">Example</a>

How do I add corner logos into Footer using Html & CSS?

So for one of my school projects, we have to design a website using Html & CSS and I've encountered a problem where I don't know how to fit 2 logos (left & right) into the corners of my footer. I've tried to change the position, float, width etc and it doesn't seem to work, the logo always seems to not go in the place I want it to be. Sorry if this sounds amateur as I've only started doing Html & CSS a few weeks ago.
This is the current image where the logo is below my ul's and is in the incorrect position. -
This is what I want the footer to look like, it would be great to have the text on the left "Sponsored by HP omen gaming" to be a image as I may swap it out with another logo in the future. -
Thanks to anyone in advance who may be able to solve this problem for me, below would be my html & CSS code.
.footer {
background-color: #035642;
margin-top: 10px;
height: 60px;
color: #efe5d0;
text-align: center;
padding: 15px;
font-size: 100%;
font-family: Arial;
display: block;
}
.footer ul {
padding: 5px;
list-style-type: none;
text-align: center;
margin: 10px;
overflow: hidden;
}
.footer li {
text-decoration: none;
text-align: center;
font-family: arial;
font-weight: bold;
list-style-type: none;
display: inline-block;
}
.footer ul li {
display: inline-block;
}
.footer ul li a {
text-decoration: none;
padding: .2em 1em;
color: #efe5d0;
background-color: #5c755e;
text-align: center;
font-family: arial;
font-weight: bold;
}
.footer ul li a:hover {
color: #000;
background-color: #fff;
}
#footer-right {
height: 50px;
width: auto;
position: fixed;
float: right;
}
<div class="footer">
<li>WBHS ESPORTS</li>
<ul>
<li>Facebook</li>
<li>|</li>
<li>YouTube</li>
<li>|</li>
<li>Twitch</li>
</ul>
<img src="hp-omen-logo.png" id="footer-right">
</div>
Try look into flexbox. Here's a quick template to do what you want.
.footer {
display: flex;
justify-content: space-between;
}
.center {
text-align: center;
}
<div class="footer">
<p>Left</p>
<div class="center">
<p>Some text</p>
<p>Your list</p>
</div>
<p>Right</p>
</div>
I think a solution can be the use of display:flex
And to be more clean try to use also the widgets, you can see in this example how it works.
.footer {
background-color: #035642;
margin-top: 10px;
height: 60px;
color: #efe5d0;
text-align: center;
padding: 15px;
font-size: 100%;
font-family: Arial;
display: block;
}
.footer ul {
padding: 5px;
list-style-type: none;
text-align: center;
margin: 10px;
overflow: hidden;
}
.footer li {
text-decoration: none;
text-align: center;
font-family: arial;
font-weight: bold;
list-style-type: none;
display: inline-block;
}
.footer ul li {
display: inline-block;
}
.footer ul li a {
text-decoration: none;
padding: .2em 1em;
color: #efe5d0;
background-color: #5c755e;
text-align: center;
font-family: arial;
font-weight: bold;
}
.footer ul li a:hover {
color: #000;
background-color: #fff;
}
#footer-right {
height: 50px;
width: auto;
position: fixed;
float: right;
}
/*my-edit*/
#the-footer{
display:flex;
flex-wrap:wrap;
justify-content:space-between;
align-items:center;
height: auto;
margin: 0;
padding: 10px;
}
#the-footer .widget{
width: 30%;
}
#the-footer .widget.left{
text-align: left;
}
#the-footer .widget.right{
text-align: right;
}
#the-footer .widget.center{
text-align: center;
}
#the-footer .widget .title{
font-weight: bold;
letter-spacing: 2px;
}
#the-footer .widget .logo-link{
color:#fff;
display: inline-block;
text-decoration: none;
max-width:150px;
}
#the-footer .widget .logo-link:hover{
color:#000;
}
<div class="footer" id="the-footer">
<div class="widget left">
<a class="logo-link" href="#" target="_blank">Sponsored by HP omen gaming</a>
</div>
<div class="widget center">
<div class="title">WBHS ESPORTS</div>
<ul class="footer-nav">
<li>Facebook</li>
<li>|</li>
<li>YouTube</li>
<li>|</li>
<li>Twitch</li>
</ul>
</div>
<div class="widget right">
<a class="logo-link" href="#" target="_blank"><img class="logo-footer" src="https://via.placeholder.com/60" alt="logo"></a>
</div>
</div>
You can use the display: grid property so you can separate your footer into 3 parts
You can learn about the grid property here: https://www.w3schools.com/css/css_grid.asp

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;
}

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>

How to center a bottom navigation bar?

Hello I added a bottom navigation bar in HTML and CSS code on my webpage but I want have that centered on the bottom of the website any help and solutions is greatly appreciated I have attached screenshots of the HTML, CSS codes and the webpage of where I have my navigation bar currently in the left hand side thanks very much in advance.
.navbar {
overflow: hidden;
background-color: #333;
position: fixed;
bottom: 0;
width: 100%
}
.navbar a {
float: left;
display: block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
margin: 0px auto;
}
.navbar a:hover {
background: #f1f1f1;
color: black;
}
<div class="navbar">
About
Contact
Facebook Page
</div>
HTML navbar code
CSS navbar code
webpage navbar
Just Replace your CSS code with below css code:
.navbar {
overflow: hidden;
background-color: #333;
position: fixed;
bottom: 0;
width: 100%;
display: flex;
align-items: center;
justify-content: center;
}
.navbar a {
color: #f2f2f2;
text-align: center;
display: inline-block;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
margin: 0px auto;
}
.navbar a:hover {
background: #f1f1f1;
color: black;
}
and your HTML with this code:
<div class="navbar">
<div class="navbar-inner">
About
Contact
Facebook Page
</div>
</div>
Hope this code will work as you expect ..
Add to .navbar:
text-align:center;
Delete from .navbar a:
float: left;
Update in .navbar a:
display: inline-block;
body {margin:0;}
.navbar {
overflow: hidden;
background-color: #333;
position: fixed;
bottom: 0;
width: 100%;
}
.navbar a {
float: left;
display: block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.navbar a:hover {
background: #f1f1f1;
color: black;
}
.navbar a.active {
background-color: #DDA0DD;
color: white;
}
.main {
padding: 16px;
margin-bottom: 30px;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
</style>
</head>
<body>
<div class="navbar">
About
Contact
Facebook page
</div>
<div class="main">
<h1>Bottom Navigation Bar</h1>
<p>Some text some text some text.</p>
</div>
</body>
</html>
How to center a nav-bar at the bottom of a page