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.
#app
@import url(https://fonts.googleapis.com/css?family=Lato:400,700,900)
$primary = #0099aa
*
box-sizing border-box
body
margin-top 2rem
font-family 'Lato', sans-serif
background lighten($primary, 7)
color white
-webkit-font-smoothing antialiased
font-size 1.1rem
// searchbox component
.searchbox
max-width 20rem
margin 1rem auto
&__input
height 3rem
width 100%
box-shadow none
border none
padding 0 1rem
color darken($primary, 20)
border-radius .2rem
&__button
width 100%
padding 1rem 2rem
border none
margin 1rem 0 0 0
background lighten($primary, 20)
color darken($primary, 10)
font-weight 700
border-radius .4rem
&:hover
background lighten($primary, 15)
// the card component
.card
position relative
background lighten($primary, 5)
max-width 20rem
margin 7rem auto 2rem
border solid 5px $primary
border-radius .4rem
padding 5.5rem 2.5rem 1rem
&__avatar
position absolute
top 0
left 50%
transform translate(-50%, -50%)
width 10rem
height auto
text-align center
margin 0 auto
display block
border-radius 50%
background $primary
padding 5px
max-width 100%
transition 0.2s
&:hover
background darken($primary, 5)
&__username
font-size 2rem
font-weight 900
text-align center
margin 1rem auto
&__link
color white
border-bottom solid 2px white
text-decoration none
margin-bottom 0
padding-bottom 0
&:hover
opacity 0.8
&__notfound
text-align center
margin 0 auto
padding 1rem
/* some non-BEM stuff to avoid overloading the JSX for demo purposes */
dl
margin 0
padding 0
dt, dd
text-align center
dt
font-weight 900
margin-bottom .2rem
dd
margin 0 0 1rem 0
const Component = React.Component
class App extends Component {
constructor(props) {
super(props)
this.state = {
username: 'simonswiss',
realName: '',
avatar: '',
location: '',
repos: '',
followers: '',
url: '',
notFound: ''
}
}
render() {
return (
<div>
<SearchBox fetchUser={this.fetchUser.bind(this)}/>
<Card data={this.state} />
</div>
)
}
// the api request function
fetchApi(url) {
fetch(url)
.then((res) => res.json() )
.then((data) => {
// update state with API data
this.setState({
username: data.login,
realName: data.name,
avatar: data.avatar_url,
location: data.location,
repos: data.public_repos,
followers: data.followers,
url: data.html_url,
notFound: data.message
})
})
.catch((err) => console.log('oh no!') )
}
fetchUser(username) {
let url = `https://api.github.com/users/${username}`
this.fetchApi(url)
}
componentDidMount() {
let url = `https://api.github.com/users/${this.state.username}`
this.fetchApi(url)
}
}
class SearchBox extends Component {
render() {
return (
<form
className="searchbox"
onSubmit={this.handleClick.bind(this)}>
<input
ref="search"
className="searchbox__input"
type="text"
placeholder="type username..."/>
<input
type="submit"
className="searchbox__button"
value="Search GitHub User" />
</form>
)
}
handleClick(e) {
e.preventDefault()
let username = this.refs.search.getDOMNode().value
// sending the username value to parent component to fetch new data from API
this.props.fetchUser(username)
this.refs.search.getDOMNode().value = ''
}
}
class Card extends Component {
render() {
let data = this.props.data
if (data.notFound === 'Not Found') {
// when username is not found...
return <h3 className="card__notfound">User not found. Try again!</h3>
} else {
// if username found, then...
return (
<div className="card">
<a href={data.url} target="_blank">
<img className="card__avatar" src={data.avatar} />
</a>
<h2 className="card__username">
<a className="card__link" href={data.url} target="_blank">{data.username}</a></h2>
<dl>
<dt>Real name</dt>
<dd>{data.realName}</dd>
<dt>Location</dt>
<dd>{data.location}</dd>
<dt>Number of public repos</dt>
<dd>{data.repos}</dd>
<dt>Number of followers</dt>
<dd>{data.followers}</dd>
</dl>
</div>
)
}
}
}
React.render(<App />, document.getElementById('app'))
Also see: Tab Triggers