Setting a width and height on an A tag

HtmlCssCross Browser

Html Problem Overview


Is it possible to set the width and height in pixels on an anchor tag? I'd like to have the anchor tag to have a background image while retaining the text inside the anchor.

li {
  width: 32px;
  height: 32px;
  background-color: orange;
}

li a {
  width: 32px;
  height: 32px;
  background-color: red;
}

<li><a href="#">Something</a></li>

Html Solutions


Solution 1 - Html

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

Solution 2 - Html

You can also use display: inline-block. The advantage of this is that it will set the height and width like a block element but also set it inline so that you can have another a tag sitting right next to it, permitting the parent space.

You can find out more about display properties here

Solution 3 - Html

All these suggestions work unless you put the anchors inside an UL list.

<ul>
    <li>
        <a>click me</a>>
    </li>
</ul>

Then any cascade style sheet rules are overridden in the Chrome browser. The width becomes auto. Then you must use inline CSS rules directly on the anchor itself.

Solution 4 - Html

It's not an exact duplicate (so far as I can find), but this is a common problem.

display:block is what you need. but you should read the spec to understand why.

Solution 5 - Html

Below working for me

display: block;
width: 100%;

Solution 6 - Html

Setting display: block on the anchor element is a solid suggestion, but I can think of another scenario that doesn't require explicitly setting it.

Supposethe anchor is nested inside a div element with display: flex property. Then all the direct children of this div aka flex items become block elements and you can set height to the anchor.

If the background image's height is not capped, however, it will cause overflow. So apply height: 100% to the it.

Below is an example. Please note that the background image in the example is SVG, but SVG is inline by default, so you can replace it with an element and get the same result.

.header {
  display: flex;
  box-shadow: 0px 2px 1px rgba(0, 0, 0, 0.2);
}
.link {
  height: 48px;
}
.svg-logo {
  height: 100%;
}

<div class="header">
<a class="link">
<svg class="svg-logo" viewBox="0 0 184 25" fill="#000"><path d="M13.8 2.9c0-2-1.9-2.5-3.4-2.5v.3c.9 0 1.6.3 1.6 1 0 .4-.3 1-1.2 1-.7 0-2.2-.4-3.3-.8C6.2 1.4 5 1 4 1 2 1 .6 2.5.6 4.2c0 1.5 1.1 2 1.5 2.2l.1-.2c-.2-.2-.5-.4-.5-1 0-.4.4-1.1 1.4-1.1.9 0 2.1.4 3.7.9 1.4.4 2.9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"></path></svg>
</a>
</div>

So all in all, this is a roundabout way of setting display: block to anchor element, but I feel like pointing it out because the layout above is very common in header logos.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLukeView Question on Stackoverflow
Solution 1 - HtmlJaredView Answer on Stackoverflow
Solution 2 - HtmlsarcastyxView Answer on Stackoverflow
Solution 3 - HtmlBradleyView Answer on Stackoverflow
Solution 4 - HtmlzzzzBovView Answer on Stackoverflow
Solution 5 - HtmlPankaj UpadhyayView Answer on Stackoverflow
Solution 6 - HtmlbytrangleView Answer on Stackoverflow