Pham Minh Tan
Pham Minh Tan

Reputation: 2096

How to make <input> and <a> element inline in a <div>

I have a div containing an input and an a element:

.wrapper {
  width: 300px;
  display: inline-block;
}
.custom-input {
  display: block;
  width: 100%;
}
.button {
  width: 20px;
  height: 20px;
  background-color: #ccc;
  display: block;
}
<div class="wrapper">
  <input class="custom-input" type="text" />
  <a class="button"></a>
</div>

Here's a jsfiddle.

I want my input and my button inline. The input with button always has 100% width of the wrapper. In some cases, I want to remove the button. The input then has 100% width of the wrapper div.

It is only inline when I use inline-flex for the wrapper. But I want it to be able to run on old browsers (IE 8-9), and I want my input and my element to always have 100% width of wrapper.

How can I do it?

Upvotes: 0

Views: 134

Answers (2)

Aru
Aru

Reputation: 1870

try to use display: inline-block instead display:block

updated css

.wrapper{
    width: 300px;
    display: block;
}

.custom-input{
    display: inline-block;    
}

.button{
    width: 20px;
    height: 20px;
    background-color: #ccc;
    display: inline-block;
}

Upvotes: 0

Kewin Dousse
Kewin Dousse

Reputation: 4027

Using width: 100% on your input will make it take all the horizontal space available, pushing the button to the line below.

This should work :

.wrapper{
    width: 300px;
    display: inline-block;
}

.custom-input{
    display: inline-block;
}

.button{
    width: 20px;
    height: 20px;
    background-color: #ccc;
    display: inline-block;
}

Here's the updated jsfiddle : http://jsfiddle.net/k6yhtf92/3/

Upvotes: 1

Related Questions