How to use two floats in navigation - html

I've been trying to get a dropdownbar in my navigation which displays the elements vertically. The dropdownbar is already here and works fine but I'm having some trouble getting it to display vertically. Thanks in advance for taking a look, sorry if it's a rookie mistake!
I've already tried using floats, vertical-align, content-align.
.nav {
border-color: lightgray;
background: transparent;
padding: 0;
position: relative;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: auto;
background: transparent;
display: inline-block;
}
li {
float: left;
}
li a {
display: block;
padding: 16px 54px;
margin-right: 30px;
text-align: center;
text-decoration: none;
color: dimgray;
/* border-right: 1px solid dimgray; */
/* border-bottom: 1px solid dimgray; */
}
li:last-child {
border-right: none;
}
li a:hover {
background-color: dimgray;
color: white;
}
.active {
background-color: #696969;
color: #ffffff;
pointer-events: none;
}
ul ul {
display: none;
}
ul li:hover>ul {
display: flex;
float: top;
vertical-align: top;
border-left: 1px solid dimgray;
border-bottom: 1px solid dimgray;
}
<nav id="navigatie" class="nav">
<h1 class="outlineNaam">navigatie</h1>
<ul>
<li><a class="active" href="index.html">Home</a>
<li>Producten</li>
<li>Personaliseren</li>
<li class="subnav"><a>Over ons</a>
<ul class="subnav">
<li>Blog</li>
<li>Contact</li>
<li>FAQ</li>
</ul>
</li>
</ul>
</nav>

U should use flex-direction: column:
ul li:hover > ul {
display: flex;
flex-direction: column;
/* float: top; */
vertical-align: top;
border-left: 1px solid dimgray;
border-bottom: 1px solid dimgray;
}
Also float property has no value "top":
see on mdn
and the float property and vertical-align is ignored in a flex container.
From the flexbox specification:
3. Flex Containers: the flex and inline-flex display values
A flex container establishes a new flex formatting context for its
contents. This is the same as establishing a block formatting context,
except that flex layout is used instead of block layout.
For example, floats do not intrude into the flex container, and the
flex container’s margins do not collapse with the margins of its
contents.
float and clear do not create floating or clearance of flex item, and
do not take it out-of-flow.

You'll simply need to apply flex-direction: column to the .subnav (you did select it using ul li:hover > ul) which will make the .subnav elements align vertically as columns.
Also, there is not value of top for the float rule and the vertical-align doesn't affect flex elements.
So, here's a working demo :
.nav {
border-color: lightgray;
background: transparent;
padding: 0;
position: relative;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: auto;
background: transparent;
display: inline-block;
}
li {
float: left;
}
li a {
display: block;
padding: 16px 54px;
margin-right: 30px;
text-align: center;
text-decoration: none;
color: dimgray;
/* border-right: 1px solid dimgray; */
/* border-bottom: 1px solid dimgray; */
}
li:last-child {
border-right: none;
}
li a:hover {
background-color: dimgray;
color: white;
}
.active {
background-color: #696969;
color: #ffffff;
pointer-events: none;
}
ul ul {
display: none;
}
ul li:hover>ul {
display: flex;
flex-direction: column;
border-left: 1px solid dimgray;
border-bottom: 1px solid dimgray;
}
<nav id="navigatie" class="nav">
<h1 class="outlineNaam">navigatie</h1>
<ul>
<li><a class="active" href="index.html">Home</a>
<li>Producten</li>
<li>Personaliseren</li>
<li class="subnav"><a>Over ons</a>
<ul class="subnav">
<li>Blog</li>
<li>Contact</li>
<li>FAQ</li>
</ul>
</li>
</ul>
</nav>

Related

how to make dropdown width (background) follow parent li without using media queries Css only

There is a question very similar to this one which already been solved, yet that solution is not helping me.
The issue I'm facing is the dropdown is not following its parent width ( position: absolute is in play here) and I think position: absolute is the only way to stop the whole nav bar to expand when the dropdown is accessed.
so is there any way I can make dropdown follow its parent width other than using media queries and defining a width for every device?
And forgive me if I'm not able to explain myself clearly. Beginner here.
Thanks
CSS
* {
box-sizing: border-box;
}
body {
margin: 0;
background: #333;
}
ul {
display: flex;
background: #9999ff;
list-style: none;
padding: 6px;
overflow: hidden;
justify-content: space-around;
line-height: 2;
}
li {
color: white;
text-decoration: none;
text-align: center !important;
flex-grow: 1;
}
.drop{
position: absolute;
visibility: hidden;
flex-direction: column;
color: white;
background-color: #9999ff;
line-height: 3;
/*width: 17em;*/
}
.drop li {
border: 1px solid transparent;
text-align: center;
}
li:hover {
background: black;
color: plum;
}
li:hover .drop {
visibility: visible;
}
.drop li:hover {
border: 1px solid rgba(255, 51, 153,1);
}
HTML
<ul>
<li>Home</li>
<li>JS
<ul class="drop">
<li>Node</li>
<li>React</li>
</ul>
</li>
<li>CSS</li>
<li>About</li>
</ul>
* {
box-sizing: border-box;
}
body {
margin: 0;
background: #333;
}
ul {
display: flex;
background: #9999ff;
list-style: none;
padding: 6px;
overflow: hidden;
justify-content: space-around;
line-height: 2;
}
li {
color: white;
text-decoration: none;
text-align: center !important;
flex-grow: 1;
}
.drop{
position: absolute;
visibility: hidden;
flex-direction: column;
color: white;
background-color: #9999ff;
line-height: 3;
/*width: 17em;*/
}
.drop li {
border: 1px solid transparent;
text-align: center;
}
li:hover {
background: black;
color: plum;
}
li:hover .drop {
visibility: visible;
}
.drop li:hover {
border: 1px solid rgba(255, 51, 153,1);
}
<ul>
<li>Home</li>
<li>JS
<ul class="drop">
<li>Node</li>
<li>React</li>
</ul>
</li>
<li>CSS</li>
<li>About</li>
</ul>
Solution for fixed number of items, all with the same width:
* {
box-sizing: border-box;
}
body {
margin: 0;
background: #333;
}
ul {
display: flex;
background: #9999ff;
list-style: none;
padding: 6px;
overflow: hidden;
justify-content: space-around;
line-height: 2;
}
li {
color: white;
text-decoration: none;
text-align: center !important;
flex-grow: 1;
width: calc(100vw / 4 - 15px);
}
.drop{
position: absolute;
visibility: hidden;
flex-direction: column;
color: white;
background-color: #9999ff;
line-height: 3;
/*width: 17em;*/
}
.drop li {
border: 1px solid transparent;
text-align: center;
}
li:hover {
background: black;
color: plum;
}
li:hover .drop {
visibility: visible;
}
.drop li:hover {
border: 1px solid rgba(255, 51, 153,1);
}
<ul>
<li>Home</li>
<li>JS
<ul class="drop">
<li>Node</li>
<li>React</li>
</ul>
</li>
<li>CSS</li>
<li>About</li>
</ul>
Relevant snippet:
li {
color: white;
text-decoration: none;
text-align: center !important;
flex-grow: 1;
width: calc(100vw / 4 - 15px); // full viewport width divided by number of elements, minus paddings
}
Explanation: Viewport width takes into account the entire viewport width. Divide this by the number of items in our header, and we get our item width.
Now this does not take into account paddings, margins and similar, so you have to supply that yourself - but calc() is pretty flexible for this, as long as you know about the actual value of these factors.
I came up with a workaround to fix your problem. It might not be the prettiest but it works!
* {
box-sizing: border-box;
}
body {
margin: 0;
background: #333;
}
ul {
display: flex;
background: linear-gradient(180deg, #9999ff 44px, transparent 0px);
list-style: none;
padding: 6px;
overflow: hidden;
justify-content: space-around;
line-height: 2;
}
li {
color: white;
text-decoration: none;
text-align: center !important;
flex-grow: 1;
}
.drop{
display: none;
flex-direction: column;
color: white;
background-color: #9999ff;
line-height: 3;
/*width: 17em;*/
}
.drop li {
border: 1px solid transparent;
text-align: center;
}
li:hover {
background: black;
color: plum;
}
li:hover .drop {
display: block;
background: #9999ff;
}
.drop li:hover {
border: 1px solid rgba(255, 51, 153,1);
}
<ul>
<li>Home</li>
<li>JS
<ul class="drop">
<li>Node</li>
<li>React</li>
</ul>
</li>
<li>CSS</li>
<li>About</li>
</ul>
Problem:
We need our .drop element to NOT have position:absolute (because we need to access its parent's width), and we also cannot specify a maximum height for the header itself, as the overflowing dropdown would be hidden from view.
Solution:
We set the background of the header to a linear-gradient which has a specified height of 44px for purple color, and 0px for transparent.
This means that even if the header changes height, the background gradient color will always have the same height.
As a result, the header itself WILL change size, but it is not visually detectable.
Is it a pretty solution? No.
Is it good practice? Probably not.
Does it work? Well, it does!
I suggest using javascript for similar problems in the future though :)

How can I make my vertical navigation bar horizontal

I recently made a vertical navigation bar and was wondering how I could make it horizontal and make it display in the same sized boxes equally spaced apart
I have tried to float text to the left but that doesn't work as well as followed W3 School tutorial on creating a horizontal navigation bar. W3 School isn't useful. I am a HTML and CSS newbie.
ul {
list-style-type: none;
margin: 0px;
padding: 0px;
overflow: hidden;
width: 100px;
border: 1px solid;
}
li a {
display: block;
color: #000000;
padding: 8px 16px;
text-decoration: none;
text-align: center;
border: 1px;
border-bottom: 1px solid;
}
li:last-child a {
border-bottom: none;
}
li a:hover {
background-color:red;
color:white;
}
<ul>
<li>Home</li>
<li>About</li>
<li>Services</li>
<li>Contact</li>
<li>LinkedIn</li>
</ul>
I expect a horizontal navigation bar that is centred and equally spaced apart in the same sized boxes that appear.
Start changing display property and add it to li element like the following snippet. You must play with the values and the properties to achieve what you need.
ul {
font-size: 0;
list-style-type: none;
margin: 0px;
padding: 0px;
overflow: hidden;
border: 1px solid;
}
ul li {
display: inline-block;
list-style-type: none;
width: 20%;
font-size: 1rem;
}
li a {
color: #000000;
padding: 8px 16px;
text-decoration: none;
text-align: center;
border: 1px;
border-bottom: 1px solid;
}
li:last-child a {
border-bottom: none;
}
li a:hover {
background-color:red;
color:white;
}
<ul>
<li>Home</li>
<li>About</li>
<li>Services</li>
<li>Contact</li>
<li>LinkedIn</li>
</ul>
Use flex. You can read more about it here.
ul {
list-style-type: none;
display: flex;
align-items: center;
justify-content: center;
margin: 0px;
padding: 0px;
width: 400px;
border: 1px solid;
}
li a {
display: block;
color: #000000;
padding: 8px 16px;
text-decoration: none;
text-align: center;
border: 1px;
border-bottom: 1px solid;
}
li:last-child a {
border-bottom: none;
}
li a:hover {
background-color: red;
color: white;
}
<ul>
<li>Home</li>
<li>About</li>
<li>Services</li>
<li>Contact</li>
<li>LinkedIn</li>
</ul>

Spacing between navigation links

My problem is that I want to have spacing between the navigation links. I have searched over the internet but all I get are reducing the space.
To be specific, I want to have spacing in between each navigation link that are bordered with a black border.
These are my codes for the navigation bar. I would really appreciate some help. thank you.
ul {
list-style-type: none;
position: fixed;
margin-top: 50px;
padding: 0;
width: 200px;
background-color: #fff;
border: 5px solid #000;
}
li a {
display: block;
color: #000;
padding: 8px 16px;
text-decoration: none;
}
li {
text-align: center;
border-bottom: 5px solid #000;
}
li:last-child {
border-bottom: none;
}
li a:hover {
background-color: #555;
color: white;
}
<ul>
<li>Home</li>
<li>News</li>
<li>Contact</li>
<li>About</li>
</ul>
Here it is:
<style>
ul {
list-style-type: none;
position: fixed;
margin-top: 50px;
padding: 0;
width: 200px;
}
li a {
display: block;
color: #000;
padding: 8px 16px;
text-decoration: none;
border: 5px solid #000;
background-color: #fff;
}
li {
text-align: center;
margin-bottom:10px;
}
li:last-child {
margin-bottom:0;
}
li a:hover {
background-color: #555;
color: white;
}
</style>
<ul>
<li>Home</li>
<li>News</li>
<li>Contact</li>
<li>About</li>
</ul>
Demo: https://jsfiddle.net/4fLbx4xa/
<ul>
<li>Home</li>
<li>News</li>
<li>Contact</li>
<li>About</li>
</ul>
<style>
ul {
list-style-type: none;
position: fixed;
margin-top: 50px;
padding: 0;
width: 200px;
background-color: #fff;
}
li{
width:100%;
display: block;
margin-top:10px; //this is the height you want
border: 5px solid #000;
color: #000;
background:#000;
padding:10px 0;
}
li a {
text-align: center;
padding: 8px 16px;
text-decoration: none;
color:#fff;
}
li:first-child {
margin-top:0;
}
li:hover {
background-color: #555;
color: white;
}
</style>
You need to use margin to add the white space, but the borders needed a little tweaking, see comments below
ul {
list-style-type: none;
position: fixed;
margin-top: 50px;
padding: 0;
width: 200px;
background-color: #fff;
/*border: 5px solid #000; moved to LI element*/
}
li a {
display: block;
color: #000;
padding: 8px 16px;
text-decoration: none;
margin-bottom:10px; /*add some margin to create the white space*/
border: 5px solid #000; /*add the border to each LI element rather than UL*/
}
li {
text-align: center;
/*border-bottom: 5px solid #000; remove this bottom border as handled in LI css*/
}
Not sure what you want to achieve but i understood a line between the links, this might help you also if you want it below the links..
ul {
list-style-type: none;
position: fixed;
margin-top: 50px;
padding: 0;
width: 400px;
background-color: #fff;
border: 5px solid #000;}
li a {
display: block;
color: #000;
padding: 8px 16px;
text-decoration: none;}
li {
text-align: center;
float: left;
border-right:solid 1px black;
}
li a:hover {
background-color: #555;
color: white;
}
<ul>
<li>Home</li>
<li>News</li>
<li>Contact</li>
<li>About</li>
</ul>

html/css submenus require variable width

I'm am working on menus for a webpage. As it is now, my submenus are restricted to an 80px width which is defined in the css file under .dropdown > li {}. If I have a lengthy submenu li element like:
<li>Most Popular Artists</li> the submenu gets truncated to just "Most".
I need some guidance on how to allow the submenus to display everything they contain. Please advise.
HTML:
<nav id="top_menu">
<img src="media/images/logo_large.jpg">
<ul class="dropdown">
<li class="dropdown_trigger">
NEWS
<ul>
<li>Most Popular Artists</li>
<li>Subitem2</li>
<li>Subitem3</li>
<li>Subitem4</li>
</ul>
<li>
<li class="dropdown_trigger">
SOCIAL
<ul>
<li>Subitem1</li>
<li>Subitem2</li>
<li>Subitem3</li>
<li>Subitem4</li>
</ul>
</li>
</ul>
</nav>
CSS:
#top_menu{
position: relative;
top:35px;
left: 90px;
width:660px;
height:55px;
background-color: black;
border:1px solid black;
opacity:0.6;
filter:alpha(opacity=60); /* For IE8 and earlier */
}
.dropdown {
background: black;
border: 1px solid black;
float: right;
padding: 1px 0 0 1px;
margin: 0 0 20px;
line-height: 55px;
list-style: none;
}
.dropdown a {
background: black repeat-x;
border: 1px solid black;
color: white;
display: block;
line-height: 25px;
overflow: hidden;
height: 25px;
text-decoration: none;
}
.dropdown a:hover {
color: #30B3FF;
background: #666;
}
.dropdown ul a {
background: black;
}
.dropdown > li {
list-style: none;
position: relative;
text-align: left;
font: bold 12px Tahoma;
*display: inline-block;
width: 80px;
/* IE7 hack to make inline-block work right */
*zoom: 1;
display: inline;
}
.dropdown li.dropdown_trigger {
display: inline;
float: left;
margin: 0 0 0 -1px;
}
.dropdown ul {
display: none;
list-style-type: none;
background: black;
border: 1px solid black;
position: absolute;
top: 26px;
left: -1px;
z-index: 9999;
}
.dropdown ul {
display: none;
}
.dropdown li.dropdown_trigger:hover ul {
display: block;
}
.dropdown a {
height: 25px;
overflow: hidden;
}
This is fixing the height of an item and cutting its contents. You can remove the height (the line-height will suffice), or change it to min-height to be sure. And remove the overflow.
.dropdown a {
min-height: 25px;
/* overflow: hidden; */
}
If you do that, then all the text will show, and the menu item will be multi-line.
You can then add
.dropdown a {
white-space: nowrap;
}
if you don't want the text to wrap. This will cause the menu to become wider.
I believe the truncation is caused by the overflow: hidden property your using on .dropdown a
.dropdown a {
background: black repeat-x;
border: 1px solid black;
color: white;
display: block;
line-height: 25px;
<!-- overflow: hidden; -->
height: 25px;
text-decoration: none;
}
Try removing that. Also, with that removed you may want to adjust the margins your using to pull the li tag further left. You can also use a negative margin if your li tag is already at zero.

HTML CSS UL menu styling

Small question on how to achieve some styling on a HTML / CSS UL menu.
I have a standard UL menu, but having some issues getting my head around how to achieve a certain look to the styling. The UL menu as it currently stands is shown here:
http://jsfiddle.net/WMQqt/
(HTML)
<ul id="nav">
<li>CONTACT US
</li>
<li>HOME
</li>
</ul>
(CSS)
#nav {
list-style: none;
margin-bottom: 10px;
*/ margin-top: -6px;
position: relative;
right: 286px;
z-index: 9;
height: 26px;
padding: 4px 4px 4px 4px;
font-weight: bold;
}
#nav li {
float: right;
margin-right: 10px;
}
#nav a {
display: block;
padding: 5px;
color: #444444;
background: #fff;
text-decoration: none;
border: 1px solid grey;
}
#nav a:hover {
color: #fff;
background: #04B431;
}
I'd like the menu buttons to have a small 1px border, but then some white space padding of around 3px before the background color starts.
Similar to how this looks:
http://jsfiddle.net/6PY7z/
Can this be done using the UL menu method?
Thanks for any advice, I'm no expert with HTML / CSS.
Add margin to a tag and move border to li
#nav li
{
float: right;
margin-right: 10px;
border: 1px solid grey;
}
#nav a
{
display: block;
padding: 5px;
color: #444444;
background: #ccc;
text-decoration: none;
margin:3px;
}
DEMO
you can use the following styles to achieve what you want:
#nav li
{
float: right;
margin-right: 10px;
border: 1px solid grey; /*put original border here*/
}
#nav a
{
display: block;
padding: 5px;
color: #444444;
background: #d8d8d8; /*new background-color*/
text-decoration: none;
border: 3px solid white; /*add white padding here*/
}
http://jsfiddle.net/WMQqt/4/
ok
in html go
<dl><div><dt>F</dt><dd>T</dd></div>
<div><dt>F</dt><dd>T</dd></div>
<div><dt>F</dt><dd>T</dd></div>
<div><dt>F</dt><dd>T</dd></div>
</dl>
in css
dl { display: flex;
flex-direction: column;}
some hints...
dt float left AND
dd float right