[ This is a content summary only. Visit our website http://ift.tt/1b4YgHQ for full links, other content, and more! ]
by Irfan Ahmad via Digital Information World
"Mr Branding" is a blog based on RSS for everything related to website branding and website design, it collects its posts from many sites in order to facilitate the updating to the latest technology.
To suggest any source, please contact me: Taha.baba@consultant.com
[special]While many aspects of building applications with React have been standardized to some degree, styling is one area where there are still a lot of competing options. Each has its pros can cons, and there is no clear best choice.[/special]
In this article, I will provide a condensed overview of the progression in web application styling with regards to React components. I will also give a brief introduction to styled-components.
The initial release of CSS was in 1996 and not much has changed since. In its 3rd major release and with a 4th on the way, it has continued to grow by adding new features and has maintained its reputation as a fundamental web technology. CSS will always be the gold standard for styling web components but how it's used is changing every day.
From the days when we could build a website from sliced up images to the times when custom hand-rolled CSS could reflect the same as an image, the evolution of CSS implementation has grown with the movement of JavaScript and the web as a platform.
Since React's release in 2013, component-built web applications have become the norm. The implementation of CSS has, in turn, been questioned. The main argument against using CSS in-line with React has been the separation of concerns (SoC). SoC is a design principle that describes the division of a program into sections, each of which addresses a different concern. This principle was used mainly when developers kept the three main web technologies in separate files: styles (CSS), markup (HTML) and logic (JavaScript).
This changed with the introduction of JSX in React. The development team argued that what we had been doing was, in fact, the separation of technologies, not concerns. One could ask, since JSX moved the markup into the JavaScript code, why should the styles be separate?
As opposed to divorcing styles and logic, different approaches can be used to merge them in-line. An example of this can be seen below:
<button style="background: red; border-radius: 8px; color: white;">Click Me</button>
In-line styles move the CSS definitions from the CSS file. This thereby removes the need to import the file and saves on bandwidth, but sacrifices readability, maintainability, and style inheritance.
button.css
.button {
background: red;
border-radius: 8px;
color: white;
}
button.js
import styles from './button.css';
document.body.innerHTML = `<button class="${styles.button}">test</button>`;
As we can see from the code example above, the CSS still lives in its own file. However, when CSS Modules is bundled with Webpack or another modern bundler, the CSS is added as a script tag to the HTML file. The class names are also hashed to provide a more granular style, resolving the problems that come with cascading style sheets.
The process of hashing involves generating a unique string instead of a class name. Having a class name of "btn" will result in a hash of "DhtEg" which prevents styles cascading and applying styles to unwanted elements.
index.html
<style>
.DhtEg {
background: red;
border-radius: 8px;
color: white;
}
</style>
.....
<button class="DhtEg">test</button>
From the example above we can see the style tag element added by CSS Modules, with the hashed class name and the DOM element we have that uses the hash.
Glamor is a CSS-in-JS library that allows us to declare our CSS in the same file as our JavaScript. Glamor, again, hashes the class names but provides a clean syntax to build CSS style sheets via JavaScript.
The style definition is built via a JavaScript variable that describes each of the attributes using camel case syntax. The use of camel case is important as CSS defines all attributes in train case. The main difference is the change of the attribute name. This can be an issue when copying and pasting CSS from other parts of our application or CSS examples. For example overflow-y would be changed to overFlowY. However, with this syntax change Glamor supports media queries and shadow elements giving more power to our styles:
button.js
import { css } from 'glamor';
const rules = css({
background: red;
borderRadius: 8px;
color: 'white';
});
const button = () => {
return <button {...rules}>Click Me</button>;
};
styled-components is a new library that focuses on keeping React components and styles together. Providing a clean and easy to use interface for styling both React and React Native, styled-components is changing not only the implementation but the thought process of building styled React components.
styled-components can be installed from npm via:
npm install styled-components
Imported as any standard npm package:
import styled from 'styled-components';
Once installed it is time to start making styled React components easy and enjoyable.
Styled React components can be built in many ways. The styled-components library provides patterns that enable us to build well-structured UI applications. Building from small UI components, such as buttons, inputs, typography and tabs, creates a more unified and coherent application.
Using our button example from before we can build a generic button using styled-components.
Continue reading %Quick Tip: How to Style React Components with styled-components%
It is said that moving house may be one of the most stressful events you encounter in life. Moving your website to a new hosting provider involves similar steps: choosing a new home, planning and preparation, and the move itself. Is it possible to do it stress-free? I decided to find out.
I’ve been thinking about changing hosting providers for a while, but never made time for it. Since my websites were actually up and running, there wasn’t a sense of urgency. So I procrastinated.
It seems that a lot of people procrastinate about changing hosting providers. Here are three common reasons why:
Good news. There is someone offering to do it for you. And they have a team of support people who do migrate websites every day!
SitePoint recently partnered with SiteGround as our official recommended host, and they offer such a service. In fact, they’ll migrate your first website for free.
That sounds like an offer too good to miss out on, so I decided to try it out. Here’s how it went.
Continue reading %A Review of SiteGround’s Migration Service%
When discussing WordPress, and specifically the development of themes (or creating new page templates inside an existing theme) you inevitably will run up against “The Loop”. The Loop is the framework within which WordPress constructs the content for any given page that user visits, whether that be a static home page or a blog view showcasing recent posts, or anything in between. It may sound a bit complex, but really, it's just a looping mechanism.
The Loop, at its simplest, is a looping structure just like any other in programming. It iterates through what amounts to a list of all of your site's content, cycling through your posts or pages, and fetching the requested content from them. At its most complicated, you can run The Loop multiple times in a page, fetching only certain parts of items from certain categories, or with particular identifying information.
Every page template within a WordPress theme will likely contain a copy of The Loop. It's one way that the template can search for and acquire content from your pages and posts, which are stored in the database. Let’s take a look at some specifics:
<?php
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
// Post Content here
}
}
?>
You can see in the above example that it’s really a pretty straightforward setup. The entire thing starts with a conditional, with have_posts checking to ensure that there are, in fact, any posts to find. Then the loop - while there are still posts (again using have_posts), then call up the_post - the one currently being iterated through.
Continue reading %Understanding “The Loop” in WordPress%
This article is part of a series created in partnership with SiteGround. Thank you for supporting the partners who make SitePoint possible.
With images making up a whopping 65% of all web content, page load time on websites can easily become an issue.
Even when properly optimized, images can weigh quite a bit. This can have a negative impact on the time visitors have to wait before they can access content on your website. Chances are, they get impatient and navigate somewhere else, unless you come up with a solution to image loading that doesn't interfere with the perception of speed.
In this article, you will learn about five approaches to lazy loading images that you can add to your web optimization toolkit to improve user experience on your website.
Lazy loading images consists in loading images on websites asynchronously, that is, after the above-the-fold content is fully loaded, or even conditionally, only when they appear in the browser’s viewport. This means that, if users don't scroll all the way down, images placed at the bottom of the page won't even be loaded.
A number of websites use this approach, but it's especially noticeable on image-heavy sites. Try browsing your favorite online hunting ground for high-res photos, and you'll soon realize how the website loads just a limited number of images. As you scroll down the page, you'll see placeholder images quickly filling up with real images for preview. For instance, notice the loader on Unsplash.com: scrolling that portion of the page into view triggers the replacement of a placeholder with a full-res photo:
There are at least a couple of excellent reasons why you should consider lazy loading images for your website:
Well, lazy loading images helps with website performance, but what's the best way to go about it?
There is no perfect way.
If you live and breath JavaScript, implementing your own lazy loading solution shouldn't be an issue. Nothing gives you more control than coding something yourself.
Alternatively, you can browse the web for viable approaches and start experimenting with them. I did just that and came across these five interesting techniques.
David Walsh has proposed his own custom script for lazy loading images. Here's a simplified version:
The src attribute of the img tag is replaced with a data-src attribute in the markup:
[code language="html"]
<img data-src="image.jpg" alt="test image">
[/code]
In the CSS, img elements with a data-src attribute are hidden. Once loaded, images will appear with a nice fade-in effect using CSS transitions:
[code language="css"]
img {
opacity: 1;
transition: opacity 0.3s;
}
img[data-src] {
opacity: 0;
}
[/code]
JavaScript then adds the src attribute to each img element and gives it the value of their respective data-src attributes. Once images have finished loading, the script removes the data-src attribute from img elements altogether:
[code language="js"]
[].forEach.call(document.querySelectorAll('img[data-src]'), function(img) {
img.setAttribute('src', img.getAttribute('data-src'));
img.onload = function() {
img.removeAttribute('data-src');
};
});
[/code]
David Walsh also offers a fallback solution to cover cases where JavaScript fails, which you can find out more about on his blog.
The merit of this solution: it's a breeze to implement and it's effective.
On the flip side, this method doesn't include loading on scroll functionality. In other words, all images are loaded by the browser, whether users have scrolled them into view or not. Therefore, you get the advantage of a fast loading page because images are loaded after the HTML content. However, you don't get the saving on bandwidth that comes with preventing unnecessary image data from being loaded when visitors don't view the entire page content.
Continue reading %Five Techniques to Lazy Load Images for Website Performance%
The range of hosting options has become bewilderingly complex during the past few years. The basics are simple: a computing device has software installed which can respond to a network event such as a request for a webpage. How that hardware and software is installed, configured, organised, packaged, promoted and sold is the primary difference between all hosting options.
A dedicated server is the easiest option to comprehend. Imagine you bought a PC from your local store, connected it to your home network, installed web server software and configured the DNS appropriately. That device would be able to run your website and serve requests from all over the world. But could it cope with system crashes, surges in traffic, unexpected power loss, theft or security breaches? Someone compromising that device could gain access to everything on your network.
Web hosts such as SiteGround provide dedicated servers which solve these issues. You effectively own (or lease) a server which sits in a rack at the host's data center.
Levels of management vary but most hosts will provide a pre-installed operating system and the software you require. More demanding operations may require multiple devices with load balancers and separate back-end databases.
Continue reading %Dedicated Server Hosting: the Pros and Cons%