stretching link throughout entire <li> - html

the text within the li is a link, where as the entire li item including the text should work as a link. How can I get it so that the entire li item operates as link as opposed to just the text within the li?
CSS:
#navbar {
width: 900px;
height: 50px;
background-image: url(http://iforce.co.nz/i/chiislxt.mp4.png);
}
#navbar ul {
list-style-type:none;
margin:0px;
padding:0px;
text-align: center;
}
#navbar ul li {
height: 50px;
width: auto;
float: left;
padding-left: 20px;
padding-right: 20px;
line-height: 50px;
border-right: 1px solid black;
}
#navbar ul li a {
display: block;
height: 100%;
}
#navbar a:link {
font-weight:bold;
color: black;
font-family: Franklin Gorthic Heavy, Helvetica, sans-serif;
text-decoration: none;
}
#navbar a:hover {
background-color: #003300;
color: gold;
}
#navbar a:visited {
color: black;
}
a:active {
background-color: #003300;
color: gold;
}
HTML:
<div id="navbar">
<ul>
<li>HOME</li>
<li>ABOUT</li>
<li>NEWS</li>
<li>READING</li>
<li>STORE</li>
<li>CONTACT</li>
</ul>
</div>
Here's a screenshot for reference: (home has my cursor over)
Cheers

here's a quick example for you:
http://jsfiddle.net/DFS5P/

You then should move some of the li css to the a css - like so
#navbar ul li {
height: 50px;
width: auto;
float: left;
}
#navbar ul li a {
display: block;
height: 100%;
padding-left: 20px;
padding-right: 20px;
line-height: 50px;
border-right: 1px solid black;
}

Give your padding & height to anchor instead of LI. Write like this:
#navbar ul li {
float: left;
border-right: 1px solid black;
}
#navbar ul li a {
display: block;
padding-left: 20px;
padding-right: 20px;
line-height: 50px;
height: 50px;
}
Check this http://jsfiddle.net/DFS5P/1/

Try adding
width:100%;
to:
#navbar ul li a {
display: block;
height: 100%;
}

If you remove the padding from the list items and give them a fixed width, it works as you need.
#navbar ul li {
height: 50px;
width: 100px;
float: left;
line-height: 50px;
border-right: 1px solid black;
}
jsFiddle example

It is definately not the best way, but sometimes I just move the entire 'li' inside the 'a' tag. This way everything witin the 'li' goes to the link in when you click on it.
Works fine by me, but as I said it is not the best way.

Related

Horizontal Navigation Bar: How to center text and set colours

I am trying to create a horizontal navigation bar with the following properties:
The anchor text is centered horizontally and vertically in the list
items.
The entire list item is a clickable link.
On hover, the background colour of the list item and the colour of the anchor text are swapped.
Here is a fiddle that manages to accomplish only #2. None of the solutions that I have found for #1 seem to work. #3 escapes me.
Advice is appreciated.
Here is the working code:
#navbar ul {
list-style-type: none;
display: table;
}
#navbar li {
display: table-cell;
float: left;
width: 200px;
height: 50px;
text-align: center;
background-color: #f0e68c;
}
#navbar li:hover {
background-color: black;
}
#navbar li a {
display: block;
text-decoration: none;
color: black;
}
<div id="navbar">
<ul>
<li>Intro</li>
<li>Bio</li>
<li>Issues</li>
</ul>
</div>
You can use this css. Check this link https://jsfiddle.net/oynd1sq6/
#navbar li:hover a{
color:#ffffff;
}
You can use below CSS to get all the 3 things you wanted.
#navbar ul
{
list-style-type: none;
display: table;
}
#navbar li{
float: left;
text-align: center;
}
#navbar li a
{
display: table-cell;
width: 200px;
height: 50px;
text-decoration: none;
color: black;
vertical-align: middle;
background-color: #f0e68c;
}
#navbar li a:hover{
color: #f0e68c;
background-color: black;
}
I have used vertical-align: middle to align text it vertically and gave your li styling to a tag to achieve this.
Fiddle here: http://jsfiddle.net/MasoomS/6mn0fun4/1/
Just two modification you need is.
To accomplish #1:
Make line height of anchor same as line height of li tag. i.e. 50px
To Accomplish #3
Add style rule for anchor.
#navbar li:hover a{
color:#f0e68c;
}
Here is fiddle
#navbar ul {
list-style-type: none;
display: table;
}
#navbar li {
float: left;
width: 200px;
height: 50px;
text-align: center;
background-color: #f0e68c;
;
}
#navbar li:hover {
background-color: black;
}
#navbar li a {
display: inline-block;
text-decoration: none;
color: black;
line-height: 50px;
/*Make the line height same as height of li tag*/
}
/*Add style for anchor*/
#navbar li:hover a {
color: #f0e68c;
}
<body>
<div id="statement">
<div id="navbar">
<ul>
<li>Intro
</li>
<li>Bio
</li>
<li>Issues
</li>
</ul>
</div>
</div>
</body>
</html>
Below Code is for html
<ul class="nav">
<li>Home</li>
<li>About</li>
<li>Work</li>
<li>Clients</li>
<li>Contact</li>
</ul>
Below code is for css
.nav{
border:1px solid #ccc;
border-width:1px 0;
list-style:none;
margin:0;
padding:0;
text-align:center;
}
.nav li{
display:inline;
}
.nav a{
display:inline-block;
padding:10px;
}
Try this i hope it will work for you
Try this ... just changed ur css
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
}
li {
float: left;
}
a:link, a:visited {
display: block;
width: 120px;
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;
}
https://fiddle.jshell.net/ofp184pn/1/
Only updated your code...
For color hover effect add:
#navbar li a:hover{
color: #f0e68c;
background-color: black;
}
And for horizontal align you should add "line-height" and "vertical-align" properties to your "#navbar li" selector:
#navbar li
{
display: table-cell;
float: left;
width: 200px;
height: 50px;
text-align: center;
background-color: #f0e68c;
line-height: 50px;
vertical-align: middle;
}
JsFiddle
Your all 3 problems are solved.
Check it out
#navbar ul{
list-style-type: none;
float:left;
}
#navbar li{ width: 100px;
height: 50px;
text-align: center;
background-color: #f0e68c;
float:left;
}
#navbar li:hover{
background-color: black;
}
#navbar li a{
display: inline-block;
text-decoration: none;
height:100%;
color: black;
}
#navbar li a:before{
height:100%;
vertical-align:middle;
content:'';
display:inline-block;
}

How to remove an empty line between two navbars

I have 2 nav bars which I want to be on top of each other without spaces, but for some reason there is an empty line as if I had used .
HTML part
<!DOCTYPE html>Logo!
<BR>
<ul class="menu">
<li>Home
</li>
<li>placeholder
</li>
<li>placeholder
</li>
<li>placeholder
</li>
<li>placeholder
</li>
<li>placeholder
</li>
</ul>
<ul class="submenu">
<li>subheader1
</li>
<li>subheader2
</li>
<li>subheader3
</li>
</ul>
CSS part:
.submenu {
text-align: left;
background-color: #C2F0C2;
}
.menu {
background-color: #98bf21;
}
body {
width: 900px;
margin: 2em auto;
}
.menu ul {
margin:0;
padding:0;
overflow: hidden;
text-align: center;
font-size:0;
}
.menu li {
display: inline;
list-style: none;
}
.menu a:link, a:visited {
display: inline-block;
margin-right: -4px;
width: 135px;
color: #FFFFFF;
font-size: small;
font-family: Verdana, sans-serif;
text-align: center;
padding: 4px;
text-decoration: none;
background-color: #98bf21;
}
.menu a:hover, a:active {
background-color: #7A991A;
}
.submenu ul {
margin:0;
padding:0;
overflow: hidden;
text-align: left;
font-size:0;
}
.submenu li {
display: inline;
list-style: none;
}
.submenu a:link, a:visited {
display: inline-block;
margin-right: -4px;
width: 135px;
color: #000000;
font-size: small;
font-family: Verdana, sans-serif;
text-align: center;
padding: 4px;
text-decoration: none;
background-color: #C2F0C2;
}
.submenu a:hover, a:active {
background-color: #7A991A;
}
Fiddle
I'm not sure which part I need to change so I included all of it.
How can I remove the empty line?
E: I failed with the fiddle and forgot to press update, should have the right one now.
Apply padding:0 and margin:0 for your menu and submenu classes.
.menu, .submenu{margin:0; padding:0;}
DEMO
You just have to add this in your CSS :
.menu {margin-bottom:0px;}
.submenu {margin-top:0px;}
Have a good day.
Note: I see on firefox all together, so it give me 2 pixels on the two square join. For that i used this technique
You can use pseudo :last-child
CSS
div {
display: inline-block;
padding: 15px;
border-left: 2px solid red;
border-top: 2px solid red;
border-bottom: 2px solid red;
margin: 0;
white-space:0;
}
div:last-child {
border-right: 2px solid red;
}
DEMO HERE

Trying to position the text in my nav

So I'm simply trying to get the text in each line item to be position at the bottom of the square rather than stuck at the top. Here is a screenshot of what my nav looks like http://imgur.com/QLPBYQK
And here is my code:
<div id="nav">
<ul>
<li>Home</li>
<li>Supplies</li>
<li>FAQ</li>
</ul>
</div>
#nav {
height: 50px;
width: 950px;
margin: auto;
background-color: #FF5252;
font-family: sans-serif, Georgia;
border: 5px solid white;
}
#nav ul {
margin: 0px;
padding: 0px;
}
#nav ul li {
float: left;
width: 145px;
}
#nav ul li a {
font-size: 20px;
text-align: center;
color: white;
background-color: #FF5252;
display: block;
text-decoration: none;
height: 50px;
}
JSFiddle.
Add this attribute:
line-height:80px;
To the Class:
#nav ul li a {
font-size: 20px;
text-align: center;
color: white;
background-color: #FF5252;
display: block;
text-decoration: none;
height: 50px;
line-height:80px;
}
Add list-style: none to #nav ul li and line-height: 50px(should be the same as height of the a element, if you want to center the text vertically) to #nav ul li a.
Demo
#nav ul li {
float: left;
width: 145px;
list-style: none;
}
#nav ul li a {
font-size: 20px;
text-align: center;
color: white;
background-color: #FF5252;
display: block;
text-decoration: none;
height: 50px;
line-height: 50px;
}
Don't use height; instead use appropriate padding-top.
#nav ul li a {
font-size: 20px;
text-align: center;
color: white;
background-color: #FF5252;
display: block;
text-decoration: none;
padding-top: 28px;
}
Use padding-top or
line-height in nav ul li a

how to position two elements inside an anchor tag

I'm trying to building an horizontal navigation bar in which every li tag has one element: the a tag; in this element the are other two elements one below the other, the first on top and the second on bottom. These two elements are a p tag and a div tag.
This is my attempt:
Here is the code:
<div class="nav">
<ul id="menu-navigation-bar" class="menu">
<li class="menu-item-72"><p>About</p><div></div></li>
<li class="menu-item-71"><p>Services</p><div></div></li>
<li class="menu-item-70"><p>Work</p><div></div></li>
<li class="menu-item-69"><p>Contact</p><div></div></li>
</ul>
</div>
The CSS:
/*navigation*/
.nav {
width: 100%;
height: 200px;
margin: 0 auto;
position: absolute;
}
.nav .menu {
height: 200px;
list-style-type: none;
text-align: center;
}
.nav .menu li {
letter-spacing: 0.1em;
display: inline;
padding-left: 110px;
padding-right: 110px;
position: relative;
}
.nav .menu li.current_page_item a {
text-decoration: underline;
}
.nav .menu li a:hover {
color: #929292;
}
.nav .menu li a p {
font-size: 1em;
position: absolute;
}
.nav .menu li a div {
position: absolute;
width: 150px;
height: 100px;
background-color: red;
display: inline;
}
What did I do wrong?
Thanks!
You are absolutely positioning the child elements and expecting them to behave like they are relatively positioned, you're also making your list items display inline, not inline-block which is creating unanticipated behaviour.
Demo Fiddle
Change your CSS to (see below for alternative):
/*navigation*/
.nav {
width: 100%;
height: 200px;
margin: 0 auto;
position: absolute;
}
.nav .menu {
height: 200px;
list-style-type: none;
text-align: center;
}
.nav .menu li {
letter-spacing: 0.1em;
display: inline-block;
padding-left: 110px;
padding-right: 110px;
position: relative;
}
.nav .menu li.current_page_item a {
text-decoration: underline;
}
.nav .menu li a:hover {
color: #929292;
}
.nav .menu li a p {
font-size: 1em;
}
.nav .menu li a div {
width: 150px;
height: 100px;
background-color: red;
display:
}
That said, the padding on your items is spacing them pretty distantly, you may want to remove it:
Demo Fiddle

Sub menu won't display

I have a hover drop down menu on my website. It displays ok until you hover over. The submenu appears but before you can click on a link, the sub menu vanishes.
Here is the HTML -
<div id="top-nav"><div id="nav">
<nav>
<ul id="menu" style="list-style-type: none;">
<li id="sub">Artists
<ul>
<li>Banks</li>
<li>Lil Silva</li>
<li>Frances and the Lights</li>
<li>Jim-E Stack</li>
</ul>
</li>
<li>Night</li>
<li>Info</li>
</ul>
</nav>
</div>
</div>
And here is the CSS -
#nav {
text-align:center;
list-style: none;
}
ul#menu {
background-color: #FFFFFF;
list-style: none outside none;
margin: 0 auto;
padding-bottom: 0px;
padding-top: 0px;
text-align: center;
width: 300px;
}
ul#menu:after {
content: "";
background-color: #FFFFFF;
height: 10px;
width: 100%;
display: block;
position: absolute;
left: 0;
margin-top: 20px;
}
ul#menu li {
float: left;
}
ul#menu li a {
color: #666666;
font-size: 12px;
margin: 0;
padding: 0px 35px;
text-decoration: none;
}
ul#menu li a:hover {
background-color: #ccc;
}
a.selected-page, ul#menu a.selected-page:hover {
background-color: #FFFFFF;
}
li#sub ul {
display: none;
position: absolute;
background-color: #FFFFFF;
z-index: 22222;
margin-top: 4px;
overflow: hidden;
}
li#sub ul li {
display: block;
float: none;
border-top-style: none;
border-width: 2px;
border-color: #FFFFFF;
text-align: left;
padding-top: 5px;
padding-bottom: 5px;
}
ul#menu li#sub:hover ul {
display: block;
}
ul#menu li#sub ul li:hover {
background-color: #FFFFFF;
}
What am I doing wrong? Any help is greatly appreciated!
the problem is the following line of your CSS code:
li#sub ul {
...
margin-top: 4px;
...
}
Just remove this margin-top and your drop down menu will work properly.
In your example there is a 4px margin space between the "Artists"-link and the drop down menu below. And if your cursors leaves the link and enters this "margin area", the browser interprets it as un-hovering the link - and hides the drop down.
Like dalucks said, remove the top margin from your CSS:
li#sub ul {
margin-top:0;
}
Also, reduce the left margin/paddings from the submenu UL and/or LI children. With the original large values (35px) the menu could be hovered over a lot of invisible space.
ul#menu li ul li a {
padding-left:5px;
}
Fiddle: http://jsfiddle.net/LXwTr/
Add Padding: 0 to this
li#sub ul {
padding: 0;
}
http://jsfiddle.net/tdQmE/