Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fill the anchor element's height to 100% in html?

Tags:

html

css

I have created a top bar that have height: 50px, then an anchor element inside it, the anchor element with it's padding supposed to be exact as the top bar height.

Testing the code in major browsers, Firefox has given the exact pixels height of top bar to anchor element, but in Chrome and IE, it have -1px result. Fiddle Here

HTML:

<div > <a href="">Text Here</a> </div>

CSS:

div {
    width: auto;
    height: 50px;
    background-color: #333;
}
a {
    padding: 10px;
    display: inline-block;
    font-size: 24px; /* Adjusting this would affect element block size */
    color: white;
    font-family:'Bebas Neue';
    background-color: #777;
}
a:hover { background-color: green; }

How could I fill the dimensions of the <a> anchor element up to the div's height?

like image 467
Aesthetic Avatar asked Feb 14 '14 08:02

Aesthetic


People also ask

How do I set height to 100 in HTML?

Syntax: To set a div element height to 100% of the browser window, it can simply use the following property of CSS: height:100vh; Example: HTML.

How do I make my HTML full height?

For a responsive full page height, set the body element min-height to 100vh. If you set a page width, choose 100% over 100vw to avoid surprise horizontal scrollbars.

How do I resize an anchor tag in HTML?

css: <style> li { width: 110px; height: 42px; background-color: black; } li a { width: 32px; height: 32px; color: white; } </style> html page: <li><a href="#">write something</a></li> you need to set anchor tag's display property as block :: 'display:block;' , then only it will accept values of height and width.

What does height 100% do CSS?

height: 100% gives the element 100% height of its parent container. height: auto means the element height will depend upon the height of its children.


2 Answers

As no one seems to have shown you the box-sizing css property: Add the following to your <a>

height:100%;
box-sizing:border-box;

EDIT:

  • To vertically align the content: Use display table-cell on the <a> element and display: table on its parent.

div {
  width: auto;
  height: 100px;
  background-color: #333;
}
a {
  padding: 10px;
  display: table;
  font-size: 24px;
  color: white;
  font-family: 'Bebas Neue';
  background-color: #777;
  height: 100%;
  box-sizing: border-box;
}
a > span {
  display: table-cell;
  vertical-align: middle;
}
a:hover {
  background-color: green;
}
<div>
  <a href=""><span>Text Here</span></a> 
</div>

Fiddle Here

like image 171
Pete Avatar answered Oct 24 '22 04:10

Pete


Here is the another way :-

Fiddle :http://jsfiddle.net/nikhilvkd/4YQtA/3/

a {
    height: 30px;/*changes here*/
    padding:10px;
    display: inline-block;
    font-size: 24px;
    color: white;
    font-family:'Bebas Neue';
    background-color: #777;
}
like image 32
Krish Avatar answered Oct 24 '22 05:10

Krish