I'm building myself a website on html css etc. (college work so that's a reason why) however I've had struggle with my navigation bar.
The first thing is that it goes to the side of the page which I don't want. I want it at the top like for example the bar at the top of stack overflow. I'm using flexbox for my navigation bar and i like the sizes of each section. My code is:
body {
background-color: #000000;
color: #FFFFFF;
font-family: Georgia;
}
nav {
background-color: #000000;
color: #888;
margin: 0px 0px 0px 0px;
overflow: hidden;
width: 100%;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
-webkit-align-items: center;
-ms-flex-align: center;
align-items: center;
}
nav ul {
float: left;
display: block;
margin: 0;
padding: 0;
}
nav ul li {
float: top;
list-style: none;
text-align: center;
}
nav > ul > li > a {
color: #aaa;
display: block;
line-height: 56px;
padding: 0;
text-decoration: none;
text-align: center;
}
nav > ul > li:hover > a {
color: rgb(255, 255, 255);
}
<nav>
<ul>
<li>
<h1>Title</h1>
</li>
<li>Home
</li>
<li>Forum
</li>
<li>Music
</li>
<li>About
</li>
<li>Shop
</li>
</ul>
</nav>
float:top
Is invalid.
The CSS float property can be declared as left || right, or left as it is
Floating elements can be tricky at the best of times. Instead, you could use something like display:inline-block instead.
more info on floating elements
All about floats
A float is a box that is shifted to the left or right on the current
line. The most interesting characteristic of a float (or "floated" or
"floating" box) is that content may flow along its side (or be
prohibited from doing so by the 'clear' property). Content flows down
the right side of a left-floated box and down the left side of a
right-floated box. The following is an introduction to float
positioning and content flow; the exact rules governing float behavior
are given in the description of the 'float' property. ~ w3.org
DEMO:
nav ul li {
display:inline-block;
vertical-align:middle;
list-style: none;
text-align: center;
}
body {
background-color: #000000;
color: #FFFFFF;
font-family: Georgia;
}
nav {
background-color: #000000;
color: #888;
margin: 0px 0px 0px 0px;
overflow: hidden;
width: 100%;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
-webkit-align-items: center;
-ms-flex-align: center;
align-items: center;
}
nav ul {
display: inline-block;
margin: 0;
padding: 0;
}
nav ul li {
display:inline-block;
vertical-align:middle;
list-style: none;
text-align: center;
}
nav > ul > li > a {
color: #aaa;
display: block;
line-height: 56px;
padding: 0;
text-decoration: none;
text-align: center;
}
nav > ul > li:hover > a {
color: rgb(255, 255, 255);
}
<nav>
<ul>
<li>
<h1>Title</h1>
</li>
<li>Home
</li>
<li>Forum
</li>
<li>Music
</li>
<li>About
</li>
<li>Shop
</li>
</ul>
</nav>
you are doing a great job. I would like to suggest a few things as I have already experienced some problems.
Use Bootstrap. All you need to do is include a CSS and a JavaScript file and write navbar in class = "navbar navbar-default navbar-fixed-top" and automatically it will be placed at the top with much better looks and having responsive design. It is not difficult at all. It will hardly take and hour or two to understand and implement it in your website.
Related
This question already has answers here:
What does margin auto mean?
(2 answers)
What is the meaning of `auto` value in a CSS property.
(4 answers)
Closed yesterday.
I'm trying to create in-page navigation menu by styling ul & li with CSS. With the help of previous answers and a lot of trial and error, I've nearly done it - but can't get the top line to align to the left. Instead, it's indented a little bit with extra space somehow.
The real page I'm trying to update is below:
https://l-a-m.org/pages/banstead
Could anyone tell me where I'm going wrong please?
ul,
li {
list-style-type: disc;
text-align: left;
padding-inline-start: 0;
min-width: fit-content;
padding-right: 2em;
padding-top: 1em;
align-self: flex-end;
color: #363535;
}
li:hover {
color: #363535;
border-bottom: transparent 1px !important;
}
ul.tabbies {
display: flex;
flex-wrap: wrap;
max-width: auto;
margin: auto;
text-align: left;
text-transform: uppercase;
justify-content(flex-start);
align-items(stretch);
align-self: flex-end;
color: #363535;
}
.tabbies .active {
color: #c70000;
text-transform: uppercase;
}
.tabbies li {
text-align: left;
text-transform: uppercase;
list-style: none;
}
.tabbies li a {
text-decoration: none;
text-align: left;
color: #363535;
}
.tabbies li:hover {
color: #c70000;
text-transform: uppercase;
border-bottom: solid #000 1px !important;
}
<ul class="tabbies">
<ul class="tabbies">
<li>Rides</li>
<li><a class="active" href="https://www.l-a-m.org/pages/banstead">Locations</a></li>
<li>Training Weekends</li>
<li>Observers</li>
</ul>
</ul>
<ul class="tabbies">
<li><a class="active" href="https://www.l-a-m.org/pages/banstead">Banstead</a></li>
<li>Tatsfield</li>
</ul>
I would imagine you're looking for something like this:
ul {
display: flex;
}
li {
padding-right: 2em;
padding-top: 1em;
color: #363535;
position: relative;
}
ul li ul {
border: 1px pink solid;
position: absolute;
top: 100%;
}
I'm guessing that you're gearing up for the sub menu to appear on hover. In that case, positioning the inner beneath the top level nav is quite useful.
Try adding class the first ul and using > to define the exact child within the parent
What is happening here is that when you enter between two tags in css that means to apply the following style to all children are within the called tag even nested child.
Where as for > it only applies to the direct child.
ul.main_ul > li {
display: inline-block;
text-align: left;
min-width: fit-content;
padding-right: 2em;
padding-top: 1em;
color: #363535;
}
ul.main_ul > li > ul > li {
display: inline-block;
}
<ul class="main_ul">
<li>Rides
<ul>
<li>Subitem 1</li>
<li>Subitem 2</li>
</ul>
</li>
<li>Locations</li>
<li>Observers</li>
</ul>
It turned out to be easier than I expected with a change in the 'margin' parameter:
ul.tabbies {
display: flex;
flex-wrap: wrap;
max-width: auto;
margin: 0px;
text-align: left;
text-transform: uppercase;
justify-content(flex-start);
align-items(stretch);
align-self: flex-end;
color: #363535;
}
Changing 'margin: auto' to 'margin: 0px' was all it needed
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
All the questions I've looked at refer to WordPress or Bootstrap (what is that?) nav bars, I have made mine using CSS.
I would like to make my nav bar bigger so that it's easier for mobile users to click the correct link. I've tried using the height: px; but all that did was push the text below further down.
What do I use to change the size of the buttons themselves? included my CSS below.
html{background:gray;}
ul {
left: 0;
top: 50%;
width: 100%;
text-align: center;
display: inline-block;
list-style-type: none;
}
li {
display: inline-block;
}
li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
margin: 0
}
li a:hover:not(.active) {
background-color: #111;
}
<ul>
<li><a class="active" href="#home">Home</a>
</li>
<li>About
</li>
<li>Contact
</li>
</ul>
Please note I have added backgrounds in order to display the navbar, and are not required in production
You are OK to use the ul and li elements within your code. In order to make the navbar appear 'taller', you would need to set both the height of the ul element itself, as well as the child li. A quick demo has been provided below.
I have given the height of the ul element 100px, although this value can be changed to your preference. Note you may also want to change line-height property of your a elements to suit this.
html,body {
background: gray;
margin:0;
padding:0;
}
ul {
left: 0;
top: 50%;
text-align: center;
display: block;
list-style-type: none;
background: dimgray;
height: 100px; /* <-- change this line*/
}
li {
display: inline-block;
height: 100%;
}
li a {
display: inline-block;
color: white;
background: lightgray;
line-height: 100px; /* <-- change this line*/
text-align: center;
padding-left: 50px;
padding-right: 50px;
text-decoration: none;
margin: 0;
height: 100%;
}
li a:hover:not(.active) {
background-color: #111;
}
<ul>
<li><a class="active" href="#home">Home</a>
</li>
<li>About
</li>
<li>Contact
</li>
</ul>
What do I use to change the size of the buttons themselves?
Add more padding! Take a look-see.
body {background-color: gray;}
ul {
left: 0;
top: 50%;
width: 100%;
text-align: center;
display: inline-block;
list-style-type: none;
}
li {
display: inline-block;
}
li a {
display: block;
color: white;
text-align: center;
padding: 2em; /* bigger button? add more padding! */
text-decoration: none;
margin: 0
}
li a:hover:not(.active) {
background-color: #111;
}
<ul>
<li><a class="active" href="#home">Home</a>
</li>
<li>About
</li>
<li>Contact
</li>
</ul>
There are many ways to increase the size of the link. This is just one way. jbutler's answer is a good way too. It just depends on what exactly you want it to do.
Hope this helps.
If you are trying to make the text itself larger you can use the font-size property.
I'm trying to make my nav bar be horizontal and centered. Right now, it's all smushed to the right of the website. How do I control where the bar is located?
Also, why can't I just have all my css under #nav (why do I need #nav ul li, etc?)
#nav{
width: 90%;
text-align: center;
font-size: 35px;
text-align: center;
}
#nav ul li{
display: inline;
height: 60px;
text-align: center;
}
#nav ul li a{
padding: 40px;
background: #25ccc7;
color: white;
text-align: center;
}
<div id="nav">
<ul>
<li>Home</li>
<li>About</li>
<li>Contact</li>
</ul>
</div>
You need the different css selectors because not all the styles are meant to be applied to the surrounding container #nav, but to certain elements inside it. With pure CSS, it is not possible to nest rules. If nesting is what you were asking for, take a look at LESS or SASS (DuckDuckGo is your friend).
Your list items were pushed a little to the right because that is the natural behaviour of list items unless you overwrite the responsible css attributes margin and / or padding (depending on the browser). So just reset both to 0 in your CSS for the list items.
#nav {
background-color: #e8e8e8;
width: 90%;
font-size: 35px;
text-align: center;
margin: 0 auto;
}
#nav ul {
display: inline-block;
white-space: nowrap;
margin: 0;
padding: 0;
}
#nav ul li {
display: inline;
text-align: center;
margin: 0;
padding: 0;
}
#nav ul li a {
padding: 40px;
background: #25ccc7;
color: white;
text-align: center;
}
<div id="nav">
<ul>
<li>Home</li>
<li>About</li>
<li>Contact</li>
</ul>
</div>
I am building a CSS only two-level horizontal navigation bar with relative sub-navigation to the parent. All menu items are inline. Dependent upon the classes 'right' or 'left', the sub-nav aligns to the parent. This is what I've managed to accomplish so far:
html:
<body>
<div class="navbar">
<ul class="topnav left">
<li>nav</li>
<li>menu1
<span class="subnav">
<ul class="subnav subnav-left">
<li>item1-1</li>
<li>item1-2</li>
<li>item1-3</li>
</ul>
</span>
</li>
<li>menu2
<span class="subnav">
<ul class="subnav subnav-left">
<li>item2-1</li>
<li>item2-2</li>
<li>item2-3</li>
</ul>
</span>
</li>
</ul>
<ul class="topnav right">
<li class="right">menu3
<span class="subnav subnav-right">
<ul class="subnav subnav-left">
<li>item3-1</li>
<li>item3-2</li>
<li>item3-3</li>
</ul>
</span>
</li>
<li class="right">menu4
<span class="subnav subnav-right">
<ul class="subnav subnav-left">
<li>item4-1</li>
<li>item4-2</li>
<li>item4-3</li>
</ul>
</span>
</li>
</ul>
</div>
</body>
css:
body {
font-family: arial;
margin: 0;
}
.navbar {
height: 40px;
background-color: black;
}
ul.topnav {
margin: 0;
padding: 0;
}
.subnav {
position: absolute;
}
.subnav-right {
right: 0;
}
ul.subnav {
position: relative;
margin: 4px 0 0 -8px;
padding: 0;
display: none;
}
ul.topnav li{
list-style: none;
display: inline-block;
color: white;
padding: 4px 8px;
font-weight: bold;
line-height: 32px;
float: left;
clear: none;
box-sizing: border-box;
}
ul.subnav li {
background-color: red;
list-style: none;
display: inline-block;
color: white;
padding: 4px 8px;
font-weight: bold;
position: relative;
line-height: 32px;
float: left;
clear: none;
box-sizing: border-box;
}
.topnav li:hover {
background-color: red;
}
.topnav li:hover ul.subnav {
display: inline-block;
background-color: red;
}
.nav ul li:hover {
background-color: black;
}
.nav ul li {
width: 100%;
}
.nav li ul {
display: inline-block;
clear: none;
position: absolute;
background-color: red;
margin: 4px 0 0 -8px;
padding: 0;
}
.left {
float: left;
}
.right {
float: right;
}
The jsfiddle:
jsfiddle.net/aLZqZ
Here is what I'm trying to accomplish:
image to nav menu
I got this for you http://jsfiddle.net/aLZqZ/99/. In under 100 tries, too. I became a little obsessed and spent at least 5 hours total. A good challenge for me and I have never really fiddled with sub navs before.
This issue was three fold:
Using float:right for a horizontal nav bar is usually not good in my experience because it causes unexpected issues, also it is negated and ignored by browsers if the same element is positioned relative or absolute (you had a lot of superfluous code, btw). I changed float:right to text-align:right where necessary. See this for horizontal nav I fixed for someone recently: Aligning/floating my nav bar to the right
The li element containing the sub menu was not positioned, therefore, the position:absolute and right:0 on the ul within it moves according to the closest containing element that is position:absolute or :relative. In this case there was not one so that element was html; thus the ul would be pushed all the way right to the end of the page. I added position:relative to these li elements which then made the right:0 behave as expected, but did not put all the li element on one line and stacked them instead.
You had tags with display:inline-block when :inline would have done it, but more importantly, no one ever really mentions that white-space:nowrap on the same elements to do what you are trying here is important. inline-block and nowrap together should force one line block like elements that you can align or float as whole as if they were a paragraph. BTW, IE7 needs some special attention for inline-block. See here: http://robertnyman.com/2010/02/24/css-display-inline-block-why-it-rocks-and-why-it-sucks/
I made special css at the bottom of yours in your fiddle to separate the left and right navs, and I basically left your original css alone. I also adjusted the html a bit. Here it all is.
HTML for the right nav (follows the HTML for the left nav):
<ul class="rightNav">
<li>menu3
<ul class="rightSubNav">
<li>item3-1</li>
<li>item3-2</li>
<li>item3-3</li>
</ul>
</li>
<li>menu4
<ul class="rightSubNav">
<li>item4-1</li>
<li>item4-2</li>
<li>item4-3</li>
</ul>
</li>
</ul>
CSS that I added to separate the right and left nav:
ul.rightNav {
margin:0;
padding:0;
text-align: right;
}
.rightNav li:hover {
background-color: red;
}
ul.rightNav li{
list-style: none;
display: inline-block;
color: white;
padding: 4px 8px;
font-weight: bold;
line-height: 32px;
position:relative;
}
ul.rightSubNav {
position: absolute;
right:0;
margin: 4px 0 0 -20px;
padding: 0;
display: none;
white-space:nowrap;
}
ul.rightSubNav li {
background-color: red;
list-style: none;
display: inline;
color: white;
padding: 4px 8px;
font-weight: bold;
position: relative;
line-height: 32px;
}
.rightNav li:hover ul.rightSubNav {
display: inline-block;
background-color: red;
}
If this helped I would appreciate the up votes and answer select. If you figured something else out and got it working differently please post. I would love to see it.