Jéjé
Jéjé

Reputation: 115

Align a form in navbar-header on small screens with Bootstrap

With the following code, on a "middle screen", the result is OK.

enter image description here

But on a small screen (smartphones), it is not.

enter image description here

I wish the input and the submit button (magnifying glass) are aligned on the same line.
How I can do that?

<nav class="navbar navbar-inverse navbar-fixed-top">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" 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="#">Diko Responsive</a>
        </div>

        <div id="navbar" class="navbar-collapse collapse">
            <form class="navbar-form navbar-right" action="index.php" method="get">
                <div class="form-group" >
                    <input id="terme" name="terme" placeholder="Veuillez saisir un terme" class="form-control input-xs" type="search">
                    <button type="submit" class="btn btn-info"><span class="glyphicon glyphicon-search"></span></button>
                </div>
            </form>
        </div>
    </div>
</nav>

Thanks.

EDIT: I added this CSS file after the advices of @NiklasMH

@media (max-width : 768px){
    .form-group input {
        width: calc(70% - 32.5px);
    }

    .form-group button {
        width: 40px;
    }   
}

But as you can see, both are still not aligned.

enter image description here

Upvotes: 1

Views: 516

Answers (2)

Fiido93
Fiido93

Reputation: 1978

You almost there. You only forgot to call class=input-group

You no need to add another media queries there.

HTML

<div class="input-group">
         <input id="terme" name="terme" placeholder="Veuillez saisir un terme" class="form-control input-xs" type="search">
            <span class="input-group-btn">
                <button class="btn btn-info" type="button"><span class="glyphicon glyphicon-search"></span></button>
            </span>
</div>

Here is the DEMO

Upvotes: 2

NiklasMH
NiklasMH

Reputation: 767

I don't know the twitter-bootstrap that good, so this answer is more about what you can do to the CSS.

Bad practice

Some say it's bad practice, but you could eigther use a table to align them on the same row. Then you also can make the left or right column in fixed width like:

td:last-child {
    width: 32px;
}

Good practice

Or you can use the calc-value in CSS, which is widly supported now (except in Opera Mini - ref caniuse.com), like:

.form-group input {
    width: calc(100% - 44px); /* Minus a little more (4px) than the button-width */
}

.form-group button {
    width: 40px;
}

Remember that padding will make the element bigger and force more space, so set box-sizing to border-box (default content-box) to avoid this:

.form-group input, .form-group button {
    box-sizing: border-box;
}

JSFIDDLE

.form-group input, .form-group button {
  box-sizing: border-box;
}

.form-group input {
  width: calc(100% - 44px);
  max-width: 160px;
}

.form-group button {
  width: 40px;
}
<div class="form-group">
  <input placeholder="text"/>
  <button>btn</button>
</div>

Upvotes: 0

Related Questions