Why do my padding and font size fail to match the height of my container?

When setting the height of my boxes to match the height of my <nav>, I encountered overflow issues. Despite using a 10rem height for the nav and a 2.25rem font, calculating the padding as 10-2.25/2 didn't result in the desired outcome. Can someone explain why this happened and how to make the boxes align perfectly with the container?

body{
      background-color: hsl(168, 39%, 64%);
      margin:0;
      padding:0;
    }

.ul-nav{
    display:flex;
    flex-direction:row;
    flex-wrap: nowrap;
    justify-content:space-evenly;
    align-content:stretch;
    height: 100%;
    margin: 0;
    list-style: none;
    margin-left: 7.5rem;
}
.li-nav{
    text-align: center;
}
.nav{
    display: flex;
    position: fixed;
    margin:0;
    padding: 0;
    top: 0;
    left: 0;
    width: 100%;
    height: 10rem;
    background-color: hsl(178, 40%, 40%);
}
.nav-a{
    text-decoration: none; 
    color: black;
    display:block;
    font: Arial;
    font-size: 2.25rem;
    margin: 0;
    padding: 3.72rem;
}
.nav-a:hover{
    text-decoration: none;
    background-color: hsl(178, 40%, 30%);
}
.nav-a:visited{
    text-decoration: none;
}

I'm puzzled as to why the padding calculation isn't just (height - font size) / 2. As a beginner in HTML/CSS who recently completed certification, I would appreciate a thorough explanation since this is my first project created after training.

<nav id="nav-bar" class="nav">
  <ul class="ul-nav">
    <li class="li-nav"><a class="nav-a" href="Grace.html">Home</a></li>
    <li class="li-nav"><a class="nav-a" href="about.html">About</a></li>
    <li class="li-nav"><a class="nav-a" href="pricing.html">Pricing</a></li>
    <li class="li-nav"><a class="nav-a" href="reviews.html">Reviews</a></li>
    <li class="li-nav"><a class="nav-a" href="contact.html">Contact</a></li>
  </ul>
</nav>

Answer №1

One reason for this issue is that the height of li a exceeds the height of the nav. There are two straightforward ways to address this problem:

  1. Include overflow: hidden in the nav CSS.
  2. Eliminate the height property from the nav and adjust the padding instead. This method is recommended

body {
  background-color: hsl(168, 39%, 64%);
  margin: 0;
  padding: 0;
}

.ul-nav {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  justify-content: space-evenly;
  align-content: stretch;
  height: 100%;
  margin: 0;
  list-style: none;
  margin-left: 7.5rem;
}

.li-nav {
  text-align: center;
}

.nav {
  display: flex;
  position: fixed;
  margin: 0;
  padding: 0;
  top: 0;
  left: 0;
  width: 100%;
  /* HERE: YOU CAN REMOVE THIS */
  height: 10rem;
  background-color: hsl(178, 40%, 40%);
  /* HERE */
  overflow: hidden;
}

.nav-a {
  text-decoration: none;
  color: black;
  display: block;
  font: Arial;
  font-size: 2.25rem;
  margin: 0;
  padding: 3.72rem;
}

.nav-a:hover {
  text-decoration: none;
  background-color: hsl(178, 40%, 30%);
}

.nav-a:visited {
  text-decoration: none;
}
<nav id="nav-bar" class="nav">
  <ul class="ul-nav">
    <li class="li-nav"><a class="nav-a" href="Grace.html">Home</a></li>
    <li class="li-nav"><a class="nav-a" href="about.html">About</a></li>
    <li class="li-nav"><a class="nav-a" href="pricing.html">Pricing</a></li>
    <li class="li-nav"><a class="nav-a" href="reviews.html">Reviews</a></li>
    <li class="li-nav"><a class="nav-a" href="contact.html">Contact</a></li>
  </ul>
</nav>

Answer №2

It is recommended to remove the height property of the Nav element as it overrides the padding setting. When you set height: 10rem;, it applies the padding at the top but cannot do so at the bottom due to this override. Removing the height property would be a better approach.

body {
  background-color: white;
  margin: 0;
  padding: 0;
}

.ul-nav {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  justify-content: space-evenly;
  align-content: stretch;
  height: 100%;
  margin: 0;
  list-style: none;
  margin-left: 7.5rem;
}

.li-nav {
  text-align: center;
}

.nav {
  display: flex;
  position: fixed;
  margin: 0;
  padding: 0;
  top: 0;
  left: 0;
  width: 100%;
  /* HERE: YOU CAN REMOVE THIS */
  height: 10rem;
  background-color: red;
  /* HERE */
  overflow: hidden;
}

.nav-a {
  text-decoration: none;
  color: black;
  display: block;
  font: Arial;
  font-size: 2.25rem;
  margin: 0;
  padding: 3.72rem;
  transition: 0.3s;
  font-weight: bold;
}

.nav-a:hover {
  text-decoration: none;
  background-color: darkRed;
  color:white;
}

.nav-a:visited {
  text-decoration: none;
}
<nav id="nav-bar" class="nav">
  <ul class="ul-nav">
    <li class="li-nav"><a class="nav-a" href="Grace.html">Home</a></li>
    <li class="li-nav"><a class="nav-a" href="about.html">About</a></li>
    <li class="li-nav"><a class="nav-a" href="pricing.html">Pricing</a></li>
    <li class="li-nav"><a class="nav-a" href="reviews.html">Reviews</a></li>
    <li class="li-nav"><a class="nav-a" href="contact.html">Contact</a></li>
  </ul>
</nav>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Unable to get the Express.js Router functioning correctly on my server, even in a basic scenario

I am encountering an issue with using express.Router(). It was not functioning correctly in my application for serving JSON from MongoDB, so I attempted to simplify the scenario. However, I am receiving a 404 Not Found error in the request. What steps shou ...

Center-aligned navigation bar with all elements except for one positioned on the right side

Currently, I am attempting to center 4 elements next to each other while having one element float to the right using a flex-based solution. The ideal outcome would be for the floated element to remain in place when resizing the browser, with the other 4 e ...

What is the best way to display the user login in the center of a page with all necessary controls

Challenge How can the user login be displayed in the center of the page? Additional Information The current layout displays user login data on the left-hand side. I am looking to have the user login data centralized on the page, which includes labels f ...

What is the method for switching the parent element following the adjustment of the selected child's opacity

I am trying to find a way to use CSS to adjust the parent element's opacity if it contains a child element with a specific class. Currently, I have only been able to achieve this on hover by adding a background color to the parent element's ::aft ...

What is the most effective way to determine which radio button is currently selected in a group with the same name using JQuery?

<form class="responses"> <input type="radio" name="option" value="0">False</input> <input type="radio" name="option" value="1">True</input> </form> Just tested: $('[name="option"]').is(':checked ...

Unable to retrieve file path from image selection in JavaScript by clicking on a button

I am trying to create a simple browsing feature that only accepts images. However, after clicking the button, I am unable to retrieve the full path in JavaScript. All I can get is the filename. JavaScript <script type="text/javascript"> functio ...

Adding text over an image in Tailwind without the need for absolute positioning is achievable

I'm struggling to position text over an image without using absolute positioning. It's working fine on large and small screens, but I'm having trouble with medium screens. I've searched for similar questions on Stack Overflow, but none ...

The use of backticks within an HTML document for nesting purposes is not permitted

Currently, I am utilizing nodemailer to send HTML template code in Node.js. However, the issue I am encountering is that I cannot nest backticks within it: Here's my code snippet: let mailDetails={ from: 'example@example.com', to: & ...

How can I incorporate a fade opacity effect into my Div scrolling feature?

I successfully implemented code to make div elements stick at the top with a 64px offset when scrolling. Now, I am trying to also make the opacity of these divs fade to 0 as they scroll. I am struggling to figure out how to achieve this effect. Below is ...

Validation (CSS 2.0): The CSS property name 'mso-number-format' is unrecognized and invalid

I am currently tasked with maintaining an ASP .Net application that includes a feature to export HTML tables to Excel. The HTML code includes elements like this: <td style="mso-number-format:\@;"> During the build process, I encounter an error s ...

error: local server did not return any data

I am currently using PHP on a Linux machine. In my HTML code, I have set up an AJAX request to the local Apache server (check http://localhost), with the intention of displaying the data from the server on the screen. However, for some reason, nothing is b ...

Requesting an API token through the body using Javascript's Fetch function

I'm currently working on developing a frontend application using Javascript Fetch to interact with an API service. One of the tasks I need to accomplish is to create a token by using the POST method and sending an apiKey parameter in the Body. Once I ...

A guide on embedding the flag status within the image tag

I would like to determine the status of the image within the img tag using a flag called "imagestatus" in the provided code: echo '<a href="#" class="swap-menu"><img id="menu_image" src="images/collapsed.gif" hspace = "2"/>'.$B-> ...

What are the drawbacks of using JavaScript to load a series of images?

Currently, I am facing an issue with the loading speed of a sequence of images on my website. The javascript code I have implemented to cycle through 50 images works perfectly fine locally but becomes slow and buggy when the site is live. Despite the image ...

What is the best way to align two divs next to each other while keeping the second one centered with flexbox?

Here are my two div elements: <div class='container'> <div class='left'></div> <div class='centered'></div> </div> I am looking to center the second inner div and have the first inner ...

What is the reason that certain CSS pages are not being utilized on my website?

I am facing issues with rendering my website on a tablet device, specifically with my last 2 css pages (800px.css and 800pxland.css). Despite working fine just hours ago, now they seem to be neglected while all the other ones work perfectly. I have tried ...

The product has been taken out of the cart, yet it has not been reinserted into the cart

The product disappears from the cart after clicking on Add to Cart, but it doesn't reappear when clicked again. //function for adding only one item to cart document.getElementById('btn1').onclick = function() { addItemToCart() }; fun ...

The Super Sub menu is failing to display correctly as intended

I encountered an issue while trying to create a Super sub-menu. The problem is that the super sub-menu appears when I hover over the main menus, instead of the sub-menus. I suspect there may be something wrong with the display: none; attribute, but I' ...

Adjust font style within an HTML/CSS document

I recently obtained the source code for an online portfolio project, but I'm struggling to figure out how to modify the font style. Can anyone provide guidance on this issue? https://github.com/akashyap2013/PortFolio_Website ...

Issue with bootstrap: the navbar highlight and anchor href are not functioning as intended

I'm currently working on creating a Navbar and implementing the navbar highlighting feature using the active bootstrap class. Below is the code I have tried: <!DOCTYPE html> <html lang="en"> <head> <!-- Bootstrap CSS ...