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 URL's 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 it's URL and the proper URL extention.
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 Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
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="app"/>
<!-- This example works for desktop browser.
Select text to show menu, and click the tweet icon on menu to tweet selected text. -->
$paleCream: #f0e6d8
$darkGrey: #3c3c3b
$darkPaleNavy: #53525e
html
height: 100%
body
padding: 4em 8em
background-color: $paleCream
h1
font-family: 'Spectral', serif
font-size: 2.5em
font-style: bold
h3
font-family: 'Spectral', serif
font-size: 0.75em
font-weight: 300
letter-spacing: 0.2em
color: $darkGrey
p
font-family: 'Spectral', serif
font-size: 1em
line-height: 1.5em
.poemContent
margin-top: 1.5em
position: relative
.tooltip, .tooltipContent
z-index: 2
position: absolute
left: 0
top: 0
display: inline-block
.tooltip .tooltipContent::after
content: ""
position: absolute
top: 100%
left: 50%
margin-left: -6px
border: 6px solid
border-color: rgba(0,0,0,0.8) transparent transparent transparent
.tooltipContent
padding: 0.25em
background-color: rgba(0,0,0,0.8)
height: 2em
width: max-content
border-radius: 0.5em
.feather
color: #ffffff
margin: 0.25em
cursor: pointer
class ContextMenu extends React.Component {
constructor(props) {
super(props)
this.setWrapperRef = this.setWrapperRef.bind(this)
}
componentDidMount() {
feather.replace()
document.addEventListener('mousedown', this.handleClickOutside)
}
componentWillUnmount() {
document.removeEventListener('mousedown', this.handleClickOutside)
}
setWrapperRef(node) {
this.wrapperRef = node
}
handleClickOutside = (e) => {
const {status, closeContextMenu} = this.props
if (status && this.wrapperRef && !this.wrapperRef.contains(event.target)) {
closeContextMenu()
}
}
render() {
const { status, text, x, y } = this.props
return (
<div
className='tooltip'
ref={this.setWrapperRef}
style={
status ?
{display:'inline-block', top:y, left:x}
: {display:'none'}}
>
<div className='tooltipContent'>
<a class="twitter-share-button"
href={`https://twitter.com/intent/tweet?related="bmwiputri,codepen"&text=${encodeURIComponent(text)}`} target="_blank">
<i data-feather="twitter"/>
</a>
</div>
</div>
)
}
}
class Poem extends React.Component {
constructor() {
super()
this.state = {
showContextMenu: {
status: false,
x: 0,
y: 0
},
highlightedText: ''
}
}
handleMouseUp = () => {
var selection = '';
if (window.getSelection()) {
selection = window.getSelection()
} else if (document.getSelection()) {
selection = document.getSelection()
} else if (document.selection) {
selection = document.selection.createRange().text
}
if (selection.toString().length > 0) {
const { scrollX, scrollY } = window
const range = selection.getRangeAt(0);
const getRect = range.getBoundingClientRect();
const { y, x, right } = getRect
this.setState({
showContextMenu: {
status: true,
x: `calc(${right}px - 2.25em)`,
y: `calc(${y + scrollY}px - 2.5em)`
},
highlightedText:selection.toString()
})
}
}
closeContextMenu = () => {
this.setState({
showContextMenu: {
status: false,
x: 0,
y: 0
},
highlightedText:''
})
}
render() {
const { showContextMenu, highlightedText } = this.state
const { status, x, y } = showContextMenu
const tweetedText = `"${highlightedText}"
-- sent from https://codepen.io/brigitamaria/pen/dEqXEm`
return(
<>
<ContextMenu status={status} text={tweetedText} x={x} y={y} closeContextMenu={this.closeContextMenu}/>
<>
<h1>If—</h1>
<h3>BY RUDYARD KIPLING</h3>
</>
<p className='poemContent' onMouseUp={this.handleMouseUp}>
If you can keep your head when all about you<br/>
Are losing theirs and blaming it on you,<br/>
If you can trust yourself when all men doubt you,<br/>
But make allowance for their doubting too; <br/>
If you can wait and not be tired by waiting,<br/>
Or being lied about, don’t deal in lies,<br/>
Or being hated, don’t give way to hating,<br/>
And yet don’t look too good, nor talk too wise:<br/><br/>
If you can dream—and not make dreams your master;<br/>
If you can think—and not make thoughts your aim; <br/>
If you can meet with Triumph and Disaster<br/>
And treat those two impostors just the same;<br/>
If you can bear to hear the truth you’ve spoken<br/>
Twisted by knaves to make a trap for fools,<br/>
Or watch the things you gave your life to, broken,<br/>
And stoop and build ’em up with worn-out tools:<br/><br/>
If you can make one heap of all your winnings<br/>
And risk it on one turn of pitch-and-toss,<br/>
And lose, and start again at your beginnings<br/>
And never breathe a word about your loss;<br/>
If you can force your heart and nerve and sinew<br/>
To serve your turn long after they are gone,<br/>
And so hold on when there is nothing in you<br/>
Except the Will which says to them: ‘Hold on!’<br/> <br/>
If you can talk with crowds and keep your virtue, <br/>
Or walk with Kings—nor lose the common touch,<br/>
If neither foes nor loving friends can hurt you,<br/>
If all men count with you, but none too much;<br/>
If you can fill the unforgiving minute<br/>
With sixty seconds’ worth of distance run, <br/>
Yours is the Earth and everything that’s in it,<br/>
And—which is more—you’ll be a Man, my son!
</p>
</>
)
}
}
ReactDOM.render(<Poem />, document.getElementById('app'));
Also see: Tab Triggers