Search code examples
cssentity

why is a HTML entity enlarging it's father HTML element?


I made a simple 2 tier navigation menu with only one dropdown menu. I've added a 🢓 entity to that 2 tier dropdown menu and it's enlarging its containing element. There's no margin/padding/border that causes this problem. Is there a way to fix that without removing the HTML entity?

nav#menu ul {list-style-type: none; position: relative; padding: 0;}
nav#menu ul li {float: left;width: 190px;}
nav#menu ul li a {
    text-decoration: none;
    display: block;
    text-align: center;
    padding: 1rem 0;
    background-color: cornflowerblue;
    color: white;
    border-right: 2px solid white;
    border-top: 2px solid white;
    font-family: sans-serif;
    font-weight: bold;
}
/* appears when floating */
nav#menu ul ul {
    display: none;
    position: absolute;
    top: 100%;
}
nav#menu ul ul li a{
    display: block;
    text-decoration: none;
    text-align: center;
    background-color: cornflowerblue;
    color: white;
    padding: 1rem 0;
    font-family: sans-serif;
    font-weight: bold;
}
nav#menu ul ul li {
    float: none;
    width: 190px;
    padding: 0;
}
nav#menu ul li:hover > ul {
    display: block;
}
nav#menu > ul::after {
    content: "";
    display: block;
    clear: both;
}
.level2 li {
    border-top: 2px solid white;
}
nav#menu a:hover, nav#menu a:focus{background-color:rgb(45, 114, 241);}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="styles.css">
    <title>Navigation Menu</title>
</head>
<body>
    <nav id="menu">
        <ul>
          <li><a href="#">Home</a></li>
          <li><a href="#">Menu &#129171;</a>
            <ul class="level2">
              <li><a href="#">One</a></li>
              <li><a href="#">Two</a></li>
              <li><a href="#">Three</a></li>
            </ul>
          </li>
          <li><a href="#">Products</a></li>
          <li><a href="#">Help</a></li>
          <li class="lastitem"><a href="#">About</a></li>
        </ul>
      </nav>
</body>
</html>


Solution

  • Yes, there is. The enlargement is caused by the glyph for the character entity being drawn from a different font, with different font metrics.

    What I recommend doing is putting the entity reference in a span, and giving the span a minimal line-height. Something like this:

    nav#menu ul {list-style-type: none; position: relative; padding: 0;}
    nav#menu ul li {float: left;width: 190px;}
    nav#menu ul li a {
        text-decoration: none;
        display: block;
        text-align: center;
        padding: 1rem 0;
        background-color: cornflowerblue;
        color: white;
        border-right: 2px solid white;
        border-top: 2px solid white;
        font-family: sans-serif;
        font-weight: bold;
    }
    nav#menu ul li a span { /* <== the added rule */
      line-height:1px;
    }
    /* appears when floating */
    nav#menu ul ul {
        display: none;
        position: absolute;
        top: 100%;
    }
    nav#menu ul ul li a{
        display: block;
        text-decoration: none;
        text-align: center;
        background-color: cornflowerblue;
        color: white;
        padding: 1rem 0;
        font-family: sans-serif;
        font-weight: bold;
    }
    nav#menu ul ul li {
        float: none;
        width: 190px;
        padding: 0;
    }
    nav#menu ul li:hover > ul {
        display: block;
    }
    nav#menu > ul::after {
        content: "";
        display: block;
        clear: both;
    }
    .level2 li {
        border-top: 2px solid white;
    }
    nav#menu a:hover, nav#menu a:focus{background-color:rgb(45, 114, 241);}
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="styles.css">
        <title>Navigation Menu</title>
    </head>
    <body>
        <nav id="menu">
            <ul>
              <li><a href="#">Home</a></li>
              <li><a href="#">Menu <span>&#129171;</span></a>
                <ul class="level2">
                  <li><a href="#">One</a></li>
                  <li><a href="#">Two</a></li>
                  <li><a href="#">Three</a></li>
                </ul>
              </li>
              <li><a href="#">Products</a></li>
              <li><a href="#">Help</a></li>
              <li class="lastitem"><a href="#">About</a></li>
            </ul>
          </nav>
    </body>
    </html>