- How To Style the HTML element with CSS
- Prerequisites
- Exploring the Element in Practice
- Adding and Styling Text in a Container
- How to Create a HTML Custom Element
- Example Custom Element For This Tutorial
- Custom Element Has Its Own HTML, CSS & Javascript
- Shadow DOM
- Defining Custom Element’s HTML & CSS
- Registering the Custom Element With Javascript
- Defining Custom Element’s Specific Javascript
- Complete Code
How To Style the HTML element with CSS
This tutorial will introduce you to styling the HTML Content Division element—or element—using CSS. The element can be used to structure the layout of a page and break up a webpage into separate components for individual styling. In this tutorial, you will create and style elements, as well as learn how to add and style other elements inside a container. These skills will prepare you to use elements as layout tools later on in the series when you begin recreating the demonstration website.
The
tags to an HTML document. On its own, the
Prerequisites
To follow this tutorial, make sure you have set up the necessary files and folders as instructed in a previous tutorial in this series How To Set Up You CSS and HTML Practice Project.
Exploring the Element in Practice
Let’s try a hands-on exercise to study how the element works. Erase everything in your styles.css file (if you added content from previous tutorials). Next, add the following CSS rule for the tag selector:
div background-color: green; height: 100px; width: 100px; >
Save the styles.css file. Next, return to your index.html file, erase everything that’s there (except for the first line of code: ) and add the following code snippet:
Notice that the element has opening and closing tags but does not require any content. Save the index.html file and reload it in your browser. (For instructions on loading an HTML file, please visit our tutorial step How To View An Offline HTML File In Your Browser).
Your webpage should display a green box 100 pixels wide and 100 pixels tall as specified by the CSS rule:
/* this code is present inside the defined class */ let template = document.querySelector('#input-plus-minus-template').content; this.attachShadow(< mode: 'open' >).appendChild(template.cloneNode(true));
// access #add within the custom element's DOM let add_button = this.shadowRoot.querySelector("#add"); let subtract_button = this.shadowRoot.querySelector("#subtract"); let count_textbox = this.shadowRoot.querySelector("#count"); // set initial "value" attribute this.setAttribute('value', '1'); // add 1 quantity add_button.addEventListener('click', () => < let current = parseInt(count_textbox.value, 10); count_textbox.value = current + 1; // set "value" attribute of the custom element this.setAttribute('value', count_textbox.value); >); // subtract 1 quantity subtract_button.addEventListener('click', () => < let current = parseInt(count_textbox.value, 10); if(current >1) < count_textbox.value = current - 1; this.setAttribute('value', count_textbox.value); >>);
With this, the Javascript for our custom element is complete. We can now start using the custom element.
Complete Code
We have set the «value» attribute to hold the current value. This can be used to get the current value of the element.
document.querySelector("#sample").getAttribute('value');