Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to right align a div with display inline-block?

Tags:

html

css

I want the first div to align to the right, if I use float right it will bring the 2nd div on the same line which I don't want, my aim is to make the first div align to right without losing its block level as in chat applications. Any help will be greatly appreciated. . THANKS

Note: I am using display inline-block just because I want the divs to fit the content.

.outer{
  display: block; 
}

.lbubble , .rbubble {
    position: relative;
    padding: 5px;
    -webkit-border-radius: 5px;
    border-radius: 5px;
    -webkit-box-shadow: 2px 2px 10px 0px #616161;
    box-shadow: 2px 2px 7px 0px #616161;
    display: inline-block;
    margin-bottom: 8px;
}

.lbubble{
    background: lightblue;
}

.rbubble{
    background: lightgreen;
}

.lbubble:after {
    content: "";
    position: absolute;
    top: 5px;
    left: -8px;
    border-style: solid;
    border-width: 10px 14px 10px 0;
    border-color: transparent lightblue;
    width: 0;
    z-index: 1;
}

.rbubble:after {
    content: "";
    position: absolute;
    top: 5px;
    right: -8px;
    border-style: solid;
    border-width: 10px 0 10px 14px;
    border-color: transparent lightgreen;
    width: 0;
    z-index: 1;
}
<div class='outer'> <div class="rbubble"> Right Bubble with align right</div> </div>
<div class='outer'> <div class="lbubble"> Left Bubble it should be on 2nd line with align left </div> </div>
like image 752
Muhammad Avatar asked Nov 21 '14 22:11

Muhammad


People also ask

How do I display inline blocks to the right?

You can use Flexbox instead and set margin-left: auto on inner div to move it to right. Save this answer.

How do you align inline elements to the right?

Make sure you set the text-align property for the first list item to "left" (ul li:first-child), and set the text-align property for the other list items (ul li) to "right". That aligns them to the left and to the right but on two suppurate lines.

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.

Does text-align work on inline block?

Even though the property says “text” align, it affects all elements inside the block-level element that are either inline or inline-block elements. The property only affects the content inside the element to which it is applied, and not the element itself.

Can a div be inline?

Generally, inline elements may contain only data and other inline elements. An exception is the inline a element which may contain block level elements such as div . Note: Links that wrap multiple lines of block-level content make for a poor-to-unusable experience for some assistive technologies and should be avoided.


1 Answers

One solution is to use float: right and clear: right like:

.outer {
  display: block;
  clear: right;/*clear float right*/
}
.lbubble,
.rbubble {
  position: relative;
  padding: 5px;
  -webkit-border-radius: 5px;
  border-radius: 5px;
  -webkit-box-shadow: 2px 2px 10px 0px #616161;
  box-shadow: 2px 2px 7px 0px #616161;
  display: inline-block;
  margin-bottom: 8px;
}
.lbubble {
  background: lightblue;
}
.rbubble {
  background: lightgreen;
  float: right;/*add float right*/
}
.lbubble:after {
  content: "";
  position: absolute;
  top: 5px;
  left: -8px;
  border-style: solid;
  border-width: 10px 14px 10px 0;
  border-color: transparent lightblue;
  width: 0;
  z-index: 1;
}
.rbubble:after {
  content: "";
  position: absolute;
  top: 5px;
  right: -8px;
  border-style: solid;
  border-width: 10px 0 10px 14px;
  border-color: transparent lightgreen;
  width: 0;
  z-index: 1;
}
<div class='outer'>
  <div class="rbubble">Right Bubble with align right</div>
</div>
<div class='outer'>
  <div class="lbubble">Left Bubble it should be on 2nd line with align left</div>
</div>

Using clear: right brings left bubble element to the desire position.

like image 101
Alex Char Avatar answered Sep 30 '22 20:09

Alex Char