简体   繁体   中英

How to make a <div> display its own width as text inside it?

thanks for reading.

I have a forum and I'm trying to add a java applet into it that is the same width as the post it's contained in. Therefore before I compile the JAR I need to know the width of the posts, which is the maximum width the applet can be.

What I need to know is how to make a div that is width 100%, so it stretches to the very sides of the post and inside the div it tells me how wide it is. It would be extremely helpful as I can't measure with a program how wide the post is because I can't see the boundaries due to the design.

Thanks in advance so much for helping, Gergy.

How about this (assuming you're using jQuery!):

$(".my_div").html($(".my_div").width());

Without jQuery, give this a shot:

<html>
   <head>

   </head>
   <body>

    <div id="mydiv">

    </div>

    <script>
       var div = document.getElementById("mydiv");
       div.innerHTML = (div.offsetWidth);
     </script>
   </body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM