Is it possible to tell the code to position by the center point of an element, rather than by the top-left point? If my parent element has
width
If adding another element is an option, consider the following:
(View the following code live here)
HTML:
CSS:
/* CSS for all objects */
div.object
{
width: 100%;
height: 100%;
position: absolute;
left: -50%;
top: -50%; /* to anchor at the top center point (as opposed to true center) set this to 0 or remove it all together */
}
div.anchor
{
position: absolute; /* (or relative, never static) */
}
/* CSS for specific objects */
div#el1
{
/* any positioning and dimensioning properties for element 1 should go here */
left: 100px;
top: 300px;
width: 200px;
height: 200px;
}
div#el2
{
/* any positioning and dimensioning properties for element 2 should go here */
left: 400px;
top: 500px;
width: 100px;
height: 100px;
}
div#el1 > div.object
{
/* all other properties for element 1 should go here */
background-color: purple;
}
div#el2 > div.object
{
/* all other properties for element 2 should go here */
background-color: orange;
}
Essentially what we're doing is setting up one object to define the position, width and height of the element, and then placing another one inside of it that gets offset by 50%, and gets it's parent dimensions (ie width: 100%
).