Trouble aligning CSS Nav Bar to the right with Flexbox

Having some trouble with implementing "flex" in CSS. My nav bar just won't move to the right no matter what I try. Spent hours adjusting margins, displays, and floats but it's stuck in the middle... Still learning, so any help is appreciated!

If you want a better visual, here's the link to my Codepen:

https://codepen.io/Saharalara/pen/pGyQWZ

The HTML code:

 <div id="page-wrapper">
 <header>
 <div class="logo">
  <img id="header-img" src="https://upload.wikimedia.org/" alt="Minnie Pic"> 
  <span id="logo-name">Minnie & Friends Inc</span>
 </div>
 <nav id="nav-bar">
  <ul id="nav-ul">
   <li class="nav-link"><a href="#stories">Stories</a><li>
   <li class="nav-link"><a href="#toys">Toys</a><li>
   <li class="nav-link"><a href="#subscribe">Subscribe</a></li>
 </ul>
</nav>
</header>
<body>
<container id="container1">
 <section>
  <h1 id="stories">Minnie & Friends Stories</h1>
   <p>
   <a href="#"> Here</a> you will find all of the best Minnie & Friends toys 
   and stories.
   Choose from a huge variety of stories and the happy gang as they go on 
   many adventures.
  </p>
   <section>
    <iframe id="video" height="180" src="https://www.youtube.com/watch? 
     v=crJ1CwD_TX0" frameborder="1" allowfullscreen ></iframe>
  </section>
<h2 id="toys">Minnie & Friends Toys</h2>
<p>
  <a href="#">Here</a> you will also find many of your favorite characters 
   to choose from and order to arrive at your doorstep to continue their 
   adventures with you.
</p>
<h3 id="subscribe">Subscribe to our newsletter</h3>
</section>
</container>
</body>

The Css

#page-wrapper {
 position: relative;
 color: black;
 margin: -9px;
 padding: 10px;
 border: 4px solid;
 border-radius: 3px;
 background-color: rgba(223, 42, 42, 0.20);
 padding: 10px 20px 20px 20px;
}

header {
 display: flex;
 font-size: 1.3em;
 margin-left: -10px;
 margin-right: -10px;
 background-image: url('https://cdn2.vectorstock.com/i/1000x1000/07/66/pink- 
 white-star-polka-dots-background-vector-7590766.jpg');
 opacity: 0.80;
}

#header-img {
 height: 120px;
}

#logo-name {
 font-size: 2em; 
 color: black;
}

h1,
h2,
h3 {
font-size: 2em;
}

//navigation bar

#nav-bar {
display: flex;
justify-content: flex-end; //**not working***
}

#nav-ul {
list-style-type: none;
display: flex;
}

nav li {
 padding: 4px;

}

//body

p {
 font-size: 1.2em;
}

#video {
 border: 5px solid;
 border-radius: 3px;
 color: pink;
}

Answer №1

Solution 1

To address this issue, a recommended approach is to manage layout at the level of the flex container (parent). This method is simple and explicit.

header {
...
  justify-content: space-between;
}

Solution 2

Alternatively, you can apply flex-grow: 1 to your #nav-bar.

#nav-bar {
  ...
  flex-grow: 1;
}

By setting flex-grow to 1, the element will expand to occupy the available space within its flex container.

Solution 3

If not using flexbox, another approach would be to use a left auto margin on the #nav-bar to push it all the way to the right.

#nav-bar {
  ...
  margin-left: auto;
}

Answer №2

When the header has the display: flex property, its children behave like flex items.

If you want to shift the entire .nav-bar to the right, you need to apply margin-left: auto to the .nav-bar element. This will push the element to the right side of the container.

Essentially, by giving .nav-bar a margin to the left without causing it to break onto a new line, the element will be shifted to the right.

Answer №3

To align the elements within the Header container, simply add justify-content: space-between; which will push the elements to the left and right. Another method is to apply position: absolute; and right: 0; directly to the nav-bar container, but the former approach is more elegant.

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

Update the hover effect specifically for mobile devices

.hovereffect a.info,.hovereffect h2{text-transform:uppercase;color:#fff} .hovereffect{float:left;position:relative;cursor:default} .hovereffect .overlay{position:absolute;top:0;left:0;opacity:0;background-color:none;-webkit-transition:all .4s ease-in-out ...

Steps to reveal a child element when the parent is set to overflow:hidden

There seems to be an issue with a child element having overflow:visible; while the parent element has overflow:hidden;. The height of the child element exceeds that of the parent element. Why is the child element hidden even when its overflow property is ...

Unusual blue outline spotted on Firefox

Could you please review the following code snippet: http://www.jsfiddle.net/tt13/5CxPr/21 In Firefox, there is a strange blue border that appears when selecting multiple rows by pressing the ctrl button. However, this issue does not occur in Chrome. Thi ...

Conceal element with transparent overlay without affecting the background

Looking to add animation to a snippet where the login menu rolls out? Consider two methods: adjusting the left position of the login menu or using another div on top to slowly reveal the login menu after moving it. The challenge lies in maintaining trans ...

Struggling to make the height attribute work in Bootstrap 4 alpha

I'm currently in the process of learning Bootstrap, and I've encountered an issue with the height attribute in version 4 (for example: "row h-25") not functioning properly. I attempted to add another CSS rule that sets the height of "container-f ...

Is there a more efficient approach to applying a basic function to multiple elements within a DIV and having it activate only upon a click event using jQuery?

This solution works, but I am looking for a more professional approach in order to adhere to the principle of "don't repeat yourself." $("document").ready(function(){ $(".child").each(function(index){ $(this).click(func ...

The "pointer-events: none" command is not functioning as expected in Chrome browser

Here's an interesting situation. I have an arrow that users click on to navigate to the next page in a photo gallery. The arrow image is wrapped in an anchor tag with the href attribute pointing to the next page, hardcoded into the HTML. <a href=" ...

Issue with content not properly aligning next to the vertical navigation bar

I've set my vertical navigation to be 10% width and my content to be 90%, but for some reason, I can't get them to align properly next to each other. I've experimented with float: left and display: inline-block, but so far nothing has worked ...

Update the color of the menu ID

Feeling frustrated trying to figure out how to modify this <nav class="mainnav "> <a id="top"/> <div class="navwrap"> <ul id="menu-top" class="menu"> <li id="menu-item-70" class="menu-item menu-item-type-custom menu-item-object- ...

Ensure consistent alignment of the Bootstrap pagination control

I have created a bootstrap pager using the code below. <style> .navigation-bar .pagination { margin-top: 0; } </style> <div class="navigation-bar"> <span class="pull-left"><strong>Total record(s) 1000</strong></s ...

Code to conceal navigation bar...positioned neither at the top nor at the bottom of the scroll

As a graphic designer with some coding knowledge, I'm struggling with a particular issue... I have a traditional navigation bar that only appears when the user scrolls down a bit, which I achieved through JavaScript. However, I want this to occur onl ...

Application of id missing on all buttons in Bootstrap tour template

I'm having an issue with applying a new id to the next button in a Bootstrap tour template. I added the id to the button, but it only seems to work for the first stage and not the subsequent ones. Can anyone provide insight into why this might be happ ...

Struggling to properly position HTML5 Slider result

Apologies for any confusion in how I'm presenting this question. What I am aiming for is to have the "output" display right next to the slider, rather than below it. To see the issue: visit alainbruno.nl/form.html As I am just beginning with HTML5, ...

Is there a way to stop text from being selected when double clicking?

#btnLeft{ width:40px; padding:2px 5px 2px 5px; background-color: #20a0a0; border: thin solid #099; border-radius:7px; text-align:center; font-family:"Futura Md BT"; font-size:large; color:#FFF; } This particular code sn ...

How to Position Two Child Divs in the Center of a Parent Div

I have been searching extensively, but I can't seem to find a solution to my problem. Let me explain with a simple example. I have two child div elements within a parent div, and I want them to be centered horizontally inside the parent div. Here is t ...

Element obscured by dropdown content now clearly visible thanks to dropdown adjustment

Something strange is happening here - the Add question div shouldn't be visible. It's somehow appearing behind the dropdown content. I've attempted to adjust the z-index of the Add Question div, setting it to a lower number than the dropdown ...

Using jQuery to smoothly scroll to a specific class name

I am faced with an HTML code snippet that looks like this: <div data-stored="storenow" data-save="save" class="saveIcon" data-unique="game">Save</div> My intention is to use jQuery to scroll to the game number 456 as shown below. var contain ...

Achieve the effect of making the Bootstrap JS Collapse text bold after it has been

Is there a way to make Bootstrap JS Collapse text Bold after it has been clicked on? <tr data-toggle="collapse" data-target="#demo8" class="accordion-toggle"> <td> <div class="fa ...

Include a border around the entire tooltip

I'm having trouble adding a border to my tooltip that follows the outer lines of the entire tooltip. Currently, I've only been able to add a border to the upper part of the tooltip, which overlaps with the arrow section and isn't the desired ...

The filename is not displaying in the file input field within the bootstrap framework

Below is the HTML code I am working with: <head> <link rel="stylesheet" href="./Depd/bootstrap.min.css"> </head> <body> <div class="input-group"> <div class="custom-file"> <input type="file" class="c ...