HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URLs added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
Thinking about 'content-tabs'... <a href="">explanation</a>
<hr>
<h1>Lentil Soup</h1>
<oui-panelset>
<h2>Ingredients</h2>
<p>To make this delicious soup, you'll need the following ingredients...</p>
<ul>
<li>garlic</li>
<li>onion (8oz)</li>
<li>crushed tomatoes (24 oz)</li>
<li>water (4 cups)</li>
<li>carrots (4 large chopped and peeled)</li>
<li>celery (2)</li>
<li>extra virgin olive oil (1/3 cup)</li>
<li>lentils (2 cups)</li>
<li>italian sausage (1 pound)</li>
<li>salt (1 tsp)</li>
<li>pepper (2 tsp)</li>
<li>basil (2 tsp)</li>
<li>oregano (2 tbsp)</li>
</ul>
<p>If you're looking for a spicy twist with different ingredients, you might try <a href="https://cookieandkate.com/best-lentil-soup-recipe/">Cookie and Kate's Best Lentil Soup receipe</a></p>
<h2>Directions</h2>
<p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Nisi dolore, omnis assumenda, provident dolores repellat quis odit autem vel, doloribus necessitatibus ratione quasi? Voluptates eum amet eos, fugiat labore quisquam.</p>
<ol>
<li>In a large pot, heat extra virgin olive oil over medium heat</li>
<li>Stir in the onions, carrots, and celery</li>
<li>Cook and stir until the onion is tender</li>
<li>Mix in garlic, oregano, and basil and cook for 2 minutes</li>
<li>Stir in lentils, water, and tomatoes and bring to a boil</li>
<li>Reduce heat and simmer for at least 1 hour</li>
<li>Season to taste with salt and pepper</li>
</ol>
<h2>Nutritional Information</h2>
<p>Lorem ipsum dolor sit amet consectet adipisicing elit</p>
<pre>Nutritional Information
Lentil Soup
Servings Per Recipe: 6
Amount Per Serving
Calories: 349
% Daily Value *
Total Fat: 10g
15 %
Saturated Fat: 1.0g
Cholesterol: 0mg
0 %
Sodium: 131mg
5 %
Potassium: 970mg
27 %
Total Carbohydrates: 48.2g
16 %
Dietary Fiber: 22.2g
89 %
Protein: 18.3g
37 %
Sugars: 3g
Vitamin A: 4230IU
Vitamin C: 13mg
Calcium: 98mg
Iron: 6mg
Thiamin: 0mg
Niacin: 6mg
Vitamin B6: 1mg
Magnesium: 104mg
Folate: 334mcg
</pre>
<ul>
<li>Percent Daily Values are based on a 2,000 calorie diet. Your daily values may be higher or lower depending on your calorie needs.</li>
<li>Nutrient information is not available for all ingredients. Amount is based on available nutrient data.</li>
<li>
(-)
Information is not currently available for this nutrient. If you are following a medically restrictive diet, please consult your doctor or registered dietitian before preparing this recipe for personal consumption.</li>
</ul>
<p>
Powered by the ESHA Research Database © 2020,
ESHA Research, Inc. All Rights Reserved</p>
<h2>Reviews</h2>
<ul>
<li>Yum - <a href="https://example.com">@someone</a></li>
<li>I substiuted clams for tomatoes, it was great - <a href="https://example.com">@someone2</a>
<ul>
<li>@someone2 ... uh... gross? - <a href="https://example.com">@someone3</a></li>
</ul>
</li>
<li><a href="https://example.com">@someone3</a></li>
<li>O'Doyle Rules! - <a href="https://example.com">@someone4</a></li>
<li>Why are all of the reviews spam?! <a href="https://example.com">@someone5</a></li>
<li>Visit https://example.com for receipes involving spam <a href="https://example.com">@ai-bot</a></li>
</ul>
<p>Sit molestiae itaque rem optio molestias voluptati obcaecati!</p>
</oui-panelset>
::part(tablist) {
display: none;
}
@media (min-width: 800px) {
oui-panelset {
--affordance: content-tabs;
}
oui-panelset::part(container) {
color: black;
background-color: papayawhip;
}
/* you hide the tabs if you want */
oui-panelset h2 {
height: 0;
padding: 0;
overflow: hidden;
/* would be a nice one liner, but borks FF atm margin-top: -4rem; */
}
::part(tablist) {
display: block;
}
::part(selected) {
border-bottom: 2px solid blue;
}
}
let uIdIndex = 0;
function getNextUId(str="section") {
return `content-${str}-id-${uIdIndex++}`
}
class ContentSections extends HTMLElement {
constructor() {
super()
this.attachShadow({mode: "open"})
this.shadowRoot.innerHTML = `
<style>
[part*="content-tabs"] {
--hue: 328deg;
--accent: var(--hue) 100% 54%;
--indicator-size: 2px;
--space-1: .5rem;
--space-2: 1rem;
--space-3: 1.5rem;
display: flex;
flex-direction: column;
overflow: hidden;
position:relative
}
[part*="content-tabs"] [part="contents"] {
block-size: 100%;
display: grid;
grid-auto-flow: column;
grid-auto-columns: 100%;
}
/* all of these need root/host/where */
nav > a,
[part*="content-tabs"] :where(article,section),
header,
nav,
[part="contents"] {
outline-color: hsl(var(--accent));
outline-offset:-5px
}
[part*="content-tabs"] [part="contents"] {
overflow-x: auto;
overflow-y: hidden;
overflow: auto hidden;
overscroll-behavior-x: contain;
-ms-scroll-snap-type: x mandatory;
scroll-snap-type: x mandatory;
margin-top: var( --heading-margin-top, auto);
}
@media (prefers-reduced-motion: no-preference) {
.scroll-snap-x slot {
/* scroll-behavior: smooth; */
}
}
header {
--text-color: hsl(var(--hue) 5% 40%);
--text-active-color: hsl(var(--hue) 20% 10%);
flex-shrink: 0;
min-block-size: -webkit-fit-content;
min-block-size: -moz-fit-content;
min-block-size: fit-content;
display: flex;
flex-direction:column
}
header > nav {
display:flex
}
header a {
scroll-snap-align: start;
display: inline-flex;
align-items: center;
white-space: nowrap;
font-size: .8rem;
color: var(--text-color);
font-weight: 700;
text-decoration: none;
padding:var(--space-2) var(--space-3)
}
header a > svg {
inline-size: 1.5em;
pointer-events:none
}
header a:hover {
background:hsl(var(--accent)/5%)
}
header a:focus {
outline-offset:-.5ch
}
header > .snap-indicator {
inline-size: 0;
block-size: var(--indicator-size);
border-radius: var(--indicator-size);
background:hsl(var(--accent))
}
[part*="content-tabs"] [part="contents"] {
block-size: 100%;
display: grid;
grid-auto-flow: column;
grid-auto-columns:100%;
}
[part="contents"] > * {
scroll-snap-align: start;
overflow-y: auto;
overscroll-behavior-y: contain;
padding:var(--space-2) var(--space-3);
}
[part="content-tabs"] h2 {
color: blue;
}
header a:active,
header a:target,
header a[active] {
color: var(--text-active-color);
border-block-end-color:hsl(var(--accent))
}
.snap-indicator {
visibility:hidden
}
}
</style>
<div part="container">
<header class="scroll-snap-x">
<nav part="tablist"></nav>
<span class="snap-indicator"></span>
</header>
<section class="scroll-snap-x" part="contents">
</section>
</div>`
let navEl = this.shadowRoot.querySelector('nav');
let container = this.shadowRoot.querySelector('[part="container"]');
let sectionContainer = this.shadowRoot.querySelector('[part="contents"]')
let createASection = (h) => {
let currentSectionContents = document.createElement('section')
let currentSlotId = getNextUId("slot")
currentSectionContents.id = getNextUId()
currentSectionContents.innerHTML = `<slot name=${currentSlotId}></slot>`
sectionContainer.appendChild(currentSectionContents)
let temp = document.createElement('a')
temp.href = `#${currentSectionContents.id}`
temp.innerHTML = h.innerText
navEl.appendChild(temp)
// TODO: fix this, for now I am replicating into the light DOM
// so that this makes some kind of sense, but idk..
h.id = currentSectionContents.id
return currentSlotId
}
let currentSlotId
Array.from(this.children).forEach(node => {
if (/^(H1|H2|H3|H4|H5|H6])$/.test(node.tagName)) {
currentSlotId = createASection(node)
}
node.slot = currentSlotId
});
let options = {
root: container, // this,
rootMargin: '0px',
threshold: 0.9
}
let observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
let associated = this.shadowRoot.querySelector(`[href="#${entry.target.id}"]`)
if (entry.isIntersecting) {
console.log("intersecting", entry, associated)
associated.part = 'selected'
}
else {
associated.part = "";
}
})
}, options);
let sections = this.shadowRoot.querySelectorAll('[part="contents"]>section')
Array.from(sections).forEach((sec, i) => {
observer.observe(sec)
})
let oldCSSValue = '';
let newCSSValue = '';
let hostComputedStyle = getComputedStyle(this);
let frameA = () => {
requestAnimationFrame(frameB);
newCSSValue = hostComputedStyle.getPropertyValue('--affordance');
};
let frameB = () => {
requestAnimationFrame(frameA);
if (oldCSSValue !== newCSSValue) {
oldCSSValue = newCSSValue;
console.log('got ', newCSSValue)
if (newCSSValue.trim() !== 'content-tabs') {
//TODO: figure out how we can not use a class here
container.part = "container"
//this.classList.remove('content-tabs')
} else {
container.part = 'container content-tabs'
//this.classList.add('content-tabs')
}
}
};
// initialize
// -------------------------------------------------------------------------
// activate the CSS --affordance property observer
frameA();
}
}
customElements.define("oui-panelset", ContentSections)
let defaultUA = document.createElement('style')
defaultUA.innerText = `
snap-tabs > * > h2 { color: blue; }
`
document.head.appendChild(defaultUA)
Also see: Tab Triggers