Solved

Making my CSS more dynamic in Javascript

Posted on 2006-11-28
6
283 Views
Last Modified: 2013-11-19
Hi there wonder if you guys can help me in getting the following code more dynamic?

<style>
a#yaprak {
    padding-left: 80px;      
    padding-top: 5px;      
    width: 227px;
    height: 40px;
    background-image: url(icon_sync.gif);
    display: block;
    text-decoration: none;
}

a#yaprak:hover {
    background-position: 0 38px;
}

a#yaprak1 {
    padding-left: 80px;      
    padding-top: 5px;      
    width: 227px;
    height: 40px;
    background-image: url(icon_groups.gif);
    display: block;
    text-decoration: none;
}

a#yaprak1:hover {
    background-position: 0 38px;
}

a#yaprak2 {
    padding-left: 80px;      
    padding-top: 5px;      
    width: 227px;
    height: 40px;
    background-image: url(icon_backup.gif);
    display: block;
    text-decoration: none;
}

a#yaprak2:hover {
    background-position: 0 38px;
}
</style>
<a href="#" id="yaprak">menu1</a>
<a href="#" id="yaprak1">menu2</a>
<a href="#" id="yaprak2">menu3</a>

As you can see, every time I add a new href, I have to increment yaprak by 1, and then add a further 2 more styles. Can you please help me in getting it more dynamic.

Hope that makes sense
Mousemat24
0
Comment
Question by:mousemat24
  • 4
6 Comments
 
LVL 54

Assisted Solution

by:b0lsc0tt
b0lsc0tt earned 100 total points
Comment Utility
mousemat24,

Use the class attribute instead.  It can be used in more than one element and can be used in CSS.  Your code would look like this ...

<style>
a.yaprak {
    padding-left: 80px;    
    padding-top: 5px;    
    width: 227px;
    height: 40px;
    background-image: url(icon_sync.gif);
    display: block;
    text-decoration: none;
}

a.yaprak:hover {
    background-position: 0 38px;
}

</style>
<a href="#" id="yaprak" class="yaprak">menu1</a>
<a href="#" id="yaprak1" class="yaprak">menu2</a>
<a href="#" id="yaprak2" class="yaprak">menu3</a>

Let me know if you have any questions or need more information.

b0lsc0tt
0
 
LVL 12

Accepted Solution

by:
jessegivy earned 400 total points
Comment Utility
The convention you're using here is for specific tags organized by id, you can however apply a style to all anchors and simply override that style for those you want to have appear different  ...So I guess all I'm seeing that's different is background image so try something like this:


a{
    padding-left: 80px;    
    padding-top: 5px;    
    width: 227px;
    height: 40px;
    display: block;
    text-decoration: none;
}

a:hover {
    background-position: 0 38px;
}

<a href="#" id="yaprak" style="background-image: url(icon_sync.gif);">menu1</a>
<a href="#" id="yaprak1" style="background-image: url(icon_groups.gif);">menu2</a>
<a href="#" id="yaprak2" style="background-image: url(icon_backup.gif);">menu3</a>

...the above style will apply to all anchors, and of course you could add the css I used in your CSS file instead of inline with the relevant id's, but there's no need to repeat all the other styles

Jesse
0
 
LVL 54

Expert Comment

by:b0lsc0tt
Comment Utility
Just to point out a thing or two.  Notice that you replace # with . to specify the class instead of the id in CSS.  Also the class attribute can be added to pretty much any tag and can be with id or other attributes.  Class, unlike id, can be duplicated.

bol
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 54

Expert Comment

by:b0lsc0tt
Comment Utility
I missed the unique background image.  You would want to remove it from the style in the head of the html and put it in the tag, like Jessegivy's showed.  Credit to him for that good example.

bol
0
 

Author Comment

by:mousemat24
Comment Utility
Thanks jessegivy!! and thankyou b0lsc0tt

But my code had 3 images, thats why I had to give most of points to jessegivy
0
 
LVL 54

Expert Comment

by:b0lsc0tt
Comment Utility
Your welcome.  I understand.  I'm glad that I could help.  Thank you for the grade, the points and the fun question.

bol
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Save signature to file 7 44
radio button value 3 45
jQuery Time Off Event 20 42
Javascript Refresh Parent Page from Popup 3 24
Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now