get height of div using css

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Warren Thai

    #1

    get height of div using css

    i have a cascading style sheet with this in it
    *************** *************** *
    ..menu
    {
    position : absolute;
    width : 60px;
    height : 50px;
    background-color : #FFC0CB;
    top : 71px;
    visibility : hidden;
    font-family : verdana;
    font-size : 10pt;
    border-color : black;
    border-width : 2px;
    border-style : solid;
    }
    *************** ***************
    i have in my html this:
    *************** ***************
    <div id="menu_1" class="menu">
    <a href="t1.html"> Item 1 </a><br/>
    <a href="t2.html"> Item 2 </a><br/>
    <a href="t3.html"> Item 3 </a>
    </div>
    *************** *************** *
    i have a javascript along with this html
    that has this:
    *************** *************** ***
    var m1 = document.getEle mentById("menu_ 1");
    function test()
    {
    alert(m1.style. height);
    }
    *************** *************** ***
    when i run this function, it shows nothing.
    if i use "m1.style.t op" it works
    but height it doesn't
    why?


  • RobG

    #2
    Re: get height of div using css

    Warren Thai wrote:[color=blue]
    > i have a cascading style sheet with this in it
    > *************** *************** *
    > ..menu
    > {
    > position : absolute;
    > width : 60px;
    > height : 50px;
    > background-color : #FFC0CB;
    > top : 71px;
    > visibility : hidden;
    > font-family : verdana;
    > font-size : 10pt;
    > border-color : black;
    > border-width : 2px;
    > border-style : solid;
    > }
    > *************** ***************
    > i have in my html this:
    > *************** ***************
    > <div id="menu_1" class="menu">
    > <a href="t1.html"> Item 1 </a><br/>
    > <a href="t2.html"> Item 2 </a><br/>
    > <a href="t3.html"> Item 3 </a>
    > </div>
    > *************** *************** *
    > i have a javascript along with this html
    > that has this:
    > *************** *************** ***
    > var m1 = document.getEle mentById("menu_ 1");
    > function test()
    > {
    > alert(m1.style. height);
    > }
    > *************** *************** ***
    > when i run this function, it shows nothing.
    > if i use "m1.style.t op" it works
    > but height it doesn't
    > why?
    >
    >[/color]

    The method you tried will work if height is set inline, however if
    using CSS rules you have to use currentStyle (IE and similar) and
    getComputedStyl e (others).

    <script type="text/javascript">
    var m1 = document.getEle mentById("menu_ 1");
    function test(){
    if ( m1.currentStyle ) {
    alert(m1.curren tStyle.height);
    } else if ( document.defaul tView.getComput edStyle ){
    alert(document. defaultView.get ComputedStyle(m 1,'').height);
    } else {
    return;
    }
    }
    </script>

    Have a look here where I posted a 'getFontSize' function that you may
    wish to modify if you want to do this frequently.

    news://freenews.iinet. net.au:119/KZAie.1500$Zn.7 5797@news.optus .net.au

    <URL:http://groups.google.c om.au/group/comp.lang.javas cript/browse_thread/thread/9d826a73a25612c 5/09fea96794139a2 4?q=getFontSize &rnum=1#09fea96 794139a24>

    --
    Rob

    Comment

    • Warren Thai

      #3
      Re: get height of div using css

      What do you mean by setting inline?

      *** Sent via Developersdex http://www.developersdex.com ***

      Comment

      • RobG

        #4
        Re: get height of div using css

        Warren Thai wrote:


        Please quote what you are replying to.

        [color=blue]
        > What do you mean by setting inline?[/color]

        <div id="menu_1" style="height: 50px;">...</div>



        --
        Rob

        Comment

        Working...