How to get CSS to link to HTML? [duplicate] - html

This question already has answers here:
How to link a CSS file from HTML file?
(2 answers)
Closed 1 year ago.
I am very new to coding, but have the basics down for the most part. I recently downloaded Atom and created very simple HTML and CSS files. I used the standard link method (as shown in the code), but cannot seem to get my css to link. My only install thus far has been webBoxio/atom-html-preview.
HTML:
<!DOCTYPE HTML>
<HTML>
<body>
<link rel="stylesheet" type="text/css" href="mine.css">
<h1>test</h1>
<p>test</p>
</body>
</HTML>
CSS:
h1, p {
font-family: Sans-Serif ;
}

As others said in the comments, the <link> tag should go between <head> and </head> tags.
So the code is:
<!DOCTYPE HTML>
<HTML>
<head>
<link rel="stylesheet" type="text/css" href="mine.css">
</head>
<body>
<h1>test</h1>
<p>test</p>
</body>
</HTML>
However, your code worked for me on Firefox.
Also, I suggest you this website if you wanna learn html (if you haven't found it yet): http://www.w3schools.com/

The reason is both files need to be in the same directory. Had the same problem and couldn't figure it out until I changed that. Both HTML and .CSS need to be in the same folder, not in the same folder within the other. Hope this helps other peeps in the future.

Related

Web developing noob - help needed for css [duplicate]

This question already has answers here:
How to link a CSS file from HTML file?
(2 answers)
Closed 1 year ago.
I am extreme beginner and I have this dumb problem.
So I wrote a css file and html file.
HTML :
<!DOCTYPE html>
<html>
<body>
<img
src="https://i.pinimg.com/originals/67/b2/a9/67b2a9ba5e85822f237caae92111e938.gif"
width="300" id="para1">
<p>Paragraph</p>
</body>
</html>
I did this for my css file
p {
color: red;
}
When I save and refresh my website, the html shows up.
The css doesnt show up like the paragraph doesnt change red. I also want to change the position of the image.
Please help!
I also want to know about indenting please.
Also should for website developing, should I learn css and html at the same time, or like html for 1 year, and then css for one year, because im learning javascript like in a 2 years, next year.
You must tell your HMTL page where to find your CSS.
To do that you have to add link tag into your head tag using:
<head>
<link href="/path/to/your/style.css" rel="stylesheet">
</head>
<link>: The External Resource Link element
The HTML element specifies relationships between the current
document and an external resource. This element is most commonly used
to link to stylesheets, but is also used to establish site icons (both
"favicon" style icons and icons for the home screen and apps on mobile
devices) among other things.
Take a look at https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link
In your case if your css file named style.css and your index.html file are on the same folder, your html should look like that:
<!DOCTYPE html>
<html>
<head>
<link href="style.css" rel="stylesheet">
</head>
<body>
<img src="https://i.pinimg.com/originals/67/b2/a9/67b2a9ba5e85822f237caae92111e938.gif" width="300" id="para1">
<p>Paragraph</p>
</body>
</html>
The HTML element contains machine-readable information
(metadata) about the document, like its title, scripts, and style
sheets.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/head
If you want to use inline css you have to put your <style> tag between your <head> tag to make it processed by HTML.
<!DOCTYPE html>
<html>
<head>
<style>
p {
color: red;
}
</style>
</head>
<body>
<img src="https://i.pinimg.com/originals/67/b2/a9/67b2a9ba5e85822f237caae92111e938.gif" width="300" id="para1">
<p>Paragraph</p>
</body>
</html>
Also should for website developing, should I learn css and html at the
same time, or like html for 1 year, and then css for one year, because
im learning javascript like in a 2 years, next year.
Well, HTML, CSS & JS are the fabric of the front end so my advice is why not all three at the same time? They each compliment each other and after 3 years studying you will have understood more about them than focusing your time and energy learning each as separate entities.
Think of HTML as your initial sketch (your structure), CSS as painting your sketch (your make-up and beautifier), and JS as making your painting come to life (your functional and interactive parts). Simply put, it's more fun with working with all 3

Text align in CSS does not seem to function on VSCODE

I am making a survey and I need to centre the h1 in the centre of the page. It seems to work on Codepen.io but doesn't seem to work on VS-CODE.
This is my HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Milkshake Survey 2020</title>
</head>
<body>
<h1 id="MS">Milkshake Survey</h1>
</body>
</html>
and CSS:
#MS {
text-align:center;
}
It seems to have worked on code.io
link: https://codepen.io/abhirajsb/pen/xxVgGwj
But on VSCODE it does not seem to work and the title is always on the left.
I need to know what mistakes i am making. (i am a beginner)
I had to include a link in my HTML file to connect the HTML page to the css.
<link rel="stylesheet" href="survey.css">
Thank you to #rioV8 for the quick response and others for the contribution.

Beginner, how do I add CSS in a different file than the HTML file? [duplicate]

This question already has answers here:
Adding external CSS in an HTML file
(5 answers)
Closed 4 years ago.
Bit of a noob, but I'll stab.
I have used SoloLearn for most of my coding, so the CSS is a tab embedded into the file, and we can't see it. So.. how do I name a css file, and how do I point it to a html file in the same folder?
Thanks in advance.
Create a file called styles.css
Import the file in the head of your html document with
<head>
<link href="styles.css" rel="stylesheet" />
<title>Title</title>
</head>
To connect your CSS file to a HTML one you can use this sample:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
So you are pointing a CSS file in HTML by this line
<link rel="stylesheet" href="styles.css">
in head
You can call it whatever you want, but you include a link to it in your HTML, rather than point it at the HTML. If you called it "style.css" your HTML would start like this:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css">

How to add css framework into html file with existing basic css

I'm wondering if you are able to add a css framework, specifically bulma, into an html sheet with existing css. The current css is just a standard < link > style.css, and I'm wondering if I can just link the bulma https://cdnjs.cloudflare.com/ajax/libs/bulma/0.5.1/css/bulma.css
with the same < link > tag just under or above it on the same sheet.
Simple Starter from Bulma Website
http://bulma.io/documentation/overview/start/
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Hello Bulma!</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.5.0/css/bulma.min.css">
</head>
<body>
<section class="section">
<div class="container">
<h1 class="title">
Hello World
</h1>
<p class="subtitle">
My first website with <strong>Bulma</strong>!
</p>
</div>
</section>
</body>
</html>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.5.1/css/bulma.css">
in the <head>...</head> of your html file and it will works
I think the question is probably missing a part.
The part where the question is raised : will this inclusion of the bulma css interfere with the existing style.css?
In that case.. probably.. as soon as pure HTML elements are described in the style.css and return in the bulma.css, for example: table {....} is present in the bulma css, it could cause some strange behavior.
There are several ways to try and bypass this behavior, some of them clean, others less clean :)
First you need to decide whether the bulma is the default css to be used or not...
If so, use your own style.css to overwrite the bulma.css.
If not, still call your style.css as last css in the row.. items that are not defined in your stylesheet will be taken care of by the bulma css

HTML page will not respond to ID's in CSS

HTML and CSS noob running into my first problem.
I am using Notepad++ and I have my css and HTML files both saved in the same folder. But whenever I launch the HTML file in a browser the css ID that I am using is not doing anything specifically centering and changing color.
This seems like one of those easy fix kind of things.
Main.css
<style>
#change{color:red; text-align:center }
</style>
example.html
<html>
<html lang="en">
<head>
<meta charset="utf-8">
<Title>sample</title>
<link rel="stylesheet" href="folder/main.css" />
</head>
<body>
<p id="change">Sample sample sample </p> //HERE IS WHERE THE ID IS
</body>
</html>
anyone have an idea what im doing wrong
To combine the two current answers:
If the directory structure of your website is simply the index.html and main.css in the same folder, than the path to link the two is:
<link rel="stylesheet" type="text/css" href"main.css"/>
The Style tags in your current css page are redundant and unnecessary. Style tags could be used in the html, though are rarely used.
Finally, not sure if this is just the code you have posted, but the html here is acting as if it is all a comment,and make sure to delete that if that is in your current code (/*).
Remove the <style> and </style> tags. Your main.css file should only contain CSS:
#change{ color:red; text-align:center }
check css path.
Are paths like below?
- example.html
- folder/
|- main.css
remove tag in main.css
In css files, any html tag MUST NOT be existed.
and welcome to the world of web designing! Things can get quite frustrating, but as long as you show your will to solve a problem, everything will go smoothly. I love the fact that you tried to solve the problem prior to posting. Even though it is a basic thing, let's go step by step:
main.css does NOT need <style> tags. Those are only required when editing CSS internally, inside of <head> element of HTML page. In *.css files, you just start defining CSS rules.
make sure that you follow proper spacing like #change { color: red; text-align: center; } (ALWAYS FINISH A CSS RULE WITH A SEMI-COLON)
you can also break them down like this:
#change {
color: red;
text-align: center;
}
Characters /* and */ are CSS comments syntax. ANYTHING between those characters shall be ignored by the browser.
<link rel="stylesheet" href="folder/main.css" /> might potentially be the cause of the problem, since you stated that html file and css file are in the same folder. If that is the case, you don't need "folder/main.css" but only href="main.css" /> Also it wouldn't hurt to add <link rel="stylesheet" type="text/css" href="main.css" /> to let the browser know the type of the file you're linking to.
Here is a working example.
#change {
color: red;
text-align: center;
}
<html lang="en">
<head>
<meta charset="utf-8">
<Title>sample</title>
<link rel="stylesheet" href="folder/main.css" />
</head>
<body>
<p id="change">Sample sample sample </p>
</body>
</html>