开发者

Why div 100% width doesn't work as expected

I'm learning CSS and finding that it's not always so intuitive (welcome to webdev, I guess). :)

In an attempt to make a simple, static progress bar, I use the HTML file below:

<html>
  <head></head>
  <body>
    <table border='1' cellspacing='0'>
      <tr>
        <td>Sample Cell</td>
        <td>
          <!-- This is meant to be a progress bar -->
          <div style="background-color: #0a0; width: 20%;">
            <div style="text-align: center; width: 300px;">
            开发者_StackOverflow社区  Text Here!
            </div>
          </div>
        </td>
      </tr>
    </table>
  </body>
</html>

and I get this:

Why div 100% width doesn't work as expected

which is good, except for the fact that the width of the second column is fixed. But if I go ahead and change width: 300px to width: 100%, the text instead goes into the green box, rather than the whole table cell.

How can I "fill" the table cell with the text, without imposing a specific length restriction?


By placing your text div inside (as a child of) your colored div, you're telling HTML that you want the text to appear inside the colored div. So a width of 100% on the inner div means whatever the width of its parent div is, which you have set to 20%.

EDIT: added code *EDIT: updated code*

<html>
<head>
    <style>
        #bar{
            width: 100%;
            position: relative;
        }

        #progress{
            background-color: #0a0;
            width: 20%;
            position: absolute;
            z-index: 0;
        }

        #progress_text{
            text-align: center;
            width: 100%;
            position: relative;
            z-index:1;
        }
        .progress_cell{

        }

    </style>
  </head>
  <body>
    <table border='1' cellspacing='0'>
      <tr>
        <td>Sample Cell</td>
        <td class="progress_cell">
          <div id="bar">
             <!-- This is meant to be a progress bar -->
             <div id="progress">
               &nbsp;
             </div>
             <div id="progress_text">
                  Text Here! Text Here! But it's really long, and it's going to overflow ...
             </div>
          </div>
        </td>
      </tr>
    </table>
  </body>
</html>


here's your intro to html / css. thank me when you get my bill ;). first ... tables are for tabular data. not layout second ...

#wrapper {
    position:absolute;
    top:10px;
    left:50%;
    width:900px;
    height:500px;
    margin:0px auto 0px -450px;
    padding:0px;
    background-color:#369;
}

#box_1 {
    width:100%;
    height:100px;
    margin:0px auto;
    padding:0px;
    background-color:red;
}

and here's the html ...

<html>
    <head>
    </head>
    <body>
        <div id="wrapper">
            <div id="box_1">
                <p>stuff</p>
            </div>
        </div>
    </body>
</html>

hope this helps get you started


If you set width to %,it will take the width of their parent element.In your case.the div takes the width of the parent element i.e td


Here's a solution (I think?)

http://jsfiddle.net/tT2HR/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜