tags:

views:

65

answers:

4

Hi Guys,

If I want to set width of a div containing some text and I want to set the width of that div to 0 in IE6 (I have not checked in IE7) does not work!!!

Please check the HTML below:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
  <head>
    <title></title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js" type="text/javascript"></script>
    <style>
        #box {
            width: 0px;
            background-color: blue;

        }
    </style>
  </head>
  <body>
    <div id="box"><span>hello</span></div>
    <script>
        $("#box").width(0);
    </script>
  </body>
</html>

The width of the div stuck to the minimum content width of the div.

Regards,
Munim

+1  A: 

Use overflow:hidden if you need the contents to be cut off. Without it, the layout will consider the size of the box to be 0, but the content that doesn't fit will extend outside of its container.

Andrew Vit
Thanks Andrew for your answer, I should have mentioned it that I have an element inside where I set the `left: - 20px` and if I set it to `overflow: hidden` then the element gets hidden. Can you please give me another solution than this. Thanks again :)
Munim Abdul
+1  A: 

If you want width and height to be 0, then display:none is the usual way to totally hide it.

Jim Leonardo
Well, I can't use `display:none` as part of it is outside the `div` using `left: -20px`
Munim Abdul
A: 

As I pointed out in the other answer, stuff that doesn't fit in the box will overflow the fixed size unless you hide it. If you can't use overflow:hidden then positioning is another way to do it:

<style>
#box {
  position: relative;
  width: 0; }
}
#box span {
  position: absolute;
  left: -20px;
}
</style>

<div id="box"><span>hello</span></div>

Any element that's positioned using position:absolute is taken out of the document flow and will not affect other elements, including the size of the container, which you can then size to the dimensions you need.

Andrew Vit
Thanks, yet cannot set width to 0
Munim Abdul
+1  A: 

The actual div does have a width of 0 (you can tell if you put a border around it). If you need the text inside the div to be hidden, but not the other element, I would suggest adding the following css:

#box span {
display: none;
}
Nicknameless