Tips to avoid bootstrap 3 (3.3.1) navbar-form from breaking onto a new line in a mobile view

I have a fixed navbar that works well on desktop browsers, but when the navbar collapses, the search form goes into two lines (). I want to ensure that the search form stays at the top even after the menu collapses, similar to the Yelp mobile interface ().

<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet" />
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>

<!-- Fixed navbar -->
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#">Brand</a>
      <form class="navbar-form navbar-header">
        <input type="text" class="form-control" placeholder="... ">
      </form>
    </div>
    <div id="navbar" class="navbar-collapse collapse">
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">Home</a>
        </li>
        <li><a href="#about">About</a>
        </li>
        <li><a href="#contact">Contact</a>
        </li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Dropdown <span class="caret"></span></a>
          <ul class="dropdown-menu" role="menu">
            <li><a href="#">Action</a>
            </li>
            <li><a href="#">Another action</a>
            </li>
            <li><a href="#">Something else here</a>
            </li>
            <li class="divider"></li>
            <li class="dropdown-header">Nav header</li>
            <li><a href="#">Separated link</a>
            </li>
            <li><a href="#">One more separated link</a>
            </li>
          </ul>
        </li>
      </ul>
    </div>
    <!--/.nav-collapse -->
  </div>
</nav>

Answer №1

The main purpose of the .navbar-form class in Bootstrap's CSS is to be utilized within the collapse feature. Similarly, the class .navbar-header should not be nested and serves as a clearing class. Therefore, consider changing the class from navbar-form navbar-header to navbar-left navbar-search, for example.

To customize the appearance, additional CSS rules need to be written. Any modifications or additions may require adjustments to this CSS.

If desired, you can lower the 400px value in the second media query.

DEMO: https://jsbin.com/sudide/1/

ALTERNATE HTML:

 <form class="navbar-left navbar-search">
    <input type="text" class="form-control" placeholder="... ">
  </form>

CSS

@media (min-width:0px) and (max-width:767px) { 
    .navbar {
        display: table;
        width: 100%;
    }
    .navbar-brand,
    .navbar-search,
    .navbar-toggle { display: table-cell }
    .navbar-search {
        padding: 7.5px 10px 0 0;
        width: 1%;
    }
}
@media (max-width:400px) { 
    .navbar-search {
        padding: 7.5px 15px;
        float: left;
        clear: both;
        width: 100%;
    }
}
@media (min-width:768px) { 
    .navbar-search {
        float: left;
        margin: 7.5px 0;
    }
}

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

How can I arrange content one on top of the other in Bootstrap 3 for mobile devices?

My issue involves working with bootstrap 3 and wanting the image-two class (along with all its child elements in the else conditional) to stack below the "box" class div containing the "New Image" contents in mobile view. Currently, it's stacking on t ...

Modifying<strong>superscript</strong> design

Is there a way to change the style of <sup> elements using CSS without removing them from the HTML code? I want to make them look like regular text. Basically, I want the result of <div>Some <sup>random</sup> text.<div> to ...

I am looking to navigate between tabs on a multi-part form in order to submit all the form data

My current form consists of five questions, each on a separate tab. What I am trying to achieve is the following: When the user submits the form data on the fifth tab An Ajax request sends the form data to a background script for processing Simultaneousl ...

The animation for the CSS background image simply refuses to cooperate

My goal is to create a CSS background animation using the Body tag. Below is the code I am currently using: body { animation-name:mymove; animation-duration:5s; animation-direction:alternate; animation-iteration-count:infinite; -webkit-animation-name:mymo ...

How to Customize the Size and Color of secureTextEntry Inputs in React Native

When it comes to styling a password input like the one below: <TextInput name="Password" type="password" mode="outline" secureTextEntry={true} style={styles.inputStyle} autoCapitalize="none" autoFocus={true} /> I also ap ...

Adjust the margin-top of the navigation bar with animation upon clicking the button

I have implemented a Cookie bar that is fixed at the top of my website. When the Close icon is clicked, the bar fades away. Due to the positioning of the div, I am also pushing down my .nav element by 64px. Inquiry: Is it feasible for the margin-top on ...

How well does position:fixed function in Internet Explorer versions 7, 8, and 9?

Before diving into using position:fixed, I'd like to confirm if it works well in Microsoft browsers. Thank you. ...

Position the div alignment to the top within the table cell

I'm having trouble aligning the div under the header to the right. Here is my HTML code and a photo for reference: HTML Code: <td> <div class="schedule-content"> <div class="timestamp& ...

Css selector that targets the first or last element of its type while also having another specified class applied

When creating a simple pager, I encountered the need to apply specific styles to the current page if it happens to be either the first or last page. However, I am unsure how to incorporate the :first-of-type/:last-of-type selector along with the usual clas ...

Toggling the form's value to true upon displaying the popup

I have developed an HTML page that handles the creation of new users on my website. Once a user is successfully created, I want to display a pop-up message confirming their creation. Although everything works fine, I had to add the attribute "onsubmit= re ...

Guide on aligning text along a baseline

CLICK HERE TO ENTER THE INTERACTIVE PLAYGROUND HTML: <div class="first">Text A</div> <div class="second">Text B</div> CSS: div { background-color: rgba(255, 0, 0, 0.3); /* For visualization only */ margin-top: 50px; ...

Tips for seamlessly integrating an Instagram post into your website without adjusting the container size

I'm trying to include an Instagram photo or video post within my WordPress article. It displays correctly on the desktop version, but on mobile devices, it causes the container to expand and everything appears smaller. Check out this screenshot of th ...

Create a div in HTML with a customized style to resemble a label with rounded edges

I'm struggling with CSS styling and trying to achieve a specific look for a div, as shown in the picture. Can someone provide guidance on how to achieve this style or share the necessary CSS code? I have come across similar buttons and div designs but ...

How can I manage the tab spacing within a <pre> element with CSS?

Can the tab spacing be customized within a <pre> tag using CSS to specify the number of pixels? For instance, imagine displaying a code snippet within a <pre> on a webpage: function Image() { this.Write = function() { document ...

Switching DropdownListFor Content in MVC4 with Bootstrap Design Activated

I am currently working on a partial view in MVC that contains two Razor dropdownlistfor elements. The first one is for selecting a customer, and the second one is for selecting a branch. @Html.DropDownListFor(model => model.CustomerId, new SelectList(M ...

React - Sort and Display Filtered List

I am looking to display only values greater than 0 on each row without removing the entire row. I attempted filtering for black > 0, but it resulted in the removal of the entire row. I am aiming to replace 0 with an empty string. Check out my website ...

Hover effect on navigation in CSS

Having trouble achieving a hover effect and could use some help. I'm working on creating a tab-style navigation menu where I want the tab to expand from the top when hovered over. However, my current CSS code is causing the tab to expand both at the ...

Can you please provide the CSS code that would style this table in a similar manner?

I am interested in utilizing the design of this table, but how can I update it to meet CSS standards and HTML5 equivalents? <html> <head></head> <body> <table border="1" bordercolor="#000000" style="background-color:#ffffff" w ...

Tips for increasing the complexity of your bottom-line animation

Hello everyone, I am currently learning the basics of CSS and have a question about a specific animation effect. Please bear with me if this type of inquiry is not suitable for StackOverflow. In my index.html file, I have the following code: <n ...

Overflow of content from `div` on top of the window

I have a section with tabs, each tab displaying different content. One of those tabs contains a table taller than the screen size. When I open it, the section stretches and overflows from the top, extending beyond the window. Despite having overflow:auto s ...