How to Align a Div Next to a Vertical Navigation Bar in Bootstrap 4

I've been attempting to position a div to the right side of a vertical navigation in Bootstrap 4, but I'm struggling to achieve the desired layout:

<div class="container mt-3">
  <div class="nav navbar navbar-expand-lg navbar-dark bg-dark">
    <a class="navbar-brand" href="#">Some cool thing</a>
    <button class="navbar-toggler" type="button"
            data-toggle="collapse" data-target="#my-navbar">
      <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="my-navbar">
      <ul class="navbar-nav">
        <li class="nav-item">
          <a href="#" class="nav-link">Some menu entry</a>
        </li>
      </ul>
    </div>
  </div><!-- end of .nav -->

  <div class="row mt-2">
    <div class="nav flex-column nav-pills col-md-5">
      <a href="#" class="nav-link active">Something</a>
    </div>
    <div class="col-md-7 border">
      <h1>Hello world</h1>
    </div>
  </div>
</div>

Unfortunately, the navigation and the div to the right (border barely visible) are extending beyond their intended boundaries, wider than the top navbar:

https://i.sstatic.net/0ldLr.png

This CodePen demonstrates the issue. Are there any Bootstrap classes that can address this or should I manually adjust the layout?

If I remove the <div class="row"> and place the components directly in the <div class="container">, the boundaries are correct but lose the desired side-by-side alignment.

Answer №1

Swap out the nav for a navbar at the top. Also, make sure the second navigation is inside a column (not the column itself).

Check out the code here

<div class="container mt-3">
    <div class="navbar navbar-expand-lg navbar-dark bg-dark">
        <a class="navbar-brand" href="#">Some cool thing</a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#my-navbar">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="my-navbar">
            <ul class="navbar-nav">
                <li class="nav-item">
                    <a href="#" class="nav-link">Some menu entry</a>
                </li>
            </ul>
        </div>
    </div>
    <!-- end of .nav -->

    <div class="row mt-2">
        <div class="col-md-5">
            <div class="nav flex-column nav-pills">
                <a href="#" class="nav-link active">Something</a>
            </div>
        </div>
        <div class="col-md-7 border">
            <h1>Hello world</h1>
        </div>
    </div>
</div>

Answer №2

When working with Bootstrap, it's important to nest a .container inside the .nav class, rather than placing it outside. This arrangement ensures that the .nav can span the full width of the page, while a second container can be used for content following the navbar.

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta.3/css/bootstrap.css">
<div class="nav navbar-expand-lg navbar-dark bg-dark">
  <div class="container mt-3">
    <a class="navbar-brand" href="#">Some cool thing</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#my-navbar">
      <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="my-navbar">
      <ul class="navbar-nav">
        <li class="nav-item">
          <a href="#" class="nav-link">Some menu entry</a>
        </li>
      </ul>
    </div>
  </div>
</div>
<!-- end of .nav -->

<div class="container mt-3">
  <div class="row mt-2">
    <div class="nav flex-column nav-pills col-md-5">
      <a href="#" class="nav-link active">Something</a>
    </div>
    <div class="col-md-7 border">
      <h1>Hello world</h1>
    </div>
  </div>
</div>

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

Error message "Module not found in Vue" occurs when changing image URL to an online image

I am currently working on a dynamic image display feature where I retrieve image names as strings from a backend. To manage a large number of images, I plan to store them online while maintaining the same logic for displaying them. Previously, I achieved t ...

Creating Custom Styles with Polymer for <content> Elements

Seeking help with styling using the ::content selector and custom CSS properties. Any insight would be appreciated! To simplify, I have a Polymer component utilizing a content tag that will always contain a paper-input: <template> <style> ...

No JavaScript needed: Create custom overflow/scroll indicators using CSS only

Is there a way to align the content of a box to the left when the box doesn't overflow? I have a scroll indicator created with CSS only, which works well but has this alignment issue. Any suggestions or improvements on the code are welcome :) html ...

The issue with the display grid position being set to absolute is causing functionality

Struggling to create an image catalog with cards containing text while using position absolute on the text and position relative on the card. Unfortunately, it's not working as expected. /*catalog style*/ .grid-container{ padding: clamp(5px,10vw,2 ...

Achieving Vertical Alignment of Text with CSS

Can you figure out what's wrong with the placement of content outside the <footer> tag in this HTML? Also, why is the text not perfectly vertically middle-aligned? <html> <head> <style> body { ...

Manipulating an SVG file with JavaScript

Within the HTML code, there is a photo already added as an SVG file. I am interested in learning how to enable the user to select between two options - either a cross or a zero. Upon clicking on the designated area, the chosen figure should appear (resembl ...

Ensures that two divs have the same dimensions

Currently, I have a line of sections set up like this: I am attempting to ensure that the second div matches the height of the first one, despite the fact that their heights are determined by the size of the pictures which may vary. Do you have any sugges ...

What is the best way to achieve this grid layout using Bootstrap 4 grids?

I'm struggling to achieve the desired layout using Bootstrap 4 grid. In the desktop view, images are in positions 2, 3, and 6, while the rest are text. https://i.sstatic.net/guhWy.jpg This is how the site should look on mobile. https://i.sstatic.ne ...

I am experiencing difficulties when attempting to insert an email link using Codeigniter 3's framework-specific syntax. The function does not

In my Codeignieter 3 project, I am faced with the task of displaying email addresses stored in the database. When using the code <a href="mailto:<?php echo $record->email; ?>"><?php echo $record->email; ?></a></td>, I e ...

Tips for referencing the second class when two classes share the same name in Selenium WebDriver

I am trying to retrieve the text from the second "109-top-dark-grey-block ng-binding" class, which is currently null but will contain some text in the future. I have attempted using tabIndex and nth-child selectors, but neither have been successful. " < ...

What is the best way to insert a row into a JavaScript table while incorporating a cell that utilizes the datepicker function in jQuery?

Currently, I am working on a javascript function: function addItem(tableID){ var table = document.getElementById(tableID); var rowCount = table.rows.length; var row = table.insertRow(rowCount); var cell1 = row.insertCell(0); var el ...

Having Trouble with Font Awesome Icon Loading in Search Box

Currently, I am working on a website located at . However, I am facing an issue where the font awesome icon is not loading properly and is only displaying a square. I have thoroughly reviewed the CSS and cannot identify any conflicting elements. Any assis ...

Automate CSS slideshow playback using JavaScript

Using only CSS, I created a basic slideshow where the margin of the element changes upon radio button click to display the desired slide. You can view the functionality in the code snippet below. Additionally, I implemented auto play for this slideshow usi ...

How to eliminate a line break following an image tag in HTML

Before I display my image with an H3 tag, I didn't use any break statements. However, the two tags are appearing on separate lines when I view it in the browser. Is there a CSS trick I can use to solve this issue, or perhaps an easier tag attribute th ...

adverse offset and backdrop

To achieve the desired layout, I am trying to adjust the negative margin for the .text div so that it aligns on top of the .image div. <div class="wrap"> <div class="image"><img src="imgage.jpg" /></div> <div class="text ...

The Mui Switch track color fails to change when checked

I'm looking to customize the colors of the track for both checked and unchecked states. Currently, the track color is working for the unchecked state but defaults for the checked state: Checked State: https://i.stack.imgur.com/eGV4a.png Unchecked S ...

Leveraging jQuery to target the div element labeled with class "signed" through a Ruby

I'm attempting to create a click event that allows a user to click a link to toggle a dropdown window, with only one window open at a time. $(".one").on('click', function(){ $(".two").slideToggle(); }); <div class="row visual_wra ...

What's the best way to integrate Bootstrap into my HTML document?

Hey there, I'm new to the coding world and just started learning. I could use some assistance with including Bootstrap v5.1 in my HTML code. The online course I'm taking is using an older version of Bootstrap, so I'm having trouble finding t ...

Stack two divs together

It may seem silly, but I just can't get it to work. I'm attempting to enclose two divs with different classes in another div, but my current code is automatically closing the divs. There are multiple sets of divs with classes .panel-heading and ...

Does the value of an Angular reactive form control only reflect what the user inputs?

I am working with a reactive form where I need to extract values and assign them to a variable defined in an interface. The form is populated using the Google Places API, which disables user interaction with all controls except for the initial address inpu ...