r/csshelp • u/ECommerce_Guy • Dec 14 '24
Does CSS have to be in root folder?
Hey guys, backend guy here, first time having to write full frontend myself and I encountered an interesting problem.
Initially, I put my style.css file within a folder called visuals and then tried to include it via <link rel="stylesheet" href="visuals/style.css">
Obviously, if it worked, wouldn't be here. So I tried with /visuals and ./visuals and no luck.
Then just for fun I dragged the file out of the subfolder back into root and included simply style.css and whatdayaknow, worked. Then just as sanity check I moved it back into visuals and after confirming that visuals/style.css is not working, I did the following:
<head><style><?php include ("visuals/style.css"); ?></style></head>
And hah! Works again.
I mean, not a real problem as it can be solved multiple ways but... Why?
3
u/tridd3r Dec 15 '24
can you share the relevant file structure?
if you have
- index.php
- visuals
- - styles.css
then your href="visuals/styles.css" will work.
The main difference you might be seeing an example of is server side vs client side resolving. href is relative from the client rendered pages, while the includes is server side.
1
u/be_my_plaything Dec 14 '24
Nope it should be fine in a folder, was the path to the folder definitely correct (going from the html file) ?
2
u/equinusocio Dec 15 '24
The path must be relative to the final HTML document position.