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.
<div id="root"></div>
body {
margin: 0;
background-color: #171b21;
font-family: "Roboto Mono", monospace;
}
.header {
display: flex;
justify-content: center;
align-items: center;
background-color: #ffdd40;
padding: 8px;
position: sticky;
top: 0;
* {
margin: 0;
}
h1 {
font-size: 20px;
}
span {
margin-left: 16px;
}
input {
width: 64px;
}
label,
input {
font-size: 16px;
margin-left: 16px;
}
}
article {
margin: 0 auto;
color: #e8e9eb;
img {
display: block;
width: 60%;
margin: 1rem auto;
}
blockquote {
border: 1px solid gray;
border-radius: 4px;
padding: 0 8px;
margin: 0 auto;
width: 60%;
p {
font-style: italic;
font-size: 0.6em;
}
}
}
function clampBuilder(minWidthPx, maxWidthPx, minFontSize, maxFontSize) {
const root = document.querySelector("html");
const pixelsPerRem = Number(getComputedStyle(root).fontSize.slice(0, -2));
const minWidth = minWidthPx / pixelsPerRem;
const maxWidth = maxWidthPx / pixelsPerRem;
const slope = (maxFontSize - minFontSize) / (maxWidth - minWidth);
const yAxisIntersection = -minWidth * slope + minFontSize;
return `clamp(${minFontSize}rem, ${yAxisIntersection}rem + ${
slope * 100
}vw, ${maxFontSize}rem)`;
}
function calculateCh(element, fontSize) {
const zero = document.createElement("span");
zero.innerText = "0";
zero.style.position = "absolute";
zero.style.fontSize = fontSize;
element.appendChild(zero);
const chPixels = zero.getBoundingClientRect().width;
element.removeChild(zero);
return chPixels;
}
const initialRootFontSize = Number(
getComputedStyle(document.querySelector("html")).fontSize.slice(0, -2)
);
function Header({ rootFontSize, setRootFontSize }) {
const [viewportWidth, setViewportWidth] = React.useState(innerWidth);
React.useEffect(() => {
addEventListener("resize", () => setViewportWidth(innerWidth));
}, []);
const rootFontSizeHandler = (e) => {
setRootFontSize(e.target.value);
document.querySelector("html").style.fontSize = `${e.target.value}px`;
};
return (
<div className="header">
<h1>Viewport width: {viewportWidth}px</h1>
<span>|</span>
<label htmlFor="root-font-size">Root font size (px):</label>
<input
id="root-font-size"
type="number"
value={rootFontSize}
min={12}
max={20}
onChange={rootFontSizeHandler}
/>
</div>
);
}
function App() {
const [rootFontSize, setRootFontSize] = React.useState(initialRootFontSize);
const articleRef = React.useRef();
React.useEffect(() => {
articleRef.current.style.fontSize = clampBuilder(320, 960, 0.7, 2.1);
articleRef.current.style.width = `${
(320 / calculateCh(articleRef.current, "0.7rem")) * 0.8
}ch`;
}, [rootFontSize]);
return (
<div>
<Header {...{ rootFontSize, setRootFontSize }} />
<article ref={articleRef}>
<img src="https://studio.uxpincdn.com/studio/wp-content/uploads/2015/10/responsive-web-design.png" />
<h1>Responsive design</h1>
<p>
In the early days of web design, pages were built to target a
particular screen size. If the user had a larger or smaller screen
than the designer expected, results ranged from unwanted scrollbars to
overly long line lengths, and poor use of space.
</p>
<p>
As more diverse screen sizes became available, the concept of
responsive web design (RWD) appeared, a set of practices that allows
web pages to alter their layout and appearance to suit different
screen widths, resolutions, etc. It is an idea that changed the way we
design for a multi-device web, and in this article, we'll help you
understand the main techniques you need to know to master it.
</p>
<h1>Diversity of devices</h1>
<blockquote>
<p>
Day by day the number of devices, platforms that need to work with
your site grows. Responsive web design presents a fundamental shift
in how we will build websites for the decades to come.
</p>
<p>— Jeffrey Veen</p>
</blockquote>
<p>
The display resolution or display modes of a digital television,
computer monitor or display device is the number of distinct pixels in
each dimension that can be displayed. It can be an ambiguous term
especially as the displayed resolution is controlled by different
factors in cathode ray tube (CRT) displays, flat-panel displays
(including liquid-crystal displays) and projection displays using
fixed picture-element (pixel) arrays.
</p>
<p>
It is usually quoted as width × height, with the units in pixels: for
example, 1024 × 768 means the width is 1024 pixels and the height is
768 pixels. This example would normally be spoken as "ten twenty-four
by seven sixty-eight" or "ten twenty-four by seven six eight" (in
American English; in British English, usually "one oh two four by
seven six eight").
</p>
</article>
</div>
);
}
ReactDOM.render(<App />, document.getElementById("root"));
Also see: Tab Triggers