"Having issues with anchor tags not aligning to the center correctly

Currently, I am working on creating some pages for the Free Code Camp CSS challenges.

However, I am facing an issue with my tags not centering properly when I collapse the grid upon @media activation. They tend to drift slightly to the right side.

If you would like to view the complete pen, click on this link:

https://codepen.io/alioshr/pen/KKPBPMr

I have attempted various solutions such as using inline-block display and justify-self to center, but the issue persists.

<html>
<header id="header">
  <figure>
    <img id="header-img"src="https://cdn.shopify.com/s/files/1/0866/9666/files/checkout_logo_4_1024x2_37df55d3-8344-46fb-8913-ea64de22f564_500x.png?v=1509363172">
  </figure>
  <div>
  <h1>Doce Meliponicultura</h1>
      <p>Reconnect with Mother-nature</p>
  </div>
    <nav id="nav-bar">
      <ul>
      <li><a href="#top-features" class="nav-link">Top Features</a></li>
      <li><a href="#products" class="nav-link">Products</a></li>
      <li><a href="#social" class="nav-link">Social</a></li>
      </ul>
  </nav>
</header>
</html>

<style>
a {text-decoration: none; display: inline-block;}

#header {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  background: gold;
  align-items: center;
  justify-self: center;
  position: fixed;
  top: 0;
  left:0;
  width: 100%;
  z-index: 99;
}

@media (max-width: 600px) {
  #header {grid-template-areas: "title" "logo" "nav-bar";}
  #header > figure {grid-area: logo;}
  #header > div {grid-area: title;}
  #header > nav {grid-area: nav-bar;}
}

#header > figure {
  justify-self: start;
  align-self: center;
  display: relative;
  max-height: 140px;
}

@media (max-width: 600px) {
  #header > figure {
    justify-self: center;
  }
}

#header > nav {
  justify-self: end;
}

@media (max-width: 600px) {
  #header > nav {
    justify-self: center;
  }
}
</style>

Answer №1

To address the problem, simply insert the following CSS within your @media (max-width: 600px) section:

#header > nav > ul{
     padding: 0px;
}

This solution should resolve the issue you are experiencing.

Answer №2

In addition to what has been shared, I would like to contribute some more information on setting up Flexbox. To simplify the design, I replaced all CSS Grid syntax with Flexbox properties. While I'm not certain about your specific requirements, this example should give you a basic understanding of how to implement it effectively.

#header {
  display: flex;
  background: gold;
  align-items: center;
  justify-content: space-around;
  width: 100%;
}

@media (max-width: 600px) {
  #header {
     flex-direction: column;
  }
}

#header>figure {
  justify-self: flex-start;
  align-self: center;
  max-height: 140px;
}

@media (max-width: 600px) {
  #header>figure {
    justify-self: center;
  }
}

#header>nav {
  justify-self: flex-end;
}

@media (max-width: 600px) {
  #header>nav {
    justify-self: center;
  }
}

You can view my implementation on this fiddle (the codepen link provided did not work for me).

If you're interested in learning more about Flexbox and its capabilities, I recommend checking out A Complete Guide to Flexbox

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

The banner <div> appears in Chrome but is not visible in IE9

I'm facing an issue with a banner div under my navigation. It seems to show up in Google Chrome but doesn't appear in IE9. Below is the HTML and CSS code: HTML: <div class="content"> <div class="slide-banner"></div> ...

How to Target HTML Tags Locally using CSS Modules in Next.js?

I am looking to implement smooth scrolling specifically on one page in my Next.js application, such as my blog. Instead of applying it to the entire site through the globals.css file, I need a way to inject scroll-behavior: smooth; into the html tag only f ...

Change the color of the navbar after scrolling past 70% of the viewport height using

I'm exploring the idea of changing the color scheme of my navbar when it reaches 70% of the viewport height (vh). My current thought is to use an @media query to apply an invert filter to the nav container and logo. The syntax might look something li ...

Out of the blue div, could you lend your assistance?

I have a code that displays 5 random images with corresponding text. My challenge is that I want to separate the text into another div so that I can add another function to it, while still keeping the images random with their corresponding text. <scr ...

What is the method for retrieving the font size of elements in the native view using appium?

Can the font size of text in the native view be retrieved using appium? I am aware of using driver.findElement(By.id("by-id")).getCssValue("font-size"); for web views. Is there a similar method for native views? ...

starter tablets

Currently, I am using Bootstrap pills for my navigation bar. However, I am experiencing an issue where the active pill is not displaying with a blue background color as it should according to the default Bootstrap styling. Here is my code: <div style=" ...

What are the steps to recreate the layout of my image using HTML?

What's the best way to create a layout in HTML that expands to fit the entire screen? ...

Tips for creating a full-screen background image using HTML and CSS

I am looking to achieve a full-screen background image using HTML and CSS. I have configured all the necessary properties for the background image. Here is where my background image is located: [![enter image description here][1]][1] I have attempted to ...

Expandable sidebar using responsive Twitter Bootstrap

I am in search of a CSS solution to implement a button that can toggle a sidebar on and off using twitter bootstrap. Is there a specific term for those small icons seen on webpages that resemble pull tabs when the sidebar is closed and then move along wit ...

Creating personalized CSS styles specifically designed for iPad Mini 2 with a retina display and optimizing it for different screen/viewport settings

Struggling to assign different colors for various screen sizes, I'm facing a perplexing issue with the iPad Mini 2's Retina Display. Despite its pixel resolution, it doesn't conform to the specified rules and I'm left wondering why. Th ...

Guide on dragging and dropping without losing the item, allowing for continuous drag and drop functionality

function allowDrop(ev) { ev.preventDefault(); } function drag(ev) { ev.dataTransfer.setData("text", ev.target.id); } function drop(ev) { ev.preventDefault(); var data = ev.dataTransfer.getData("text"); ev.target.appendChild(document.getElementB ...

How can CSS grids adapt to different screen sizes and resolutions?

Currently experimenting with Unsemantic for responsive grids in CSS, but struggling to comprehend how it can effectively handle various resolutions like 400, 768, and 1024 with just two classes: grid-XX and mobile-grid-XX. The Unsemantic CSS files contain ...

Skipping every third index in a JQuery .each loop after removing an element

I have a project where I need to display elements in rows of 4, and when an element is deleted, I want the remaining elements to shift up. To achieve this, I am currently assigning a class "last" to every fourth item after a deletion and inserting a spacer ...

When the cursor hovers over an item, I would like to enlarge its width while simultaneously shrinking the width of two additional items

Here is a section that I have created, you can view the mockup here. The section is divided into 3 parts, each taking up around 33.3% of the width and floated. My goal is to make it so that when a specific element, like .col-one in my example, is hovered ...

Issues with HTML5 links not functioning as intended

I am currently working with HTML5 and have been following a tutorial which can be found here- https://www.youtube.com/watch?v=kDyJN7qQETA Below is the code snippet I have been working on- <!DOCTYPE html> <html lang="en"> <head> <met ...

Discovering the point of exit for a mouse from an image

Visit this website to see the effect in action: I am curious about how the image scrolls into and out of the direction where the mouse enters and leaves. Can you explain how this is achieved? ...

Arranging two div elements side by side with spacing between them using CSS

Can anyone help me with aligning two divs side by side using CSS? I attempted a few methods on my own but seem to be stuck. Any suggestions would be greatly appreciated! CSS: .posts{ display: flex; flex-direction: row; } .posts .col-md-6{ pa ...

How to centrally align header elements in Bootstrap 4 on mobile devices?

In this setup using Bootstrap 4, I have a header structure as follows: <div class="container"> <div class="row"> <div class="col-md-6"> <div class="navbar-brand"><img src="..."></div> </div> <div class="col-m ...

Achieving and accessing multiple left panels in Jquery/CSS and automatically adjusting the main content container

Delving deeper into advanced CSS and JQuery for a unique layout, I've hit a roadblock. It seems like I might be overlooking something obvious or this could require more intricate scripting to achieve. Despite days of searching, I haven't come acr ...

Is the JavaScript click event ineffective for <select> <option>s?

It seems like there's a problem with the code below that is causing it to not work as expected. I'm trying to figure out why the onclick events on the <option>s are not triggering, but everything else seems fine. function displayInput() ...