Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS - Why am I not able to set height and width of <a href> elements?

Tags:

html

css

I'm trying to create css buttons by using the following html markup:

<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?

like image 714
Kenny Bones Avatar asked Apr 14 '10 06:04

Kenny Bones


People also ask

Why height and width is not working in CSS?

While the display property is inline , height and width will not work. display :inline; By changing the display property from inline to block or inline-block , height and width should be worked properly.

How do you change the width and height of an anchor tag?

You need to make your anchor display: block or display: inline-block; and then it will accept the width and height values.

Can we give height and width in anchor tag?

An anchor is an inline element by default so you can't add dimensions to it unless you change its display property.

Can we set width and height of inline elements in CSS?

Inline element properties: The width of an inline element is the width of the content. The height and width of an inline element cannot be set in CSS. You cannot set the height and width of block-level elements in CSS.


2 Answers

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 about it.

The real use of this value is when you want to give an inline element a width. In some circumstances some browsers don't allow a width on a real inline element, but if you switch to display: inline-block you are allowed to set a width.

like image 117
nickf Avatar answered Oct 15 '22 15:10

nickf


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.

like image 21
kingjeffrey Avatar answered Oct 15 '22 15:10

kingjeffrey