Background image position on navigation item - html

Not too sure what I'm doing wrong here. I swear I've had this working in the past, but for some reason I cannot get the background image to show when you hover over the navigation item.
I've tried to add a width, but that doesn't seem to work. That only makes the hover all sorts of crazy.
Here's the current CSS:
.nav ul li {
display: inline-block;
margin-left: 15px;
}
.nav ul li a {
text-decoration: none;
color: #760706;
display: block;
position: relative;
}
.nav ul li a:hover {
background: url(imgs/ornament.png) -10px 2px no-repeat;
}
But this is the current result when you hover:
If I increase the width of the anchor element and change it to inline-block it only increases the width of the navigation:
CSS:
.nav {
float: right;
font: 1.2rem "rosewoodfill", Georgia, "Times New Roman", Times, serif;
margin-top: 35px;
}
.nav ul li {
display: inline-block;
margin-left: 15px;
}
.nav ul li a {
text-decoration: none;
color: #760706;
display: inline-block;
position: relative;
width: 150px;
}
.nav ul li a:hover {
background: url(imgs/ornament.png) -10px 2px no-repeat;
}
Result:
Not too sure if I am missing something or what.
I created a pen to help illustrate what I'm trying to do:
http://codepen.io/ultraloveninja/pen/GfAuI

I think the problem lies with the position of the background.
.nav ul li a:hover {
background: url(imgs/ornament.png) -10px 2px no-repeat;
}
The -10px means the background position is -10 pixels on the x axis and is positioned 2 pixels down on the y axis. So if your image is 10px wide you won't be seeing it appear as it is being pulled to the left by 10px. If you set the code to:
.nav ul li a:hover {
background: url(imgs/ornament.png) 0 2px no-repeat;
}
The image will appear to the very left of the anchor tag on hover, and will be placed 2px down. You can then add some padding to the left hand side of the anchor so that the text doesn't cover the image (assuming you would want this).
.nav ul li a {
text-decoration: none;
color: #760706;
display: inline-block;
padding-left: 15px;
position: relative;
width: 135px;
}
.nav ul li a:hover {
background: url(imgs/ornament.png) 0 2px no-repeat;
}

Because you are using an inline-block element you can't have the background image extend beyond the content. However, if you use the pseudo-selector :before you can make it work like this:
.nav ul li a:before {
content: "."; /*You have to have some sort of content for this to work.*/
display: inline-block;
width: 15px;
color: #fff /*This should match your background color so that you can't see it*/
}
.nav ul li a:hover:before {
background:url(img/url) 0 2px no-repeat;
}
That will provide the space for the image to show up. Then when you hover over the item it will show up.
You have to have content in the before for it to work. A period is small. If you then set the color to the same as the background color that it is on then you won't be able to see it.

Related

PSD to HTML navigation bar slicing concern

In my PSD file I have a nav bar with some light effect in the background. I'm a bit concerned whether should I slice the whole nav bar and then add text or just slice it into smaller pieces? Which would be the best practice?
I'm new to this subject so any ideas would be so much appreciated.
You for sure want to cut the nav bar images into pieces. Add them to the background of each of the nav bar links with css. It will save you a big headache later.
Here is a good example of a jsfiddle
Css
#nav {
display: block;
height: 40px;
line-height: 35px;
list-style: none outside none;
margin: 20px auto 0 auto;
position: relative;
max-width: 750px;
min-width: 750px;
width: 78.2%;
}
#nav li {
display: inline;
text-align: center;
float:left;
}
#nav li a{
color: #fff;
display: block;
float: left;
font-family: HelveticaNeue-light,Helvetica,sans-serif;
font-size: 1em;
height: 100%;
padding: 0px 23px 0;
text-align: center;
text-decoration: none;
line-height: 40px;
background:#4D4D4D;
position: relative;
}
#nav li a span {
position: absolute;
left: 0;
top: 40px;
display: none
}
#nav li a:hover, #nav .active a {
background: url("path/to/image.png") no-repeat bottom center #a4c723;
color: #4d4d4d;
}
#nav li a:hover span, #nav .active a span {
display: block;
}
if you mean gradients, you can use gradient generator http://www.colorzilla.com/gradient-editor/
As for me, using as few images as possible is best practice. Draw your navbar with css, good luck

How to get an a-tag to fullsize

it would be great, when I can click the <li> container and in it is the <a> ..
So my container is width: 100px and height: 100px but my tag only width: 20px and height: 15px.
So I can only click on the text in the tag.
Can I change that?
<li>
SEM
</li>
CSS:
.menu ul li {
background-image: url("../images/menu_background.png");
background-position: 1px center;
background-repeat: repeat-x;
border-color: #000000;
border-right: 1px solid #000000;
float: left;
font-family: 'Open Sans',sans-serif;
font-size: 16px;
height: 38px;
list-style-type: none;
padding: 0 34px;
vertical-align: middle;
}
.menu ul li a {
color: #FFFFFF;
display: block;
line-height: 35px;
text-decoration: none;
}
that doesn't work..
Set a block context property on it.
li a {
display:block;
}
You should apply display: block to your 'a' tag
An example : http://jsfiddle.net/e49Tg/
You can try li > a { display: block; } in your stylesheet. Make sure you prefix the li with the appropriate ul class or selector, just so you keep your change isolated to that occurrence.
I can't clearly get what your problem is, if you want an <a> tag to be sized, the solution is as proposed: the "display" property should be either "block" or "inline-block".
In case you want it to have the same size as the parent it should also have the size directives, like:
li a { display: block; width:100%; height: 100% }
and if you want it to be so when clicked, a pure css solution would be
li a:active {
display: block; width:100%; height: 100%;
}
If this isn't answer to your question, explain more.

HTML drop down element width not aligning to siblings

Quite difficult to explain, so I created a codepen for you to have a look at. When you hover over a navigation item and the dropdown menu appears. You'll see that some of the items don't line up correctly. I.e. the width of some items are bigger than their sibling items. I just want them all to stretch to the width of the widest list item. (confused yet?) Just have a look at the code pen..
You need to make changes to the below CSS to get them aligned.
For Instance.
#main-navigation ul li ul li {
background: none repeat scroll 0 0 #DE5D48;
width: 140px;
text-align:left;
}
WORKING DEMO
Hope this helps.
You should use display : inline-block instead of float: left in /* list item */ css and should add display : table-row; into /* sub list item */ .
#dark-blue: #31394C;
#light-gray: #E6E6E6;
#gray: #B1B2B5;
#dark-gray: #6D6D6D;
#dark-red: #d7351b;
#red: #ee2a2e;
#light-red: #f75d1f;
#dusty-red: #DE6450;
#light-dusty-red: #fdc29e;
#dark-dusty-red: #b50b03;
#sub-nav-red: #DE5D48;
#white: #ffffff;
#import url(http://fonts.googleapis.com/css?family=Rosario);
body {
background: #dark-blue;
}
#main-navigation {
/* initial list */
ul {
list-style: none;
/* list item */
li {
display: inline-block;
position: relative;
background: #dark-red;
min-width: 105px;
height: 21px;
text-align: center;
margin-right: 10px;
/* link */
a {
color: #white;
display: block;
line-height: 21px;
text-decoration: none;
font-family: 'Rosario', sans-serif;
font-size: 12px;
text-transform: uppercase;
}
a:hover {
color: #white;
background: #dark-red;
}
/* sub list */
ul {
position: absolute;
top: 100%;
left: -40px;
display: none;
border-top: 10px solid rgba(255, 255, 255, 0.0);
z-index: 99;
/* sub list item */
li {
background: #sub-nav-red;
display : table-row;
/* sub list link */
a {
padding: 0 5px 0 5px;
white-space: nowrap;
}
}
}
ul:hover {
display: block;
}
}
li:hover > ul {
display: block;
}
}
}
I suggest moving the background, size, and positioning from the li to the ul, that way the whole list will expand if one item is too big.
Try to give more min-width
#main-navigation ul li {
float: left;
position: relative;
background: #D7351B;
min-width: 140px;
in css, li element has: min-width: 105px;
change it to 155px and it will solve it.
hope that helps.
Its quite simple,
You can't use: top: 100%
You'll be wanting to update this to be top: 0 this will align it to the top of its parent.
You may need top: 11px in your exact case.

Can't Center My Navigation

I have a navigation bar that has to be 900 pixels wide but the links inside don't necassarily always span the entire width of the bar so I'd like to center the links. The issue is no matter what I try, the links won't center. Here's my CSS:
.center {
text-align: center;
}
nav {
width: 900px;
text-align: center;
font-family: Arial, "Helvetica Neue", Helvetica, sans-serif;
font-weight: bold;
text-transform: uppercase;
font-size: 11px;
letter-spacing: 1px;
}
nav ul ul {
display: none;
}
nav ul li:hover > ul {
display: block;
}
nav ul {
margin: 0 auto 20px auto;
padding: 0 20px;
border-radius: 10px;
list-style: none;
position: relative;
background: #C0C0C0;
}
nav ul:after {
content:" \2022 ";
clear: both;
display: block;
}
nav ul:last-child:after {
content:"";
}
nav ul li {
display: inline-block;
}
nav ul li a {
text-align: center;
display: block;
padding: 20px 15px;
text-decoration: none;
}
nav ul ul {
border-radius: 0px;
padding: 0;
position: absolute;
top: 100%;
z-index: 100;
}
nav ul ul li {
float: none;
position: relative;
}
nav ul ul li a {
padding: 15px 40px;
border-top: 1px solid #FFFFFF;
border-bottom: 1px solid #FFFFFF;
}
nav ul ul ul {
position: absolute;
left: 100%;
top:0;
}
I tried adding auto margins to the nav id, and the nav ul id to no avail, then I tried wrapping the list in a div with a class that aligns the content in the center, but that didn't work either. Then I tried text-align to no avail. I don't know what to do so any help would be greatly appreciated!
Here's a JDFiddle I've been working with: http://jsfiddle.net/VCKMU/2/
UPDATE: After taking #Adrifts advice and changing the list items from float:left to inline-block, they now align in the center but now all the child items are inline blocks instead of vertical lists. Any ideas?
Updated JSFiddle: http://jsfiddle.net/VCKMU/6/
Instead of floating the list-items, just change their display value to inline-block; as you have specified text-align: center; on their containing block.
http://jsfiddle.net/VCKMU/6/
The only problem is that this causes the child items to turn into inline lists when they're supposed to be vertical.
You just need to modify the selector to only target the list-items are the children of the first <ul>:
.center > ul > li {
display: inline-block;
}
http://jsfiddle.net/VCKMU/8/
I think it should work if you give nav { width: auto; margin: auto; }
I usually fool around in the Chrome debugger for issues like this.

removing first child in web menu

I'm trying to make a menu for a web page and I'm inserting images as menu separators.
maybe this is a little stupid question but I'm trying to remove the first image of my menu
this is my code:
.menu ul li {
background: url(separator.png) no-repeat left;
display: inline;
float: left;
height: 50px;
line-height: 50px;
margin: 0;
width: 155px;
and tried this to remove the first separator:
.menu a.first {
background-image: none;
}
I tried to do what that's in this pages:
http://jsfiddle.net/Jaybles/uJdhH/1/
http://www.e-blueprint.co.uk/2011/how-to-use-an-image-as-a-menu-separator/
but it don't work
Try this:
.menu li:first-child{
background-image: none;
}
because your background is on the li tag and your .first class is on an anchor tag you are removing what ever background the anchor holds not the list item.
change to :
.menu li.first {
background-image: none;
}
You are specifying a background property above for a list item and then removing the background image on the a.first class. You should update .menu class to:
.menu li.first { background:none; }
Well, you had background to < li > ... then removed background from < a > .. 'Won't work'
.menu ul li {
background: url(separator.png) no-repeat left;
display: inline;
float: left;
height: 50px;
line-height: 50px;
margin: 0;
width: 155px;
and tried this to remove the first separator:
.menu ul li:first-child {
background-image: none;
}
jsfiddle : http://jsfiddle.net/sd6Vu/