Aligning the initial item to the left and the rest to the right in a list using flexbox styling

I've been working hard to troubleshoot the header on my personal webpage. As it stands now, here is how it looks:

https://i.sstatic.net/C4zSQ.png

Currently, there's a simple paragraph with "ZH" on the left and three list items next to the logo. What I'm aiming for is to float these three list items to the right, similar to this:

https://i.sstatic.net/MbkN4.png

Below is the HTML code generated:

<ul class="nav">
        <a class="navbar-brand" href="http://localhost:8888/wordpress/">
            <p>ZH</p>
        </a>
        <li class="nav-item">
            <a class="btn btn-lg nav-link" href="http://localhost:8888/wordpress/about/" role="button">About</a>
        </li>
        <li class="nav-item">
            <a class="btn btn-lg nav-link" href="http://localhost:8888/wordpress/projects/" role="button">Projects</a>
        </li>
        <li class="nav-item">
            <a class="btn btn-lg nav-link" target="_blank" href="http://localhost:8888/wordpress/wp-content/themes/zachary_hughes_wordpress/assets/hughes_zachary_cv.pdf" role="button">Resume</a>
        </li>
</ul>

And here is the CSS for the ul.nav:

.nav {
    display: -ms-flexbox;
    display: flex;
    -ms-flex-wrap: wrap;
    flex-wrap: wrap;
    padding-left: 0;
    margin-bottom: 0;
    list-style: none;
}

If possible, I would like to achieve this without altering the HTML markup. Any guidance or assistance would be highly appreciated.

Answer №1

To achieve this layout, you can apply the CSS property justify-content: space-between to the .nav class. By wrapping the li elements within two flex-items, they will be positioned as far apart as possible:

ul {
  list-style: none;
}

.nav {
  display: -ms-flexbox;
  display: flex;
  justify-content: space-between; /* Aligns items with maximum space between them */
  -ms-flex-wrap: wrap;
  flex-wrap: wrap;
  padding-left: 0;
  margin-bottom: 0;
  list-style: none;
}

.flex-container {
  display: flex;
}

.flex-container > li {
  margin: 0 5px; /* Adjust spacing */
}
<div class="nav">
  <a class="navbar-brand" href="http://localhost:8888/wordpress/">
    <p>ZH</p>
  </a>
  <ul class="flex-container">
    <li class="nav-item">
      <a class="btn btn-lg nav-link" href="http://localhost:8888/wordpress/about/" role="button">About</a>
    </li>
    <li class="nav-item">
      <a class="btn btn-lg nav-link" href="http://localhost:8888/wordpress/projects/" role="button">Projects</a>
    </li>
    <li class="nav-item">
      <a class="btn btn-lg nav-link" target="_blank" href="http://localhost:8888/wordpress/wp-content/themes/zachary_hughes_wordpress/assets/hughes_zachary_cv.pdf" role="button">Resume</a>
    </li>
  </ul>
</div>

I've wrapped the li elements in a ul element and used a div for the outer flex-container.

Answer №2

Utilizing solely native Bootstrap classes, you can achieve all of this WITHOUT the need for any custom CSS. This showcases the versatility of Bootstrap 4.

In this scenario, simply adding the ml-auto class to the ul element is sufficient. It's that straightforward!

However, there are shortcomings in other sections of your code. Deviating from the structure and elements provided by Bootstrap 4 forces reliance on custom CSS, undermining the essence of using Bootstrap. The magic of Bootstrap 4 lies in its capability to handle almost everything without requiring direct manipulation of CSS!

I've restructured your code to deliver a neat and sophisticated solution utilizing only Bootstrap 4 classes and NO custom CSS. For instance, the m-0 class removes unnecessary margins on your brand. If a margin is desired, altering 0 to 1 or 2 will suffice. Employ mx-1 for horizontal margins exclusively, and my-1 for vertical margins, among others.

So here's the refined code as promised:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" integrity="sha384-Zug+QiDoJOrZ5t4lssLdxGhVrurbmBWopoEl+M6BdEfwnCJZtKxi1KgxUyJq13dy" crossorigin="anonymous">


<nav class="navbar navbar-expand-lg navbar-light bg-light">
   <div class="container">
       <a class="navbar-brand" href="http://localhost:8888/wordpress/">
           <p class="m-0">ZH</p>
       </a>
       <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
           <span class="navbar-toggler-icon"></span>
       </button>

       <div class="collapse navbar-collapse" id="navbarSupportedContent">
           <ul class="navbar-nav ml-auto">
               <li class="nav-item">
                   <a class="btn btn-lg nav-link" href="http://localhost:8888/wordpress/about/" role="button">About</a>
               </li>
               <li class="nav-item">
                   <a class="btn btn-lg nav-link" href="http://localhost:8888/wordpress/projects/" role="button">Projects</a>
               </li>
               <li class="nav-item">
                   <a class="btn btn-lg nav-link" target="_blank" href="http://localhost:8888/wordpress/wp-content/themes/zachary_hughes_wordpress/assets/hughes_zachary_cv.pdf" role="button">Resume</a>
               </li>
           </ul>
       </div>
   </div>
</nav>


<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/js/bootstrap.min.js" integrity="sha384-a5N7Y/aK3qNeh15eJKGWxsqtnX/wWdSZSKp+81YjTmS15nvnvxKHuzaWwXHDli+4" crossorigin="anonymous"></script>

Answer №3

This example demonstrates the use of flex and justify-content. For more information, visit this site: flexBox

<html>
    <head>
    <title>My Favorite Films</title>
<style>
#header{
display: flex;
justify-content: flex-end;
      background-color: pink;
}

h1{
width: 125px;
display: flex;
background-color: antiquewhite;
}
</style>
    </head>

    <body>
        <div id="container">
            <div id="header">
                <h1 class="home-page-header">My Favorite Films</h1>
            </div>
        </div>
    </body>
</html>

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

Combining an image and text within a single line in a div container

My goal is to have the envelope image in the same line as the text on the right side: View my CodePen example here .ico{ float:left; margin-right: 5px; vertical-align: middle; I can't seem to get it right - what mistake am I making? ...

Determination of an arc trajectory using JavaScript

I have been trying to figure out how to replicate this effect. The Red arrowhead remains stationary while the background features a curved path. Currently, I am adjusting the background position to give the illusion of the arrowhead moving along the curved ...

Footer div is being covered by the page

I am currently working on a website built with "WordPress", and I have implemented a mobile footer plugin that is designed to only appear on mobile devices. However, I am encountering an issue where the page content overlaps the footer on phones. I attemp ...

Tips for adding an additional div inside a navigation container, evenly spacing objects, and aligning the search bar to the right

I'm currently working on designing a simple navigation bar but I'm facing difficulties in aligning the elements correctly. See my progress here: https://jsfiddle.net/zigzag/bL1jxfax/ Here are my objectives: 1) Ensure that the navigation bar rea ...

When it comes to the CSS `:visited` pseudo-class, I have

I'm having trouble changing the color of the anchor tag and blurring the image after visiting the link. Despite my CSS code, only the color is changing and the image remains unchanged. Here's my CSS code: <style> .image123{ paddin ...

Deselect a checkbox by selecting a radio button with just Javascript code

Hey there! I'm currently delving into the world of pure JavaScript and could really use some guidance on a particular issue. Here's what I'm aiming to accomplish: I'd like to design a checkbox that triggers the opening of a window whe ...

Steps for displaying a div when clicking a link in the navigation

Hello there, I'm from the Netherlands so please excuse any mistakes in my English. I will do my best to explain my question clearly.... Objective The goal is to have a navigation bar where each item, when clicked on, will display a div with content ...

What is the most effective way to implement a single modal throughout my web application without having to duplicate HTML code on each page?

After realizing I was repetitively adding the same div to every page for a modal dialog, I decided to place a single div in the site.master page and call it when needed. This method worked fine until I began implementing ajax with partial page updates. H ...

Elements displaying outside of block 'a' tag

I have a set of <a> elements that are styled with display:block. These contain various nested <divs>, for example: <a href="#" class="sliderframe"> <div class="container-caption"> <div class="slider ...

Tips for making an appended element match the height of an image

After embedding the div .soonOverlay into specific .smallCatalogBlock's, I am facing difficulty in adjusting the height of soonOverlay to match only the height of the img within smallCatalogBlock. Currently, its height extends throughout the entire co ...

Is it possible to create a single CSS style class that can be used for various section tiles on a website?

I am currently working on a website that includes different section titles such as About, Projects, Contact, and more. To make these titles stand out against the background image, I have decided to fill them with white. The text color of the titles is dar ...

Troubleshooting a CSS overflow problem in the main slider

While exploring the website , I encountered a minor issue. When I added "overflow: hidden" to the featured slider div, the images appeared stacked if JavaScript was enabled or the user's internet connection was slow. However, a new problem has arisen ...

Guide: "Adding markers to user-uploaded images - A step-by-step tutorial"

I'm struggling to create a website that allows users to upload images and use specific tools. However, I am facing an issue where the marker I want to add is appearing all over the webpage instead of just on the image itself. How can I confine it to o ...

Managing the height of a hero section with TailwindCSS and React.js

Embarking on my first website project using React and Tailwind has been an exciting learning experience. My vision is to showcase a Hero Section prominently at the top, with a Nav bar situated elegantly at the bottom of the screen. To bring this concept ...

Is there a way to use jQuery to verify if an LI element contains a parent LI element?

Struggling with customizing the comments section of a WordPress theme? Utilizing jQuery for styling can be tricky, especially when dealing with nested UL elements like admin comments. The challenge lies in traversing the DOM structure to target specific el ...

We encountered an issue while trying to bootstrap react with the './node_modules/bootstrap/dist/js/bootstrap.min.js' module. The error was a result of the module not being able

Error : I encountered a problem when trying to add a navbar using Bootstrap. Without importing the jQuery part, the navbar works but the collapsing feature does not work. However, when I add the jQuery part, I receive the following error: ./node_modules ...

Troubleshooting problem with divs overlapping in Internet Explorer 7

Check out this webpage: http://jsfiddle.net/aJNAw/1/ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta ...

What is the best way to center a div vertically inside another div?

Aligning a div element horizontally within another div element is possible by using the margin: 0 auto; property as long as both elements have a specified width other than auto. However, this method does not work for vertical alignment. Is there a way to ...

What is the best way to include multiple font styles for different tags?

While working on a website project, the client requested to use Roboto as the main font (by Google). Different styles of Roboto such as Roboto Thin or Roboto Light were used for various elements on the page. However, I utilized the @font-face selector to e ...

Identify support for the :first-child pseudo-class

Is there a way to determine with JavaScript whether the browser is compatible with the CSS :first-child selector? ...