简体   繁体   中英

I am having some problems with my css and html file

This is my html code:

<html>
<head>

<style rel="stylesheet" type="text/css" href="{{ url_for('static', filename='main.css') }}"></style>

</head>



<body>

   <h2 align="centre">Hello!</h2>
   <h1>Hello Again!</h1>

   <p>this is a paragraph!</p>
</body>
</html>

this is css code:


body {
    background: #fafafa;
    color: red;
}
header {
    color: blue
}

this is python code:


from flask import Flask
from flask import render_template
from flask import request


app = Flask(__name__)


@app.route('/')
@app.route('/home')
def home():
    return render_template("base.html")

if __name__ == "__main__":
    app.run(debug=True)

just one url but its still not working i am not a html or css "expert" so i need some help, and i dont have any friends so stackoverflow was the only option

i also saw many people use <link> tag instead of <style> idk the difference and google is showing something else, any help would be appriciated!

Here, I Sorted The Linkings Between Files...


This Is The Required Folder Structure

Here Is The , ,

body {
  background: #fafafa;
  color: red;
}

header {
  color: blue;
}

Here Is The ,

<html>
<head>
  <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='main.css') }}"></link>
</head>

<body>
  <h2 style="text-align: center;">Hello!</h2>
  <h1>Hello Again!</h1>
  <p>this is a paragraph!</p>
</body>

</html>

Here is ,

from flask import Flask
from flask import render_template
# from flask import request


app = Flask(__name__, static_url_path='/static')


@app.route('/')
# @app.route('/home')
def home():
    return render_template("base.html")


if __name__ == "__main__":
    app.run(debug=True)

And The Result of the program

I don't know python, but why are you trying to get your css file using 'href' tag when you can use 'src' like this

<head>
    <style rel="stylesheet" type="text/css" src="yourfile.css"></style>
</head>

where src can be absolute path to your file ('c:/yourproject/yourfile.css') or, better, relative path so if you have html and css in the same path you can simply use your css file name?

To include an external stylesheet, you can use <link rel="stylesheet" href="styles.css"> where styles.css is the name of the stylesheet. This should appear within the <head> tags. The <style> tag can be used for internal styles such as:

<style>
    body {
        background: #fafafa;
        color: red;
    }
    header {
        color: blue
    }
</style>

in your HTML. w3schools has a page explaining the different ways to include CSS.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM