Challenge with Responsive Header Design in Bootstrap: Positioning Logo and Search Bar on Mobile Display

Currently, I am working on creating a responsive header with Bootstrap. While the layout looks good on desktop screens, I'm encountering some alignment issues in the mobile view. To give you an idea of what I'm aiming for and the problems I'm facing:

The search bar consists of two div elements: Input type="text" and Input type="submit"

  • -For the input type="text", I want it to occupy 80% of the available width and align it to the left.
  • For the submit button, it should take up the remaining 10% and be aligned to the right (leaving a 10% margin).

I've attempted to use Bootstrap's d-flex, justify-content, and text-end classes, but they haven't yielded the desired layout. Below is my current code:

<header class="container-fluid">
    <div class="row align-items-center">
        <!-- Logo Column -->
        <div class="col-md-6 col-xs-12 d-flex">
            <img src="../logo_msp_Part.png" alt="Logo" class="logo">
        </div>

        <!-- Spacer Column (Visible in Desktop only) -->
        <div class="col-3 d-none d-md-block"></div>

        <!-- Search Bar Columns -->
        <div class="col-2 justify-content-start">
            <form>
                <input type="text" class="form-control w-200" placeholder="Search" id="searchInput"gt;
            </form>
        </div>
        <div class="col-1">
            <input type="submit" value="ok" class="btn btn-primary" id="confirm_search">
        </div>
    </div>
</header>

I have adjusted the widths of the col-* classes to ensure proportional columns. Applied d-flex and justify-content-start/justify-content-end classes to the input fields. Tried using w-100 and w-80 classes to control the sizes of the text input and submit button respectively. Additionally, I removed my custom CSS related to the header from media queries to prevent conflicts with Bootstrap styling.

Answer №1

In my opinion, having an empty column is not necessary as it can limit the growth of elements. There are alternative approaches to consider:

  1. Using offset-* to align the search element and button at the end (link here). This method creates space between elements, but there is a risk of them breaking out in certain dimensions. It works well if you are confident that the element won't exceed its container.

  2. Increasing the width of the parent container to accommodate the search element's size. This option is suitable when you have a good estimate of the search bar's dimensions and ensures easier adaptation to smaller screens.

If your goal is to keep the search button and bar together, utilize the input-group class for integration. Even if this is not necessary, both elements should be within the same column to maintain alignment.

I combined the input search and button within the same column, allowing them to be flexed to the end by adjusting the search element to inline-block with a restricted width. To ensure appropriate mobile dimensions, both containers need the col-* class or else they default to col-12 width.

To style the elements, use the following CSS code:

#rechercheInput {
  width: 80%;
  display: inline-block;
}

#form {
  width: 300px;
}

Additionally, the provided HTML snippet demonstrates the implementation of these concepts using Bootstrap classes for responsiveness. Make sure to adapt the code to suit your specific requirements.

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

Tips for creating a full screen div layout with fixed top and bottom navigation bars

Can someone help me achieve the following layout style? +-----------------------------------------------------------+ | Top Sticky Nav | +--------------------------------------------------------+--+ | ...

Clicking on the current component should trigger the removal of CSS classes on its sibling components in React/JSX

Currently, I am working on creating a navigation bar using React. This navigation bar consists of multiple navigation items. The requirement is that when a user clicks on a particular navigation item, the class 'active' should be applied to that ...

Tips for activating list-groups upon clicking in Angular 5

I am currently experimenting with Bootstrap list-groups and Angular 5. I have encountered an issue where clicking on a specific item in the list-group activates all items instead of just the one clicked. Is there a way to enable only the clicked item in ...

Establishing headings and frames for a collection of Essays

I have integrated a RSS Feed into my Vue.js application to display a series of articles. I want to organize these articles in a container similar to tweets on the left, with a heading labeled Latest Articles. However, when I use a tag in the template sect ...

Transferring a selection from a dropdown menu

I am currently working on a dropdown menu where each entry has an ID and a value. The issue I am facing is that when I click on an item on the list, the click-handler is called and passes the div. While the click handler can access the id without any probl ...

Flexbox allows you to easily manage the layout of your website

I am currently working on a CSS project and have encountered an issue. Whenever I apply the "display: flex" property to the .student element, the border around it mysteriously doubles. The reason for wanting to use the flex property is to align the text ve ...

Tips for creating a menu bar where the entire item area is clickable

Currently, I am working on implementing a menu bar control. Below is the CSS and design code that I have used: CSS : .Menu { background:transparent url(../images/blueslate_background.gif) repeat-x; text-align:center; font-family : "lucida g ...

Resolve the issue of text overlapping on an image when zooming in

There seems to be an issue with overlapping text and images when zooming the page. I have included a screenshot for reference, please take a look and provide a solution. Thank you in advance.https://i.sstatic.net/oVlGN.png Here is the CSS code causing the ...

Utilizing CSS and HTML exclusively, display content on hover

Is there a way to display a child div when hovering over an anchor tag? I'm looking to reveal a div with the class of botm when the cursor hovers over the anchor tag. Here's the specific layout I have in mind, so please maintain the order of tags ...

Is there a print button in Drupal similar to a print link?

Is there a way in Drupal to create a button using an API function similar to the print link function? <?php print l(t('Announcement'), 'node/30'); ?> l turns text into a link. I'm looking for a way to create buttons in Drup ...

The dynamic addition of divs to the HTML body is causing them to overlap

Check out this functional plunker that is nearly complete. The current issue I am facing is with the chat divs being dynamically added to the body using $compile, but they all have the same CSS class and end up overlapping each other, like so: .chat-wind ...

What is the method for distributing additional width in a table among its columns?

Have you ever wondered how excess space in an HTML table is distributed among the columns? I too was curious about this, but couldn't find a definitive answer even after extensive searching. Therefore, I decided to create a simple test page to investi ...

Positioning Images in Bootstrap 4

I'm encountering an issue with Bootstrap 4. The left box isn't aligning properly with the right box. This involves both HTML and CSS. How can I go about resolving this issue? <section> <div class="container"> <div class="row"& ...

How to create nested nav menus that are optimized for touch-screen devices?

When it comes to creating responsive websites, one challenge I frequently encounter is designing a multi-level nav menu that functions effectively for touch devices. Many plugins and methods exist, but most fall short because they do not allow a second-l ...

Personalizing Web Push Alerts (Google Chrome)

I successfully implemented a web push notification for Google Chrome using Google Project and Service Worker. One thing I'm curious about is how to customize or style the push notification. The plain message box doesn't quite cut it for me – I ...

preventing elements from moving unexpectedly as the navigation bar becomes fixed at the top of the page

I have a website that features a Bootstrap 3 navbar. This navbar is positioned 280px below a block div and becomes sticky at the top of the page when scrolled to that point. HTML (within < head > tags) <script> $(document).ready(function() ...

Targeting and sharing selected block quotes from social media posts

Within my Wordpress site's index.php file, there is a div that displays the post title, category, and featured image. Code snippet from index.php: <div> <h1><?php the_title(); ?></h1> <h3><?php the_category(& ...

Position the scroll down arrow at the center bottom of a full-screen div using WPBakery Visual Composer

I have a series of full-screen sections in Visual Composer and I am trying to add an arrow at the bottom of each one to indicate to users that they should scroll for more content. However, my attempts with absolute positioning on the divs containing the ic ...

Vuetify vueJS dialog with elevated design

Is there a way to completely remove the elevation of a v-dialog so that it has no shadow at all? The elevation property in the v-dialog itself and using the class as Class = "elevation-0" have not worked for me. Here is the link to the component ...

CSS transformation: skewing without any blurring effects

Is there a way to create tilted drivers with borders and text without sacrificing the sharpness of the font and border? I have used the transform: skew(); CSS rule for tilting, but it has caused blurriness. How can I add font smoothing and maintain sharp b ...