Html common header all pages

Html common header all pages

  • Introduction to HTML CSS | Learn to Design your First Website in Just 1 Week
  • HTML Course | Structure of an HTML Document
  • HTML Course First Web Page Printing Hello World
  • HTML Course Basics of HTML
  • HTML Course : Starting the Project – Creating Directories
  • HTML Course Understanding and Building Project Structure
  • HTML Course : Creating Navigation Menu
  • HTML Course : Building Header of the Website
  • HTML Course : Building Main Content – Section 1
  • HTML Course | Building Main Content – Section 2
  • HTML course | Building Main Content – Section 3
  • HTML Course | Building Footer
  • HTML Course | Practice Quiz 1
  • HTML Course | Practice Quiz 2

HTML Questions

  • Create a Sticky Social Media Bar using HTML and CSS
  • Create a Search Bar using HTML and CSS
  • How to create Right Aligned Menu Links using HTML and CSS ?
  • How to add a Login Form to an Image using HTML and CSS ?
  • How to Create a Tab Image Gallery ?
  • How to create a Hero Image using HTML and CSS ?
  • How to design Meet the Team Page using HTML and CSS ?
  • How to Create an Image Overlay Icon using HTML and CSS ?
  • How to Create Browsers Window using HTML and CSS ?
  • How to Create Breadcrumbs using HTML and CSS ?
  • How to Create Section Counter using HTML and CSS ?
  • How to Create Toggle Switch by using HTML and CSS ?
  • How to Create a Cutout Text using HTML and CSS ?
  • How to make a Pagination using HTML and CSS ?

HTML Basics

  • HTML Tutorial
  • HTML Introduction
  • HTML full form
  • HTML Editors
  • HTML Comments
  • HTML Basics
  • HTML Layout
  • HTML Elements
  • HTML Heading
  • HTML Paragraphs
  • HTML Text Formatting
  • HTML Quotations
  • HTML Color Styles and HSL
  • HTML Links
  • HTML Images
  • HTML Tables
  • HTML Lists
  • HTML Block and Inline Elements
  • HTML Iframes
  • HTML File Paths
  • HTML Viewport meta tag for Responsive Web Design
  • HTML Computer Code Elements
  • HTML Entities
  • HTML Charsets
  • HTML | URL Encoding
  • HTML | Deprecated Tags

HTML Tags

HTML Attributes

  • HTML Attributes
  • HTML | accept Attribute
  • HTML accept-charset Attribute
  • HTML accesskey Attribute
  • HTML| action Attribute
  • HTML align Attribute
  • HTML alt attribute
  • HTML | async Attribute
  • HTML input autocomplete Attribute
  • HTML autocomplete Attribute
  • HTML autofocus Attribute
  • HTML input autofocus Attribute
  • HTML autofocus Attribute
  • HTML autofocus Attribute
  • HTML Attributes Complete Reference

HTML Graphics

HTML APIs

HTML DOM

  • DOM (Document Object Model)
  • HTML DOM activeElement Property
  • HTML DOM anchors Collection
  • HTML DOM close() Method
  • HTML DOM baseURI Property
  • HTML DOM body Property
  • HTML DOM createAttribute() Method
  • HTML DOM doctype Property
  • HTML DOM writeln() Method
  • HTML DOM console error() Method
  • HTML DOM URL Property
  • HTML DOM embeds Collection
  • HTML DOM console warn() Method
  • HTML DOM console trace() Method
  • HTML DOM Complete Reference
Читайте также:  Webkit css only one

HTML Audio/Video

  • HTML DOM Audio Object
  • HTML DOM Video Object
  • HTML DOM Video canPlayType( ) Method
  • HTML DOM Audio audioTracks Property
  • HTML DOM Audio autoplay Property
  • HTML DOM Audio buffered Property
  • HTML DOM Audio controls Property
  • HTML DOM Audio currentSrc Property
  • HTML DOM Audio currentTime Property
  • HTML DOM Audio defaultMuted Property
  • HTML DOM Audio defaultPlaybackRate Property
  • HTML DOM Audio duration Property
  • HTML DOM Audio ended Property
  • HTML DOM Audio loop Property
  • HTML DOM Audio/Video Complete Reference

HTML 5

HTML 5 MathML

  • Introduction to HTML CSS | Learn to Design your First Website in Just 1 Week
  • HTML Course | Structure of an HTML Document
  • HTML Course First Web Page Printing Hello World
  • HTML Course Basics of HTML
  • HTML Course : Starting the Project – Creating Directories
  • HTML Course Understanding and Building Project Structure
  • HTML Course : Creating Navigation Menu
  • HTML Course : Building Header of the Website
  • HTML Course : Building Main Content – Section 1
  • HTML Course | Building Main Content – Section 2
  • HTML course | Building Main Content – Section 3
  • HTML Course | Building Footer
  • HTML Course | Practice Quiz 1
  • HTML Course | Practice Quiz 2

HTML Questions

  • Create a Sticky Social Media Bar using HTML and CSS
  • Create a Search Bar using HTML and CSS
  • How to create Right Aligned Menu Links using HTML and CSS ?
  • How to add a Login Form to an Image using HTML and CSS ?
  • How to Create a Tab Image Gallery ?
  • How to create a Hero Image using HTML and CSS ?
  • How to design Meet the Team Page using HTML and CSS ?
  • How to Create an Image Overlay Icon using HTML and CSS ?
  • How to Create Browsers Window using HTML and CSS ?
  • How to Create Breadcrumbs using HTML and CSS ?
  • How to Create Section Counter using HTML and CSS ?
  • How to Create Toggle Switch by using HTML and CSS ?
  • How to Create a Cutout Text using HTML and CSS ?
  • How to make a Pagination using HTML and CSS ?

Источник

Reusable HTML Components – How to Reuse a Header and Footer on a Website

Kris Koishigawa

Kris Koishigawa

Reusable HTML Components – How to Reuse a Header and Footer on a Website

Imagine you’re building a site for a client, a small mom-and-pop store, that only has two pages.

That’s not a lot. So when you finish working on the landing page and start on the contact page, you just create a new HTML file and copy over all the code from the first page.

The header and footer are already looking good, and all you need to do is change the rest of the content.

But what if your client wants 10 pages? Or 20? And they request minor changes to the header and footer throughout development.

Suddenly any change, no matter how small, has to be repeated across all those files.

This is one of the major problems things like React or Handlebars.js solve: any code, especially structural things like a header or footer, can be written once and easily reused throughout a project.

Until recently it wasn’t possible to use components in vanilla HTML and JavaScript. But with the introduction of Web Components, it’s possible to create reusable components without using things like React.

Читайте также:  Html map generator svg

What Are Web Components?

Web Components are actually a collection of a few different technologies that allow you to create custom HTML elements.

  • HTML templates: Fragments of HTML markup using elements that won’t be rendered until they’re appended to the page with JavaScript.
  • Custom elements: Widely supported JavaScript APIs that let you create new DOM elements. Once you create and register a custom element using these APIs, you can use it similarly to a React component.
  • Shadow DOM: A smaller, encapsulated DOM that is isolated from the main DOM and rendered separately. Any styles and scripts you create for your custom components in the Shadow DOM will not affect other elements in the main DOM.

We’ll dive into each of these a bit more throughout the tutorial.

How to Use HTML Templates

The first piece of the puzzle is learning how to use HTML templates to create reusable HTML markdown.

Let’s look at a simple welcome message example:

The developer console showing the welcome message template element.

To actually render the welcome message, you’ll need to use a bit of JavaScript:

The browser showing the welcome message template element, and the actual welcome message content.

Even though this is a pretty simple example, you can already see how using templates makes it easy to reuse code throughout a page.

The main issue is that, at least with the current example, the welcome message code is mixed in with the rest of the page’s content. If you want to change the welcome message later, you’ll need to change the code across multiple files.

Instead, you can pull the HTML template into the JavaScript file, so any page the JavaScript is included in will render the welcome message:

The browser with the header component.

Now adding a header to the page is as easy as adding a tag pointing to components/header.js , and adding wherever you want.

Note that, since the header and its styling are being inserted into the main DOM directly, it’s possible to style it in the style.css file.

But if you look at the header styles included in connectedCallback , they’re quite general, and can affect other styling on the page.

For example, if we add Font Awesome and a footer component to index.html :

The browser with the header and footer components.

The styling from the footer component overrides the styling for the header, changing the color of the links. That’s expected behavior for CSS, but it would be nice if each component’s styling was scoped to that component, and wouldn’t affect other things on the page.

Well, that’s exactly where the Shadow DOM shines. Or shades? Anyway, the Shadow DOM can do that.

How to Use the Shadow Dom with Custom Elements

The Shadow DOM acts as a separate, smaller instance of the main DOM. Rather than act as a copy of the main DOM, the Shadow DOM is more like a subtree just for your custom element. Anything added to a Shadow DOM, especially styles, are scoped that particular custom element.

In a way, it’s like using const and let rather than var .

Let’s start by refactoring the header component:

The browser with the Shadow DOM header component and regular footer component.

And here’s the footer component refactored to use the Shadow DOM:

The browser showing the Shadow DOM versions of both the header and footer components.

Now that the footer component is encapsulated within its own Shadow DOM, it no longer has access to the Font Awesome CDN link in index.html .

Читайте также:  GeekTechStuff

Let’s take a quick look at why this is, and how to get Font Awesome working again.

Encapsulation and the Shadow DOM

While the Shadow DOM does prevent styles from your components from affecting the rest of the page, some global styles can still leak through to your components.

In the examples above, this has been a useful feature. For example, the footer component inherits the color: #333 declaration that’s set in style.css . This is because color is one of a handful of inheritable properties, along with font , font-family , direction , and more.

If you’d like to prevent this behavior, and style each component completely from scratch, you can do that with just a few lines of CSS:

:host is a pseudo-selector that selects the element that’s hosting the Shadow DOM. In this case, that’s your custom component.

Then the all: initial declaration sets all CSS properties back to their initial value. And display: block does the same thing for the display property, and sets it back to the browser default, block .

For a full list of CSS inheritable properties, check out this answer on Stack Overflow.

How to Use Font Awesome With the Shadow DOM

Now you might be thinking, if font , font-family and other font-related CSS properties are inheritable properties, why doesn’t Font Awesome load now that the footer component is using the Shadow DOM?

It turns out that, for things like fonts and other assets, they need to be referenced in both the main DOM and Shadow DOM to work properly.

Fortunately there are a few simple ways to fix this.

Note: All of these methods still require that Font Awesome is included in index.html with the link element as in the code snippets above.

The most straightforward way to get Font Awesome to work in your Shadow DOM component is to include a link to it within the component itself:

The developer console network tab showing that Font Awesome only loads once.

#2: Import Font Awesome Within Your Component

Next, you can use @import and url() to load Font Awesome into your component:

const footerTemplate = document.createElement('template'); footerTemplate.innerHTML = ` @import url("https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css"); footer < height: 60px; padding: 0 10px; list-style: none; . 

Note that the URL should be the same one you're using in index.html .

#3: Use JavaScript to Dynamically Load Font Awesome to Your Component

Finally, the DRYest way to load Font Awesome within your component is to use a bit of JavaScript:

The developer console showing that Font Awesome was dynamically added to the Shadow DOM component.

Final Code

Here's what the final code across all files looks like, and using method #3 to load Font Awesome into the footer component:

In Closing

We've covered a lot here, and you might have already decided just to use React or Handlebars.js instead.

Those are both great options!

Still, for a smaller project where you'll only need a few reusable components, a whole library or templating language might be overkill.

Hopefully now you have the confidence to create your own reusable HTML components. Now get out there and create something great (and reusable).

Источник

Оцените статью