Why are the navigation links stacked when using inline-block? - html

I want the navigation links: "about", "resume", "projects", and "contact" to line up horizontally in the navigation bar.
Why does this only work with display: inline-block?
It is my understanding that inline-block boxes allows these elements to be side by side. I need it to be inline-block instead of just inline because I want to size it to the nav bar's exact height.
What am I doing wrong?
Here is the HTML and CSS for my nav:
/* ----------------------------NAVIGATION SECTION-------------------------------- */
.headerContainer {
background-color: #000;
text-align: center;
height:60px;
margin-left: 600px;
margin-right: 600px;
font-family: 'Monda', sans-serif;
text-transform: uppercase;
position: fixed;
}
nav {
padding-left: 1000px;
padding-right: 1000px;
}
nav li {
list-style: none;
display: inline-block;
background-color: #000;
height: 40px;
padding-top: 20px;
width: 120px;
}
nav li:hover {
background-color: #e1e1e1;
-webkit-text-stroke: 2px #000;
}
a:link {
color: #fff;
text-decoration: none;
margin-left:25px;
margin-right:25px;
}
a:visited {
color: #fff;
}
a:focus {
color: #fff;
}
a:hover {
}
a:active {
color: #fff;
}
<!------------------------------NAVIGATION SECTION---------------------------------->
<header class="headerContainer">
<nav>
<ul>
<!-- you put the end tag ">" at the beginning of next line to get rid of whitespace between the links -->
<li>About</li
><li>Resume</li
><li>Projects</li
><li>Contact</li>
</ul>
</nav>
</header>

You have a massive amount of padding inside the nav element.
nav {
padding-left: 1000px;
padding-right: 1000px;
}
This doesn't leave very much space for the content to render in.
The li elements are laid out side by side until they run out of space, at which point they word wrap.
If you zoom out, you'll see the fit in one line.
If you look at the live demo in your question, with the very narrow frame, you will see everything squashed to the side.
Don't set a huge padding on the nav element.

Related

why are my buttons not on top of each other when I have already used display: block?

i want 2 of my buttons to be stacked (on top of each other). however, after using display: block, the buttons are still side by side. here are my html & css code!
does anyone know why? i had done my research and display:block is the way to code my buttons to make them on top of each other. would appreciate any help!
thank you in advance for everyone that have helped me! i'm really grateful. :)
.homepage_viewcurrentproject
{
font-family : Times New Roman;
font-size : 28px;
color : #FFFFFF;
color : rgb(255, 255, 255);
}
nav#button
{
float: left;
padding: 15px;
width: 150px;
height:350px;
margin-top: 15px;
margin-left: 65px;
text-align: center;
}
nav#button ul li
{
margin: 0; /*Setting margins and padding to 0 to remove browser default settings*/
padding: 0;
list-style-type: none; /*remove the bullets*/
color: white;
}
nav#button li a
{
display: block; /*to allow changes of- width,height, padding and margin around the link*/
width: 350px; /* length of button */
padding: 5px 10px; /*top and bottom:5px left and right:10px*/
background-color: #25374C;
text-decoration: none; /* remove the underline of hyperlink */
margin:10px;
padding: 30px 40px;
background: #25374C;
border-radius: 18px;
}
nav#button a:link{
color: white;
text-decoration: none;
}
nav#button a:visited{
color: white;
}
nav#button li a:hover, /*add a style to an element when you mouse over it*/
button li a:focus /*add a style to an element that has keyboard input focus*/
{
background-color: #2c425c;
color: #CCFFC5;
}
nav#button li a:active /*add a style to an element that is activated*/
{
background-color: #25374C;
}
<nav id="button" class="homepage_viewcurrentproject">
<ul>
<li>
Check out my Python Study
</li>
<li>
View latest project
</li>
</ul>
</nav>
enter image description here
You can use this method:
nav ul {
display: flex;
flex-direction: column;
}

Navbar chopped off when resizing

My navbar gets chopped off from both sides whenever I resize the window, like in this picture.
Chop chop
I want the navbar to fit the entire screen, even when the window is resized, so that all the links and logo are visible.
I tried making the width 100vw but it has no visible effect.
Here is my HTML and CSS:
<header>
<nav>
<ul>
<li><div class="container"><img src="https://i.ibb.co/SP0TLzQ/broozeb.png" alt="logo" class="logo" border="0"><div class="overlay"><div class="text-test">米</div></div></div></li>
<li>ホーム</li>
<li>米さんについて</li>
<li>日本の文化</li>
<li>学習の情報</li>
<li>English Stuff</li>
</ul>
</nav>
</header>
nav {
background-color: blue;
border-bottom: solid #09316b;
white-space: nowrap;
}
nav ul {
margin: 0;
padding: 0;
display: flex;
justify-content: center;
}
nav ul li {
list-style-type: none;
margin-left: 5px;
}
nav ul li a {
color: white;
background-color: blue;
display: block;
line-height: 3em;
padding: 2em 4em;
text-decoration: none;
font-weight: bold;
}
nav ul li .logo {
background-color: white;
border-radius: 50%;
margin-left: 10px;
}
nav a:hover {
color: red;
background-color: white;
transition: all 0.3s ease 0s;
}
I'm really sorry to ask such basic questions, but this has been troubling me for a few weeks. I very very much appreciate your help!! My webpage can be found here: https://komesannonihongotabi.neocities.org/culture.html
This is being caused by the following CSS line:
nav ul li a {
padding: 2em 4em;
}
Your a tags have a padding on left and right of 4em. Since em is an absolute unit and not a relative one, it will stay the same on any screen size. That is, unless you change the font-size to be smaller but I don't think that would be a good solution here. Just try adding a relative unit like %, or use a breakpoint as follows:
nav ul li a {
padding: 2em 1em;
}
#media screen and (min-width: 900px) {
nav ul li a {
padding: 2em 4em;
}
}
Another, and maybe better solution to your problem would be to use a sidenav with hamburger icon. This may however be a bit complex for beginners.
What if you remove this line white-space: nowrap; from nav {}
Before
nav {
background-color: blue;
border-bottom: solid #09316b;
white-space: nowrap;
}
After:
nav {
background-color: blue;
border-bottom: solid #09316b; }
It starts to resize now. If you want more info about white space -> Click here -<

HTML overlapping text on a nav bar

im trying to get text to overlap the nav bar, when I say overlap I mean expand outside the nav bar white keeping the nav where it is. here is my code I tried using different padding techniques but none have worked. any help would be appreciated :
div {
padding-top:80px;
}
ul {
list-style-type: none;
padding: 0;
overflow: hidden;
margin:15px;
background-color: black;
line-height: 5px;
}
li {
float:left;
font-family: Courier New;
}
li a {
display: block;
color: white;
padding:20px;
text-decoration: none;
text-align: center;
text-decoration: none;
padding: 2px 25px;
}
li a:hover {
/*background-color: ;*/
color: white;
}
li a.active {
background-color: #4CAF50;
}
<div>
<ul>
<li>Million-Air</li>
<li>Men</li>
<li>Wemon</li>
<li style="float:right"><a class="active" href="account.html">Account</a></li>
</ul>
</div>
So I figured it out, it was due to the fact I had float left on the li element and it was pushing all the blank space to the left. So I had to use 'clear: both' for it to work
Try removing your overflow: hidden property. The text will extend outside the box if you do.
If I am misunderstanding your intent, try linking to what you're trying to do if you have examples online.

How do I get my menu links into my <nav> block?

I'm really sorry if I didn't phrase my question correctly, I'm really new at all of this.
I want to put my menu items (I made an unordered list) within my nav block, but they are showing underneath it instead. It overlaps with my body content (not pictured), which is really problematic. Could someone help me?
The pink box is my nav block. I want to put my menu buttons inside it.
I know that the pink block is in fact the nav block?
HTML:
<header>
<h1>Header</h1><h2> | chapter</h2>
</header>
<nav>
<ul id="menu">
<li>alpha</li>
<li>beta</li>
<li>gamma</li>
<li>delta</li>
</ul>
</nav>
CSS:
header{
background-color: green;
border: 1px solid purple;
width: 100%;
height: auto;
}
nav{
background-color: pink;
border: 1px solid grey;
width: 100%;
height: auto;
}
h1, h2{
display: inline;
}
/*Set li as buttons*/
#menu li{
float: left;
list-style-type: none;
width: 5em;
margin-left: -2.5em; /*Removes default indentation of lists*/
margin-right: 5em;
display: block;
}
/*display anchor tags as buttons*/
#menu a{
display: block;
background-color: white;
border: 3px solid blue;
text-align: center;
text-decoration: none;
color: black;
}
/*display setting on button hover*/
#menu a:hover{
color: white;
background-color: black;
}
Thank you!
There are many errors in your CSS:
list-style-type: none; goes on the list, not on its items. It's what disables default list-behavior and makes the list stand on one line.
float: left; will make the elements float, but will also make the parent shrink as if it didn't have any content, which is why the elements sit below the nav block.
display: block; on items makes them stand on their own line. If you want multiple elements to stand side-by-side yet still have margins and paddings like blocks, you need to use inline-block instead. This is much easier to maintain than floating elements.
The margins on the list items are also way too big, I got rid of those. Honestly though, I really don't get why people use lists anymore. You could very well just put the links in the nav directly and save a lot of code.
header {
background-color: green;
border: 1px solid purple;
width: 100%;
height: auto;
}
nav {
background-color: pink;
border: 1px solid grey;
width: 100%;
height: auto;
}
h1,
h2 {
display: inline;
}
/*Set li as buttons*/
#menu {
list-style-type: none;
}
#menu li {
width: 5em;
margin: 0;
display: inline-block;
}
/*display anchor tags as buttons*/
#menu a {
display: inline-block;
background-color: white;
border: 3px solid blue;
text-align: center;
text-decoration: none;
color: black;
}
/*display setting on button hover*/
#menu a:hover {
color: white;
background-color: black;
}
<header>
<h1>Header</h1>
<h2> | chapter</h2>
</header>
<nav>
<ul id="menu">
<li>alpha
</li>
<li>beta
</li>
<li>gamma
</li>
<li>delta
</li>
</ul>
</nav>
You need to clear the container of the floated elements, as they don't properly stretch the container.
Add the clearfix CSS to your sheets:
.clearfix:after {
visibility: hidden;
display: block;
font-size: 0;
content: " ";
clear: both;
height: 0;
}
.clearfix { display: inline-block; }
/* start commented backslash hack \*/
* html .clearfix { height: 1%; }
.clearfix { display: block; }
/* close commented backslash hack */
and then add the clearfix class to menu:
<ul id="menu" class="clearfix">
fiddle
Alternatively, pick one of the other clearfix solutions from here (where I got the solution above).
Get rid of the float left under menu li and replace it with
#menu li{
display:inline-block;
list-style-type: none;
width: 5em;
margin-left: -2.5em; /*Removes default indentation of lists*/
margin-right: 5em;
}
and if you want to move it over to the right a bit more
#menu li{
display:inline-block;
list-style-type: none;
width: 5em;
margin-right: 5em;
}

Menu Bar Not Stretching Full Length of Cell

I have a menu bar that isn't stretching the entire length of the div it is in. I have 6 main menu options and I want these to stretch across the entire div tag they are in and not just take up the exact spacing of the words. I've tried to set the div and the menu elements to 100% width, but nothing is working. My code is listed below.
CSS
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
}
li {
float: left;
}
a:link,a:visited {
display: block;
width: 100%;
font-weight: bold;
color: #FFFFFF;
background-color: #98bf21;
text-align: center;
padding: 4px;
text-decoration: none;
text-transform: uppercase;
}
a:hover,a:active {
background-color: #7A991A;
}
.testmenu {
width: 100%;
}
HTML
<div class="testmenu">
<ul>
<li>Home</li>
<li>News</li>
<li>Contact</li>
<li>About</li>
</ul>
</div>
Here's an edit I made from your code:
http://jsfiddle.net/fatgamer85/grLkE/
I've added few classes to the list and list items.
I've made the list width 100% to fill the area (I'm assuming menu bar?), then removed the float from list items, made them inline, and added padding to the items
.menu-item{
display: inline-block;
padding: 2%;
}
and this is how it looks:
http://jsfiddle.net/fatgamer85/grLkE/embedded/result/
Hope this helps.
Change
li{float:left;}
to
li{display:inline-block;width:25%;margin:0;padding:0;}
Fiddle here.
you can give the UL a width of 100%, and then the li each a width of 25%
the css that you have compiles to give the testmenu a 100% width, and then YOU have to explicitly tell the UL and LI how much width they should have.
ul {
list-style-type: none;
overflow: hidden;
display:table-row;
}
li {
display:table-cell;
border:1px solid black;
}
a:link,a:visited {
display: block;
width: 100%;
font-weight: bold;
color: #FFFFFF;
background-color: #98bf21;
text-align: center;
padding: 4px;
text-decoration: none;
text-transform: uppercase;
}
a:hover,a:active {
background-color: #7A991A;
}
.testmenu {
width: 100%;
display:table;
table-layout:auto;
}