Javascript: a elements inside div: highlight height when hover

2356 views html
4

I have a custom navbar that looks like this:

:root {
  --navbar-background-color: #ff1a1a;
  --navbar-text-color: var(--header-text-color) !important;
  --navbar-height: 23px;
  --navbar-top: 40px;
  --navbar-font-size:
}

#custom-navbar {
  overflow: hidden;
  background-color: var(--navbar-background-color);
  height: var(--navbar-height);
  top: var(--navbar-top);
  right: 0;
  left: 0;
  position: fixed;
  overflow-y: hidden;
  z-index: 1;
  padding-right: 50px;
  padding-top: 3px;
  display: inline;
  text-align: right;
}

#custom-navbar p {
  color: var(--navbar-text-color);
  display: inline;
  padding: 10px;
  font-size: var(--navbar-font-size);
  text-decorations: none;
  height: 100% !important;
}

#custom-navbar a:hover {
  background-color: grey;
}
<div id="custom-navbar">
  <a href="#">
    <p>LINK 1</p>
  </a>
  <a href="#">
    <p>LINK 2</p>
  </a>
  <a href="#">
    <p>LINK 3</p>
  </a>
  <a href="#">
    <p>LINK 4</p>
  </a>
</div>

When I hover a mouse over one of the navbar's link, the link gets highlighted in grey. However, there's still some navbar initial color visible: the highlight color doesn't cover all the navbar height.

jsfiddle: https://jsfiddle.net/rafn51sk/3/

How do I fix it? I tried setting height: 100% to navbar's p element, but it didn't work.

answered question

2 Answers

7

Don't use P inside A, it is a bad thing look this working example

HTML:

<div id="custom-navbar">
       <a href="#">LINK 1</a>
       <a href="#">LINK 2</a>
       <a href="#">LINK 3</a>
       <a href="#">LINK 4</a>
</div>

CSS:

:root  {
    --navbar-background-color: #ff1a1a;

    --navbar-text-color: var(--header-text-color) !important;
    --navbar-height: 23px;
    --navbar-top: 40px;
    --navbar-font-size:

}

#custom-navbar {
    overflow: hidden;
    background-color: var(--navbar-background-color);

    height: var(--navbar-height);

    top: var(--navbar-top);
    right: 0;
    left: 0;
    position: fixed;
    overflow-y: hidden;
    z-index: 1;

    padding-right: 50px;
    padding-top: 3px;
    display: inline;
    text-align: right;

}


#custom-navbar a  {
    color: var(--navbar-text-color);
    display: inline;
    padding: 10px;
    font-size: var(--navbar-font-size);
    text-decoration: none;

    height: 100% !important;
}

#custom-navbar a:hover  {
    background-color: grey;
}

posted this
7

Give the a tags

display: inline-block;

after that the height: 100%; will work. (inline element don't have any height, inline-block have height. You also need to cancel the padding in the navbar div.

posted this

Have an answer?

JD

Please login first before posting an answer.