Showing posts with label CSS. Show all posts
Showing posts with label CSS. Show all posts

Monday, December 19, 2011

Quick Tip: How to Work with @Font-face

Due to the fact that @font-face can be a bit overly complicated, it hasn’t caught on quite as much as it should. Once you start reading about licensing, different font formats, browser consistencies, it can potentially become more trouble than it’s worth.
But – in five minutes, I’ll try to simplify the process of working with custom fonts as much as I possibly can. Services like Font Squirrel help to make the task a cinch!


Final CSS

  1. @font-face {  
  2. font-family: 'blok-regular';  
  3. src: url('type/Blokletters-Potlood.eot');  
  4. src: local('Blokletters Potlood Potlood'),  
  5.  local('Blokletters-Potlood'),  
  6.  url('type/Blokletters-Potlood.ttf') format('truetype');  
  7. }  
  8.   
  9. @font-face {  
  10. font-family: 'blok-italic';  
  11. src: url('type/Blokletters-Balpen.eot');  
  12. src: local('Blokletters Balpen Balpen'),  
  13.  local('Blokletters-Balpen'),  
  14.  url('type/Blokletters-Balpen.ttf') format('truetype');  
  15. }  
  16.   
  17. @font-face {  
  18. font-family: 'blok-heavy';  
  19. src: url('type/Blokletters-Viltstift.eot');  
  20. src: local('Blokletters Viltstift Viltstift'),  
  21.  local('Blokletters-Viltstift'),  
  22.  url('type/Blokletters-Viltstift.ttf') format('truetype');  
  23. }  
  24.   
  25. h1 { font-family: blok-heavy, helvetica, arial; }  
Notice how we’re referencing both an .eot and .ttf font? This is because, of course, Internet Explorer only uses its own format, that has yet to truly catch on. As such, we must first import that .eot file, and then move on to the different formats for Firefox, Safari, etc. It’s essential that you load the .eot version first.
Next, we search for the font on the user’s computer by using the “local” attribute. If it’s unfound, only then do we pass a url that will load the font.

Why Doesn’t IE Try to Load the TTF Fonts?

This was definitely a concern. If Explorer can’t work with the truetype format, we don’t want to waste time trying to download the font. Luckily, because of all those local attributes, and the commas, IE won’t understand any of it. As such, it will simply skip the line all together, thus, only utilizing the .eot version.

CSS Media Queries & Using Available Space

Jul 6 2010
We've covered using CSS media queries to assign different stylesheets depending on browser window size. In that example, we changed the layout of the entire page based on the space available. It isn't required that we make such drastic changes with this technique though, so in this tutorial we'll go over a design tweak with a smaller scope. We'll also cover the syntax for using media queries within a single stylesheet and more examples of that.

The CSS media query syntax for calling an external stylesheet is like this:
<link rel='stylesheet' media='screen and (min-width: 701px) and (max-width: 900px)' href='css/medium.css' />

You may be familiar with the media attribute, normally being "screen" or "print" or even a comma separated list, like "screen, projection". The media attribute can be brought directly inside a CSS file, like this:
@media screen {
      body {
          width: 75%;
      }
}
@media print {
      body {
          width: 100%;
      }
}
Likewise, you can use more advanced CSS media queries like:
@media all and (max-width: 699px) and (min-width: 520px), (min-width: 1151px) {
  body {
    background: #ccc;
  }
}
You may use as many media queries as you would like in a CSS file. Note that you may use the and operator to require multiple queries to be true, but you have to use the comma (,) as the or operator to separate groups of multiple queries. The not keyword can be used to alter the logic as well.

Example

Let's say we have a fluid width design where the sidebar is 35% of the width of the page. That means depending on the width of the browser window, that sidebar could be quite narrow or quite wide, that's just the nature of fluid width designs. With CSS media queries, we can say "if the browser is really narrow, do this, if it's wider, do this, if it's really wide, do this." Note that measuring width isn't the only thing media queries can do, it's just a particularly practical example.
In our example sidebar, we are going have a list of names of the Super Team which function as email links. The HTML is fairly simple:
<ul id="nav">
   <li><a data-email="chriscoyier@gmail.com" href="mailto:chriscoyier@gmail.com">Chris Coyier</a></li>
   <li><a data-email="isuredo@likeher.com" href="mailto:isuredo@likeher.com">Elisabeth Moss</a></li>
   <li><a data-email="marry@me.com" href="mailto:marry@me.com">Amanda Righetti</a></li>
</ul>
It's just a list of links. The href attribute is a mailto: link. The only thing you might find unusual is the data-email attribute. In HTML5, you can use attributes prefixed with data- to store information, and it's perfectly valid. We are going to want to use that data later, but the href value isn't quite what we need having that mailto: link, hence the data attribute.
The default styling for the list will be this:
#sidebar ul li a {
  color: #900;
  text-decoration: none;
  padding: 3px 0;
  display: block;
}
At narrow browser window widths, the default styling applies. Just a list of links.
When the browser gets a bit wider, in our example between 520 and 699px, we're going to use that extra space that opens up in the sidebar to apply an email icon to each list item.
@media all and (max-width: 699px) and (min-width: 520px) {
  #sidebar ul li a {
    padding-left: 21px;
    background: url(../images/email.png) left center no-repeat;
  }
}
Icons are applied to the list items as we know that we have room for them now.
As we get wider, from 700 to 1000px, we'll use the extra space again to preface the links with the text "Email: " (using CSS Content) instead of just the icon.
@media all and (max-width: 1000px) and (min-width: 700px) {
  #sidebar ul li a:before {
    content: "Email: ";
    font-style: italic;
    color: #666;
  }
}
Using CSS content / psuedo element, we can preface the link with descriptive text without altering the content in the HTML
Wider still, at browser window widths above 1001px, we'll literally append the email address to the links. This is where that HTML5 data attribute comes in.
@media all and (min-width: 1001px) {
  #sidebar ul li a:after {
    content: " (" attr(data-email) ")";
    font-size: 11px;
    font-style: italic;
    color: #666;
  }
}
Again the content is adjusted, the email is added as a pseduo element purely with CSS.
At really wide widths, above 1151px, we will again add the icon as we used before. The cool part here is that we don't have to write an additional media query segment, we can just append an additional media query to our already existing one using a comma (behaves like OR operator) on the medium-width one we already wrote.
@media all and (max-width: 699px) and (min-width: 520px), (min-width: 1151px) {
  #sidebar ul li a {
    padding-left: 21px;
    background: url(../images/email.png) left center no-repeat;
  }
}
At the widest, we can use all the flair we have available.

All together now

#sidebar ul li a {
  color: #900;
  text-decoration: none;
  padding: 3px 0;
  display: block;
}
@media all and (min-width: 1001px) {
  #sidebar ul li a:after {
    content: " (" attr(data-email) ")";
    font-size: 11px;
    font-style: italic;
    color: #666;
  }
}
@media all and (max-width: 1000px) and (min-width: 700px) {
  #sidebar ul li a:before {
    content: "Email: ";
    font-style: italic;
    color: #666;
  }
}
@media all and (max-width: 699px) and (min-width: 520px), (min-width: 1151px) {
  #sidebar ul li a {
    padding-left: 21px;
    background: url(../images/email.png) left center no-repeat;
  }
}

Video demo

Live demo / download

View Demo   Download Files

Browser support

The browser support for media queries is surprisingly decent. For the queries in this particular demo (utilizing min and max widths), current version of Firefox, Safari (including Mobile), Chrome, and Opera are all supporting it. Internet Explorer 9 will be supporting it, but 8 and below do not. If I wanted to deliver the best possible experience in IE 8 and below, I'd either fake it with JavaScript like I did in this article, or use an IE specific stylesheet and style it in the same style as the most common browser width according to analytics.
Note that milage may vary on individual types of queries. For example, the iPhone supports the width queries but does not support the orientation queries. The iPad supports both.

More on media queries

Measuring width is a nice practical example of media queries, but it isn't the only thing available. Below are some more of them (not a comprehensive list). The spec lists lots more.

Types

HTML4 had these media types, which are all still valid: aural, braille, handheld, print, projection, screen, tty, and tv. HTML5 may include more as it needs them. The spec includes ‘3d-glasses’, which is awesome. The all keyword will target all types.
@media screen, projection { ... }

Dimensions

You get height and width, which query against the current browser window height and width. You could use them as-is, but that would probably be rare. Both of them accept min/max prefixes, so more commonly you'd used them as min-width, max-width, min-height, max-height.
There is also device-width and device-height, which also provide min-device-width, max-device-width, min-device-height, and max-device-height.
@media (min-device-width: 640px) { ... }

Orientation / Aspect Ratio

You can query against the aspect ratio of the screen as well with device-aspect-ratio.
@media screen and (device-aspect-ratio: 16/9) { ... }
Or if the screen is in portrait (height larger than width) or landscape (width larger than height) mode.
@media (orientation:portrait) { ... }

Color

You can query on if the screen is in color or not and details about that.
@media (color) { /* Screen is in color */ }
@media (min-color-index: 256) { /* Screen has at least 256 colors */ }
@media (monochrome) { /* Screen is monochrome */ }

Elsewhere

This has been a hot topic lately. You can hear some smart people talk about it on The Big Web Show. Here are some other cool uses of it going around recently:
  • Andy Clarke shows how the the longer the line length the more readable text can be with taller line height.
  • Jon Hick's site rearranges itself to have double sidebars, single sidebar, or no sidebar according to available space. It even scales down to mobile sizes nicely.
  • Ethan Marcotte's demo for A List Apart doesn't use media queries, but shows other ways a design can be effectively flexible.
  • Simon Collison's site rearranges it's grid to fit different browser sizes.
  • Panic uses @media in their HTML emails to make them look awesome in desktop or mobile email clients.

Wednesday, November 23, 2011

How to change site Looking with time day or night

Style switchers have become a popular feature on websites these days. CSS style sheets allow a Web Designer to change the look and feel of a website with little effort. Some sites use “Day” and “Night” type of style switchers that automatically change the site theme based on the time of day.
This tutorial shows you how to create a time based CSS style sheet switcher using PHP that lets you change multiple style sheets at once at specific times of the day. There’s also a little bit of jQuery UI thrown in just for fun!


Why PHP instead of JavaScript?

The one drawback to using PHP for a time based CSS style sheet switcher is that the time is based on either the web hosting server or on a “time zone” set in the script.
When I was building my personal portfolio site, I created a time based style sheet switcher using JavaScript and jQuery. The time function of the script worked correctly. However, there was always a brief “flash” of the default style sheet when the web pages loaded.
JavaScript heavy websites can have problems with conflicts between various JavaScript scripts. I decided to use PHP instead for the CSS style sheet switcher. Because PHP is server side, there were no JavaScript conflicts and the script worked as expected.
The one drawback to using PHP for a time based CSS style sheet switcher is that the time is based on either the web hosting server or on a “time zone” set in the script. With JavaScript you can set time functions to be based on a user’s computer.

Before We Begin

If you are new to PHP, you need to set up and configure a server environment on your local computer (PHP files can not be viewed in a web browser). My personal preference is XAMPP. This article includes a few software options and basic instructions on how to set up a local server.
Although you can use Notepad to create and edit PHP files, a code editor can be very helpful when writing web pages in PHP and other programming languages. Please refer to these articles for additional information on choosing a code editor:
The code used in the tutorial creates a basic CSS style sheet switcher and you can view the demo here. The source files include two additional demos.
Now, let’s get started!

Step 1: Create the 1st CSS Style Sheet (Night)

Create a CSS style sheet called style-1.cssand include the following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*  Default Styles
--------------------------------------------------- */
 
body {
   margin: 0px;
   padding: 0px;
   background: url("../images/bokeh_2.jpg");
   background-position: top left;
   background-repeat: no-repeat;
   background-attachment: fixed;
   background-color: #666;
   font-size: 20px;
   font-family: Arial, Helvetica, sans-serif;
   color: #fff;
   text-align: left;
}
 
#wrapper {
   width: 965px;
   margin: 0 auto;
   position: relative;
}
 
a:link, a:visited, a:active {
   color: #99ccff;
}
 
a:hover {
   color: #0073ea;
}
 
.content_container_1 {
   float: left;
   width: 650px;
   margin: 0px 0px 60px 0px;
}
 
p.main_description   {
   margin: 5px 0px 50px 0px;
   text-align: center;
}
 
/* Main Image
--------------------------------------------------- */
 
.main_image {
   background: url("../images/main_image_1.jpg");
   background-repeat: no-repeat;
   width: 630px;
   height: 425px;
   border: #fff solid 10px;
   margin: 40px 0px 10px 0px;
}
 
/*  Tabs
--------------------------------------------------- */
 
#tabs p  {
   font-family: Arial, Helvetica, sans-serif;
   font-size: 20px;
}
 
a.tabs_link_1:link, a.tabs_link_1:visited, a.tabs_link_1:active {
   text-decoration: underline;
   color: #0073ea;
   font-weight: bold;
}
 
a.tabs_link_1:hover {
   text-decoration: underline;
   color: #ff0084;
   font-weight: bold;
}
 
#tabs .tabs_img   {
   float: left;
   background-color: #ddd;
   padding: 8px;
   margin: 0px 12px 0px 0px;
}

Step 2: Create the 2nd CSS Style Sheet (Morning)

Open style-1.css and save it as style-2.css. Change the body background image from bokeh_2.jpg to bokeh_4.jpg. Change main_image_1.jpg to main_image_4.jpg.

Step 3: Create the 3rd CSS Style Sheet (Afternoon)

Use style-1.css or style-2.css to create a 3rd style sheet style-3.css. Change the body background image to bokeh_3.jpg and the main image to main_image_5.jpg

Step 4: Create the 4th CSS Style Sheet (Evening)

Create a 4th style sheet style-4.css. Change the body background image to bokeh_1.jpg and the main image to main_image_8.jpg
For steps 2 through 4, feel free to change other elements like font colors and image borders. Instead of using the images provided in the source files, you can use your own images to create custom CSS styles.

Step 5: Download jQuery and jQuery UI

To show how the PHP script changes multiple style sheets at once, jQuery Themeroller Themes are being used to style the tabbed content area of the web page.
Go to the jQuery UI Download Page and download the “Flick” and “Start” themes.
Next, go to the official jQuery website and download jQuery.

Step 6: The Markup

Create a PHP file that includes the following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
 
<!-- CSS style sheets -->
<?php include("style sheets.php"); ?>
 
<!-- jQuery / JavaScript -->
<script type="text/javascript" src="js/jquery-1.6.3.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.8.16.custom.min.js">></script>
<script type="text/javascript">
   $(function() {
      $( "#tabs" ).tabs();
   });
</script>
 
<meta name="description" content="This demo uses PHP to change multiple CSS style sheets (Main CSS and jQuery UI Theme) at specific times during a 24 hour period. The timezone for this demo is set to Australia/Melbourne." />
<meta name="keywords" content="time,based,css,stylesheet,switcher,php" />
<title>Simple Time Based CSS Style Sheet Switcher</title>
 
</head>
<body>
 
<!-- Begin Wrapper -->
<div id="wrapper">
 
<!-- Begin Content Container 1 -->
<div class="content_container_1">
 
<!-- Main Image Switches at Set Times -->
<div class="main_image"></div>
<p class="main_description">
Main Image changes at the times set in PHP Script (Australia/Melbourne).
</p>
 
<!-- Begin Tabs Area -->
 <div id="tabs">
   <ul>
      <li><a href="#tabs-1">About Envato</a></li>
      <li><a href="#tabs-2">Marketplaces</a></li>
      <li><a href="#tabs-3">Tuts+ Network</a></li>
   </ul>
 
   <div id="tabs-1">
   <p>
   <img class="tabs_img" src="images/envato_1.jpg" width="300" height="195" alt="About Envato" />
   Envato is a startup based out of Australia with people around the world and sites serving pages every second. We started in a living room in 2006 and have been steadily working to build our company into a world-class contender. Our background is creative, we love open source, we believe that work is about way more than just making money, and we're totally committed to making products that are awesome!
   </p>
   <p>
   <a class="tabs_link_1" href="http://envato.com/" target="_blank" title="Visit The Envato Website">Visit The Envato Website</a>
   </p>
   </div>
 
   <div id="tabs-2">
   <p>
   <img class="tabs_img" src="images/marketplace_1.jpg" width="300" height="195" alt="Marketplaces" />
   The Envato Marketplaces allow anyone to buy or sell digital goods like WordPress themes, background music, After Effects project files, Flash templates and much more. The marketplaces are home to a thriving community of over 500,000 users, authors and buyers and every day hundreds of new files are added.
   </p>
   <p>
   <a class="tabs_link_1" href="http://themeforest.net/" target="_blank" title="Visit the Theme Marketplace">Visit the Theme Marketplace</a>
   </p>
   </div>
 
   <div id="tabs-3">
   <p>
   <img class="tabs_img" src="images/psdtuts_1.jpg" width="300" height="195" alt="Tuts+ Network" />
   At Envato we're very passionate about education, that's why we've created one of the most popular networks of educational blogs around. Tuts+ serves up over 18 million pageviews a month across its many subsites on subjects like graphics, web development, audio production, iPhone development and motion graphics.
   </p>
   <p>
   <a class="tabs_link_1" href="http://tutsplus.com/" target="_blank" title="Visit Tuts+ Hub">Visit Tuts+ Hub</a>
   </p>
   </div>
 
</div>
<!-- End Tabs Area -->
 
</div>
<!-- End Content Container 1 -->
 
</div>
<!-- End Wrapper -->
 
</body>
</html>

Step 7: PHP CSS Stylesheet Switcher Code

Create a PHP file that includes the following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
 
date_default_timezone_set("Australia/Melbourne"); // Set default time zone
 
$time = date("H"); // Set the time in 24 hour format
 
if (00 <= $time && $time < 07) // 12:00am to 7:00am   Night
   {
   echo
'<link href="css/style-1.css" rel="stylesheet" type="text/css" media="screen" /><link href="css/flick/jquery-ui-1.8.16.custom.css" rel="stylesheet" type="text/css" media="screen" />';
   }
 
elseif (07 <= $time && $time < 12) // 7:00am to 12:00pm   Morning
   {
   echo
'<link href="css/style-2.css" rel="stylesheet" type="text/css" media="screen" /><link href="css/start/jquery-ui-1.8.16.custom.css" rel="stylesheet" type="text/css" media="screen" />';
   }
 
elseif (12 <= $time && $time < 18) // 12:00pm to 6:00pm   Afternoon
   {
   echo
'<link href="css/style-3.css" rel="stylesheet" type="text/css" media="screen" /><link href="css/start/jquery-ui-1.8.16.custom.css" rel="stylesheet" type="text/css" media="screen" />';
   }
 
else // all other hours   Evening
   {
   echo
'<link href="css/style-4.css" rel="stylesheet" type="text/css" media="screen" /><link href="css/flick/jquery-ui-1.8.16.custom.css" rel="stylesheet" type="text/css" media="screen" />';
   }
 
?>

Breakdown of the Markup

PHP Include for rel="stylesheet" Links

The script for the CSS Style sheet Switcher is going to be contained in a PHP include file called stylesheets.php.
A PHP include() statement includes and evaluates a specified file. The stylesheets.php include file is referenced by this code:
1
2
<!-- CSS style sheets -->
<?php include("style sheets.php"); ?>
Because we are using a PHP script to change CSS style sheets at set times, the rel=”stylesheet” links will be generated by the PHP script.

Reference jQuery and jQuery UI Libraries

We need to make sure that the web page references the jQuery and jQuery UI libraries.
Between the head tags of the web page the following code has been added:
1
2
3
<!-- jQuery / JavaScript -->
<script type="text/javascript" src="js/jquery-1.6.3.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.8.16.custom.min.js"></script>

Initialize jQuery UI Tabs

This tutorial includes jQuery UI Tabs using the default functionality. The following code initializes the tabs:
1
2
3
4
5
<script type="text/javascript">
   $(function() {
      $( "#tabs" ).tabs();
   });
</script>

Container Div for jQuery UI Tabs

1
2
3
4
5
<!-- Begin Wrapper -->
<div id="wrapper">
 
<!-- Begin Content Container 1 -->
<div class="content_container_1">
(Ending </div> tags for Wrapper and Content Container 1 are shown in the Code block for the Markup of the jQuery UI Tabs)
Most of the jQuery UI Widgets are programmed to expand to 100% width of the content area where they are placed.
The container div content_container_1 has been set to a fixed width of 650px in the CSS so that the tabbed content area does not expand the entire width of the screen.

Main Image Markup

In order for the main image to also be changed at set times using the PHP script, it needed to be included on the web page as a CSS background image.
So instead of adding the image to the web page using markup like this:
1
<img src="main_image_1.jpg" width="630" height="425" alt="Image" />
It was added to the web page as a CSS background image of a <div> with the class “main_image”, like this:
1
2
3
4
5
<!-- Main Image Switches at Set Times -->
<div class="main_image"></div>
<p class="main_description">
Main Image changes at the times indicated.
</p>

Markup for the jQuery UI Tabs

The below markup adds the tabbed content area that is powered by jQuery UI and has been styled using jQuery Themeroller Themes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<!-- Begin Tabs Area -->
 <div id="tabs">
   <ul>
      <li><a href="#tabs-1">About Envato</a></li>
      <li><a href="#tabs-2">Marketplaces</a></li>
      <li><a href="#tabs-3">Tuts+ Network</a></li>
   </ul>
 
   <div id="tabs-1">
   <p>
   <img class="tabs_img" src="images/envato_1.jpg" width="300" height="195" alt="About Envato" />
   Envato is a startup based out of Australia with people around the world and sites serving pages every second. We started in a living room in 2006 and have been steadily working to build our company into a world-class contender. Our background is creative, we love open source, we believe that work is about way more than just making money, and we're totally committed to making products that are awesome!
   </p>
   <p>
   <a class="tabs_link_1" href="http://envato.com/" target="_blank" title="Visit The Envato Website">Visit The Envato Website</a>
   </p>
   </div>
 
   <div id="tabs-2">
   <p>
   <img class="tabs_img" src="images/marketplace_1.jpg" width="300" height="195" alt="Marketplaces" />
   The Envato Marketplaces allow anyone to buy or sell digital goods like WordPress themes, background music, After Effects project files, Flash templates and much more. The marketplaces are home to a thriving community of over 500,000 users, authors and buyers and every day hundreds of new files are added.
   </p>
   <p>
   <a class="tabs_link_1" href="http://themeforest.net/" target="_blank" title="Visit the Theme Marketplace">Visit the Theme Marketplace</a>
   </p>
   </div>
 
   <div id="tabs-3">
   <p>
   <img class="tabs_img" src="images/psdtuts_1.jpg" width="300" height="195" alt="Tuts+ Network" />
   At Envato we're very passionate about education, that's why we've created one of the most popular networks of educational blogs around. Tuts+ serves up over 18 million pageviews a month across its many subsites on subjects like graphics, web development, audio production, iPhone development and motion graphics.
   </p>
   <p>
   <a class="tabs_link_1" href="http://tutsplus.com/" target="_blank" title="Visit Tuts+ Hub">Visit Tuts+ Hub</a>
   </p>
   </div>
 
</div>
<!-- End Tabs Area -->
 
</div>
<!-- End Content Container 1 -->
 
</div>
<!-- End Wrapper -->
The Menu “Tabs” are generated by an unordered list. The content sections are generated by <div> tags that have unique IDs that correspond to anchor links in the <li></li> tags.
For example: <div id=”tabs-1″> corresponds with <a href=”#tabs-1″>

Breakdown of PHP Stylesheet Switcher Code

Set the Default Time Zone:

1
date_default_timezone_set("Australia/Melbourne"); // Set default time zone
The code date_default_timezone_set sets the default timezone used by all date/time functions in a script.
If the script does not include date_default_timezone_set, then the web hosting server time will be used for all date/time functions in the script.
Please refer to the List of Supported Timezones to find the appropriate timezone for your location.
Newer versions of PHP incorporate “Daylight Savings Times” into the functionality of date_default_timezone_set. Some older versions of PHP do not take “Daylight Savings Times” into account.

Set the Time in 24 Hour Format:

1
$time = date("H"); // Set the hour in 24 hour format
date(“H”) formats the local time/date in a 24-hour format of an hour, with leading zeros. Click Here to view information about the 24-hour clock.

Breakdown of If, Elseif, Else Statement

I’ve broken down the rest of the PHP code to show how the PHP script will function line by line. There are several functions being utilized in the code including:

if

1
2
3
4
5
if (00 <= $time && $time < 07) // 12:00am to 7:00am   Night
   {
   echo
'<link href="css/style-1.css" rel="stylesheet" type="text/css" media="screen" /><link href="css/flick/jquery-ui-1.8.16.custom.css" rel="stylesheet" type="text/css" media="screen" />';
   }
If 00 (midnight) is Less than or equal to current time AND current time is Less than 07 (7:00am) Then the script will apply style-1.css and flick CSS style sheets.

elseif

1
2
3
4
5
elseif (07 <= $time && $time < 12) // 7:00am to 12:00pm   Morning
   {
   echo
'<link href="css/style-2.css" rel="stylesheet" type="text/css" media="screen" /><link href="css/start/jquery-ui-1.8.16.custom.css" rel="stylesheet" type="text/css" media="screen" />';
   }
If 07 (7:00am) is Less than or equal to current time AND current time is Less than 12 (12:00pm) Then the script will apply style-2.css and start CSS style sheets.

elseif

1
2
3
4
5
elseif (12 <= $time && $time < 18) // 12:00pm to 6:00pm   Afternoon
   {
   echo
'<link href="css/style-3.css" rel="stylesheet" type="text/css" media="screen" /><link href="css/start/jquery-ui-1.8.16.custom.css" rel="stylesheet" type="text/css" media="screen" />';
   }
If 12 (12:00pm) is Less than or equal to current time AND current time is Less than 18 (6:00pm) Then the script will apply style-3.css and start CSS style sheets.

else

1
2
3
4
5
else // all other hours   Evening
   {
   echo
'<link href="css/style-4.css" rel="stylesheet" type="text/css" media="screen" /><link href="css/flick/jquery-ui-1.8.16.custom.css" rel="stylesheet" type="text/css" media="screen" />';
   }
If NONE of the conditions are met in the “if…elseif” statements, Then the script will apply style-4.css and flick CSS style sheets.

Conclusion

With CSS, you can create many different styles for your web pages. Using PHP, you can apply those styles dynamically to your website. Throw in a little jQuery UI, and your web pages will not only be fun for you to create, but fun for your visitors as well!
If you have any questions about this tutorial, please ask them below, Thank you so much for reading!