user8888293
user8888293

Reputation:

Change width of HTML <form> using CSS

I have an input field surrounded by a form in a navbar and I would like to reduce the size of the form to about 75% of the width of the container it is currently inside.

When there is no <form></form>, the input is exactly 75% the size of the container it's in and it looks perfect.

Incomplete Navbar HTML code:

<nav class="navbar fixed-top navbar-expand-lg navbar-dark">
        <div class="container" id="home">
            <form method="post">
                <div class="input-group md-form form-sm form-2 pl-0" id="search" method="post">
                    <input type="text" name ="search" id ="search" placeholder="Search Users" aria-label="Search" class="form-control my-0 py-1">
                    <div class="input-group-append">
                        <button class="btn btn-outline-secondary" type="submit" id="navbarButton"><i class="fas fa-search text-grey" aria-hidden="true"></i></button>
                    </div>
                </div>
            </form>
            <button class="navbar-toggler ml-auto" type="submit" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>

CSS code I tried:

#search {
    max-width: 75%;
}

form {
    width: 75%;
}

The form does not change size and remains the same size regardless of the screen width.

Upvotes: 4

Views: 21664

Answers (2)

user206
user206

Reputation: 1105

I think I understand what you mean:
Change

container

to

container-fluid

Upvotes: 0

symlink
symlink

Reputation: 12209

Looks fine to me:

.container{
  width: 100%;
  background: tomato;
}

#search {
    max-width: 75%;
}

form {
    width: 75%;
  background: gray;
  margin: 0 auto;
}
<nav class="navbar fixed-top navbar-expand-lg navbar-dark">
        <div class="container" id="home">
            <form method="post">
                <div class="input-group md-form form-sm form-2 pl-0" id="search" method="post">
                    <input type="text" name ="search" id ="search" placeholder="Search Users" aria-label="Search" class="form-control my-0 py-1">
                    <div class="input-group-append">
                        <button class="btn btn-outline-secondary" type="submit" id="navbarButton"><i class="fas fa-search text-grey" aria-hidden="true"></i></button>
                    </div>
                </div>
            </form>
            <button class="navbar-toggler ml-auto" type="submit" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>

Upvotes: 2

Related Questions