I have a Vue/Webpack application that uses an external-referencing, embedded CSS link in my index.html file. It looks something like:
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<link rel="icon" href="<%= BASE_URL %>favicon.ico" />
<title><%= htmlWebpackPlugin.options.title %></title>
<link
rel="stylesheet"
href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900"
/>
...
</head>
If I run npm run build in the project directory, the resulting dist/index.html file will contain this link.
However, I am deploying my project in a Docker container. At the end of that container building process, this CSS link and others like it will be gone in the generated index.html file, causing several elements on my page to break graphically.
What settings in the webpack/Docker config could cause this to happen?
I have found the solution - it seems that my Dockerfile included a copy of the src and resources folders for the working directory, but not the public folder which contained the favicon as well as the base index.html. This meant that the index.html that was autogenerated as part of npm run build would never take the above lines of code into account.
Related
The margin is different between <button> elements depending on if its rendered by react or if it's just plain old html. Plain old html adds a small margin between the buttons, while react completely removes it.
Does anyone know why these are different? And is there a way to bring back that default margin in React?
Details
Here is the result from a simple react app created with create react app at 500% zoom:
I took the source html that react generated, copied it, and put into another index.html file that I opened on my browser. Again at 500% this is what it shows:
Note there is no additional CSS that is added. The developer tools look exactly the same.
Steps to replicate:
React
Create a react app with npx create-react-app <some name>
Remove all css from generated index.css
Use the following jsx in the index.js file:
<React.StrictMode>
<button>Vanilla</button>
<button>Vanilla</button>
</React.StrictMode>
Plain Html
I copied the html that was rendered in the browser from the react app and pasted it into a separate index.html file. The html looked like:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="/manifest.json" />
<!--
Notice the use of in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root">
<button>Vanilla</button>
<button>Vanilla</button>
</div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
<script src="/static/js/bundle.js"></script><script src="/static/js/0.chunk.js"></script><script src="/static/js/main.chunk.js"></script></body>
</html>
Thanks to G-Cyrillus, we found out that the problem occurs because react is minifying the html. So the jsx:
<React.StrictMode>
<button>Vanilla</button>
<button>Vanilla</button>
</React.StrictMode>
once minified, turns into:
<button>Vanilla</button><button>Vanilla</button>
The solution I found to keep the gap is to write the jsx like this:
<React.StrictMode>
<button>Vanilla</button> <button>Vanilla</button>
</React.StrictMode>
I am trying to put an icon next to my page title. I have read quite a few articles and similar questions on the internet but i still can't see my little icon up there next to the page title. I literally don't know what is wrong with my code.
I have the following directories among other: data/public, data/files, public (which includes all my css files), views (which includes all my ejs files). Inside the /views folder i have a few folders and a few files that are responsible for my viewing experience while using the web app i am building. Now in the /views/includes directory i have a file named head.ejs which contains all my head code i will use in pretty much every other ejs file. so i just have the head.ejs file and i can include it in every ejs file i want. The head.ejs file looks like that:
<!DOCTYPE html>
<html lang="en">
<head>
<!-- <link rel="shortcut icon" href="/data/public/boxTitle.jpg" type="image/jpg" /> -->
<link rel="icon" href="favicon.ico" type="image/x-icon" />
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title><%= pageTitle %></title>
<link rel="stylesheet" href="/css/main.css">
I expected to see the icon up there but still haven't seen it. I have tried to move my favicon.ico image around in different directories and play a bit around with the paths. the best for me is for the image to be in the /data/public/ directory. while the head.ejs is in the /views/includes/ directory. The image is 16x16, black and white. Tried with a jpg image, too. I am probably failing really hard cause it's seems pretty easy and straight forward but... Any help is appreciated! I will delete the post if the answer is totally obvious and the post is worthless.
Assuming your icon is in your public folder inside an images folder.
If you have defined your public folder as you mentioned, with something like this:
app.use(express.static(path.join(__dirname, 'public')));
then you can easily display your icon this way:
<link rel="icon" href="/images/your-icon" type="image/x-icon" />
I just installed this Create React Apps program from here https://github.com/facebookincubator/create-react-app and I have all the files downloaded and I am able to edit the App.js file, but I already have made some html files for a website, I want to know how to link the App.js file to my current files. The html on the given Index.html, shows no <script> element used. Here is the full html code:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="theme-color" content="#000000">
<!--
manifest.json provides metadata used when your web app is added to the
homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json">
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>
You need to enable JavaScript to run this app.
</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
Inside your project run
npm run build
This create the folder "/build" with the "index.html" file. Inside this you can see how all this magic is done. You can put your other/old stuff also there.
I'm not entirely sure what you are asking, but you can have normal html/css on the same page as React. React is rendering the App component in the div id="root" section of the page. Elsewhere, you can just paste your old html stuff and it should work.
I have created a vue webpack project using vue-cli.
vue init webpack myproject
And then ran the project under dev mode:
npm run dev
I got this error:
Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:8080/favicon.ico
So inside webpack, how to import the favicon.ico correctly?
Check out the Project Structure of webpack template: https://vuejs-templates.github.io/webpack/structure.html
Note that there is a static folder, along with node_modules, src, etc.
If you put some image into the static folder, like favicon.png, it will be made available at http://localhost:8080/static/favicon.png
Here is the documentation for static assets: https://vuejs-templates.github.io/webpack/static.html
For your favicon issue, you can put a favicon.ico or favicon.png into the static folder and refer in the <head> of your index.html as follows:
<head>
<meta charset="utf-8">
<link rel="shortcut icon" type="image/png" href="/static/favicon.png"/>
<title>My Vue.js app</title>
...
</head>
If you do not define a favicon.ico in your index.html, then the browser will request for a favicon from the website root (default behaviour). If you specify a favicon as above, you will not see that 404 anymore. The favicon will also start showing up in your browser tabs.
As a side note, here is the reason why I prefer PNG instead of ICO file:
favicon.png vs favicon.ico - why should I use PNG instead of ICO?
For some reason, the above solutions did not work for me before converting the default favicon.ico file to favicon.png and renaming it to favicon-xyz.png e.g. (I have put this file in /public folder) and edited the index.html file as follows:
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link rel="icon" href="<%= BASE_URL %>favicon-xyz.png">
.
.
.
</head>
Might be useful for someone.
Little update for Laravel 5/6/7/8/9, place your favicon.ico or favicon.png into the /public folder and refer to it in your index.html like this:
<head>
<meta charset="utf-8">
<link rel="shortcut icon" type="image/png" href="/favicon.png"/>
<title>My Vue.js app</title>
...
</head>
Hope it helps !
As of 2022, #Mani answer is a little outdated as static assets are now served in public folder other than static.
Just generate a .ico favicon file (this site provides free online favicon generation), rename it to favicon.ico and place it in public folder, no need to change index.html, reload and new favicon will be displayed.
You may use vue-pwa-asset-generator in order to generate a full set of favicons foreach platform.
Thanks. This did the trick as per eightballs comments. I needed to move the file to the /public folder and rename it :D
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<link rel="icon" href="<%= BASE_URL %>gear.png">
<title>Prodigy</title>
</head>
Just add shortcut in rel tag like that :
<link rel="shortcut icon" href="<%= BASE_URL %>favicon.ico">
I would like to use Font Awesome locally without using the cdn.
My code fails to add font awesome icons to my page.
I can make it work easily using the cdn link but the local link fails to do anything.
Thanks
Jason.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>How it Works | Rubberdesk </title>
<!-- Bootstrap Core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- Font-Awesome CSS -->
<link href="/font-awesome-4.3.0/css/font-awesome.min.css" rel="stylesheet">
Writing this may be helpful for someone like me :
Actual folder of font-awesome.min.css is public/css/font-awesome.min.css
In font-awesome.min.css file font-family source refers to URL *'../fonts/fontawesome-webfont.eot'. So following files must be located in public/fonts/
FontAwesome.otf
fontawesome-webfont.eot
fontawesome-webfont.svg
fontawesome-webfont.ttf
fontawesome-webfont.woff
fontawesome-webfont.woff2
What you have to do when you use bootstrap/font-awesome files locally is:
Path stored must be clear.
Path written inside the font-awesome.min.css should equal to (1)
Version must have the same.
the fonts in folder fonts/ are indeed related to font-awesome.min.css you've got right now.
you must know how to write it on your script, as in; <span class="fa fa-home"></span>, etc.
use all.css ,not fontawesome-min.css.
ref to : https://fontawesome.com/how-to-use/on-the-web/setup/hosting-font-awesome-yourself
Check the path is right and placed in right folder
try this code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>How it Works | Rubberdesk </title>
<!-- Bootstrap Core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- Font-Awesome CSS -->
<link href="font-awesome-4.3.0/css/font-awesome.min.css" rel="stylesheet">
check path
check path in bootstrap.min.css
are all same
It will look same as in question try this
You need to link to the all.css file or the all.min.css file for font-awesome to work locally on your pc and make sure you copy the webfonts folder to the font-awesome folder.
Here is an update for version 5.8.1. As mentioned above you need to download the font files locally in order to run font Awesome locally.
The folder to use is now called /webfonts
Download these files locally and put them in a folder called /webfonts.
Example: (https://use.fontawesome.com/releases/v5.8.1/webfonts/fa-brands-400.eot)
The css is referencing the following URL's.
../webfonts/fa-brands-400.eot
../webfonts/fa-brands-400.woff2
../webfonts/fa-brands-400.woff
../webfonts/fa-brands-400.ttf
../webfonts/fa-brands-400.svg
../webfonts/fa-regular-400.eot
../webfonts/fa-regular-400.woff2
../webfonts/fa-regular-400.woff
../webfonts/fa-regular-400.ttf
../webfonts/fa-regular-400.svg
../webfonts/fa-solid-900.eot
../webfonts/fa-solid-900.woff2
../webfonts/fa-solid-900.woff
../webfonts/fa-solid-900.ttf
../webfonts/fa-solid-900.svg
Total size is around 2.5 MB for all the font files.
I tried to implement serving font-awesome.min.css in my Django project.
Issue 1.
My first problem was how to serve the font files themselves. Initially the browser would come up with error (of font files not being accessible).
However the browser errors themselves were quite revealing:
Hovering the mouse pointer on the error lines would throw the following:
"..../static/my_project/fonts/fontawesome-webfont.woff2?v=4.7.0"
So this solved my first problem as to where the font files should reside. It's telling me to put them in the sub-folder "fonts" in the static folder created (defined in the "settings.py" file like this):
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
And the following in "urlpatterns" (in the project's urls.py file):
static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
So the path of the .css file and the associated fonts would be something like this:
project_root
------ static
-------app_name
--------css
----------font-awesome.min.css
--------fonts
----------<font_files>
Next was the problem of font files themselves.
Issue 2: Where are the font files?
Initially I downloaded the font file by following the CDN path in by the browser (i.e. the css file itself along with the associated files which also included the folder named "webfonts"). But even after the folder was copied to the "fonts" folder the fonts were not served up and browser continued to generate errors.
In the ".css" file the font files are referred to but the referenced files/paths themselves were not available in the "webfonts" folder (as also confirmed by the browser error as stated above ("..../static/my_project/fonts/fontawesome-webfont.woff2?v=4.7.0").
On searching the web for "fontawesome-webfont.woff2" I came across the site which had the files listed for download:
fontawesome-webfont.eot
fontawesome-webfont.ttf
fontawesome-webfont.woff
fontawesome-webfont.woff2
FontAwesome.otf
Once the files (s.no. 1 - 5 above) were copied in the folder "fonts" the fonts were served up successfully and as desired.
Thanks for all of your help everyone.
It looks like it was an "fa fa-icon" class issue. I thought I had them in there but they must have been lost during editing - probably due to poor version control.
I think I've been using some 3.x.x code by mistake.
Interesting that everything still worked using the cdn code.
Thanks again. Should be able to fix everything now.
Jason.
I had the same issue on Mac OS. My solution was to
sudo chown -R _www:staff project/