tags:

views:

206

answers:

1

I am trying to get better about minimizing any unnecessary markup and using optimal CSS.

<label id="cart_promocode">Promo-code:
    <span><%= Html.TextBox("PromoCode") %>
       <a href="#" id="lnkApplyCoupon" class="hidden">Apply Coupon</a>
    </span>
</label>

The 'Apply Coupon' link should be positioned underneath the textbox. This is the css I am using. Note that the <A> is displayed as a block so it gets its own line.

#cart_promocode a
{
     display: block;
     margin: 4px 0 0 0;
     font-size: 93%;
     color: Blue;
}

.hidden {
     display: none;
}

I will show and hide it using jQuery with :

 $('#lnkApplyCoupon').show() and hide()

The problem is that I want the item to be initially hidden. The 'hidden' class applied initially doesnt work.

The best solution I have come up with is

$(function() {
     $('#lnkApplyCoupon').hide();
});

This will hide the apply coupon link when the page loads, but then if the user doesnt have Javascript enabled it will not initially be hidden - and in fact in that case I dont want it to EVER appear.

What am I missing - either a trick or a different approach to my CSS.

+2  A: 

The 'hidden' class applied initially doesnt work

#cart_promocode a { display:block; }

Has a higher specificity ( 101 AFAIK ) than your other rule, specify the ID before the class and you'll have a higher specificity ( 110 AFAIK ).

Ids = 100, classes = 10, base selectors = 1.

#cart_promocode .hidden, .hidden { display:none; }

Two selectors because the second one will do a universal target.

meder
@Anon: why the downvote?
meder
this doesnt actually give it a higher specificity. it is still initially not visible. also if it is set to '!important' then show() never works becasue it is always hidden
Simon_Weaver
Um - it *does* give it a higher specificity. You said that the .hidden class doesn't work, when my css is applied it makes it hidden.See: http://jsbin.com/ahacu
meder
Toggling: http://jsbin.com/igome
meder
@meder sorry i must have had something else interfering with it. very much appreciate your time
Simon_Weaver
@meder finally came back and fixed the upvote for u :-) needed this again and forgotten what i'd done
Simon_Weaver