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. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.
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.
If the stylesheet 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 CSS 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.
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.
<!--<script src="https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js"></script>-->
<div id="root">
</div>
// You have the array of quotes. Each quote has body and author.
// It would be better to use "body" key instead of "quote"
// because an object is a quote itself.
// With that you will avoid this situations:
// const { quote } = quotes[quoteIndex];
// and then when you use it - it would be something like:
// <div>{quote.quote}</div>
// As you see <div>{quote.body}</div> would be better.
// Of course in many cases it is an API response, but when possible, try to name varibles/properties properly
const quotes = [
{quote: 'If you cannot do great things, do small things in a great way.', author: 'Napoleon Hill'},
{quote: 'Permanence, perseverance and persistence in spite of all obstacles, discouragements, and impossibilities: It is this, that in all things distinguishes the strong soul from the weak.', author: 'Thomas Carlyle'},
{quote: 'The supreme art of war is to subdue the enemy without fighting.', author: 'Sun Tzu'},
{quote: 'Keep your face always toward the sunshine - and shadows will fall behind you.', author: 'Walt Whitman'},
{quote: 'Being entirely honest with oneself is a good exercise.', author: 'Sigmund Freud'},
{quote: 'Ever tried. Ever failed. No matter. Try Again. Fail again. Fail better.', author: 'Samuel Beckett'},
{quote: 'Not all those who wander are lost.', author: 'J. R. R. Tolkien'},
{quote: 'I have not failed. I\'ve just found 10,000 ways that won\'t work.', author: 'Thomas A. Edison'},
{quote: 'Tell me and I forget. Teach me and I remember. Involve me and I learn.', author: 'Benjamin Franklin'},
{quote: 'A leader is one who knows the way, goes the way, and shows the way.', author: 'John C. Maxwell'},
{quote: 'Very little is needed to make a happy life; it is all within yourself, in your way of thinking.', author: 'Marcus Aurelius'},
{quote: 'If opportunity doesn\'t knock, build a door.', author: 'Milton Berle'},
{quote: 'The secret of getting ahead is getting started.', author: 'Mark Twain'},
{quote: 'Problems are not stop signs, they are guidelines.', author: 'Robert H. Schuller'},
{quote: 'Life is not a problem to be solved, but a reality to be experienced.', author: 'Soren Kierkegaard'},
{quote: 'Life isn\'t about finding yourself. Life is about creating yourself.', author: 'George Bernard Shaw'},
{quote: 'The only true wisdom is in knowing you know nothing.', author: 'Socrates'},
{quote: 'The pessimist complains about the wind; the optimist expects it to change; the realist adjusts the sails.', author: 'William Arthur Ward'},
{quote: 'In three words I can sum up everything I\'ve learned about life: it goes on.', author: 'Robert Frost'},
{quote: 'Blessed are the hearts that can bend; they shall never be broken.', author: 'Albert Camus'},
{quote: 'Success is not final, failure is not fatal: it is the courage to continue that counts.', author: 'Winston Churchill'},
{quote: 'Do not go where the path may lead, go instead where there is no path and leave a trail.', author: 'Ralph Waldo Emerson'},
{quote: 'Do not dwell in the past, do not dream of the future, concentrate the mind on the present moment.', author: 'Buddha'},
{quote: 'Don\'t judge each day by the harvest you reap but by the seeds that you plant.', author: 'Robert Louis Stevenson'},
{quote: 'Find a place inside where there\'s joy, and the joy will burn out the pain.', author: 'Joseph Campbell'},
{quote: 'Try to be a rainbow in someone\'s cloud.', author: 'Maya Angelou'},
{quote: 'It is during our darkest moments that we must focus to see the light.', author: 'Aristotle'},
{quote: 'Good, better, best. Never let it rest. \'Til your good is better and your better is best.', author: 'St. Jerome'},
];
class Card extends React.Component {
constructor(props) {
super(props);
this.state = {
quoteIndex: this.getRandomQuoteId(quotes)
}
// You had "bind" method here.
// You don't need it when you use "named" methods like:
/* someMethod = () => {
return 'Something'
} */
// You will see it below
}
// Also, use more descriptive names for your functions and state
getRandomQuote = () => {
this.setState({
quoteIndex: this.getRandomQuoteId()
});
}
// Use DRY (Dont Repeat Yourself) method
// If you see that you can create a function to avoid code duplication -> do it
// Less code, less bugs
// But be carefull. You dont need to generalize everything
getRandomQuoteId = () => {
return Math.floor(Math.random() * quotes.length);
}
render () {
// Check how I get the quoteIndex
// Looks cleaner and readable.
// Again, try to avoid variables like "result" because it is too ambiguous
const { quoteIndex } = this.state;
const quote = quotes[quoteIndex];
return (
<div id="quote-box">
<p id="text">{quote.quote}</p>
<p id="author">{quote.author}</p>
<button id="new-quote" onClick={this.getRandomQuote}>New Quote</button>
<a id="tweet-quote" href="https://www.twitter.com/intent/tweet" target="_blank">Tweet Quote</a>
</div>
);
}
};
ReactDOM.render(<Card />, document.getElementById('root'));
Also see: Tab Triggers