473,396 Members | 1,779 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to reposition a div inside another?

I want to be able to center the div 'testDiv' inside the parent div 'testContainer' (which has a height of 400, but for the sake of simplification I just wrote 400 in as a constant. I'm very new to javascript so I have little idea of what I'm doing wrong.

CSS:
Expand|Select|Wrap|Line Numbers
  1. body {
  2.     overflow-x: hidden;
  3.     background-color: #ededed;
  4. }
  5. #testContainer {
  6.     height: 400px;
  7.     width: 1100px;
  8.     background-color: #ffffff;
  9.  
  10.     margin-top: -8px;
  11.     margin-left: auto;
  12.     margin-right: auto;
  13. }
  14. #testDiv {
  15.     color: black;
  16.     text-align: left;
  17.     font-family: "Helvetica Neue";
  18.     font-style: italic;
  19.     font-weight: bold;
  20.     font-size: 24px;
  21.     height: auto;
  22.     max-height: 400px;
  23.     width: 500px;
  24. }
  25.  
HTML/javascript:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3.     <link rel="stylesheet" type="text/css" href="style.css" media="screen"/>
  4. </head>
  5. <body>
  6. <div id="testContainer">
  7.     <div id="testDiv">"Here is a sample of text which gives offsetHeight a real value."</div>
  8. </div>
  9.  
  10. <script language="javascript" type="text/javascript">
  11.     var divH = document.getElementById('testDiv').offsetHeight;
  12.     var divT = (400-document.getElementById('testDiv').offsetTop)/2;
  13.     document.getElementById('testDiv').offsetTop = divT;
  14. </script>
  15.  
  16. </body>
  17. </html>
  18.  
Jan 29 '11 #1
1 1713
JKing
1,206 Expert 1GB
You cannot set the offsetTop property. It only returns the value.

Try setting a top margin instead.
Expand|Select|Wrap|Line Numbers
  1.     document.getElementById('testDiv').style.marginTop = divT;
  2.  
Also there are CSS methods out there for centering an element vertically.
Feb 1 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Georges Heinesch | last post by:
Hi. Is it possible to put a tab control inside another tab control. I tried it several times, but for some reason this doesn't seem to work. Can someone confirm this, or tell me what the trick...
3
by: mandible | last post by:
I'm trying to call one stored procedure inside another. I was wondering if this is possible Some ideas I was toying with is putting the first stored procedure inside of a temp table but haven't...
2
by: gerotica | last post by:
I need to set int28 10 secs after my tsr is loaded. So i´m using int8 (tried with 1c also), counting a few times and then write int28 vector. Here is a piece of the code: int f1 = 1; .......
4
by: psbasha | last post by:
Hi, How to declare a Class inside another class as avariable? For Example" Class X{ MethodX1();
3
by: Jimmy | last post by:
Is it possible to open a pop-up browser window (modal-style) inside another browser? e.g. // assuming something is calling this function inside the html as some event got triggered function...
3
by: leonardodiserpierodavinci | last post by:
Hi. Sorry for what is perhaps a neophyte question: is it possible to pass a variable to a PHP script from inside another PHP piece of code? For instance, the file test.php (which of course...
3
by: robin1983 | last post by:
HI all, how are you ? i need help from you people. This time my problem is that i m running a query inside another query. But, the result that i got is something not exactly what i want. and one...
7
by: =?Utf-8?B?U2hpdmEgUmFtYW5p?= | last post by:
I have a windows application with multiple forms & another web application. Now I need a parent EXE or Application which need to act as a container to these application. Example : My existing Web...
7
by: 200dogz | last post by:
Hi, I've just noticed if there is a control that runat server and another one directly inside it, it will result in a compilation error. eg. If I have an asp:Label directly inside a...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.