Brand new to html/css, is there a way to manage recurring elements on a site such as a toolbar? For example, if I have a website with 10 pages and want to change the HTML for the universal toolbar, do I have to change it on all 10 pages?
The easiest way would be a server-side include.
Meaning, you have:
index.php
head.php
footer.php
nav.php
And in index.php you looks something like:
<?php include_once "head.php"; ?>
Actual content here
<?php include_once "footer.php"; ?>
head.php for example, would look like:
<!doctype HTML>
<html>
<head>
<title>Hello</title>
<?php include_once "nav.php"; ?>
</head>
<body>
Depends. If the HTML is written in 10 different places then Yes, you have to change it on all 10 pages.
If you're adding the HTML to all pages through a common usercontrol(if you are .net that is) or using a template, then you can do it in 1 place & be done with it.
If you mean having recurring items check out Madara's answer. If you mean recurring styles, the following will give you an example:
If you want a certain text to show up as red every time, you can name it "Red":
In css:
.red{
color: red;
}
The "." indicates its a recurring element and you can name it whatever you want as long as its being used in the html.
In html:
<span class="red">Sample text</span>
You have few options:
Use some server-side language (i.e. PHP) - recommended
Use iframe tag:
<iframe src="toolbar.html"></iframe>
Related
My main web page (index.html) follows a common structure (simplified):
<html>
<head>
<title>...</title>
<meta name=description content="...">
<link rel=stylesheet href="main.css"/>
[... including #font-face loads ]
</head>
<body>
<div id=menu>...</div>
<div id=mainContent>
...
</div>
<div id=footer>...</div>
<script src="jquery.min.js"></script>
... more scripts
<body>
</html>
The web server is well configured such that all the static files are cached and don't get reloaded on the client side if refreshing the page.
Upon choosing a link from the site, mainly from the 'menu' or the 'footer', I want to display different content within the div tag 'mainContent'. Page layout, CSS, fonts, scripts, menue, footer - all is the same. I have identified several means to achive this:
Construct a new subPage.html file copying everything from index.html, then rewrite the div 'mainContent'
with the desired other stuff and change page specifics like title, description etc.
Or use php to include the desired content in mainContent and to change the page specific values like 'title'.
Link from index.html goes to href="subpage.html".
Drawbacks:
Maintenance: when changing anything in the outside 'wrapper', I'll have to edit
every subPage.
no idea how to easily transport values from index.html
to subPage.html, beside cookie (not always permitted) or URL
parameters.
use a javascript onClick handler (event listener) to load requested content from server using XHttpsRequests and exchange the innerHtml of div mainContent.
Drawbacks:
no noscript version possible.
my changing content is probably not indexed by Google bot and alike, since it is not loaded with index.html. Would it change the situation if the 'alternativ content' was saved in .html files in the base directory, such that it would be browsable and discoverable?
Pre:
keeps javascript variables
no need to reload outer page, thus best user experience.
use a 2nd div 'mainContent2' with style="display: none". With a javascript onClick handler toggle display style of both mainContent divs to none <-> block.
Pre:
easy to implement.
all content loaded and thus SEO indexed.
Drawback:
Everything has to be loaded at once, so the index.html might get pretty big.
[4. iframe probably not an option (as the src attribut is static)]
I tend to opt to alternative #2.
Any other technics recommended? What is the 'best practice'? How is this generally done by the pros? Suggestions? Please elaborate.
I'll give you a few answers based on each option:
PHP
You can use PHP to import the header and footer instead of the main
content, that way you have just one file with a header and another
with a footer and all the pages that you create with different
contents will import the header and footer, avoiding duplications.
JS
Do you need a no-script version? I have never seen someone who disabled js but I don't know your app, it could be a pre-requirement.
You can use a modern js framework like Next + React / Nuxt + Vue / Remix / Svelte / ... There is a lot of options here that can provide you an SSR (Server Side Render) and make Google Bot happy
SPA
This seems to be a SPA. You can use some of the modern js frameworks that I mentioned in the second item. You need to think about lazing load the images too. I don't know how big is this content, but you can try google lighthouse to see if there is some problem with page size in this approach, also, you could enable the gzip on the server.
OR...
All of the above
You can use all of them together too. A frontend with a framework getting data from an API written with PHP, why not? PHP can validate the request type and delivery an HTML if it's the first request or a JSON if the application is already loaded.
Most common solution probably a variant of your option 1. But different then you think. Create a header.php with the content
<html>
<head>
<title>...</title>
<meta name=description content="...">
<link rel=stylesheet href="main.css"/>
[... including #font-face loads ]
</head>
<body>
<div id=menu>...</div>
and create a footer.php with the content:
<div id=footer>...</div>
<script src="jquery.min.js"></script>
... more scripts
<body>
</html>
Then create an index.php like
<?php include('header.php'); ?>
<div id=mainContent>
...content index page...
</div>
<?php include('footer.php'); ?>
And then create subpages like subpage.php
<?php include('header.php'); ?>
<div id=mainContent>
...content subpage...
</div>
<?php include('footer.php'); ?>
This way if anything in the header or footer needs to change you edit the header.php file and the changes will take effect on all pages because the header.php gets included on every page.
I was trying to insert another html page into an php page by using "include"
and that html page is having some stylesheets imported,
When I insert that page, it disturbs my PHP page..
How can I restrict the CSS for that particular page only?
place the imported page inside a container div, and then give it an id like:
<div id='included_page'> Your page goes here...</div>
Then add #included_page .someClass{mystyle: property;} to each and every style defined.i.e, increase the level.
Add a class or id to each <body> and write styles accordingly like below
HTML
<body class='home'>
Home page content
</body>
<body class='about'>
About page content
</body>
CSS
.home .someclass{
}
.about .someclass{
}
Try this.
In Page[restrict the CSS]:
<?php
$type = 'exclude';
inlcude('page_to_include.php');
page_to_include.php:
<?php
if(!isset($type) or ($type !== 'exclude')) {
// Things to be excluded from Page 1;
}
Ref: PHP - include a php file and also send query parameters
This question already has answers here:
How to create a template in HTML?
(3 answers)
Closed 9 years ago.
I'm new to web development and I'm trying to make a basic webpage that displays a list (like a restaurant menu). Desired result:
<html>
<head>
<title>Restaurant menu</title>
</head>
<body>
<ul>
<li>Item1</li>
<li>Item2</li>
<li>Item3</li>
</ul>
</body>
</html>
Except that these items are stored locally as a .txt file, and are subject to change so I don't want to copy paste them to the html every single time.
Your best bet is to use a server side language like PHP, or whatever you are familiar with or would like to learn. For example with PHP...
The text file:
Item1
Item2
Item3
Your page, lets call it list.php
<?php
// reads the lines of menu.txt into an array
$menu = file('menu.txt');
?>
<html>
<head>
<title>Restaurant menu</title>
</head>
<body>
<?php if(!empty($menu)): ?>
<ul>
<?php foreach($menu as $item): ?>
<li><?php echo $item ?></li>
<?php endforeach; ?>
</ul>
<?php else: ?>
<p>No items.</p>
<?php endif; ?>
</body>
</html>
You could use Javascript to do this. The problem there is that if for some godforsaken reason the user has javascript disabled, or if they dont have a browser that can run javascript, then you cant even view the content. So really, serverside language should be used for this.
Also, please dont just cut and paste this code. There are a great many things not accounted for, and it will probably lead to issues. You can use it as a starting point, but youll really want to familiarize yourself with the language and then perhaps post more specific questions about how to parse the format of the file, or what format you should use, error handling, and other things.
Your options are a server side language (PHP, .NET) or a Javascript call to server-side code.
Plain HTML won't cut it for what you need to do.
You have to use a web language like PHP, JavaScript or many of the other ones.
HTML code is static, and does not change.
I'd recommend looking at jQuery for quick and simple dynamic pages.
Is it possible to write the 2 html codes in the same html file and call it when required with help of name property
<html name ="abc1">
<body>
</body>
</html>
<html name ="abc2">
<body>
</body>
</html>
Thanks in advance
- Miss subanki
I don't believe that is valid. Depending on what you're doing, couldn't you just do the same things with div tags?
<html>
<body>
<div id="abc1">
<!-- Content goes here -->
</div>
<div id="abc2">
<!-- Content goes here -->
</div>
</body>
</html>
You can use a scripting language if you want to add conditions like these.
PHP example for page.php?show=abc1
<?php
if ($_GET['show'] == 'abc1') {
echo 'something';
} elseif ($_GET['show'] == 'abc2') {
echo 'something else';
}
?>
I agree with Alec's post that you should probably use a Server Side Scripting (http://www.w3schools.com/web/web_scripting.asp) to achieve what you are trying to do. It sounds like you are taking users input and deciding what to show them.
So a very high general overview of how server side scripting works is you generate a page request with information from the user and then on the server side, a script parses the url and decides based on the parameters passed in, what to show the user.
The desired effect of showing one section at a time can also be achieved through Javascript or any client side scripting - http://www.w3schools.com/js/default.asp
Is there any alternative for using a frame. I'm just a beginner and I still don't know what the web designers are doing.
Almost all of the websites I see. Retains the header in the page. How can I apply that so I can stop from using frames.
Use a server-side language like PHP in order to generate a full HTML page.
For example, create three files:
header.php
page.php
footer.php
In the header.php file you have to put the first part of the HTML page.
In the page.php file you have to put the main content of the HTML page.
In the footer.php file, like the header.php, you have to put the end part of the HTML page.
So you can change the page file and the header and the footer remain.
header.php:
<html>
<head>
</head>
<body>
<div id="header">
Place your header here.
</div>
page.php:
<?php include('header.php'); ?>
<div id="main_content">
Place your page content here.
</div>
<?php include('footer.php'); ?>
footer.php:
<div id="footer">
Place your footer here.
</div>
</body>
</html>
For more information, search for a PHP tutorial with Google.
In regards to what you see in most websites, they just reuse the same code.. (usually in an external file and insert it in all their pages)..
Take a look at Server Side Includes for more info
Depending on what you wish to display you could look at using divs or using includes.