Tutorial on HTML element positioning #879
TobyKLight
started this conversation in
Show and tell
Replies: 1 comment
-
great tutorial! thank you |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Deep dive on CSS position modes
This will be a little crash course on the CSS position property, with end goal to understand how HTML elements can be placed on top of a canvas. For some cases Cables.gl handles this seamlessly, but other times, particularly when embedding, you might need to get your hands dirty with HTML and CSS to understand what's going on.
There is lots of info on the web already but I felt I should make a tutorial for cables.gl users because this is one of the times where we are in the deep end of the 'dynamic web content' swimming pool. Lots of the existing tutorials on this topic just cover the cases for basic web development and it's not clear how it comes together to make accessible content for cables.gl.
This tutorial assumes you are already familiar with:
Codepen interactive tutorials
Reference info is below but it may be easier to learn incrementally with some tutorials.
Here is a little series I made on codepen.
Start with the first tutorial here
Collection of all the tutorials
CSS Position
The CSS position property can be confusing because each of its values controls several separate behaviours:
To add further confusion position settings can also influence and be influenced by element size.
I will only cover the cases necessary for typical use in cables.gl.
position:static
Static is the default position setting for HTML elements.
Static means a HTML element takes its layout from the normal flow of a document. We have to understand this as the baseline even if we will rarely use it in cables.gl.
Normal Flow is actually quite a complicated concept.
We will only worry about regular blocks, not inline-blocks as that's not so important for cables.gl.
In brief:
position:Absolute
Ok that sounds like something we can use, we do want HTML elements to overlap our canvas.
In order to use it properly we have to use position:relative as well.
position:Relative
Relative position means that
All of this adds up to some honestly arcane and confusing behaviour.
You can blame the iterative nature of decades of web development. We've got to deal with it.
Bringing it together in cables.gl
As the last step in the codepen tutorial demonstrated, the key to aligning HTML elements with the canvas is:
To ind the parent element of the canvas and set your new dynamic elements as siblings of the canvas see this patch.
See this example for how to set the DOM order of HTML elements
To actually dynamically set the offset positions of elements in a cables patch use the op TransformElement.
If you inspect a cables patch where you are using this you will see it dynamically sets the top and left css properties of the specific elements.
See the example patch for TransformElement here
To get HTML elements perfectly aligned with content drawn on the canvas see the op TransformElementWorldRect from AccessExt team.
This will also allow you to set size of elements dynamically taking into account camera perspective.
Check the documentation and example for TransformElementWorldRect here
References for CSS positioning.
See the MDN article on CSS position
MDN article on 'normal flow' of web pages
A good introduction video to CSS position
A good deep dive video on CSS position
Beta Was this translation helpful? Give feedback.
All reactions