www.webdeveloper.com
+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2007
    Posts
    15

    100% width, keep height in proportion?

    I've got a div that contains a videoplayer. I want that div to have style="width: 100%", and I want the height to be in proportion to that.

    So, to clarify. The video is 400 x 200 px. But I want to stretch the width to 100% of the size of the surrounding div, and the height shall not be 100%, but in proportion to the width.

    Anyone got an idea on how to solve this? Is JavaScript the solution, or can this be done by CSS?

    Thanks, Henrik

  2. #2
    Join Date
    Jun 2006
    Posts
    384
    A very basic solution (assuming that the height is always half the width (400x200))
    HTML Code:
    dv = document.getElementById("MyDiv")
    dv.style.height = dv.offsetWidth / 2 +"px"
    where "MyDiv" is the id of the div you want to resize.

    The problem with this is that resizing the window, the height will stay the same when the width changes.

    To Solve that, put the above code in a function :

    HTML Code:
    function Resize()
    {
         dv = document.getElementById("MyDiv")
         dv.style.height = dv.offsetWidth / 2 +"px"
    }
    and put the following in the body tag of your page :

    HTML Code:
    <body onresize="Resize()"  onload="Resize()">
    Which tells the document to call Resize() every time the window is re-sized.
    This works in both IE and FF, haven't tried in other browsers.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center



Recent Articles