Sprites and Absolute Positioning issue: 2 work and 2 don't? - html

sorry I know sprites are covered quite a lot but I haven't been able to find an answer out there with my specific context.
I have 4 absolutely positioned buttons using the same .png file with 3 states (link,hover,active) for the 'home' button the hover works but the hover area is not the whole button, for the 'cars' button the hover is all of the button, but the other 2 buttons have no clickable or hoverable area.
Most articles dealing with this problem say to adjust the height/width, but all that does for me is move the image but not the text and doesn't change any of the hovering issues... not sure what else to try..
necessary style:
span.nav-button-adjust { display:block;
position:relative;
top:3px;
left:9px;
}
span.nav-button a:link,
span.nav-button a:visited { display:block;
width: 91px; height: 30px;
background-image: url(images/nav-button.png);
background-position: top;
background-repeat: no-repeat;
font-family:arial black;
text-decoration:none;
color:#1461b2;
}
span.nav-button a:hover { background-image: url(images/nav-button.png);
background-position: center;
}
span.nav-button a:active { background-image: url(images/nav-button.png);
background-position: bottom;
}
necessary html:
<span class="nav-button"><span class="nav-button-adjust"> HOME</span></span>
<span class="nav-button"><span class="nav-button-adjust"> ABOUT</span></span>
<span class="nav-button"><span class="nav-button-adjust"> CARS</span></span>
<span class="nav-button"><span class="nav-button-adjust">SEARCH</span></span>
EDIT: per request:
the image I am using: https://dl.dropboxusercontent.com/u/12017360/cars/images/nav-button.png

You have too much going on with your CSS and especially your HTML. You can tidy up what you want to do with the following. You don't need to use absolute positioning.
In the following jsFiddle you'll notice that when you hover or click a link, a tiny sliver of another part of the background image pokes through. That has to do with how you made/setup your sprite.
http://jsfiddle.net/ucsNH/
Not sure if those links have to be 30px tall. If not you can fix them by setting #nav a { height: 29px; line-height: 29px; and adjusting the background-positions by 1px so they'd be 0 -1px, 0 -30px, 0 -59px. See second jsFiddle.
http://jsfiddle.net/ucsNH/2/
CSS
ul, li {
margin: 0;
padding: 0;
list-style: none;
font: 12px/1.5em Arial, sans-serif; /* this should be inherited */
}
#nav {
width: 202px; /* ( 5px + 91px + 5px ) x 2 */
}
#nav li {
float: left;
margin: 5px;
}
#nav a:link,
#nav a:visited {
display: block;
width: 91px;
height: 30px;
line-height: 30px; /* vertically centers text */
text-transform: uppercase;
color:#1461b2;
background: url(images/nav-button.png) no-repeat 0 0;
text-decoration: none;
text-align: center;
font-weight: bold;
}
#nav a:hover {
background-position: 0 -29px;
}
#nav a:active {
background-position: 0 -58px;
color: white;
}
.home,
.about {
position: relative;
left: 25%;
}
HTML
<ul id="nav">
<li class="home">Home</li>
<li class="about">About</li>
<li>Cars</li>
<li>Search</li>
</ul>
That HTML is a lot tidier wouldn't you say?
Navigation should be placed in an un-ordered list <ul>. Please avoid inline styles like you have on your anchor <a> tags. The span tag is not need inside the anchor tag and you should not use to push things (text) around. If you need some space use margin and padding. That's what they're for!

Here you go i created a jfiddle with all the hover buttons working, just click on it to check, is this what you were looking for? If yes then please select this as the correct answer by clicking on the tick to the left.
The only changes i made were to the positioning,
<span class="nav-button"><a href="home.php" style="position:absolute;left:220px;top:17px"><span class="nav-button-adjust"> HOME</span>
</a>
</span>
<span class="nav-button"><a href="about.php" style="position:absolute;left:322px;top:17px"><span class="nav-button-adjust"> ABOUT</span>
</a>
</span>
<span class="nav-button"><a href="cars.php" style="position:absolute;left:192px;top:59px"><span class="nav-button-adjust"> CARS</span>
</a>
</span>
<span class="nav-button"><a href="search.php" style="position:absolute;left:296px;top:59px"><span class="nav-button-adjust">SEARCH</span>
</a>
</span>

Related

Why is my paragraph element displaying behind a background color

So im making a website for a school project and all was hunky dory until i tried to put a paragraph element in and it displays above the title text behind the background color
.container {
width: 80%;
margin: 0;
padding: 0;
}
#logotext {
float: left;
font-family: 'Doppio One';
margin-left: 25px;
}
nav {
float: right;
}
#nav {
list-style-type: none;
text-decoration: none;
margin-top: 35px;
}
ul li {
display: inline;
}
li a {
text-decoration: none;
color: white;
}
li a:hover {
color: #fc9516;
}
.darkwrap {
background-color: #414a4c;
position: fixed;
overflow: hidden;
}
.active {
color: #22cc25;
}
#clock {
position: absolute;
top: 0;
right: 0;
margin-top: 25px;
margin-right: 25px;
font-family: Rajdhani;
font-size: 30px;
}
<div class="container darkwrap">
<div id="logotext">
<h1>JF Web Design</h1>
</div>
<!-- Navigation Bar -->
<nav>
<ul id="nav">
<li> Page 1 </li>
<li> About </li>
<li> Page 3 </li>
</ul>
</nav>
</div>
</header>
<span id="clock"></span>
<p>
Hello
</p>
<footer></footer>
i usedchrome to highlight the faulty element so its clear whats happening here its literall positioned at the top behind the bg color
Console Highhlighted element
.darkwrap is position: fixed.
This takes it out of normal flow and locks its position relative to the viewport.
The rest of the content is laid out as normal as if the .darkwrap element didn't exist … so it ends up covered up by it.
You could use margins to compensate for the space covered up by .darkwrap when the viewport is scrolled to the top. I would simply avoid using position: fixed in the first place: The benefits of having the menu on screen all the time very rarely outweigh the drawback of using up all that vertical space all the time.
If you use float: left and float:right please remember to add clear:both to the next element on the website. Here is fixed code:
https://codepen.io/anon/pen/jKRqLz

opacity background at bottom of div not attaching properly

I'm trying to create a clickable image that has text running across the bottom with a background behind the text set to half opacity. Quite common in a lot of websites. I have this working other than a bottom lip running the length of the image poking out below the text background. It seems to be coming from the li containing the image and anchor. I've searched online and tried several ways to remove it but without any success.
How can I get the background color for my text to fit neatly to the bottom of my image ?
<ul id='main-aside-buddhaImgs'>
<li>
<div class='titleCont'>
<a href='#'><img src='images/squarebuddha.jpg' alt='budda' height='120' width='120'/>
<span>title</span>
</a>
</div>
</li>
ul#main-aside-buddhaImgs {
float:left;
}
ul#main-aside-buddhaImgs li {
float:left;
padding:12px 15px;
}
ul#main-aside-buddhaImgs li div.titleCont {
position:relative;
}
ul#main-aside-buddhaImgs li a {
display:block;
}
ul#main-aside-buddhaImgs li a span{
display:block;
position: absolute;
bottom: 0;
left:0;
width:100%;
color:#fc6;
text-align: center;
line-height: 30px;
background: #300;
background: rgba(51,0,0,.5);
}
Change your bottom attribute for ul#main-aside-buddhaImgs li a span to 4px
ul#main-aside-buddhaImgs li a span{
display:block;
position: absolute;
bottom: 4px;
left:0;
width:100%;
color:#fc6;
text-align: center;
line-height: 30px;
background: #300;
background: rgba(51,0,0,.5);
}
http://jsfiddle.net/Kr5QP/

CSS horizontal nav list align text with image links vertically

I am pretty new at CSS but have been learning, doing my moms small business website to save her money but I'm having a little CSS trouble with my nav bar.
Basically if you go here: http://area25dallas.com/s and look at the nav bar, I'm having trouble with the il listing to have the images line up vertically (instead of aligning with the top which is what they currently do) with the text, also for some reason the images are going on top of each other instead of sitting next to each other (I don't want them in separate lists like the text links because the margins are too spread out).
I have been playing around with the CSS and also googled the hell out of this but still haven't found a solution. Is there any quick fix to this?
Thanks!
EDIT:
Here is the HTML and CSS blips though if you are using chrome I feel just inspecting the elements are the easiest way to see what's going on
<div id = "header">
<div class = "container">
<ul id = "main-menu">
<li class = "active">home</li>
<li>about</li>
<li>gallery</li>
<li>press</li>
<li>contact</li>
<li><img src="images/twitter_newbird_boxed_ white.png" />
<img src="images/Pinterest_Favicon white.png" /></li>
</ul>
</div>
</div>
and the CSS
#main-menu
{
float: right;
position:relative;
top:122px;
right:150px;
}
#main-menu li
{
float: left;
margin: 30px 12px 15px 12px;
padding:0;
height:23px;
list-style:none;
line-height:20px;
}
#main-menu li:hover, #main-menu li.active { background-position: 0 -23px;}
#main-menu li:hover a, #main-menu li.active a{
background-position: 100% -30px;
}
#main-menu li a
{
display:block;
padding:0px 15px 5px 10px;
font-size:17px;
color:#fff;
text-decoration:none;
}
The images are broken onto multiple lines because they reside inside an <a> tag which has been styled as a block level element. Change the style to something like:
#main-menu {
float: right;
position: relative;
right: 75px; /* Changed */
top: 122px;
}
#main-menu li a {
color: #fff;
display: inline-block; /* Changed */
font-size: 17px;
padding: 0 15px 5px 10px;
text-decoration: none;
}
/* New */
#main-menu li a img {
position: relative;
top: -10px;
}
The new rule at the bottom moves the images up a little bit. You can play around with your css and get the same results in a lot of different ways - I went with a method that didn't involve many changes to the existing work.
Thanks for the tips, guys, this helped me out too with images in my css navigation.
I'd also recommend some added code to alleviate your spacing issue ...
#main-menu li a img {
position:absolute;
background:inherit;
top: 0px;
margin-bottom:auto;
max-height: 33px;
}

How do I adjust the background of the <ul> element for a large vertical sliding door?

I'm creating a rounded box for a nav list in a sidebar. The issue I'm having is that I've created an extra long partially transparent image to act as the bottom of the sliding door, but no matter which element I set it as the background for- it doesn't seem to want to extend properly over the whole list, starting and stopping at the first link when used as the background for the tag. I've provided the code below:
CSS:
#sidebar{float:left;
width: 200px;
text-align: center;
margin: 0 0 0 0;
}
.nav {
} /*Attempting to display it here leads to evil. As you might expect from the code below*/
ul.nav {font-family: arial, san serif;
margin-left:auto;
margin-right: auto;
margin-top:0;
margin-bottom: 0;
text-align: left ;
width: 200px;
padding: 0;
height: 1.35em;
list-style: none;
background-image:url(headbutt2.png); /*Here, it only exists as background for the first link*/
background-repeat:none;
background-position:top;
}
#navwid{background-image:url(head2.png); /*This is the extra long image. Here, it does not display at all*/
background-repeat:none;
background-position:bottom;}
ul.nav li {
overflow: hidden;
}
ul.nav a {
text-align: center;
font-weight: bold;
font-size: 1em;
padding: 0 1em 0 1em;
height: 1.35em;
text-decoration: none;
color: black;
}
.sidetop {margin:0 auto 0 auto;
background-image:url(head3.png); /*Caption background. Displays fine with no issues.*/
background-repeat: no-repeat;
display:block;
width:200px;}
HTML:
<div id=sidebar>
<div class="navwid">
<!--Nav widget container-->
<div class="sidetop">
<!--Caption-->
</div>
<div class="nav">
<ul class="nav">
<li>Dummylink</li>
<li>Dummylink</li>
<li>Dummylink</li>
<li>Dummylink</li>
<li>Dummylink</li>
<li>Dummylink</li></ul>
</div> <!--nav end-->
</div><!--navwid end-->
</div> <!--sidebar end-->
It's because you have the nav class on both the ul and all the lis. Duplicate the css for ul.nav and change one of them to ul li.nav. For the same one, remove all the background attributes.
(What you're doing is saying "Apply this css to any ul with class nav as well as its children with class nav.")
The answer to the question, as I just realized, lies in my setting a height of 1.35em to the ul tag. It was sitting there in front of me the whole time while I spent a few hours bashing away at the keyboard. Just goes to show you can't overlook the small stuff when things get buggy.

how to align text under image in css menu

I want to align menu text at the bottom of image how to i achieve it?
Expected output:
Image Image Image Image
[menutext] [menutext][menutext] [menutext]
Actual output :
Image[menutext] Image[menutext] Image[menutext] Image[menutext]
my Css Code:
#vilaniHeader
{
margin: 0;
padding: 0;
height: 80px;
background-color: Black;
}
#vilaniHeader h1
{
padding-left: 15%;
font: Arial;
font-size: 30px;
color: #ffffff;
font-weight: bold;
float: left;
}
#vilaniHeader #menu
{
color: #ffffff;
font: Arial;
font-size: 18px;
font-weight: bold;
padding-top: 30px;
padding-left: 30%;
}
#vilaniHeader #menu ul
{
list-style: none;
margin: 0;
padding: 0;
padding-right: 300px;
padding-bottom: 300px;
}
#vilaniHeader #menu li
{
display: inline;
margin: 0 15px 0 15px;
float: none;
text-align:center;
}
#vilaniHeader #menu a
{
text-decoration: none;
color: #ffffff;
}
#vilaniHeader #menu .menuHome
{
color: red;
clear:both;
padding-top:50px;
background-image:url:("Styles/menuHome.png") ;
vertical-align:text-top;
}
and My HTML code
<div id="vilaniHeader">
<h1>
Comany name
</h1>
<div id="menu">
<ul>
<li class="menuHome"><img src="Styles/menuHome.png" />Home</li>
<li><a href="About.aspx">Car</li>
<li><a href="About.aspx">Mobile</li>
<li><a href="About.aspx">OldThings</li>
<li><a href="About.aspx">Matrimoni</li>
</ul>
</div>
</div>
I want menu text should be align at the bottom of the image plese help me to do that.
I came up with this solution building upon the answer here from tejash. My answer validates and is search engine friendly.
I prefered to use links within a div but I imagine this will work with an ul
I use a background image that does not show if CSS is disabled
I use a span set displayed as block because a div inside an a tag does not validate
I use a class to place the image but use ids if you want different pics for each link
Change the width + heights to suit your needs
HTML
<div id="nav">
<span class="image"></span><span>About Us</span>
<span class="image"></span><span>Investors</span>
</div>
CSS
#nav a {
display:block;
float: left;
width:100px;
}
.image {
display:block;
background: url("myimage.jpg") no-repeat scroll center center transparent;
height:40px;
width:100px;
}
Make the img a block element so it takes the full width / line-breaks afterwards.
#menu li { display:block; }
That’s all.
I would suggest add some wrapper on text and make image and wrapper both display:block;
You can use span tag as an wrapper for text.
HTML
<ul>
<li><a><img src="Styles/menuHome.png" /><span>Home</span></a></li>
</ul>
CSS
li img, li span
{
display:block;
}
If you want your text to overlay your image, but at the bottom, you should try to play around with the line-height property. That will cause your text to move down, so it will be in the center of it's line.
I have two solutions for you. style1 works for items with text smaller than the image. style2 works for items with text wider than the image. Easiest is to make sure that the images are always wider or smaller than the text, so that you need only one style.
CSS:
#menu {
list-style:none
}
#menu li {
float:left;
text-align:center
}
#menu .style1 img, #menu .style2 span {
overflow:hidden
}
#menu .style1 span, #menu .style2 img {
display:block
}
HTML:
<div id="vilaniHeader">
<h1>Comany name</h1>
<ul id="menu">
<li class="style1"><img src="Styles/menuHome.png" width="10" alt="" /> <span>Home</span></li>
<li class="style2"><img src="Styles/menuHome.png" width="100" alt="" /> <span>Car</span></li>
</ul>
</div>
I'm not a span-fan but it seems like you can't do it without here.
BTW, why don't you just add a br?
CSS:
#menu {
list-style:none
}
#menu li {
float:left;
text-align:center
}
HTML:
<div id="vilaniHeader">
<h1>Comany name</h1>
<ul id="menu">
<li><img src="Styles/menuHome.png" width="10" alt="" /><br />Home</li>
<li><img src="Styles/menuHome.png" width="100" alt="" /><br />Car</li>
</ul>
</div>
I guess that's the most easy and reliable solution.
You can do this way, obviously replacing the image sample I used. For the link to work, you can use a jQuery click event on LI, so it searches for the link inside the clicked LI and then opens the desired link.
http://jsfiddle.net/WcePK/
HTML
<ul>
<li class="menuHome"><img src="Styles/menuHome.png" />Home</li>
<li style="background-image: url('http://jsfiddle.net/img/logo.png')">Car</li>
<li style="background-image: url('http://jsfiddle.net/img/logo.png')">Mobile</li>
<li style="background-image: url('http://jsfiddle.net/img/logo.png')">OldThings</li>
<li style="background-image: url('http://jsfiddle.net/img/logo.png')">Matrimoni</li>
</ul>
CSS
LI {
float: left;
margin: 5px;
padding: 50px 10px 10px;
min-width: 100px;
background-repeat: no-repeat;
background-position: center 10px;
background-color: #366D93;
text-align: center;
cursor: pointer
}