Tips for creating a responsive input field within a navbar

Can anyone assist me with setting the input field width in my navbar to adjust to the remaining width of the navbar and be responsive to different device sizes? I need the input field to dynamically change its width based on the screen size. Any help would be greatly appreciated.

li {
list-style: none;
}
.post-edit .nav-header {
    border: 1px solid #ddd;
    height: 40px;
}

.post-edit .nav-header ul li button {
    border: 1px solid #727272;
    background-color: #fff;
    color: #727272;
    font-size: 14px;
    padding: 3.5px 8px;
    border-radius: 3px;
    margin-top: 5.3px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>

<body>
  <div class="container-fluid p-0">  
    <!-- Main Container -->
    <main class="container-wrapper">
    
      <div class="post-edit">
        <form action="" method="post">
          <div class="nav-header navbar navbar-expand-sm">
            <!-- Left -->
            <ul class="navbar-nav">
              <li class="nav-item">
                <input type="text" name="p-title" placeholder="Title">
              </li>
            </ul>

            <!-- Right -->
            <ul class="navbar-nav ml-auto">
              <li class="nav-item">
                <button class="nav-link">Preview</button>
              </li>
              <li class="nav-item">
                <button class="nav-link">Publish</button>
              </li>              
            </ul>
          </div>
      </div>
      </form>
  </div><!-- /.post-edit -->
  </main> <!-- /Main Container -->  
  </div>
</body>

Answer №1

To enhance your website, consider upgrading to bootstrap 4 and above to take advantage of helper and utility classes that simplify your tasks.

Here's how you can make the transition with bootstrap 4. You have the option to revert to bootstrap 3 styling or utilize legacy css3.

  • Step 1: Modify the wrapping container's display to flex-box
  • Step 2: Apply flex grow 1 to all child elements, allowing them to expand and occupy the remaining space of the parent element.
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"
        integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"
        integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous">
    </script>
    <style>
        body {
            padding: 4px;
            margin-top: 2rem;
        }

        li {
            list-style: none;
        }

        .post-edit .nav-header {
            border: 1px solid #ddd;
            height: 40px;
        }

        .post-edit .nav-header ul li button {
            border: 1px solid #727272;
            background-color: #fff;
            color: #727272;
            font-size: 14px;
            padding: 3.5px 8px;
            border-radius: 3px;
            margin-top: 5.3px;
        }
    </style>
</head>

<body>

    <div class="container-fluid p-0">
        <!-- Main Container -->
        <main class="container-wrapper">

            <div class="post-edit">
                <form action="" method="post">
                    <div class="nav-header navbar navbar-expand-sm d-flex">
                        <!-- Left -->
                        <ul class="navbar-nav bg-primary flex-grow-1">
                            <li class="nav-item flex-grow-1 mr-1 ">
                                <div class="bg-success d-flex flex-grow-1">
                                    <input type="text" name="p-title" placeholder="Title" class="flex-grow-1">
                                </div>
                            </li>
                        </ul>

                        <!-- Right -->
                        <ul class="navbar-nav ml-auto">
                            <li class="nav-item">
                                <button class="nav-link">Preview</button>
                            </li>
                            <li class="nav-item">
                                <button class="nav-link">Publish</button>
                            </li>
                        </ul>
                    </div>
            </div>
            </form>
    </div><!-- /.post-edit -->
    </main> <!-- /Main Container -->
    </div>

    <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"
        integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous">
    </script>
    <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="25554a555540570b4f5665140b14130b14">[email protected]</a>/dist/umd/popper.min.js"
        integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous">
    </script>
</body>

</html>

Answer №2

To make your input wider, adjust the width in the CSS with the following formula:

Formula:

width = 100% - (nav margin) - (ul margin);

input {
 width: calc(100% - 15px - 1em); // 15px > nav-margin | 1em > ul margin
}

Take a look at the code snippet below for reference:

li {
  list-style: none;
}

.post-edit .nav-header {
  border: 1px solid #ddd;
  height: 40px;
}

.post-edit .nav-header ul li button {
  border: 1px solid #727272;
  background-color: #fff;
  color: #727272;
  font-size: 14px;
  padding: 3.5px 8px;
  border-radius: 3px;
  margin-top: 5.3px;
}

input {
  width: calc(100% - 15px - 1em);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />

<body>
  <div class="container-fluid p-0">
    <!-- Main Container -->
    <main class="container-wrapper">

      <div class="post-edit">
        <form action="" method="post">
          <div class="nav-header navbar navbar-expand-sm">
            <!-- Left -->
            <ul class="navbar-nav">
              <li class="nav-item">
                <input type="text" name="p-title" placeholder="Title">
              </li>
            </ul>

            <!-- Right -->
            <ul class="navbar-nav ml-auto">
              <li class="nav-item">
                <button class="nav-link">Preview</button>
              </li>
              <li class="nav-item">
                <button class="nav-link">Publish</button>
              </li>
            </ul>
          </div>
      </div>
      </form>
  </div>
  <!-- /.post-edit -->
  </main>
  <!-- /Main Container -->
  </div>
</body>

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

When a named capture group is included in the regex of a GET path, Express crashes with the error message: "Cannot read property 'name' of undefined at Layer

I am looking to process a GET request and extract specific information from the URL. Let's consider this scenario: const REGEX_QUERY = /^\/?house\/(?<street>[a-z]+)\/(?<house>[0-9]+)$/i; const REGEX_QUERY_NO_NAMES = /^\ ...

Shopping cart has encountered an issue with storing the data correctly

While I've managed to successfully integrate another service, the challenge now lies in implementing the logic for correctly generating cart items. My goal is to increment the quantity of items in the cart by one with each function call, but it seems ...

Tips for generating AJAX requests directly from HTML documents

Hey there! I'm fairly new to JavaScript and have a question that might seem silly to some. How exactly can I invoke a function from a Coffeescript file within my HTML code? I'd like to give users the option to choose the language in which they v ...

Tips on adjusting the label size of a radar chart in chart.js

My radar chart labels are appearing skewed and messed up on mobile devices, so I decided to scale them using the following code within ComponentDidMount(): const plugins = [{ beforeDraw: function(c) { var chartHeight = c.chart.height; c ...

I am looking to update the background color of the material UI helper text

In the image below, you can see that my background color is gray and my text field color is white. When an error is shown, the text field's white color extends and the password error message doesn't look good. I want the text field to remain whit ...

What is the best way to click within a web browser control?

I'm attempting to automate the process of clicking a specific div id button using vb.net. I have integrated a webbrowser for this purpose, and here is my current code snippet. The goal is to click the 'Do Job' button on the website through v ...

What is the procedure for updating or adding data to a JSON file with angularJS?

After successfully creating a local JSON file and retrieving data from it using app.controller('appCtrl', function($scope, $http){ $http.get('employees.json').success(function(data){ $scope.employees=angular.fromJson(data.employee ...

Ensure that both Vue methods are executed synchronously

I am working with two Vue methods: (1) this.retrieveSavedSearches() (2) this.updateDefaultSelectOption() Is there a way to ensure that method (2) only executes after method(1) has completed its execution? ...

What is the best way to define file paths in a webpage to ensure that the same file works seamlessly on both server and

Currently, I am working on developing a website locally with the intention of later transferring it via FTP to my server. In my index.php file, there is a line that reads: <?php include($_SERVER['DOCUMENT_ROOT'] . "/includes/header.php");?&g ...

Having trouble modifying the Input with split() in angularJS

I am faced with a nested JSON object that contains an array as one of its properties. Each item in the array is separated by a ';'. My goal is to use ';' as a delimiter to split each array item and make necessary changes. However, I am ...

Bringing in a variable from a component that is dynamically imported

I have a dynamically imported (Map) component on my page. const Map = dynamic(() => import('../components/Mapcomp'), { ssr: false, }) In addition to the component, I also need to import a variable from it. const [taskImg, setTaskImg] = useS ...

What is the best way to prevent event bubbling in this particular code snippet?

$('#div1').on('click', '#otherDiv1', function(event){ //Show popup $('#popupDiv').bPopup({ modalClose: false, follow: [false, false], closeClass: 'close ...

Unable to modify the name of an element's class due to restrictions in JavaScript

I am trying to switch the class of an element from score -box1 to score -box1.-active. I defined a constant $target in order to access the class score -box1, however it is not functioning as expected. const $target = document.getElementByClassname('sc ...

Passing an array from PHP to the DataTables JavaScript library

I am attempting to pass a PHP array to the JS Datatable library for testing purposes. Instead of fetching data from a database, I have simplified my approach. Here is a basic representation of my PHP code: $data_fmt['data'][] = array("TEST"); e ...

Tips for combining multiple fields into a single variable in PHP

When I click the submit button, the image source does not show any result but the image color is displayed. Any help would be greatly appreciated. <div class="form-group"> <input type="file" name="images[source][]" class="form-control input-l ...

Refreshing a component using a sibling component in React

Currently, I am delving into the realm of React and embarking on a journey to create a ToDo App. However, I've hit a snag while attempting to add a new task to the list. Despite successfully adding an item to the list upon clicking the add button, upd ...

Merge two arrays of objects containing Google Map coordinates

I am facing a challenge with merging two object arrays that have similar values. var Cars = [{ lat: 42, lng: -72 }, { lat: 40.7127837, lng: -74.0059413 }, { lat: 40.735657, lng: -74.1723667 }]; var Trucks = [{ lat: 43, lng ...

PHP Ajax Image Uploading and Storage

Is there a way to effortlessly upload an image and save it in the uploads folder without any page refresh? Not only that, but can we also have the uploaded image displayed on the screen within a designated div section? Unfortunately, I seem to encounter a ...

Adjust Navbar Header Color Based on Screen Size

I am completely new to front-end development. I am in the process of building my own responsive website using Bootstrap 3. One issue I am facing is that when I resize my browser window to simulate a phone screen, I want the navigation bar color to change ...

updating a div with URL redirection instead of global redirect

I am facing an issue with redirecting my website flow to the login page when a user clicks any link on the page after the session has expired (either due to timeout or manual logout from another window). In an attempt to solve this, I inserted the followi ...