Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why "Inline-block" doesn't work properly in this CSS?

Tags:

Please check the CSS below.

 /*rex is the container of ex,ex2,ex3*/ div.rex{ height:200px; border:0px; margin:60px auto; padding: 0; vertical-align:top; }  div.ex{ width:34%; height:200px; background-color:#4f443a; display:inline-block; margin: 0; padding: 0; vertical-align:top; }  div.ex2{ width:0.5%; height:200px; display:inline-block; margin: 0; padding: 0; vertical-align:top; }   div.ex3{ width:65.5%; height:200px; background-color:#7e8547; display:inline-block; margin: 0; padding: 0; vertical-align:top; } 

The result in browser: enter image description here

What I need:

enter image description here

like image 925
JUST16 Avatar asked Mar 14 '14 06:03

JUST16


People also ask

How does inline-block work in CSS?

The display: inline-block Value Compared to display: inline , the major difference is that display: inline-block allows to set a width and height on the element. Also, with display: inline-block , the top and bottom margins/paddings are respected, but with display: inline they are not.

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 I stop inline-block 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.

What is the difference between inline flex and inline-block?

There is only one main difference between the inline-block and inline-flex: inline-block: Create specific block for each element under its section maintain the structure of each element. inline-flex: Does not reserved any specific space in normal form.


2 Answers

This is actually expected behavior in HTML. Because you are using inline-block, any newline character or whitespace you have after the element and before another inline element, will be counted as a space. If you want the blocks to stack side by side like in your picture, your HTML would need to be like this.

<div class="rex">     <div class="ex"></div><div class="ex2"></div><div class="ex3"></div> </div> 

working demo

It's not very pretty, but then again, I would recommend using another approach, possibly floating the elements instead.

Refer to here for a more in depth explanation of why this occurs.

How to remove the space between inline-block elements?

like image 161
Tristan Avatar answered Sep 20 '22 16:09

Tristan


Just extending answer giving by @Tristan here.

You have repeated the css code unnecessarily. You can minify it by using multiple css like :

.ex, .ex2, .ex3 {     display: inline-block;     vertical-align: top;     margin: 0;     padding: 0;     height: 100%;  /* no need of height: 200px; here */ }                  /* if you need to extend it to parent height */                    /* then use height: 100% */ 

OR

div.rex > div { /* code here */ } 

You can keep elements side by side by using any of the below approaches:

  • Using display: table-cell

  • Using float: left

  • Using display: inline-block (check @Tristan's solution)

like image 42
Mr_Green Avatar answered Sep 18 '22 16:09

Mr_Green