issue125:c_c
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
issue125:c_c [2017/10/06 14:25] – d52fr | issue125:c_c [2017/10/11 11:12] (Version actuelle) – auntiee | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
**The last few months of my life have been dedicated to a large project for university. In the course of this project, I was designing a website to go along with a database (the main focus). Therefore, I decided to stick to only the newest CSS technologies, | **The last few months of my life have been dedicated to a large project for university. In the course of this project, I was designing a website to go along with a database (the main focus). Therefore, I decided to stick to only the newest CSS technologies, | ||
- | Ces derniers mois d' | + | Ces derniers mois ont été dédiés à un grand projet universitaire. Au cours de ce projet, j'ai conçu un site Web pour accompagner une base de données (le sujet central). Par conséquent, |
**Code | **Code | ||
Ligne 16: | Ligne 16: | ||
For anyone who is not experienced with older approaches to layouts in CSS, this was not the case. Originally it was done with floating items left and right (which involved a fair few hacks and caveats), and, more recently, it was done with Flexbox. While CSS Grids do not replace Flexbox, they are more suited to general layout. The reason for this is that Flexbox acts in one direction - horizontally or vertically. Imagine you have 3 elements, and you want a split of 1 element left, and two elements stacked vertically on the right half. With Flexbox you would need to define a container for both the horizontal and the vertical directions. With CSS Grid, you simply define the element on the left as taking up two rows.** | For anyone who is not experienced with older approaches to layouts in CSS, this was not the case. Originally it was done with floating items left and right (which involved a fair few hacks and caveats), and, more recently, it was done with Flexbox. While CSS Grids do not replace Flexbox, they are more suited to general layout. The reason for this is that Flexbox acts in one direction - horizontally or vertically. Imagine you have 3 elements, and you want a split of 1 element left, and two elements stacked vertically on the right half. With Flexbox you would need to define a container for both the horizontal and the vertical directions. With CSS Grid, you simply define the element on the left as taking up two rows.** | ||
+ | |||
+ | Le plan | ||
+ | |||
+ | Comme la plupart des lecteurs l'ont probablement lu dans mon article du FCM n° 123, les grilles CSS sont un nouvel outil d' | ||
+ | |||
+ | Pour ceux qui n' | ||
**So far, so good. The problem I originally ran into was that I was applying the ideas of CSS Grids to my ‘usual’ approach - I was redefining everything as I needed to change it for media queries. This means my mobile-first approach (no media query) would define a CSS Grid completely, and later media queries (for larger screens) would completely redefine it. I was then also reassigning elements repeatedly to make sure they appeared in the right locations on the grid. | **So far, so good. The problem I originally ran into was that I was applying the ideas of CSS Grids to my ‘usual’ approach - I was redefining everything as I needed to change it for media queries. This means my mobile-first approach (no media query) would define a CSS Grid completely, and later media queries (for larger screens) would completely redefine it. I was then also reassigning elements repeatedly to make sure they appeared in the right locations on the grid. | ||
I then read an article on CSS Custom Properties (see Further Reading #1). After reading it, I was rethinking how you could use CSS Custom Properties and media queries to effectively stop repeating yourself. I immediately thought to myself “I can use this with CSS Grids!”. And so some experimenting began - focusing mainly on CSS Grids’ area definitions, | I then read an article on CSS Custom Properties (see Further Reading #1). After reading it, I was rethinking how you could use CSS Custom Properties and media queries to effectively stop repeating yourself. I immediately thought to myself “I can use this with CSS Grids!”. And so some experimenting began - focusing mainly on CSS Grids’ area definitions, | ||
+ | |||
+ | Jusqu' | ||
+ | |||
+ | Après cela, j'ai lu un article sur les propriétés personnalisées de CSS (CCS Custom Properties - voir la première ligne de « Pour aller plus loin »). Après l' | ||
**The Explanation | **The Explanation | ||
The plan above may have left you wondering exactly what I meant. That’s what this section is for! At this point, I ask you to please open the Codepen link from above, and to follow along as I explain.** | The plan above may have left you wondering exactly what I meant. That’s what this section is for! At this point, I ask you to please open the Codepen link from above, and to follow along as I explain.** | ||
+ | |||
+ | L' | ||
+ | |||
+ | Le plan ci-dessus pourrait vous avoir laissé perplexe sur ce que je voulais dire. C'est le rôle de cette partie ! Arrivés ici, je vous demande, s'il vous plaît, d' | ||
**CSS Custom Properties | **CSS Custom Properties | ||
Ligne 34: | Ligne 48: | ||
This means we end up with a grid that has 4 equally sized columns, and 3 rows (with a fixed-height header/ | This means we end up with a grid that has 4 equally sized columns, and 3 rows (with a fixed-height header/ | ||
+ | |||
+ | Les propriétés personnalisées de CSS | ||
+ | |||
+ | Je les définis tout en haut du fichier CSS, en utilisant le sélecteur :root (qui sélectionne l' | ||
+ | |||
+ | Je n'ai aussi défini que 3 propriétés personnalisées - var-columns qui configure les colonnes de base de notre grille, var-rows qui fait de même pour les lignes de la grille et var-aside. La variable var-aside n'est là que parce que j'ai une disposition où la barre latérale est complètement cachée. On peut faire cette étape sans les propriétés personnalisées, | ||
+ | |||
+ | Ainsi, au final, nous terminons avec les valeurs des variables suivantes : var-columns=1fr 1fr 1fr 1fr, var-rows=6rem 1fr 6rem, var-aside=block. | ||
+ | |||
+ | Cela signifie que nous finissons avec une grille qui a quatre colonnes de dimensions identiques et 3 lignes (avec une ligne d' | ||
**Defining Our Areas | **Defining Our Areas | ||
Ligne 40: | Ligne 64: | ||
The next few lines are the most important. I define display: grid; to get the process started. Then I load in the rows and columns variables to initialize the grid. The key point is the next declaration, | The next few lines are the most important. I define display: grid; to get the process started. Then I load in the rows and columns variables to initialize the grid. The key point is the next declaration, | ||
+ | |||
+ | Définir nos zones | ||
+ | |||
+ | Dans le sélecteur suivant, (ligne 7), je définis le corps de notre site Web. Je règle la hauteur et la largeur à 100vh et 100vw respectivement pour m' | ||
+ | |||
+ | Les quelques lignes suivantes sont les plus importantes. Je définis display: grid; pour initialiser le processus. Ensuite, je charge les variables des lignes et des colonnes pour initialiser la grille. Le point-clé est la déclaration suivante, où je définis les différentes zones auxquelles j' | ||
**Redefining Our Grid | **Redefining Our Grid | ||
Ligne 48: | Ligne 78: | ||
One very important fact to remember is that your grid-template-areas must have the right number of rows and columns. Even if you’re assigning an entire row with 5 columns to one area, you will need to include the area name for each column.** | One very important fact to remember is that your grid-template-areas must have the right number of rows and columns. Even if you’re assigning an entire row with 5 columns to one area, you will need to include the area name for each column.** | ||
+ | |||
+ | Redéfinir nos grilles | ||
+ | |||
+ | L' | ||
+ | |||
+ | Maintenant, pour être honnête, je plaide vraiment pour une première approche pour les mobiles dans les vrais projets. Pour cette démo, c' | ||
+ | |||
+ | Un fait très important à se remémorer est que votre ensemble grid-template-areas doit avoir le bon nombre de lignes et de colonnes. Même si vous assignez une ligne entière sur 5 colonnes à une seule zone, vous devrez inclure le nom de zone de chaque colonne. | ||
**Some readers may also be wondering why we’re redefining our variables in a body selector instead of :root. This is the strength of CSS Custom Properties - you can override them like any other CSS property. This means no more need to reset variables (except as needed for children elements). Since we’re only using the variables in body, it made sense to define them there and shorten our media query (by removing a selector). | **Some readers may also be wondering why we’re redefining our variables in a body selector instead of :root. This is the strength of CSS Custom Properties - you can override them like any other CSS property. This means no more need to reset variables (except as needed for children elements). Since we’re only using the variables in body, it made sense to define them there and shorten our media query (by removing a selector). | ||
This may become clearer by looking at the no-aside version of the body (lines 47-56). As you can see, the only change needed for the body was the redefining of our grid-template-area (no custom properties were overwritten). However, a second selector (body.no-aside aside) then changes the var-aside variable to none. Since the variable is used later in the CSS file, nothing else needs to be done here. By defining that variable to be none, the display: statement becomes “display: none;” and for other views, the variable is untouched. It also works in this case if you leave out the default declaration (var-aside: block;) as the statement will just silently fail with an invalid variable, and since the default display is “block”, | This may become clearer by looking at the no-aside version of the body (lines 47-56). As you can see, the only change needed for the body was the redefining of our grid-template-area (no custom properties were overwritten). However, a second selector (body.no-aside aside) then changes the var-aside variable to none. Since the variable is used later in the CSS file, nothing else needs to be done here. By defining that variable to be none, the display: statement becomes “display: none;” and for other views, the variable is untouched. It also works in this case if you leave out the default declaration (var-aside: block;) as the statement will just silently fail with an invalid variable, and since the default display is “block”, | ||
+ | |||
+ | Certains lecteurs peuvent se demander aussi pourquoi nous redéfinissons nos variables dans le sélecteur du corps plutôt que dans :root. C'est la force des propriétés personnalisées de CSS - vous pouvez les écraser comme toute autre propriété de CSS. Cela signifie qu'il n'y a plus besoin de remettre les variables à zéro (sauf au besoin pour les éléments enfants). Comme nous n' | ||
+ | |||
+ | Cela deviendra plus clair en regardant la version « no-aside » du corps (lignes 47-56). Comme vous pouvez le voir, le seul changement nécessaire dans le corps était de redéfinir notre grid-template-area (aucune propriété personnalisée n'est écrasée). Cependant, un second sélecteur (body.no-aside aside) met ensuite la variable var.aside à zéro. Comme la variable est utilisée par la suite dans le fichier CSS, il n'y a rien d' | ||
**The HTML | **The HTML | ||
Ligne 58: | Ligne 100: | ||
The onclick definitions in the nav tag are also there only to enable the JS swapping of classes. This means you can finally begin writing clear, semantic HTML5 markup!** | The onclick definitions in the nav tag are also there only to enable the JS swapping of classes. This means you can finally begin writing clear, semantic HTML5 markup!** | ||
+ | |||
+ | Le HTML | ||
+ | |||
+ | Comme vous pouvez probablement le voir, le HTML utilisé ici est assez clair. Il n'y a que les quatre éléments principaux (header, main, aside et footer). Je mets l' | ||
+ | |||
+ | Les définitions onclick dans l' | ||
**Can Children Inherit the Grid? | **Can Children Inherit the Grid? | ||
No. CSS Grids can be applied only to direct descendents of the container element (so header, main, aside, and footer in our above example). Anything deeper than that (nav, for example) cannot be placed on the grid. That being said, you can either nest another CSS grid, or, since this is dealing with only one direction, use Flexbox.** | No. CSS Grids can be applied only to direct descendents of the container element (so header, main, aside, and footer in our above example). Anything deeper than that (nav, for example) cannot be placed on the grid. That being said, you can either nest another CSS grid, or, since this is dealing with only one direction, use Flexbox.** | ||
+ | |||
+ | Les enfants peuvent-ils hériter de la grille ? | ||
+ | |||
+ | Non. Les grilles CSS ne peuvent être appliquées qu'aux descendants directs de l' | ||
**Progressive Enhancement Approach? | **Progressive Enhancement Approach? | ||
Ligne 68: | Ligne 120: | ||
Suppose, for example, you’ve defined a basic “fallback” layout, which gives a usable (but different) experience for any browser that doesn’t support flexbox or grid. Then, at the end of the file, you will want to define a @supports(display: | Suppose, for example, you’ve defined a basic “fallback” layout, which gives a usable (but different) experience for any browser that doesn’t support flexbox or grid. Then, at the end of the file, you will want to define a @supports(display: | ||
+ | |||
+ | Une approche d' | ||
+ | |||
+ | La réalité du monde de la conception Web est telle que tout le monde n' | ||
+ | |||
+ | Supposez, par exemple, que vous avez défini une mise en page basique « de repli », qui donne un rendu utilisable (mais différent) pour tout navigateur qui ne supporte pas flexbox ou les grilles. Alors, en fin de fichier, vous voudrez définir un @supports(display: | ||
**Should I Use It In Production? | **Should I Use It In Production? | ||
Ligne 73: | Ligne 131: | ||
That depends entirely on what you’re producing. If you’re simply worried that CSS Grids is too new to be used, keep in mind that websites such as the New York Times have already shipped production sites with Grids. If you’re asking because you mainly support IE and Microsoft Edge, then I would say use it as you can, but don’t force yourself to spend massive amounts of time on features both browsers don’t fully support. That being said, Microsoft Edge does support CSS Grids - though there are a few restrictions. | That depends entirely on what you’re producing. If you’re simply worried that CSS Grids is too new to be used, keep in mind that websites such as the New York Times have already shipped production sites with Grids. If you’re asking because you mainly support IE and Microsoft Edge, then I would say use it as you can, but don’t force yourself to spend massive amounts of time on features both browsers don’t fully support. That being said, Microsoft Edge does support CSS Grids - though there are a few restrictions. | ||
- | However you decide to do it, if you want to use Grids, I recommend you start with the grid layout first. Why? Because it’s by far the most efficient and quick way to lay out a website. I use it for all my prototypes and mockups, because, in those cases, time is of the essence, and I can always pass it to my clients with the indicator that it must be viewed in a modern browser. Only once you’re happy with the new, fast approach, should you slog back into the wasteland that is browser hacks and idiosyncrasies.** | + | However you decide to do it, if you want to use Grids, I recommend you start with the grid layout first. Why? Because it’s by far the most efficient and quick way to lay out a website. I use it for all my prototypes and mockups, because, in those cases, time is of the essence, and I can always pass it to my clients with the indicator that it must be viewed in a modern browser. Only once you’re happy with the new, fast approach, should you slog back into the wasteland that is browser hacks and idiosyncrasies.** |
+ | |||
+ | L' | ||
+ | |||
+ | Ça dépend complètement de ce que vous produisez. Si vous êtes simplement inquiet de ce que les grilles CSS sont trop récentes pour être utilisées, gardez en tête que des sites Web comme le New York Times a déjà mis en production des sites avec Grids. Si vous le demandez parce que vous supportez principalement IE et Microsoft Edge, alors je pourrais dire de l' | ||
+ | |||
+ | Peu importe la façon de le faire, si vous voulez utiliser les grilles CSS, je vous recommande de commencer d' | ||
**I hope this article is interesting for at least a few of my readers. If you’re one of them, feel free to share examples of your CSS Grids projects or talks/ | **I hope this article is interesting for at least a few of my readers. If you’re one of them, feel free to share examples of your CSS Grids projects or talks/ | ||
Ligne 82: | Ligne 146: | ||
https:// | https:// | ||
+ | |||
+ | J' | ||
+ | |||
+ | Pour aller plus loin | ||
+ | |||
+ | https:// | ||
+ | |||
+ | https:// |
issue125/c_c.1507292737.txt.gz · Dernière modification : 2017/10/06 14:25 de d52fr