Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

CSS in PHP?

Featured Replies

Hi,

 

I am very new to php so sorry if this is a simple question.

 

I'm not sure if I can explain it but I'll try my best. I basically have 2 php files with nothing in them but HTML code, one is called table.php and the other is called nav.php.

 

When working on those php files I would obviously like to style them but how can include an external stylesheet?... if I link a stylesheet file in those php files called "main.css" it works. But then when I try and php include the table.php and nav.php in my main.php file it doesn't work, I think because it's linking path is wrong and not only that it then calls the same main.css file twice. I can always link to the main.css in my main.php file but then I lose the styling in table.php and nav.php!!

 

so i guess my question is what is the best way to link a css file from a php document? and then when i php include them in my main.php file it only calls it once etc...

 

-thanks for any help!!!

Edited by NerdTech

Can you do something like this:

 

<html>
<head>
	<meta charset="UTF-8">
	<title>main.php</title>
	<link rel="stylesheet" href="main.css">
</head>
<body>

<?php include 'nav.php' ?>
<?php include 'table.php' ?>

<!-- Carry on with the rest of the code here. -->


	
</body>
</html>

Like Mr Ben says above this is the method to include the files.

 

Within the nav.php and table.php you shouldn't re-reference the style sheet and as you are including them within the main.php then you can just have HTML within the nav and table files.

 

EG:

 

<html>
<head>
	<meta charset="UTF-8">
	<title>main.php</title>
	<link rel="stylesheet" href="main.css">
</head>
<body>

<?php include 'nav.php' ?>
<?php include 'table.php' ?>

<!-- Carry on with the rest of the code here. -->


	
</body>
</html>

Thanks Mr Ben.

 

Then the nav.php and table.php

 

look like:

 

<ul>
<li>item 1</li>
<li>item 2</li>
<li>item 3</li>
<li>item 4</li>
<li>item 5</li>
<li>item 6</li>
<li>item 7</li>
<li>item 8</li>
<li>item 9</li>
<li>item 10</li>
</ul>

this code is then output within the <body> tags of main.php

  • 2 weeks later...

 

Can you do something like this:

 

<html>
<head>
	<meta charset="UTF-8">
	<title>main.php</title>
	<link rel="stylesheet" href="main.css">
</head>
<body>

<?php include 'nav.php' ?>
<?php include 'table.php' ?>

<!-- Carry on with the rest of the code here. -->


	
</body>
</html>

 

That's what I'd do.

 

You could use an absolute path though I suppose.

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.