How can I ensure that my Bootstrap 4 column adjusts to its content appropriately?

In the following container, there are two rows: the first row serves as the header, and the second row contains a left navigation bar and main content on the right side.

The left nav bar is designed to shrink to the width of the content. By pressing a toggle button on the nav bar, it collapses to show only icons. By default, it shows icons and labels. On smaller devices, such as phones or tablets, the nav bar should display only icons.


        <div class="row h-100">
            <div
              class="col-12 w-100  sticky-header"
              style="background-color: darkgrey;"
            >
              <h1>This is top header<h1/>
            </div>

            <div class="col-2">
              <div class="fixed-box col-2">
                <h1>SideNavBar</h1>
                <ul>

            <li>
                  <router-link class="nav-link" to="/contacts">
                    <img :src="ContactsImage"  />
                    <span>Contacts</span>
                  </router-link>
                </li>
              <li>
                  <router-link class="nav-link" to="/home">
                    <img :src="HomeImage"  />
                    <span>Home</span>
                  </router-link>
              </li>
              <li>
                  <router-link class="nav-link" to="/help">
                    <img :src="HelpImage"  />
                    <span>Help</span>
                  </router-link>
              </li>

                </ul>
              </div>
            </div>
            <div class="col-10 bg-warning h-100 no-gutters">
              <p>This is main content you see when you click a link on left<p/>
            </div>
          </div>
        </div>
    </div>

     @media only screen and (max-width: 768px) {
        .sidenavbar span {
          display: none;
        }

        .sidenav {
          width: 75px !important;
        }

      }

     .sidenav {
          width: 150px !important;
      }

I managed to achieve the desired functionality by using CSS to apply a different width for the side nav bar column. However, this approach leaves some unused space in the col-2 section. Is there a way to make col-2 shrink to the size of its content and allow the main content col-10 to expand?

Answer №1

When using Bootstrap 4, you have the ability to utilize responsive classes like d-none and d-md-none to control the visibility of elements on different devices. Check out more here

By applying these classes, you can hide specific elements, such as <span></span>, on mobile devices.

For a practical demonstration, see this example on Codeply: Codeply Example

On mobile, you can use these classes to show or hide certain elements, like <span>, depending on your design needs.

Additionally, switching from col-10 to col-md-10 can help you arrange content differently on mobile devices, like positioning content under a 'SideNavBar' for better mobile user experience.

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

Zooming in on the background with an animated effect

I'm experimenting with a zoom in animation on a background image for a brief moment. It seems to work initially but then resets itself. To see an example, I've created a jsfiddle link: https://jsfiddle.net/onlinesolution/tk6rcLdx/ Any idea what ...

Toggle jQuery to hide a div and update its CSS styling

There is an anchor with the class of "hide-btn1" that I want to trigger a series of actions when clicked: The rcol-content should hide and the text should change from Hide to Show The #container width needs to increase to 2038px The table.status-table wi ...

Setting up laravel-vue-pagination with Nuxt js: A step-by-step guide

Step 1: Install the package npm i laravel-vue-pagination Step 2: Create a new file called "laravel-vue-pagination.js" in the plugins folder import Vue from 'vue' import LaravelVuePagination from 'laravel-vue-pagination' Vue.use(&ap ...

How to set textboxes as read-only depending on the drop-down choice?

After developing some scripts to automatically populate the data-length and data-width in textboxes based on a dropdown selection, I now face the challenge of making the .width and .length textboxes readonly depending on the selected dropdown option. Is t ...

I have tried using justify-content: center; and align-items: center; to center this div, but the container div is still not centered. How can I successfully center

I am having trouble centering the container in my HTML code HTML code:- <body> <div class="container"> <div class="image"> <img src="./images/image-qr-code.png" alt="qrcode"> & ...

What is preventing the div container from extending to its full width?

The content on the right side of this div container is struggling to reach full width for some unknown reason. Click here to see an image of the issue. The right column is not extending to full width. This snippet shows my CSS code. The 'main-conte ...

Make multiple axios.get requests to retrieve and show data from the specified endpoint repeatedly

I am currently working on a backend built in Flask that provides a REST API for various tasks, one of which involves requesting deployment. Once the request is made, logs are stored in the database. To address this, I have created another REST API endpoint ...

Creating a Star Rating system with jQuery using a dropdown menu for multiple selections

Recently, I came across a simple jQuery Star rating system that I want to implement on my website. I have been following the code from http://jsfiddle.net/, and it works perfectly for one star rating system. However, I have multiple star rating systems on ...

How can I ensure that my boxes are aligned in a straight line?

I'm having trouble with applying the style="display: inline-block" to each div. Can someone please assist me? https://i.sstatic.net/Gi75l.png Below is my HTML code: <div class="hobby" style="display: inline-block"> <di ...

Craft a backdrop with subtle hints of italics

I am looking to create a unique background for my webpage using HTML and CSS. I want to have repeated italic shades in various shades of blue, red, and white, similar to rectangular shapes. For inspiration, take a look at the design on this website: If n ...

flexible design using flexbox with image overflow and responsive footer

I created a website and utilized flexbox for designing the layout. The page structure is quite simple, consisting of only 3 tags: <header> <section> <footer> [index.html] <html> <head> <link rel="stylesheet" type="t ...

Exploring Laravel and Vue: A guide to extracting URL parameters in Vue

I have included an id in the URL like this: (). Now, I am trying to retrieve that id in Vue in order to fetch data specifically for that id. However, I am unsure how to obtain the id in Vue. Below is my Vue script: <script> export default ...

The initial child expands in width according to the content until it reaches the boundaries of the parent, triggering the

My goal is to achieve a layout where the parent container's width can vary. The first child element (red) should expand based on its content until it reaches the end of the parent, at which point text-overflow: ellipses should be applied. The second c ...

The request response was a JSON object that started with the keyword "POST"

My frustration is growing as I encounter a strange issue with the stack template I purchased from envato market. Every time I attempt a simple ajax request, the json returned from my PHP script seems to be invalid. Interestingly, the exact same code works ...

`Slide bootstrap carousel without moving other elements`

.carousel { position: relative; height: 500px; .carousel-inner .item { height: 500px; } .carousel-indicators > li { margin: 0 2px; background-color: $maincolor; border-color: $maincolor; opacity: .7; ...

Issue with Iframe DOM: encountering undefined values while attempting to retrieve form field data

For some reason, I've been struggling to debug a piece of JavaScript that should be straightforward. I've experimented with various approaches, such as using a form to access fields and using getElementById. I've also played around with incl ...

laravel active li class dynamic

This is the route I have set up: <?php /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web route ...

Converting AngularJS scope objects into plain JavaScript arrays: a comprehensive guide

What is the best way to convert an AngularJS scope object into a simple JS array? I have a function that checks if any checkbox is checked and adds the corresponding value to an object. Now, I am trying to transfer the object values to an array and alert ...

What is the best way to display a multi-level sub-menu in front of its parent menu?

Code: nav#desktop-nav ul li > ul { position: relative; margin-top: 0; text-align: left; opacity: 0; visibility: hidden; position: absolute; top: 100%; transform: scaleY(0); transform-origin: 0 0 0; transition: al ...

Difficulty dealing with Firestore using get() followed by add()

Recently started learning Vue.js and Firestore, facing a challenge with what should be a simple task. 1) I am trying to fetch default values from an existing template document in my Firestore database. 2) The goal is to use these default values to create ...