php - 我该怎么做才能清理我的 php 文件以不在其中包含 html 和 css?

标签 php html css

下面是我想在我的网站上普遍使用的导航栏的 html 和 css,有没有办法把它放在一个单独的文件中,然后在 html 和 php 文件的开头调用该文件对于我的网页?我试图将它们放在单独的文件中并将它们包含在其他 html 页面中。至于 php,我可以包含“path/to/html/file”还是将其放在 php 之上?我只是想要一种更有条理的方式来做到这一点。感谢您的帮助,谢谢!

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>


body, html {
  height: 100%;
  margin: 0;
  font-family: Arial, Helvetica, sans-serif;
}

* {
  box-sizing: border-box;
}

.bg-image {
  /* Full height */
  height: 50%; 

  /* Center and scale the image nicely */
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
}

/* Images used */
.img1 { background-image: url("logo.jpeg"); }


/* Position text in the middle of the page/image */
.bg-text {

  color: black;
  font-weight: bold;
  font-size: 80px;

  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 2;
  width: 300px;
  padding: 20px;
  text-align: center;
}
</style>
</head>

<style>
.navbar {
    overflow: hidden;
    background-color: #333;
    font-family: Arial, Helvetica, sans-serif;
}

.navbar a {
    float: left;
    font-size: 16px;
    color: white;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
    }

.dropdown {
    float: left;
    overflow: hidden;
    }

.dropdown .dropbtn {
    font-size: 16px;    
    border: none;
    outline: none;
    color: white;
    padding: 14px 16px;
    background-color: inherit;
    font-family: inherit;
    margin: 0;
    }

.navbar a:hover, .dropdown:hover .dropbtn {
    background-color: red;
    }

.dropdown-content {
    display: none;
    position: absolute;
    background-color: #f9f9f9;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    z-index: 1;
}

.dropdown-content a {
    float: none;
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
    text-align: left;
}

.dropdown-content a:hover {
    background-color: #ddd;
}

.dropdown:hover .dropdown-content {
    display: block;
}
</style>
</head>
<body>

<div class="navbar">
    <a href="../login/homePage.html">Lewis University</a>
    <div class="dropdown">
        <button class="dropbtn">Colleges</button>
        <div class="dropdown-content">
            <a href="../colleges/show_college.php">List Colleges</a>
            <a href="../colleges/search_college.html">Search Colleges</a>
            <a href="../colleges/new_college.html">Add Colleges</a>
        </div>
    </div> 
    <div class="dropdown">
        <button class="dropbtn">Department</button>
        <div class="dropdown-content">
            <a href="../department/show_department.php">List Department</a>
            <a href="../department/search_department.html">Search Department</a>
            <a href="../department/new_department.html">Add Department</a>
        </div>
    </div> 
    <div class="dropdown">
        <button class="dropbtn">Program</button>
        <div class="dropdown-content">
            <a href="../program/show_program.php">List Program</a>
            <a href="../program/search_program.html">Search Program</a>
            <a href="../program/new_program.html">Add Program</a>
        </div>
    </div> 
    <div class="dropdown">
            <button class="dropbtn">Concentration</button>
            <div class="dropdown-content">
                <a href="../concentration/show_concentration.php">List Concentration</a>
                <a href="../concentration/search_concentration.html">Search Concentration</a>
                <a href="../Concentration/new_concentration.html">Add Concentration</a>
            </div>
        </div> 

     <div class="dropdown">
            <button class="dropbtn">prefix</button>
            <div class="dropdown-content">
                <a href="../prefix/show_prefix.php">List Prefixes</a>
                <a href="../prefix/search_prefix.html">Search Prefixes</a>
                <a href="../prefix/new_prefix.html">Add Prefixes</a>
            </div>
        </div> 

    <div class="dropdown">
            <button class="dropbtn">Course</button>
            <div class="dropdown-content">
                <a href="../courses/show_course.php">List Course</a>
                <a href="../courses/search_course.html">Search Course</a>
                <a href="../courses/new_course.html">Add Course</a>
            </div>
        </div> 
    <div class="dropdown">
            <button class="dropbtn">Coures & Concentration</button>
            <div class="dropdown-content">
                <a href="../course_concentration/show_course_concentration.php">List Coures & Concentration</a>
                <a href="../course_concentration/search_course_concentration.html">Search Coures & Concentration</a>
                <a href="../course_concentration/new_course_concentration.html">Add Coures & Concentration</a>
            </div>
        </div> 

    <div class="dropdown">
        <button class="dropbtn">Course Type</button>
        <div class="dropdown-content">
            <a href="../course_type/show_course_type.php">List Course Type </a>
            <a href="../course_type/search_coures_type.html">Search Course Type</a>
            <a href="../course_type/new_course_type.html">Add Course Type</a>
        </div>
    </div> 


    <div class="dropdown">
        <button class="dropbtn">Course & Prereq</button>
        <div class="dropdown-content">
            <a href="../course_prereq/show_course_prereq.php">List Course & Prereq</a>
            <a href="../course_prereq/search_course_prereq.html">Search Course & Prereq</a>
            <a href="../course_prereq/new_course_prereq.html">Add Course & Prereq</a>
        </div>
    </div> 

    <div class="dropdown">
        <button class="dropbtn">Phrase</button>
        <div class="dropdown-content">
            <a href="../phrase/show_phrase.php">List Phrase</a>
            <a href="../phrase/search_phrase.html">Search Phrase</a>
            <a href="../phrase/new_phrase.html">Add Phrase</a>
        </div>
    </div> 

</div>

</head>

<body>
    <main role="main" class="container-fluid">
    <h1> Colleges </h1>

最佳答案

对于 CSS,使用外部样式表。在您的 header 中,包括这一行:

<link rel="stylesheet" type="text/css" href="mystyle.css">

然后从 <head> 中剪下所有的 css 并将其粘贴到 mystyle.css 中。

这样做的目的是,无论何时链接上面的代码行,该页面都会从 mystyle.css 调用 css 样式。

要清理您的 html 代码,您不应该清理整个 html,而应该只清理重复出现在其他页面中的 html 代码。比如你的导航菜单。

您可以使用名为 include 的 php 函数来完成此操作

试试这个:

1) 创建一个名为 navigation.php 的新文件 - 在此文件中,从 <div class="navbar">.....</div> 开始粘贴整个导航 html 代码。

2) 现在对于您的 html,您必须将其重命名为 .php 。所以 index.html 应该是 index.php

3) 现在 include 通过这样做导航它:

<body>
   <?php include 'navigation.php';?>
   // your html codes of other things below your navigation
</body>

这样你就可以调用导航菜单、页脚等重复的东西。

关于php - 我该怎么做才能清理我的 php 文件以不在其中包含 html 和 css?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58479214/

相关文章:

css - 将 sass 列表传递给具有多个参数的 mixin

仅 css div 内部边框

php - Symfony3 : How to delete current user and redirect to home?

PHP CodeIgniter-combobox 值未在数据库中更新

javascript - MVC4 Devexpress 排序、分组和过滤不起作用

javascript - Canvas 中的绝对位置是否始终基于视口(viewport)?

html - CSS:在悬停时为按钮添加边框。如何避免以下元素的转移?

php htmlspecialchars 和 jquery .text()

php - 向MySQL表插入大量数据只插入1行?

jQuery - 如何在表格中导航并删除属性,