Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS resizing the <div> to fit the text

Tags:

css

I have a <div> called "container" which wrap another <div> called "box". "box" has text inside it.

The problem is I can't seem to resize the <div class="box"> to fit the text. I also got a diagram to explain my situation.

figure

<!DOCTYPE html>
<meta charset="utf-8" />
<link rel="stylesheet" href="float.css">
<div class="container">
    <br>
    <br>
    <br>
    <br>
    <div class="box">Hello my name is jack</div>
</div>
<div class="container">
    <br>
    <br>
    <br>
    <br>
    <div class="box">Hello my name is jack</div>
</div>

My CSS

.container {
    background-color: #ED8713;
    height: 300px;
    width: 300px;
    margin: 60px;
    margin-top: 50px;
}
.box {
    background-color: #FFFFFF;
    width: 270px;
    margin: 0 auto;
    margin-top: 15px;
    text-align: center
}
like image 948
JackRoster Avatar asked Apr 21 '13 17:04

JackRoster


3 Answers

You know what? We're all idiots. Here's the REAL answer:

HTML:

<div class="container">
    <br>
    <br>
    <br>
    <br>
    <span class="box">Hello my name is jack</span>
</div>

CSS:

.container {
    background-color: #ED8713;
    height: 300px;
    width: 300px;
    margin: 60px;
    margin-top: 50px;
    text-align: center;
}
.box {
    background-color: #FFFFFF;
    margin-top: 15px;
}
like image 185
Niet the Dark Absol Avatar answered Sep 24 '22 08:09

Niet the Dark Absol


Let the .box be an inline-block...

.box {
  display: inline-block;
}

...and center it!

.container {
  text-align: center;
}

Adding this two snippets to your stylesheet and removing the width: 270px should suffice.

like image 31
Alba Mendez Avatar answered Sep 23 '22 08:09

Alba Mendez


You should remove the width or set it to auto : width : auto

like image 29
Melki Avatar answered Sep 25 '22 08:09

Melki