7

I have 4 different html web pages. The html code below is for a menubar which I want to apply to all four pages. Is there any way I can do this using CSS instead of copying/pasting this menubar html code on all 4 of my html web pages? Basically the four pages are Home, News, Contact, About. Whenever someone clicks on a menubar item, it will redirect them to one of the 4 pages. And on all 4 of those pages, I want the menubar to be displayed. I want to create a CSS file which I can just link all 4 pages to and then the menubar will be displayed (code below). Thanks in advance!

Is there any way I can create a CSS file which at least takes care of the styling? And I will manually add the menubar buttons to each html page?

<!DOCTYPE html>
<html>
<head>
<style>
body {
    margin: 0;
}

ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
    width: 25%;
    background-color: #f1f1f1;
    position: fixed;
    height: 100%;
    overflow: auto;
}

li a {
    display: block;
    color: #000;
    padding: 8px 0 8px 16px;
    text-decoration: none;
}

li a.active {
    background-color: #4CAF50;
    color: white;
}

li a:hover:not(.active) {
    background-color: #555;
    color: white;
}
</style>
</head>
<body>

<ul>
  <li><a class="active" href="page1.html">Home</a></li>
  <li><a href="page2.html">News</a></li>
  <li><a href="page3.html">Contact</a></li>
  <li><a href="page4.html">About</a></li>
</ul>

<div style="margin-left:25%;padding:1px 16px;height:1000px;">

</div>

</body>
</html>
Mi-Creativity
  • 9,554
  • 10
  • 38
  • 47
Programmer
  • 1,266
  • 5
  • 23
  • 44

4 Answers4

6

You shouldn't/can't do this with CSS. You need either:

- For a non-programmatic solution: <link rel="import" href="sidebar.html"> save your sidebar in a sidebar.html file and call it with this snippet. Check out the support tables for this function. (Deprecated)

There are more solutions, but these are the most obvious AFAIK.

fnune
  • 5,256
  • 1
  • 21
  • 35
1

You cannot achieve this with HTML and CSS only. If you have PHP server then, I suggest to create a PHP file and put your nav-bar code and then use the following PHP code in the all pages.

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

Here is the code for nav.php:

<?php
echo 
'<ul>
  <li><a class="active" href="page1.html">Home</a></li>
  <li><a href="page2.html">News</a></li>
  <li><a href="page3.html">Contact</a></li>
  <li><a href="page4.html">About</a></li>
</ul>';
?>
Maihan Nijat
  • 9,054
  • 11
  • 62
  • 110
1

Not with CSS, you have following options:

  • Use web component and javascript (1) to load it.

  • Jade template engine which is a language for writing HTML templates, produces HTML and supports dynamic code with the support for reusability (DRY), it gives you the ability to include (2) partial HTML files in .jade with this command

    include ./path/to/sidebar.jade
    

    Example from Jade:

    //- index.jade
    doctype html
    html
      include ./includes/head.jade
    body
      h1 My Site
      p Welcome to my super lame site.
      include ./includes/foot.jade
    
  • Server-Side solution like PHP or ASP.NET.

--------------------------------------------------------------------------

(1) http://webcomponents.org/

(2) http://jade-lang.com/reference/includes/

Mi-Creativity
  • 9,554
  • 10
  • 38
  • 47
1

There is no way to do that by css, html is in charge of the elements and css only of styling them, you can't create elements by css. I suggest you to copy and paste the code. Or look for other tools, populate it with js, php or something.