using css code multiple times with different values for different files - css

I created this code in oder to use it to place image and text side by side in a HTML page.
.mydiv {
width:646px;
height: auto;
}
.myimage {
float:left;
width:378px;
height:291px;
margin:5px;
}
The proble I am having is that I want to use the code multiple times in different files and with different image values and I don't want to be creating css file for all of them. So how can I write all the code for the with different values for all the files in one css file?

First, you can put your CSS in a CSS file, then include this file in all your html page by using this in the
<LINK href="special.css" rel="stylesheet" type="text/css">
(See W3C)
For the values that can't be reuse between your html files (the SRC of your image, for instance), you will have to right it manually in each file. You can do this directly in your HTML (if you use ) or by declaring in your header.

Create a .css file with the above code. You would need to specify the height of the .myimage class too. In all the files where you want the above file import the file using
<LINK href="example.css" rel="stylesheet" type="text/css">.
Now for the div holding the image give the class as .myimage and the src attribute is independent of the file
Edit- Suppose you have 3 html files in your site and you want the above css classes to be implemented in all the files, then open a new notepad file, paste the css classes in the file and save as somestyle.css. Now in each html file you use the above link tag to import the css and use the classes as you would do normally.

Related

CSS external sheet

I'm trying to create a webpage using a "central" CSS external sheet that is called by THREE HTML files. The problem that I have is to do with background color; each HTML file should have a different colour. I start off by adding the line
<link rel="stylesheet" TYPE="text/css" href="EuropeanCountries.css" />
within the and of my HTML file called "France.html". I add exactly the same line within the and of my other two HTML files called "Italy.html" and "Germany.html". I then add the line inside "France.html" and inside "Italy.html" and inside "Germany.html". Then, I go to my css file called "EuropeanCountries.css" and I add the lines
body#page1{background-color:rgb(255,0,0);}
body#page2{background-color:rgb(0,255,0);}
body#page3{background-color:rgb(0,0,255);}
I then save ALL the HTML files and css external sheet inside the same directory. I then try to open "France.html" with the Opera browser and the background color is WHITE, which is what it should NOT be. The background color for the other two HTML files are ALSO white! So something is wrong. When I link ONE HTML file with the css file, eg. "France.html" with "EuropeanCountries.css" (and there are no other HTML files in my directory), the background color works just fine. But when I try to link multiple files with one CSS file, things go awry. Can anyone please point out to me exactly where I've gone wrong?
according the css, your body should have tags:
<body id="page1">...</body>
etc,
but I'd go with
.red { background: red; }
.green { background: green; }
.blue { background: blue; }
and
<body class="red">...</body>
correspondingly

Include CSS-File in *.tpl

I'm trying to program a template with smarty, so i started to build a layout in HTML and, as expected, with CSS in an extra .css-file. When i had finished it, i started to adapt it to smarty, but i had realized, that Smarty does not work with "normal" css. Damn delimiters ^_^
Though, i tried to include the .css-file with:
{include file="templates_css.css"}
and changed my css-code a bit:
<style type="text/css">
.body {ldelim}
width: 990px;
margin: 0 auto;
{rdelim}
.title {ldelim}
font-family: Verdana;
font-size: 275%;
margin-left: 230px;
padding: 40px;
color: #929292
{rdelim}
</style>
This is what i read the last hour about "including css-files in smarty, and they recommended "Thanks! It
It is better to have stylesheets seperated from .tpl files.
It is simple to use html tags with smarty.
And so you can simply call css from .tpl file as:
<link rel="stylesheet" type="text/css" href="{$RootDirectory}/path_to_css_directory/templates_css.css" />
Hi
You Can call external style sheet like add following line in your tpl file
OR
If you want to use internal style then just write
{literal}
here start style tag
add inline style here
Here End style Tag
{/literal}
Thanks
The secret on using smarty is not how to use the delimiters. The secret hides in the way smarty handles the path. For example:
...\htdocs\smarty <- smarty library
...\htdocs\myWebsite\css\your.css <- your stylesheet
...\htdocs\myWebsite\templates\base.tpl <- the file where you load your .css
...\htdocs\myWebsite\index.php <- the file where you load the template (base.tpl)
So... If you see your template is in \templates\base.tpl and your css in \css\your.css, you may think that you need the relative path from your template to your stylesheet. That is wrong! Note, that the, let's call it "position", of your template file will be the same as the file which is loading the template file(here it is the index.php). That means your css include, like you do it in HTML will not have the path from your template to your css, but from your index.php to your css.

Background image not showing when in external style sheet

Don't know what's going on, but for some reason my background image is not showing when linked in external style sheet.
Example1: (working)
<html>
<head>
...
</head>
<body style="background: url(images/image.jpg);">
...
</body>
</html>
Example2: (non-working - external css file)
body {
background: url(images/image.jpg);
}
The image is displays when/if I use the first example, but no image is displayed when I use the second one.
Any suggestions? Thank you in advanced...
The directory your external CSS file is stored in may be different from the directory of the page you are putting the inline styles on. You may need to start your path with a slash.
url(/images/image.jpg)
or perhaps go back a directory like
url(../images/image.jpg)

Sprites inside css with aspnet image framework

Hi im using the sprite framework http://aspnet.codeplex.com/releases/view/50140
I have it working so that i can do things like:
#Microsoft.Samples.Web.ImageSprite.Image("~/App_Sprites/icons/calendar.png")
but how can i use the images within css files?
eg
#wrapper {
width: 980px;
margin: 0 auto;
background: url(/App_Sprites/images/img01.gif) repeat-y left top;
}
If the image you are referencing in the CSS file exists, then it should work. The CSS path is relative to the CSS file. O you have the path correct? You can check with firebug.
This will require you to create CSS file on the fly, at runtime and fix up the paths in your classes and IDs.
The whole idea of the image framework is that it generates the CSS files dynamically. So let's suppose that you have placed your images inside the ~/App_Sprites folder: img1.png, img2.png, img3.png. Now you simply register the HTTP module in your web.config:
<httpModules>
<add type="Microsoft.Samples.Web.ImageOptimizationModule"
name="Microsoft.Samples.Web.ImageOptimizationModule" />
</httpModules>
and then inside the head section of your page you include the dynamically generated CSS:
#ImageSprite.ImportStylesheet("~/App_Sprites/")
which will render the following:
<link href="App_Sprites/highCompat.css" media="all" rel="stylesheet" type="text/css" />
Now all that is left is to use the rules contained inside this CSS:
<div class="img1-png"></div>
Or if you wanted to directly include the image inside your markup:
#ImageSprite.Image("~/App_Sprites/img1.png")
So the idea is that you use directly the dynamic CSS generated by the framework, you cannot use those rules in your CSS files.
Also make sure you read the documentation for the different modes and the settings.xml file which allows you to customize those modes.

Is it possible to abstract out color theme definitions in CSS

We have many images which are actually picked up based on a color theme (e.g. blue, red, gray ...). We create files with a common name under each theme (e.g. background, ...), is there a way to define the color theme in a common place so that the definition can be abstracted out. This would prevent me from changing the color theme all over the css file.
body {
background: url('../img/blue/background.png');
font-size: 13px;
margin: 0px;
}
While the options suggested here are viable approaches, I'd like to mention SASS and LESS
They are two CSS extension languages, which amongst other things provide variables for doing this sort of color stuff you mention.
You can create dynamical CSS file by using any serverside scripting language like PHP.
style.css.php:
<?php
header("Content-type: text/css");
$theme = 'blue';
$color1 = '#fefefe';
?>
body {
background: url('../img/<?=$theme?>/background.png');
font-size: 13px;
margin: 0px;
}
.sometext {
color: <?=$color1?>
}
I don't believe you can with a pure css implementation as you would need to define your base paths for your images in a variable which you set with some logic (switch statement, if/else if, ..etc.) and then use that variable in the css.
Here are some options I thought of to do this. If you create a pseudo css file with your variable defined as a string that does not occur in css (ex: $basePath) and build out all your css rules in this fake css file as "$basePath+image.jpg". Then with some server side code retrieve the css file and create your template css files by replacing $basePath+ with the actual base path for that theme. The server side code would then save those css files as theme1.css, theme2.css, ...etc.
You then could use url variables to switch between themes using some server side code to insert a reference to the correct css theme file.
This way you would only need to maintain your pseudo template css file. Although you would need to rerun your css creation code each time you change the template css file so that your theme css files get updated.
Not natively in CSS - but you can write some scripts to compile your theme CSS files from templates with variable substitution. I would suggest having a 'layout' and a 'colour' css. The layout would be consistent irrespective of which theme the user is using. The colour css contains only those settings that change per theme.
<style type="text/css">
#import ulr(layout.css);
#import ulr(theme_<?= $activeTheme ?>_.css);
</style>
You could use a tool such as http://lesscss.org/ (if you like Ruby) to create your themed CSS files.
We use NAnt for something similar to this (not CSS, but same idea), and it saves a heap of time rather than maintaining multiple files that differ only by values.

Resources