Handling files is a pro feature in Codepen. Free users can't add local files like images to use them in the editor. In this quick 3 steps tutorial, w
5 years ago
|3 min read
![A quick hack to use local images in Codepen](https://cdn.sanity.io/images/8oc8onsu/production/de2377a43ab2c0ddeeb437a12f84103bfb71f6d4-1496x846.png?w=1000&fit=fill)
Handling files is a pro feature in Codepen. Free users can't add local files like images to use them in the editor.
In this quick 3 steps tutorial, we'll learn to host an image on Github Pages and use it in Codepen.
Step 1: Upload an image to a new repository.
![new repository](https://cdn.sanity.io/images/8oc8onsu/production/973aff3869e8bbd50265756ac7a3b30103dc3c5f-1310x756.jpg?w=800&fit=fill)
- Click on the + button (top right) then New repository
![enter repository name](https://cdn.sanity.io/images/8oc8onsu/production/93a8ea1393b74516556f9b0844ca4dba1558613e-1048x714.jpg?w=800&fit=fill)
- Add a repository name (I've called it hosted-assets). Then leave the other options to default and click on Create repository.
![upload file](https://cdn.sanity.io/images/8oc8onsu/production/30e031d5e441c4b43ee1835c05bff39583c0af5a-1082x816.jpg?w=800&fit=fill)
- Click on uploading an existing file.
![choose file](https://cdn.sanity.io/images/8oc8onsu/production/79d5823b1af7f07320482717a02d9a79fb3e960b-1214x816.jpg?w=800&fit=fill)
- Either drag files from your PC or click on choose your files to upload. Then click on Commit changes.
Step 2: Host your repository with Github Pages.
![click on settings](https://cdn.sanity.io/images/8oc8onsu/production/d716a91131ff82b0fd64ce645ebc54c6d7179039-1322x816.jpg?w=800&fit=fill)
As you can see, pink-kitty.jpg has been added to the github repository.
- Click on Settings.
![change the source](https://cdn.sanity.io/images/8oc8onsu/production/9b7ddc0cf2b00768f535e8bb863de5e8c48f7838-1034x816.jpg?w=800&fit=fill)
- Scroll to Github Pages and change the source from none to main branch then click on the Save button.
![newly created link](https://cdn.sanity.io/images/8oc8onsu/production/c443798ec5571669f39dbdf29e8564cbe2573d53-978x816.jpg?w=800&fit=fill)
The page will reload, and on scrolling back to the Github Pages section, you'll find the newly created link.
- Click on the link to go to your Github Pages.
![404 page](https://cdn.sanity.io/images/8oc8onsu/production/0672e079533935d9548e6eb1908807dfd926be8b-970x808.jpg?w=800&fit=fill)
The page returns 404 not found because we haven't set a home page
![change url](https://cdn.sanity.io/images/8oc8onsu/production/f70f8e06397ac69a629c4b282b6c4cfbd06a2226-1298x934.jpg?w=800&fit=fill)
Add your file name at the end of that url. In this case pink-kitty.jpg
And your image should pop up.
Step 3: Use the uploaded image in codepen.
- Copy the previous link and replace the local image src in your codepen. For example replace:
<img src="./pink-kitty.jpg" alt="pink kitty">
with:
<img src="https://ljc-dev.github.io/hosted-assets/pink-kitty.jpg" alt="pink kitty">
And tada π!!!
![final result](https://cdn.sanity.io/images/8oc8onsu/production/7cbcf8e24bb3673eb5ebc0ff9d618bf5ba8cc122-840x625.jpg?w=800&fit=fill)
Tips:
- Be careful to use the Github Pages link and not the Github repository link.
- The next time you upload an image, you can click on Add files in the home of your repository and Upload files.
- There should now be an Environment section in the home of the repository with github pages. Click on it to view deployments and your live site.
- To add a folder, either drag the folder into the upload window if you are on a PC or follow this stackoverflow solution to adding folders.
Update:
@Pav1an on Twitter has suggested a quick hack to make github host images π₯ (doesn't work for other file types).
![pav1an hack](https://cdn.sanity.io/images/8oc8onsu/production/aa780a713549f6b1cc8e432b97b274c461351e1a-1256x816.jpg?w=800&fit=fill)
- Go to the homepage of one of your repositories or create one.
- Click on the Issues tab and Create an issue.
- Instead of writing an issue report, drag an image to the issue box. Github will save the image as a png file and show a link in markdown format like this:
![pink-kitty](https://user-images.githubusercontent.com/62596124/104592898-39710980-566f-11eb-8613-44762bcc233f.jpg)
- Copy the image link and use it as the src for your image in Codepen.
Thanks for reading ππ !!!
Comments
Be the first one to comment.