Ways to arrange Bootstrap 4 navigation bar links side by side and align them to the right

I am facing a challenge in designing a navigation bar where the brand logo is on the left, while the navigation links are placed on the right. Currently, the links appear stacked on top of each other instead of side by side:

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
  <a class="navbar-brand" href="#">Peek Solutions</a>
  <div class="navbar-nav justify-content-end">
    <a class="nav-item nav-link" href="#">About</a>
    <a class="nav-item nav-link" href="#">Services</a>
    <a class="nav-item nav-link" href="#">Contact</a>
  </div>
</nav>

I've attempted to align them to the right using .justify-content-end as per the documentation at https://getbootstrap.com/docs/4.0/components/navs/, but the result doesn't meet my expectations. (I also investigated the Bootstrap 4 source code without identifying the issue quickly).

Update

If I use the snippet below,

<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
  <a class="navbar-brand" href="#">Peek Solutions</a>
  <div class="navbar justify-content-end">
    <a class="nav-item nav-link" href="#">About</a>
    <a class="nav-item nav-link" href="#">Services</a>
    <a class="nav-item nav-link" href="#">Contact</a>
  </div>
</nav>

then the layout is correct (refer to https://jsfiddle.net/qxdsam8t/10/) but the link colors revert to default blue rather than the light gray theme color. Adding the class navbar-nav to the inner div restores the theme colors but results in the display being 'stacked' like initially observed. It appears that these properties are linked together; how can I retain the colors without stacking?

Answer №1

The stacking of menu items is controlled by the .navbar-expand-lg class, which causes xs/sm/md viewports to stack the menu items when the collapsed menu view (hamburger menu) is activated. You can modify this behavior by using the .navbar-expand-sm class instead, as demonstrated in the code snippet below.

To align the navigation elements to the right, you can add .justify-content-between to the <nav> element, or use .ml-auto within the .navbar-nav <div>, or combine .w-100 with .justify-content-end for full-width alignment.

For a complete navbar setup, wrap the .navbar-nav element with:

<div class="collapse navbar-collapse"></div>
and include a toggler button following Bootstrap's documentation at: https://getbootstrap.com/docs/4.0/components/navbar/#toggler

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<nav class="navbar navbar-expand-sm navbar-dark bg-dark justify-content-between">
  <a class="navbar-brand" href="#">Peek Solutions</a>
  <div class="navbar-nav justify-content-end">
    <a class="nav-item nav-link" href="#">About</a>
    <a class="nav-item nav-link" href="#">Services</a>
    <a class="nav-item nav-link" href="#">Contact</a>
  </div>
</nav>

Answer №2

The resource you provided suggests using either an <ul> element or a <nav> element.

Utilizing classes throughout allows for great flexibility in your markup. You can opt to use <ul> elements as shown above, or customize your own navigation with a <nav> element. By applying display: flex to the .nav class, the navigation links will function similarly to navigation items, eliminating the need for additional markup.

<nav class="nav">
  <a class="nav-link" href="#">Link</a>
  <a class="nav-link" href="#">Link</a>
  <a class="nav-link" href="#">Link</a>
  <a class="nav-link" href="#">Link</a>
</nav>

Answer №3

Your current setup is not meeting the standard. I would recommend referring to the bootstrap documentation here for guidance. In order to align your navigation to the right, you can utilize the "ml-auto" bootstrap class within the ul element of your nav. Remember, Bootstrap 4 heavily focuses on flexbox layout, so HTML markup precision is crucial.

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

What is the method for activating scrolling in an inner div instead of the browser window?

In the code snippet provided, I am encountering an issue where the browser window scrolls instead of the specified div with overflow-y: scroll when minimizing the browser window. How can I ensure that scrolling occurs within the div itself without making ...

Eliminating Redundant Styles in Specific CSS Files with Gulp, When They Already Exist in a Shared CSS File

Is there a way to eliminate redundant styles in individual css files that are already defined in a common.css file? common.css contains the following styles. .nav { background-color: red; } home.css includes the following styles. .nav { backgroun ...

Tips for enlarging an image by tapping on it in handlebars

I currently have the handlebars template engine integrated with node.js, and I'm facing an issue where thumbnail images from the database are displaying at a fixed width and height of 70. Is there a way to enable users to click on these images in orde ...

The color of the button in HTML5 will remain constant and not shift

I have several buttons that function like radio buttons - only one can be selected at a time: <button id="btn-bronze" name="btn-bronze" type="button" class="blue-selected">Bronze</button> <button id="btn-silver" name="btn-silver" type="butt ...

Can the border become fainter and then more pronounced when hovered over?

Does anyone know of a simple method to create a fading effect for a CSS border-bottom when hovering over an image, and then have the border fade out once the hover is removed? <div class="hover"> <img src="images/ex.jpg"/> </div> ...

What is the best way to add an image to a question or answer in a JavaScript quiz?

I'm in the process of designing a quiz that revolves around using images as questions or answer choices. For example, presenting an image of a cat and asking the user to select the corresponding 'cat' button. Unfortunately, my attempts to i ...

Issues with the performance of input range sliders in iOS Safari is causing frustration

I recently developed a basic range slider component for an application built with NextJS. This component utilizes an <input type="range"> element. While the range slider functions properly on Desktop and Android devices, I encountered sign ...

What type of layout engine is used to determine the positioning of HTML elements on a webpage?

I am currently working on a web data classification project and I'm interested in obtaining the coordinates of HTML elements as they are displayed in a web browser, without considering any CSS or JavaScript present on the webpage. I primarily use C++ ...

Tips on steering clear of the issue of "Automatic grid alignment" when working with Bootstrap?

I'm attempting to achieve something along the lines of, |--------------Empty space---------------|-----first column(aligned to the right)-----| |---logo---||------Empty space-------|----second column(aligned to the right)---- However, it is automat ...

Using Bootstrap 4 to validate credit card information with personalized error messages

How can I apply Bootstrap 4 validation to ensure that the credit card number contains only numbers, is 16 digits long, and display appropriate messages based on user input? I want to display three distinct messages: 1. When no input is provided, show "Cre ...

CSS behaving strangely with the height property

I've run into a specific issue with a div element that contains multiple buttons. I am trying to adjust the height and position of one button so that it takes up space above the other buttons, but every time I make changes to its height and position, ...

Scroll to the top of jQuery DataTables when clicking pages in a random order from the bottom to the top and

In my current project, I am working with jQuery Datatables and I need to customize its default behavior. Currently, when I navigate to page 61, the page scroll remains at the bottom of the page. However, if I then click on a lower page number like 60, th ...

What is the best way to create a button with a background image that is also transparent?

Is it possible to set a background image for a button in HTML with a transparent background using CSS? Here is the HTML code I currently have: <button style="background-image: url('../images/example.png');"></button> ...

What is the best way to show a default option in a loop while hiding the others without causing an infinite loop to occur?

Currently, I am working on a way to have a default item displayed in a loop while hiding the rest. I would like the hidden items to only display when called upon. The current issue I am facing is that the loop seems to be infinite. I am open to suggestio ...

Retrieve the content inside the HTML body of a specific <link> element using jQuery based on its unique ID

I currently have the following code snippet in the header of my website <link rel="stylesheet" id="swp-google-font-headline-css" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro&ver=4.5.3" type="text/css" media="all"> My goal is to ...

Having trouble with loading a 3D gltf model on A-frame.io?

I'm experiencing some difficulties when attempting to load a gltf model onto an a-frame. Unfortunately, the model does not appear to be loading properly. Here is the code snippet in question: <html> <head> <title>Tree model</t ...

JavaFX text label does not adjust its font according to the CSS file

In my FXML file, the structure is as follows: ... <BorderPane id="header"> <right> <Label styleClass="title" text="A Label" /> </right> </BorderPane> ... I also have a CSS file with the following content: #h ...

Current page with animated CSS3 menus

I found a cool menu example on this webpage: http://tympanus.net/Tutorials/CreativeCSS3AnimationMenus/index10.html. I want to modify it so that the current page's menu item has a different color from the others. For example, when I'm on the home ...

Is there a way to retrieve data from two tables by linking the foreign key in one table to the primary key in another table?

I'm currently working on a menu item and have run into an issue with the information being displayed for the second level submenu (letters). Despite trying various join methods, I am unable to get the correct data - it either pulls only one of my subm ...

Tips on utilizing radio buttons in place of check boxes

How can I create a dynamic menu with multiple categories and submenus that toggle open and close when clicked? I currently have a menu structure with "What's New" and "Categories", and within the "Categories" section, I have three main categories each ...