Category Archives: CSS

dynamic data in the DOM with css and js effects

ref – https://stackoverflow.com/questions/34896106/attach-event-to-dynamic-elements-in-javascript

index.html

1) we write an HTML page with a ul list that has three items.
2) apply some js effects to our li elements
3) create a button that would dynamically add a li item

script.jsjs

We have a simple script that attaches inline css and js effects to all the li elements.

Now, when you run the page, you’ll see the list with its effects. Specifically, some js added inline CSS effects and a click handler that displays the text of the li element.

This is because when the page has finished putting its DOM together, it runs the script.js file, and it also declares the addItem function.

The execution of script.js file makes it so that it applies the inline CSS and click handler to the existing li elements in the DOM.

The declaration of the addItem function exists to that later when the user clicks on the add button, we have a function that can run.

Thus, all js code executed so far comes after the readiness of the DOM.

Now, when you click on the li element, it would respond to the click event like so:

However, there is a problem. When you click on the ‘add’ button to dynamically add an item, you don’t see the inline css effects anymore. If you try clicking on the li elements, nothing happens:

CSS

Inline CSS is when we attach the CSS to the element itself.
Internal CSS is when we declare CSS on top of the html page.
External CSS is when we attach the CSS code in a separate file.

Thus, when we use inline CSS, it only applies to the loaded DOM. Any dynamic new data that comes in after won’t get the CSS effects.

In order to have CSS effects on all elements, including later added dynamic elements, simply use internal or external CSS.

In your script.js remove the css:

Then in the head tag of the html page, add some internal CSS:

Now, when you add dynamic li elements, you’ll see tht the CSS is applied immediately:

Note that you can also apply some inline CSS when the items were created and added to the DOM. That will work too, but it’s cumbersome, and we miss out on the benefits that CSS brings us:

Events were not applied

If you were to click on the li items, you’ll notice that even though internal/external CSS will aways apply to past, current, and future DOM changes, JS events won’t be applied to future DOM additions.

In order to apply it, we’ll implement an add event listener. It simply listens for a certain event, and when that event happens, it executes code.

script.js

Now when you refresh the page, every newly added li elements will the click event.

In jQuery, you’d use the .on function

In this way, all dynamically added elements will have your chosen events bound.

In certain situations when you are working with templates, a lot of effects are applied by JS on data elements. When these data elements are added dynamically, you won’t see your effects for these newly added elements. What you have to do is:

1) Make sure effects on data that are added/removed are CSS based.
2) Using addEventListener or .on, you’ll have to apply these effects on the fly.

tooltip using css and html (balloon)

css

html

selecting ranges with nth-child

https://stackoverflow.com/questions/15639247/css-selector-for-nth-range

Say we have a list.

We want to select all the list items and border it red.

We want to select all the odd

we want to seelct all the evens

We want to select ranges.

html

css

block, inline, and inline-block

block

start new line, takes up 100% of width. In other words, begins on a “new line”, and has an “carriage return” at the end. Notice how the last div with inline starts at new line:

inline

Gets appended to the left, and lines up towards the right.

They don’t have a definable height and width and will not create new rows in a layout (thus they appear “inline”).

If inline contains a block, the effect is negated. For example, in the example, the span has no effect. The testing 1 2 3 will all act like blocks.

inline-block

ref – http://designshack.net/articles/css/whats-the-deal-with-display-inline-block/

Historically, we use floats to align blocks of content, we would use a container with a width to make sure they stay in place. However, a quirky behavior is that when we float the children content, it would collapse the parent div because the parent div was not floated. You can resolve this by floating the parent div, however.

In other situations, if you do not float the parent div, your next div will be awkwardly lined up to the parent’s div. What you do in that case is to clear:both for the next div, and it would notice that the parent’s div are floating left, and thus would float below them. If you don’t do this, it would float below the collapsed parent’s div.

If you were to use inline-block, you wouldn’t need a parent div at all. it would work as expected. There would be no clear hacks. However you may need to use the vertical-align because due to inline-block starting text from bottom up, rather than top down.