What is the best way to ensure uniform spacing between header elements?

I am having an issue with aligning elements in my header while using justify-content: space-between. When I add padding-right: 20px to my navigation links, it causes my h2 element to shift to the right and disrupts the alignment. How can I maintain the padding while keeping all elements evenly distributed? Any help would be greatly appreciated. Thank you.

View Example on JSFiddle

HTML

<header>
 <div id="header-wrapper">
  <nav>
   <a>Projects</a>
   <a>About</a>
   <a>Contact</a>
  </nav>

 <h2>EMMANUEL OJIJI</h2>
 <p>SOCIAL MEDIA HERE</p>
 </div>
</header>

<section id="intro">
<div id="intro-wrapper">
<h1>I'm Lorem Ipsum — a Birmingham-based Visual Designer with a passion and 
a firm belief in considered and meaningful design.</h1>
</div>
</section>

CSS

html {
margin: 0;
padding: 0;
height: 100%;
}

* {
margin: 0;
padding: 0;
box-sizing: border-box;
}

header {
height: 100px;
background-color: white;
box-shadow: 0 0 5px rgba(0, 0, 0, 0.2);
display: flex;
justify-content: center;
align-items: center;
padding-left: 50px;
padding-right: 50px;
}

#header-wrapper {
width: 90%;
display: flex;
justify-content: space-between;
align-items: center;
}

nav a {
padding-right: 20px;
}

#intro {
height: 90vh;
display: flex;
justify-content: center;
align-items: center;
}

#intro-wrapper {
display: flex;
text-align: center;
width: 50%;
}

#intro-wrapper h1 {
font-weight: 700;
font-family: 'Raleway', sans-serif;
font-size: 2.2em;
line-height: 60px;
color: #333;
}

Answer №1

Make sure the last element in the navigation doesn't have padding:

nav a:not(:last-child)

html {
  margin: 0;
  padding: 0;
  height: 100%;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

header {
  height: 100px;
  background-color: white;
  box-shadow: 0 0 5px rgba(0, 0, 0, 0.2);
  display: flex;
  justify-content: center;
  ;
  align-items: center;
  padding-left: 50px;
  padding-right: 50px;
}

#header-wrapper {
  width: 90%;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

nav a:not(:last-child) {
  padding-right: 20px;
}

#intro {
  height: 90vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

#intro-wrapper {
  display: flex;
  text-align: center;
  width: 50%;
}

#intro-wrapper h1 {
  font-weight: 700;
  font-family: 'Raleway', sans-serif;
  font-size: 2.2em;
  line-height: 60px;
  color: #333;
}
<header>
  <div id="header-wrapper">
    <nav>
      <a>Projects</a>
      <a>About</a>
      <a>Contact</a>
    </nav>

    <h2>EMMANUEL OJIJI</h2>
    <p>SOCIAL MEDIA HERE</p>
  </div>
</header>

<section id="intro">
  <div id="intro-wrapper">
    <h1>I'm Lorem Ipsum — a Birmingham -based Visual Designer with a passion and a firm belief in considered and meaningful design.</h1>
  </div>
</section>

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

Align navigation items in the center of the navigation container

I've been grappling with aligning the content of a nav-item inside a nav container in my project. I'm unable to apply a margin on the ul>li items as it doesn't adjust properly on different resolutions. I've tried using percentages, b ...

Implementing PHP logic for adding a class to an HTML tag

I have a PHP file that handles the sending of my form. Everything is functioning correctly, but I need to make a small modification. When the message is successfully sent, I want PHP to dynamically add the "active" class to a specific div in my HTML. This ...

What are some alternative ways to create a text field with the same style as Material UI's without relying on the Material UI

With the power of HTML, CSS, and Reactjs I am aiming to create a functionality where the placeholder animates up to the border upon clicking on the text field An example can be seen with Material UI text field: https://material-ui.com/components/text-fie ...

How can you line up various form elements, like pickers, in a row using Material UI?

As someone new to material ui, I haven't come across a solution for my specific issue yet. While there are similar questions, none seem to address the problem of aligning different form field types. My observation is that the material ui date picker ...

Tips for adjusting the button spacing on a bootstrap navbar

As someone who doesn't work on front-end tasks frequently, I have encountered an issue while creating a navbar using Bootstrap. The problem arises when trying to add multiple buttons as it causes the spacing between them to appear strange. Does anyon ...

spacing between elements vertically

             I am struggling with a common issue and despite searching, I have not been able to find a solution that works for me. Here is my setup: <div id="wrapper">   <div class="content-area-top"></div>   <div clas ...

What is the best way to tally up words contained within an HTML element using Javascript?

When I type or paste words into a textarea, the total number of words will be displayed at the top. I have managed to count the words when using the element <textarea id="text"></text>. However, I am unable to count the total number of words ...

What causes HTML validator errors in all head meta-tags?

After running my HTML through a validator, I was shocked to discover around 80 errors even though the site appeared to be functioning properly. Here is an excerpt of the code: <!DOCTYPE html> <html lang="da-DK"> <head> <meta charset=" ...

Troubleshooting a unique CSS problem on an Android PhoneGap

We are currently working on an Android application using PhoneGap, HTML, CSS, and jQuery Mobile. After compiling the application in Eclipse, everything seems to work correctly. However, I am facing an issue where my CSS styles are not updating when recompi ...

What could be the reason why Bootstrap Reboot is not functioning properly in ReactJS

After struggling for days and attempting various solutions found online, I still can't seem to get the bootstrap reboot to work in my reactjs project. I even attempted adding it directly into my public/index.html file: <link rel="stylesheet" hre ...

The synchronization between the First Column Scroll and Table Filter Function is out of alignment

I haven't coded for a while and my knowledge of JavaScript is still in its early stages, so please excuse me if this question seems a bit naive. Essentially, I've created code that filters an HTML table based on the items in the first column. Th ...

Utilizing Ajax for Multiplication

Is there a way to dynamically calculate and display the total amount by multiplying the unit price with the quantity entered in the text box as it changes? <div> <label id="" name="price" class="unitprice"><?php echo "Price: <label ...

Tips on snapping pictures using an html5 camera without encountering security prompts

Is there a way to capture an image from a webpage without triggering security warnings? The webpage where I need webcam functionality cannot be switched to HTTPS protocol. I have already installed root certificates and made them trusted, but still no succ ...

Display a single item using Jquery when clicking on an element with the same class

I'm relatively new to JQuery/Javascript scripting and I'm having some difficulty. How can I display one item with the same class without affecting the other items? Here is my code: $(function() { $('.embedContainer > .myPosterImage& ...

What is the best character encoding for my website and how can I determine the right one to use?

What is the significance of specifying character encoding in an HTML page and how can I determine which type of character encoding is best for my website? In the past, I always defaulted to using UTF-8 for all HTML pages. However, is there a particular ra ...

JS instant Toggle (just one click) - initial value of toggled item

Can you explain why we have to click twice on this link (http://jsfiddle.net/xL8hyoye/4/): html: <a href="#" onclick="toggle_visibility('foo');">Click here to toggle visibility of element #foo</a> <div id="foo">This is foo< ...

When you hover over it, the text moves above the background content

I am looking to enhance my table by having the text expand and hover over the content with a white background. I have achieved the expand effect, but I am struggling to get the overflow to work properly. You can view the code in action on JsFiddle: JsFiddl ...

Updating the content with HTML and JavaScript

Hello everyone, I am currently working on a project to change the content of a div using JavaScript for educational purposes. Here is what I have done so far - <div id="navbar"> ... <ul> <li> <text onclick="getWordProcessing() ...

Tips for discovering the exact positions of child divs that are Nth siblings within a parent div

I have designed a new calendar interface with dynamic functionality. When the user clicks on any time slot displayed in IMAGE1, a span element is created dynamically to represent 8 hours starting from that clicked time. My question is, how can I access th ...