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 class="marker-container">
<div class="marker-items">
選択範囲マーカー
<div class="marker">
<input id="input-marker" />
</div>
<div class="marker">
<textarea id="textarea-marker"></textarea>
</div>
</div>
</div>
body {
text-align: center;
}
.marker {
}
.marker-container {
display: flex;
justify-content: center;
padding: 24px;
}
.marker-items {
margin: 8px;
}
input {
width: 200px;
margin: 8px auto;
padding: 8px;
}
textarea {
width: 200px;
height: 120px;
padding: 8px;
}
.input-marker {
display: block;
font-size: 12px;
padding: 4px 6px;
position: absolute;
background-color: black;
border-radius: 8px;
color: yellow;
white-space: nowrap;
width: auto;
z-index: 9999;
}
const mouseDownCoords = { x: 0, y: 0 };
const setMouseDownCoords = (e) => {
mouseDownCoords.x = e.clientX
mouseDownCoords.y = e.clientY
}
/**
* テキスト入力内の指定された選択箇所におけるspanの絶対位置のx、y座標を返却
* @param {object} input - 座標を取得する入力要素
* @param {number} selectionPoint - 入力の選択箇所
*/
const getCursorXY = (input, selectionPoint) => {
const {
offsetLeft: inputX,
offsetTop: inputY,
} = input
// 入力のクローンとなるダミー要素を作成
const div = document.createElement('div')
// 入力の計算されたスタイルを取得し、ダミー要素にコピー
const copyStyle = getComputedStyle(input)
for (const prop of copyStyle) {
div.style[prop] = copyStyle[prop]
}
div.style.position = "fixed";
div.style.top = "0px";
div.style.left = "0px";
div.style.opacity = 0;
// <input/>の場合空白を置き換える
const swap = '*'
const inputValue = input.tagName === 'INPUT' ? input.value.replace(/ /g, swap) : input.value
// テキストエリアの選択箇所までのdivの内容を設定する
const textContent = inputValue.substring(0, selectionPoint)
// ダミー要素divのテキストコンテンツを設定
div.textContent = textContent
if (input.tagName === 'TEXTAREA') div.style.height = 'auto'
if (input.tagName === 'INPUT') div.style.width = 'auto'
// span要素を作成してキャレット位置を取得する
const span = document.createElement('span')
span.textContent = inputValue.substring(selectionPoint) || '.'
// ダミー要素にspanマーカーを追加
div.appendChild(span)
// ダミー要素をbodyに追加
document.body.appendChild(div)
const { offsetLeft: spanX, offsetTop: spanY } = span
document.body.removeChild(div)
return {
x: inputX + spanX,
y: inputY + spanY,
}
}
/**
* ユーザーが入力コンテンツを選択した位置にマーカーを表示
* @param {object} e - テキスト選択のmouseupイベント
*/
/**
* ユーザーが入力コンテンツを選択した位置にマーカーを表示
* @param {object} e - テキスト選択のmouseupイベント
*/
const setMarkerNearSelectionArea = e => {
// grab the input element
const { currentTarget: input } = e
// 入力に関連するプロパティを取得する
const {
offsetLeft,
offsetWidth,
scrollLeft,
scrollTop,
selectionStart,
selectionEnd,
} = input
/**
* マーカーの座標を選択範囲を元に設定する
*/
const setMarkerPosition = () => {
// 選択開始位置の座標を取得
const { y: startTop, x: startLeft } = getCursorXY(input, selectionStart)
// 選択終了位置の座標を取得
const { y: endTop, x: endLeft } = getCursorXY(input, selectionEnd)
const { paddingRight, fontSize } = getComputedStyle(input)
const fontSizeNumber = parseInt(fontSize.replace("px", ""))
// 選択範囲の上部に表示するので endTop は無視
// scrollTop: スクロール位置の考慮
// 水平位置はお好みで
const endPoint =
startTop !== endTop ? offsetLeft + (offsetWidth - parseInt(paddingRight, 10)) : endLeft
// x: 選択開始位置 + 始点と終点の半分の値
const newLeft = startLeft + ((endPoint - startLeft) / 2)
// マーカーの位置を設定する
// フォントサイズの2倍くらいの高さを引くといい感じの所になる
input.__marker.setAttribute('style', `left: ${newLeft - scrollLeft}px; top: ${startTop - scrollTop - fontSizeNumber*2 }px`)
}
/**
* マーカーを作成する
* @param {*} content マーカーのテキスト
* @returns
*/
const createMarker = (content) => {
const marker = document.createElement('div')
marker.classList.add('input-marker', 'input-marker--visible')
marker.textContent = content
input.__marker = marker;
// documentに挿入
document.body.appendChild(marker)
document.addEventListener('click', removeMarkerIfClickOutPosition)
setMarkerPosition()
return marker
}
const removeMarker = () => {
document.body.removeChild(input.__marker)
document.removeEventListener('click', removeMarkerIfClickOutPosition)
input.__marker = null;
}
// 条件によってマーカーを非表示にする
const removeMarkerIfClickOutPosition = evt => {
// 入力系タグの外をクリックした
if (e !== evt && e.target !== evt.target) {
toggleMarker(false);
}
// mousedown時とmouseup時の座標が一緒
if (mouseDownCoords.x === e.clientX && mouseDownCoords.y === e.clientY) {
toggleMarker(false);
}
}
// マーカーの表示を切り替える関数を作成する
const toggleMarker = (flag) => {
// 一旦マーカーは削除する
if (input.__marker) {
removeMarker()
}
if (flag) {
createMarker("selection!")
}
}
// 選択範囲なし -> マーカーを非表示にする
if (selectionStart === selectionEnd) {
toggleMarker(false)
return
}
// マーカーが表示されておらず、選択範囲がある場合はマーカーを表示する
if (selectionStart !== selectionEnd) {
toggleMarker(true)
}
}
const getSelectionInputArea = document.querySelector('#input-marker');
getSelectionInputArea.addEventListener('mousedown', setMouseDownCoords)
getSelectionInputArea.addEventListener('mouseup', setMarkerNearSelectionArea)
const getSelectionTextArea = document.querySelector('#textarea-marker');
getSelectionTextArea.addEventListener('mousedown', setMouseDownCoords)
getSelectionTextArea.addEventListener('mouseup', setMarkerNearSelectionArea)
document.addEventListener("DOMContentLoaded", () => {
document.getElementById("input-marker").value="LFc739EWpCMCAyfuoIbA6YjhRM6xwrMJAoUCAQbHEka5Nb6rhC9ZjqTq6MO82yVdKjajbApViSGAa5wotRcF2movvsqzfcCAdy5JzTE8VGzHCyIT9wDIhem12YiV7gsCWiQXygXrzwJ2TzGCbw0yoWRC1Z6qjXPFsv5sqG3JVCICtgb6Mkx332CTdGwdhsvxazcC5F0ZYdohynt8bUFTDld7VWCsMluDRrV5hEZ9Pq5vPfQsJYqrbW9mmEqJIHD9NvZCjMuMUW1faIJfdfgtW4cRBUdHRVQCEy3DUwjBqGbnmvmUF5lkyoRCgfD36jaGDo2Wgw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XbRCjE7hnz1JDywwQFU4p3KPA6OluJVVj1STV7IdfcjO1waQXcPxWNaE2ZHK47MuuNNgfgvlCvAJBnT5MZchW4mwSC07QdFj6C7awrns0CRV6Go6WOI8GGsGmYyPo3BM0LOndFR9CcJPi6D9IZD6xYBoT4A2iJYOjdY4K4w1N7LTvyeKeGxFTxepYGOQqpWEFt0yCzX5OpG1SSHYFDeSKfVQbNEqIEZiYMV2LXIFAV9PwU6PiMuj5SAlssyZCFyC9WqlwoDJot3GY3qrjpC75fuG1Z0bZ2Apgd4LZIHwsDghlSWvUlUmPHj5YFCUlkMKadtAMjJYwVt5CZtyV3h1HkkY6gTufhfVRqk9YezvECxAzUSLJsNTIICs3M46t1VJ2DgJXr24LCX6Okd4dhBLzeOZQDa25R03PshRkjGDjzr6KdLSz1lqF7crz08StQMdiq8OwbvVl305JMDURzB9fDTAqQYYAknSHh6rZprLML88212E9AoXj7LpgUWC95SlzNUuxCDHRC2Hq6R8YWob1pUINUC0LOOobNCQSDSuYdw2JH5IW6HkDNTDPv4hSh3ijsU6drz7TSat1ueU7jDkxZslBd1FzriLXP4Lhj";
})
Also see: Tab Triggers