2010-04-14 62 views
16

I'm trying to create css buttons by using the following html markup:CSS - Neden <a href> elements?

<a href="access.php" class="css_button_red">Forgot password</a> 

But it ends up being not bigger than the text in the middle. Even though I've set the class's height and width.

You can preview the problem here btw, www.matkalenderen.no Notice the first button, that's a form button and it's using it's own class. At first I tried to use the same class on the css button as well and the same problem appeared, so I tried to separate them into their own classes. In case there was some kind of crash. But it didn't matter anyway.

What am I missing here?

cevap

43

As the others have said, by default <a> is an inline element, and inline elements can't specify a width or height. You can change it to be a block element like this:

a { 
    display: block; 
} 

Though it will then display (unsurprisingly) as a block, sitting on its own, outside the flow of the surrounding text. A better solution is to use display: inline-block which might be a best-of-both-worlds solution depending on your situation.

See PPK's writeup yükseklik ve genişliğini ayarlayamıyorum.

Bu değerin gerçek kullanımı, bir satır içi elemanın genişliğini vermek istediğiniz zamandır. Bazı durumlarda, bazı tarayıcılar gerçek bir satır içi eleman üzerinde genişliğe izin vermez, ancak görüntülemeye geçiş yaparsanız: satır içi bloğu bir genişlik ayarlamaya izin verilir.

+0

Haha! Inline-block benim için bir çok şeyi sabitledi! Sadece görüntüleme kullanırken: blok, tüm düğme yanlış hizalanmış. Ama şimdi, form düğmesiyle aynı akışı takip ediyor :) Thanx! –

+1

Satır içi bloğa dikkat edin. Bu tarayıcılar tarafından tek biçimli olarak desteklenmez. Analizlerinizi öğrenin ve siteniz için tüm istatistiksel olarak önemli tarayıcılarda iyi görüntülendiğinden emin olun. Görünüşe göre, IE 6 ve 7'nin her ikisi de katı satır içi blok desteğinden yoksundur. Bkz. Http://www.quirksmode.org/css/display.html#t03 – kingjeffrey

+0

@kingjeffrey Quirksmode, IE6 & 7'nin, yerel olarak satır içi olmayan öğelere uygulandığında satır içi blokla ilgili sorunları olduğunu belirtir. Bu nedenle, bir "" için uygulandığında sorun olmaz. – nickf

8

Because <a>'s are inline elements by default. In CSS define a { display:block; } and height and width settings will be applied.

Of course, you may not want to declare all anchor tags as block level elements, so filter by class or id as needed.

+0

Thanx, bunu zaten denediğimi düşündüm. Şimdi aniden işe yaradı. Adam, sadece css kodlaması söz konusu olduğunda yapısal bir zihne sahip olmanın ne kadar önemli olduğunu gösterir! –

5

, en doğru çözüm hala inline unsur olarak ele alınacaktır element için height ayarlamasına olanak tanıyacak display: inline-block; olduğunu düşünüyorum.

İlgili konular