Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make the width of outer div to fit inner divs automatically

Tags:

html

css

I have 2 inner divs inside an outer div, and I want to make the outer div to automatically fit to the width of the inner divs. Is that possible?

body {
  font-size: 0;
}
#outer {
  border: 1px solid black;
}
.inner {
  font-size: 12px;
  display: inline-block;
  border: 1px solid red;
}
<div id='outer'>
  <div class='inner'>text1</div>
  <div class='inner'>text2</div>
</div>
like image 458
frosty Avatar asked Oct 08 '15 21:10

frosty


1 Answers

Your outer div is a block-level element. You need to make it an inline-level element. Inline elements automatically take the size of the content it contains. In terms of the question you've asked, just setting :

display: inline-block

on your outer div will do the trick. See the code snippet below for the demo :

  body {
      font-size: 0;
    }
    #outer {
      border: 1px solid black;
        display: inline-block;
    }
    .inner {
      font-size: 12px;
      display: inline-block;
      border: 1px solid red;
    }
<div id='outer'>

  <div class='inner'>
    text1
  </div>
  <div class='inner'>
    text2
  </div>

</div>

Hope this helps!!!

like image 155
Satwik Nadkarny Avatar answered Nov 23 '22 21:11

Satwik Nadkarny