Showing posts with label css. Show all posts
Showing posts with label css. Show all posts
Circular loading bar using css only
Improving the AngularJS app starter template
I've built a few projects using AngularJS & Twitter Bootstrap, and have now settled on a structure which feels easy to maintain and understand. I will attempt to explain it here with examples.
Responsive cross browser lightbox using only html and css
Responsive mobile dropdown navigation using css only
Here we will look at a css only solution to a responsive dropdown navigation!
Improved cross browser responsive css grid layout
After creating an awesome percentage based layout, I implemented it on several sites and found it to speed up development and be the most important tool I use. However there where some issues with my previous implementation.
Css only carousel dynamic width
Carousels are common features in pages and apps we use every day. As frontend developers we need to build them efficiently and to work across a range of devices and browsers. if we can do it without Javascript then even better!
The main features of a carousel are:
- multiple items in a list
- list has hidden content which can be scrolled into view
- items are the width of the carousel view or are the width of their own content
- we can have links to show each item in the view
- the carousel should work cross device and browser
- touch or mouse operated
So lets start with some simple html:
and some css to make the items flow using their content size, so multiple items are seen at the same time:
or alternatively if you would like to size the carousel items to fit the view, so only one is visible at one time:
This is a great solution that works down to IE8 and Firefox 3.6. If you would like to support IE7 you should float the items rather than using display inline-block.
Also we would like to add links to jump to certain slides within the content area. This is also achievable without JavaScript, we can use anchor tags. Here is an example of paging/item navigation html:
and we also need to update our carousel html to have id's when the anchor tags point to:
Now when you click the nav items the browser will try to show the item in the scroll area, vertically and horizontally. This works really well except for when the content is already slightly in view. One way to improve the experience is to show the user which slide they have selected is to use the target class:
You can see a full working example of it here:
http://jsfiddle.net/kmturley/6yp6Q/5/
Updated example with CSS3 animations:
http://jsfiddle.net/kmturley/fs6wge3f/1/
The main features of a carousel are:
- multiple items in a list
- list has hidden content which can be scrolled into view
- items are the width of the carousel view or are the width of their own content
- we can have links to show each item in the view
- the carousel should work cross device and browser
- touch or mouse operated
So lets start with some simple html:
<div class="carousel">
<ul>
<li>
<img src="image.jpg" />
<span>Text</span>
</li>
<li>
<img src="image.jpg" />
<span>Text</span>
</li>
<li>
<img src="image.jpg" />
<span>Text</span>
</li>
</ul>
</div>
and some css to make the items flow using their content size, so multiple items are seen at the same time:
.carousel {
overflow: auto;
padding: 10px;
}
.carousel ul {
display: table;
table-layout: fixed;
}
.carousel li {
display: table-cell;
vertical-align: top;
padding: 0 10px 0 0;
}
.carousel {
overflow: auto;
padding: 10px;
}
.carousel ul {
white-space: nowrap;
}
.carousel li {
width: 100%;
display: inline-block;
white-space: normal;
vertical-align: top;
}
.carousel img {
float: left;
}
.carousel span {
clear: left;
float: left;
}
Also we would like to add links to jump to certain slides within the content area. This is also achievable without JavaScript, we can use anchor tags. Here is an example of paging/item navigation html:
<div>
<ul>
<li><a href="#item1">Item 1</a></li>
<li><a href="#item2">Item 2</a></li>
<li><a href="#item3">Item 3</a></li>
</ul>
</div>
<div class="carousel">
<ul>
<li id="item1">
<img src="image.jpg" />
<span>Text</span>
</li>
<li id="item2">
<img src="image.jpg" />
<span>Text</span>
</li>
<li id="item3">
<img src="image.jpg" />
<span>Text</span>
</li>
</ul>
</div>
Now when you click the nav items the browser will try to show the item in the scroll area, vertically and horizontally. This works really well except for when the content is already slightly in view. One way to improve the experience is to show the user which slide they have selected is to use the target class:
:target { outline: solid red }
You can see a full working example of it here:
http://jsfiddle.net/kmturley/6yp6Q/5/
Updated example with CSS3 animations:
http://jsfiddle.net/kmturley/fs6wge3f/1/
Sharing Server side and Client side templates
Often when creating a module or gadget you will want to render something server-side, then as the module loads with JavaScript, update the html with the new data/logic.
This presents a few questions:
- will the data load once, or refresh?
- is the data already embedded in the page?
- could the data take time to load?
- will we know that before creating our module?
- if data is coming from a javascript ajax request, will the html be empty until it loads?
- should we show a loader icon while this is happening?
- what if the server wants to render the module with some data and not load a feed?
- do we need a server-side template and then a javascript template
So many questions that prevent us from completing our module, and potentially could also force us to make changes to it later down the line as the functionality changes. I've come across this problem many times and come to a great solution.
Sharing server-side and client-side templates
Your html template should be the same for both server-side and client-side which means you need to pick the same library for both or implement the same custom template code. For this example i'm going to be using:
- Jinja for python,
- Twig for php
- Twig.js for JavaScript
A normal html file with div for javascript to attach to, javascript template and data would look something like this:
Main.html
<div id="list1" class="list"></div>
<script id="tmpl" type="text/template">
<ul>
{% for item in items %}
<li><a href="{{ item.url }}">{{ item.name }}</a></li>
{% endfor %}
</ul>
</script>
<script>
var list1 = new List('list1', [{'name': 'Item 1', 'url': 'http://www.google.co.uk'}]);
</script>
This is bad because html is combined with javascript script tags. The key is to separate the two cleanly.
Main.html
<div id="list1" class="list">
{% include 'html/modules/Item.html' %}
</div>
<script id="tmpl" type="text/template">
{% include 'html/modules/Item.html' %}
</script>
<script>
var list1 = new List('list1', [{'name': 'Item 1', 'url': 'http://www.google.co.uk'}]);
</script>
List.html
<ul>
{% for item in items %}
<li><a href="{{ item.url }}">{{ item.name }}</a></li>
{% endfor %}
</ul>
Now you can keep three files for each module which makes developing really easy:
List.html
List.css
List.js
Automated modules
Patterns start to emerge from writing our code this way, which allows us to automate processes. Lets output the modules dynamically:
Main.html
{%
set modules = [
{ 'name': 'List', 'id': 'list1', 'data': [{'name': 'Item 1', 'url': 'http://www.google.co.uk'}] },
{ 'name': 'List', 'id': 'list2', 'data': [{'name': 'List 1', 'url': 'http://www.google.com'}] }
]
%}
{% for item in modules -%}
<div id="{{ item.id }}" class="list">
{% include 'html/modules/'~item.name~'.html' %}
</div>
<script id="tmpl" type="text/template">
{% include 'html/modules/'~item.name~'.html' %}
</script>
<script>
var {{ item.id }} = new {{ item.name }}('{{ item.id }}', {{ item.data }});
</script>
{% endfor -%}
By outputting automatically you can keep all of your modules consistent while developing and ensure the all conform to the same structure.
An added benefit is that your backend developer can use you object to create the model for the CMS. He has all of the fields that the view requires to be rendered.
Saves a world of pain too when the data needs to be updated via ajax instead of server side.
This presents a few questions:
- will the data load once, or refresh?
- is the data already embedded in the page?
- could the data take time to load?
- will we know that before creating our module?
- if data is coming from a javascript ajax request, will the html be empty until it loads?
- should we show a loader icon while this is happening?
- what if the server wants to render the module with some data and not load a feed?
- do we need a server-side template and then a javascript template
So many questions that prevent us from completing our module, and potentially could also force us to make changes to it later down the line as the functionality changes. I've come across this problem many times and come to a great solution.
Sharing server-side and client-side templates
Your html template should be the same for both server-side and client-side which means you need to pick the same library for both or implement the same custom template code. For this example i'm going to be using:
- Jinja for python,
- Twig for php
- Twig.js for JavaScript
A normal html file with div for javascript to attach to, javascript template and data would look something like this:
Main.html
<div id="list1" class="list"></div>
<script id="tmpl" type="text/template">
<ul>
{% for item in items %}
<li><a href="{{ item.url }}">{{ item.name }}</a></li>
{% endfor %}
</ul>
</script>
<script>
var list1 = new List('list1', [{'name': 'Item 1', 'url': 'http://www.google.co.uk'}]);
</script>
This is bad because html is combined with javascript script tags. The key is to separate the two cleanly.
Main.html
<div id="list1" class="list">
{% include 'html/modules/Item.html' %}
</div>
<script id="tmpl" type="text/template">
{% include 'html/modules/Item.html' %}
</script>
<script>
var list1 = new List('list1', [{'name': 'Item 1', 'url': 'http://www.google.co.uk'}]);
</script>
List.html
<ul>
{% for item in items %}
<li><a href="{{ item.url }}">{{ item.name }}</a></li>
{% endfor %}
</ul>
Now you can keep three files for each module which makes developing really easy:
List.html
List.css
List.js
Automated modules
Patterns start to emerge from writing our code this way, which allows us to automate processes. Lets output the modules dynamically:
Main.html
{%
set modules = [
{ 'name': 'List', 'id': 'list1', 'data': [{'name': 'Item 1', 'url': 'http://www.google.co.uk'}] },
{ 'name': 'List', 'id': 'list2', 'data': [{'name': 'List 1', 'url': 'http://www.google.com'}] }
]
%}
{% for item in modules -%}
<div id="{{ item.id }}" class="list">
{% include 'html/modules/'~item.name~'.html' %}
</div>
<script id="tmpl" type="text/template">
{% include 'html/modules/'~item.name~'.html' %}
</script>
<script>
var {{ item.id }} = new {{ item.name }}('{{ item.id }}', {{ item.data }});
</script>
{% endfor -%}
By outputting automatically you can keep all of your modules consistent while developing and ensure the all conform to the same structure.
An added benefit is that your backend developer can use you object to create the model for the CMS. He has all of the fields that the view requires to be rendered.
Saves a world of pain too when the data needs to be updated via ajax instead of server side.
Subscribe to:
Posts (Atom)

















