Como vincular css e js html [duplicado]

Sep 06 2020

Estou trabalhando em um pequeno projeto de site usando HTML, CSS e JavaScript. Então eu segui o tutorial, mas agora que tenho um arquivo .html, um arquivo .css e um arquivo .js, não sei como fazê-los trabalhar juntos. Desculpe pela pergunta boba, mas eu realmente preciso saber como vincular o arquivo .css e o arquivo .js no arquivo .html. obrigado

Respostas

5 NikhilSingh Sep 05 2020 at 23:14

Para vincular CSS - <link rel="stylesheet" href="styles.css">

Para vincular JS - <script src="myscripts.js"></script>

Seu documento HTML ficaria assim -

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="styles.css">
    <title>Title of your HTML page</title>
</head>
<body>
    <!-- Put your HTML here  -->

    <script src="myscripts.js"></script>
</body>
</html>

3 BenjamínColin Sep 05 2020 at 23:17

Para importar arquivos css em html, use o link de tag

<link rel="stylesheet" href="style.css"> // file.css

Para importar arquivos js use o script de tag

<script src="file.js"></script>  // file.js
1 wilkoklak Sep 05 2020 at 23:17

De modo geral, você deseja colocar seu CSS (folha de estilo) em sua <head>tag.

Por exemplo:

<head>
    <link rel="stylesheet" href="path_to_css.css" />
</head>

Falando em scripts, hoje em dia acho que os navegadores carregam JavaScript em paralelo, então não faz grande diferença, mas pelo que aprendi no passado - colocar o javascript antes do final da <body>tag é uma boa prática.

<body>
  ... your HTML content here
  ...
  <script src="path_to_js.js" />
</body>