" />

Using global variables in HTML/CSS, is it possible?

javascript html css

114 просмотра

3 ответа

898 Репутация автора

Let me try and explain what I mean here.... I have a lot of images on my page, and different paths where they are stored.

<img src="what/ever/folder/pic01.jpg">
<img src="and/even/longer/path/name/pic09.jpg">
<img src="what/ever/folder/pic01.jpg">
<img src="and/even/longer/path/name/pic09.jpg">

etc... they are scattered all over the long page

Would it be possible with CSS or HTML or.. to make a variable containing each path, and then use that in the HTML?

PATH01 = 'what/ever/folder/'
PATH02 = 'and/even/longer/path/name/'

And then do the images, styles etc..

<img src={PATH01}"pic01.jpg">

Or something like that? (am I making any sense?`hehe)

Автор: torbenrudgaard Источник Размещён: 19.07.2016 07:23

Ответы (3)


2 плюса

1676 Репутация автора

Решение

HyperText Markup Language, commonly abbreviated as HTML, is the standard markup language

This means that here are no vars in html. But there a lot of html server-side processors like php,node.js etc which can give you result you are looking for.

PHP example:

<?php
$path01 = 'some/path/';
?>

<img src="<?=$path01?>pic01.jpg">
<br/>
<img src="<?php echo $path01?>pic01.jpg">
Автор: Maksym Semenykhin Размещён: 19.07.2016 07:28

0 плюса

1862 Репутация автора

No you can't do that using purely HTML or CSS. You could however do something like that using javascript or the server language of your choice.

Автор: Thijs Размещён: 19.07.2016 07:29

0 плюса

81 Репутация автора

good question, <base> is what you are looking for

Specify a default URL and a default target for all links on a page

http://www.w3schools.com/tags/tag_base.asp

in your case

<img src="what/ever/folder/pic01.jpg">

can be written in

<base href="http://whatever.com/what/ever/folder" />
<img src="pic01.jpg">

but <base> is a unique element in single page, which means you can have only one ENV variable set.

p.s. 99% of time we use html template system (php, jsp or react.js, handlebars) to solve this kind of problem in real life

Автор: FE Bear Размещён: 19.07.2016 07:43
32x32