Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two divs with inline-block style not aligned

Tags:

html

css

I have two div tags and only one has input tag; why output is in this way?

div.logo, div.form { 
  display: inline-block;
  height: 200px;
  width: 200px;
  border: 1px dotted;
}
<div class="logo">
  <input type="text">
</div>

<div class="form">
</div>

Output:

enter image description here

Can anyone explain this? Here is the fiddle: https://jsfiddle.net/ag487L5m/

like image 560
Vinay Aggarwal Avatar asked Jul 05 '17 18:07

Vinay Aggarwal


People also ask

How do you align inline block elements?

To align things in the inline direction, use the properties which begin with justify- . Use justify-content to distribute space between grid tracks, and justify-items or justify-self to align items inside their grid area in the inline direction.

How do you prevent inline block divs from wrapping?

As a result, the elements can sit next to each other. The major difference between display: inline; and display: inline-block; is that, display: inline-block; also allows us to set the width and height of the element. We can prevent inline-block divs from wrapping by adding suitable Bootstrap classes.

How do I align two divs on the same line in CSS?

The most common way to place two divs side by side is by using inline-block css property. The inline-block property on the parent placed the two divs side by side and as this is inline-block the text-align feature worked here just like an inline element does.

Can we place two div blocks at same line?

The problem of aligning 2 divs horizontally: Since <div> tag is a block element, only one div can be placed in a line, and all the space to the left and right is taken by it.


2 Answers

By default, inline and inline-block elements are set to vertical-align: baseline.

Since your div.logo has a text input, div.form which is now an inline-block element, aligns itself on the baseline of the input.

Adding vertical-align: top to the CSS should fix it. As in:

div.logo, div.form { 
    display: inline-block;
    height: 200px;
    width: 200px;
    border: 1px dotted;
    vertical-align: top;
}

div.logo, div.form { 
    display: inline-block;
    height: 200px;
    width: 200px;
    border: 1px dotted;
    vertical-align:top;
}
<div class="logo">
  <input type="text">
</div>

<div class="form">

</div>
like image 168
Tejasvi Karne Avatar answered Sep 29 '22 01:09

Tejasvi Karne


That's because the vertical alignment of inline elements defaults to baseline. Change it to top:

div.logo, div.form { 
  display: inline-block;
  height: 200px;
  width: 200px;
  border: 1px dotted;
  vertical-align:top;
}

div.logo, div.form { 
  display: inline-block;
  height: 200px;
  width: 200px;
  border: 1px dotted;
  vertical-align:top;
}
<div class="logo">
  <input type="text">
</div>

<div class="form">

</div>
like image 23
j08691 Avatar answered Sep 29 '22 03:09

j08691