Arrange elements in different directions to accommodate smaller screens

I need help aligning my brand and the links on opposite sides (brand on left, other links on the right) for mobile view. The issue I'm facing is that the links on the right end up moving to the next line. Any suggestions or advice would be greatly appreciated. My website is built using Bootstrap 5. Below is a snippet of the code:

<html>
    <head>
    <link href="https://cdn.jsdelivr.net/npm/your-css-file.css" rel="stylesheet">

    <script src="https://cdn.jsdelivr.net/npm/@popperjs/popper.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap/dist/js/bootstrap.min.js"></script>
    </head>
    <body>
        <div class="container">
            <div class="row">
                <nav class="nav-navbar navbar-expand-lg navbar-light">                    
                    <div class="col border border-danger">
                        <div class="d-sm-flex p-2 bd-highlight">
                            <button
                            class="navbar-toggler align-items-sm-start"
                            type="button"
                            data-bs-toggle="collapse"
                            data-bs-target="#navbarSupportedContent"
                            aria-controls="navbarSupportedContent"
                            aria-expanded="false"
                            aria-label="Toggle navigation"
                            >
                                <span class="navbar-toggler-icon"></span>
                            </button>
                            <a class="navbar-brand" href="#">brand name</a>            
                            <div class="d-flex flex-sm-row-reverse">
                                <div class="p-2 bd-highlight">Search</div>
                                <div class="p-2 bd-highlight">cart</div>
                                <div class="p-2 bd-highlight">Login</div>
                                <div class="p-2 bd-highlight">Sign up</div>
                            </div> 
                        </div>            
                    </div>
                    <div class="collapse navbar-collapse" id="navbarSupportedContent">
                        <ul class="navbar-nav flex-column">
                            <li class="nav-item">
                                <a class="nav-link active" aria-current="page" href="#"
                                    >NEW ITEMS</a
                                >
                            </li>
                            <li class="nav-item">
                                <a class="nav-link" href="#">BRANDS</a>
                            </li>
                            <li class="nav-item">
                                <a class="nav-link" href="#">CURATED PIECES</a>
                            </li>
                            <li class="nav-item">
                                <a class="nav-link" href="#">TOP ITEMS</a>
                            </li>
                            <li class="nav-item">
                                <a class="nav-link" href="#">FEATURED</a>
                            </li>
                        </ul>
                    </div>                
                </nav>
            </div>    
        </div>
    </body>
</html>

Answer №1

Items are moving to the next line at smaller breakpoints because of the use of .d-sm-flex. The flex display is only activated for small and larger screens.

To ensure consistent flex behavior across all screen sizes, use .d-flex.
https://getbootstrap.com/docs/5.1/utilities/flex/#enable-flex-behaviors

Your code also has a few corrections that have been addressed in the snippet provided below:

<html>

<head>
  <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1f7d70706b6c6b6d7e6f5f2a312f311d">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">

</head>

<body>
  <div class="container">
    <div class="row">
      <nav class="nav-navbar navbar-expand-lg navbar-light col">
        <div class="border border-danger">
          <div class="d-flex p-2 bd-highlight">
            <button class="navbar-toggler align-items-sm-start" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
                                <span class="navbar-toggler-icon"></span>
                            </button>
            <a class="navbar-brand" href="#">brand name</a>
            <div class="d-flex flex-sm-row-reverse">
              <div class="p-2 bd-highlight">Search</div>
              <div class="p-2 bd-highlight">cart</div>
              <div class="p-2 bd-highlight">Login</div>
              <div class="p-2 bd-highlight">Sign up</div>
            </div>
          </div>
        </div>
        <div class="collapse navbar-collapse" id="navbarSupportedContent">
          <ul class="navbar-nav flex-column">
            <li class="nav-item">
              <a class="nav-link active" aria-current="page" href="#">NEW ITEMS</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="#">BRANDS</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="#">CURATED PIECES</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="#">TOP ITEMS</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="#">FEATURED</a>
            </li>
          </ul>
        </div>
      </nav>
    </div>
  </div>

  <script src="https://cdn.jsdelivr.net/npm/@popperjs/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="284b475a4d681a0611061a">[email protected]</a>/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="70121f1f04030402110030455e405e42">[email protected]</a>/dist/js/bootstrap.min.js" integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous"></script>
</body>

</html>

Answer №2

If you're utilizing Bootstrap 5, head over to https://getbootstrap.com/ and utilize the top navigation bar to find your way to https://getbootstrap.com/docs/5.1/examples/. Within that section, there's a sample page titled "Headers", accessible here: https://getbootstrap.com/docs/5.1/examples/headers/.

These header examples can serve as valuable references for structuring your code.

Another useful resource is the Bootstrap 5 navigation bar component page located here: https://getbootstrap.com/docs/5.1/components/navbar/

This page showcases various examples that may spark some inspiration for your project.

If you're just starting out with HTML, CSS, and/or Bootstrap, I recommend starting with one of their predefined examples/templates and customizing it to fit your needs, rather than starting from scratch.

Additionally, improving the readability of your code is important. Consider using an HTML formatter like: or tools such as .

Wishing you the best of luck in your coding endeavors!

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 best way to resize a Drawable to fit the width of a TextView?

When trying to render HTML with image tags within a TextView, I encountered issues with scaling the images using the getDrawable() code. Here is an example snippet of the code: myTextView.setText(Html.fromHtml(htmlSource, this, this)); @Override public Dr ...

Is there a quicker method than using document.getElementById('element-id').innerHTML to retrieve content?

Currently, I am developing a user-friendly step-by-step wizard for my website to gather information about custom orders. Utilizing JavaScript, I have been tasked with updating the content of each "page" using the document.getElementById('element-id&ap ...

Responsive cards using Bootstrap styling

Currently trying my hand at mastering flexbox for responsive design with Bootstrap. In the picture, there are 4 cards where 2 should be displayed at the bottom and the other 2 at the top, but on mobile phones, they should all stack at the bottom. I attempt ...

The elements of the second flex item should also be arranged in a line next to each other

<script src="https://use.fontawesome.com/2a2c577e07.js"></script> <div class="parent-flex"> <div class="class1">Class 1</div> <div class="class2"> <img src="http://farm4.static.flickr.com/3140/3506456511_43787 ...

Discover the importance of Node.js integration with HTML

I am trying to display a Node-js value in an HTML file using the pug engine. In my app.js file: const express=require('express'); const app=express(); var bodyParser = require('body-parser'); app.set('views','views&apo ...

"Transforming the Website Background with a Splash of Color

I am trying to figure out how to change the color scheme of my website with a button click. Currently, when I select a different color, only the background of the webpage I'm on changes, not the entire website. I have applied CSS but it's not ref ...

Tips for creating a transition effect when hovering over a CSS sprite

Below is a snippet of my CSS sprite code: #IconSet a { width: 36px; height: 36px; display: inline-block; } #DeviantArtIcon { border-width: 0px; border-style: none; background-image: url(http ...

When using Websocket, an error message stating "Invalid frame header" will be triggered if a close message of 130 or more characters is sent

I am utilizing the ws node.js module along with html5's WebSocket. The Websocket connection is established when a user triggers an import action, and it terminates once the import is completed successfully or encounters an error. At times, the error ...

Expand the video comparison slider to occupy the entire width and height

I am striving to develop a video comparison slider that fills the height and width of the viewport, inspired by the techniques discussed in this informative article: Article Despite my efforts, I have not been successful in achieving this effect so far a ...

Select checkboxes by clicking a button that matches the beginning of a string in JavaScript

I have a form with a list of users and checkboxes below their names. Each user has a button that should select all checkboxes assigned to them. Below is the code for the dynamically created buttons and checkboxes. The included function takes the form name ...

The hover effect does not carry over to duplicated HTML

I successfully added a node, but I forgot to include the hover function of the node in my application. The hover function is not necessary, and I need it to work with ie8 compatibility. Here's my HTML: <div id="appendCell" style="color:green; colo ...

AngularJS: Solving the issue of controllers not exchanging data by utilizing a service

I am working with the following controllers and service: angular.module('myApp', []); angular.module('myApp').factory('ShareService', function() { var information = {}; information.data = ""; information.setD ...

Replacing default hover behavior from an external library

Currently, I am utilizing a JS library that comes with a specific widget. Basically, I have the following list (I removed unnecessary DOM code): <li class="disabled"> When I hover over this list item, it turns into: <li class="disabled state-a ...

Switch the view to a grid layout upon clicking

Using bootstrap 5, I've successfully created a list view: <div class="col"> Click to switch to grid/list </div> Below is the content list: <div class="row mt-3 list"> list view ... ..... ....... </div ...

"Move a div towards the mouse's location by animating it with JavaScript when

I've been working on making the ball element move and shrink towards the goals upon mouse click. I successfully created a function that captures the mouse click coordinates in the goals, but I'm facing challenges with the ball animation. I consi ...

During testing, the Vuetify expansion panel body is hidden with a display none style

Greetings! I am currently facing an issue while debugging my testing site. The problem is that the expansion panels are not displaying due to a style attribute attached to the div element of v-expansion-panel__body. Strangely, this issue does not occur on ...

div with fixed position and scrollable content

I'm exploring ways to create a simple webpage layout inspired by the traditional index setup. The idea is to have a fixed header/navbar div that is 200px in height, with a second div below it containing scrollable content that fills the remaining vert ...

Having trouble aligning page in the center with flexbox and styled components

Hey there, I'm having some trouble figuring out how to center my page using flexbox within styled components in a Next.js app. Any suggestions or tips? Let me share with you my Blog Component import ReactMarkdown from 'react-markdown' impor ...

Having trouble with GSAP Scrolltrigger PIN functionality in Next.js?

When I comment out the pin: true property in my code snippet below, everything works as expected except that the container wrapping the sections that should scroll horizontally does not stick to the top. However, if I uncomment the pin: true line, the enti ...

How to style HTML li elements to wrap like sentences within a paragraph

I am looking to format text list elements in a way that they wrap like sentences within a paragraph. Here is the accompanying HTML & CSS code snippet that demonstrates how li element behaves when it exceeds the width of the ul container. ul { ...