CSS Tips And Tricks 2 | I Wish Somebody Told Me Before

Lama Dev
25 Oct 202312:35

Summary

TLDRIn diesem Video teilt der Sprecher nützliche CSS-Tipps für Frontend-Entwickler. Er erklärt den Unterschied zwischen dem universellen Selektor, dem HTML- und dem Body-Tag und zeigt, wie sie auf eine Webseite angewendet werden. Zudem werden praktische Techniken wie das Scroll-Snapping, die Verwendung von REM anstelle von Pixeln und das Erstellen von Zählern in CSS ohne JavaScript behandelt. Abschließend zeigt er, wie man die Reihenfolge von Flexbox-Elementen einfach mit der 'order'-Eigenschaft anpassen kann. Ein informatives Video für Entwickler, die ihre CSS-Kenntnisse erweitern möchten.

Takeaways

  • 🌍 Der universelle Selektor ist kein Elternteil des HTML-Tags, sondern wählt alle Elemente im Dokument aus.
  • 🖥️ Die Hintergrundfarbe kann sowohl dem HTML- als auch dem Body-Tag zugewiesen werden, hat jedoch unterschiedliche Auswirkungen auf die Größe und Abdeckung.
  • 🔍 Body-Tags decken nicht automatisch das gesamte Viewport ab; sie passen sich dem Inhalt an.
  • 📏 Die Verwendung der Eigenschaft `box-sizing: border-box` hilft, die Größe eines Elements bei der Berechnung von Padding und Rand zu kontrollieren.
  • ✂️ Der universelle Selektor kann verwendet werden, um Standardmargen und -abstände von HTML-Elementen wie Listen und Überschriften zu entfernen.
  • 📏 Die Verwendung von `rem` statt `px` für Schriftgrößen ist besser für die Skalierung auf verschiedenen Bildschirmen, da `rem` auf der Schriftgröße des Root-Elements basiert.
  • 🧮 Der Trick, die globale Schriftgröße auf 62,5% zu setzen, erleichtert die Umrechnung von Pixelwerten in `rem`.
  • 📜 Mit CSS-Countern kann man Zähler für Elemente erstellen, ohne JavaScript zu verwenden, z.B. für automatische Kapitelnummerierungen.
  • 📋 Die Verwendung der Scroll-Snap-Eigenschaft ermöglicht das Scrollen mit automatischem Einrasten an bestimmten Stellen auf der Seite.
  • 🧩 Flexbox-Elemente können mithilfe der `order`-Eigenschaft neu angeordnet werden, ohne das HTML-Dokument zu ändern.

Q & A

  • Was ist der Unterschied zwischen dem HTML-Tag und dem Body-Tag?

    -Der HTML-Tag umfasst das gesamte Dokument, während der Body-Tag den sichtbaren Inhalt darstellt. Der HTML-Tag bestimmt allgemeine Strukturen und Attribute, aber der Body-Tag ist spezifisch für den Inhalt, den der Benutzer sieht.

  • Warum ändert sich die Größe des Body-Tags manchmal nicht sichtbar?

    -Die Größe des Body-Tags wird durch den Inhalt bestimmt. Wenn der Inhalt klein ist, bleibt der Body ebenfalls klein, auch wenn die Hintergrundfarbe sich auf den gesamten Viewport ausdehnt, was ein Standardverhalten von CSS ist.

  • Was bewirkt der universelle CSS-Selektor (*)?

    -Der universelle Selektor (*) wendet Stile auf alle Elemente im Dokument an. Dies ist nützlich, um Standardeigenschaften wie Margin und Padding zu entfernen, die von HTML-Elementen geerbt werden.

  • Warum ist die Verwendung von 'box-sizing: border-box' sinnvoll?

    -'box-sizing: border-box' stellt sicher, dass Padding und Rahmen in die Gesamtabmessungen eines Elements einbezogen werden. Ohne diese Eigenschaft würden Padding und Rahmen die Gesamtgröße eines Elements erhöhen.

  • Wann ist es besser, keine globalen Margen- und Polsterstile zu verwenden?

    -Wenn man an einem Projekt wie einer Blogseite arbeitet, auf der viele Textabsätze, Überschriften und Listen vorkommen, kann das Entfernen von globalen Margen und Polstern zu unerwarteten Problemen führen. Es ist besser, Stile selektiv anzuwenden.

  • Warum sollte die Schriftgröße in rem anstelle von Pixeln angegeben werden?

    -Die Verwendung von rem anstelle von Pixeln ermöglicht es, die Schriftgröße dynamisch anzupassen, je nach den Browsereinstellungen des Benutzers. Dies verbessert die Barrierefreiheit und ermöglicht es den Nutzern, die Schriftgröße nach Bedarf zu skalieren.

  • Wie wird der rem-Wert einfach berechnet?

    -Durch die Umstellung der Standardgröße im HTML-Tag auf 62,5% kann man 1 rem als 10 Pixel definieren. Dies vereinfacht die Berechnung von Schriftgrößen, da 2 rem beispielsweise 20 Pixel entspricht.

  • Was ist der Vorteil der Scroll-Snap-Eigenschaft in CSS?

    -Mit 'scroll-snap' kann man ein sanftes, automatisches Scroll-Verhalten definieren, bei dem der Inhalt zu einem bestimmten Punkt springt, sobald der Benutzer die Scroll-Bewegung beendet. Dies ist nützlich für ein organisiertes und benutzerfreundliches Scroll-Erlebnis.

  • Wie kann man mit CSS eine Zählung ohne JavaScript erstellen?

    -Mit 'counter-reset' und 'counter-increment' können Zähler in CSS erstellt werden. Diese werden mit dem 'before'-Pseudoelement verwendet, um automatisch nummerierte Listen oder Sektionen zu generieren.

  • Wie kann man das Layout von Flexbox-Elementen mithilfe der 'order'-Eigenschaft ändern?

    -Mit der 'order'-Eigenschaft kann man die Reihenfolge von Flexbox-Elementen ändern, ohne die HTML-Struktur zu verändern. So lassen sich bestimmte Elemente in der Anzeige priorisieren.

Outlines

00:00

🔧 Einführung in den universellen, HTML- und body-Selektor

In diesem Abschnitt erklärt der Sprecher die Unterschiede zwischen dem universellen Selektor (*), dem HTML-Tag und dem body-Tag in CSS. Es wird beschrieben, wie man den Hintergrund eines Elements definiert und die unterschiedlichen Auswirkungen der Hintergrundfarbe auf HTML und body demonstriert. Wichtig ist hier das Verständnis, dass die body-Größe vom maximalen Inhalt abhängt, während HTML nicht die gesamte Ansicht abdeckt, was häufig zu Missverständnissen führt. Ein häufiges Problem besteht darin, dass Entwickler globale Styles nicht richtig anwenden und später auf Probleme stoßen. Der Abschnitt behandelt außerdem das Entfernen von Standardmargen und -abständen bei Elementen mithilfe des universellen Selektors und zeigt, wie box-sizing: border-box verwendet wird, um die Größenberechnung zu optimieren.

05:03

🌀 Scroll-Snap-Effekte und flexible Fontgrößen

Dieser Abschnitt führt in das Erstellen eines Scroll-Snap-Effekts ein, bei dem sich der Bildlauf automatisch an vordefinierte Positionen ausrichtet. Der Sprecher erklärt, wie man Abschnitte erstellt, die den gesamten Viewport einnehmen, und wie man das Scroll-Verhalten zwischen den Abschnitten mit scroll-snap-type (mandatory oder proximity) steuert. Außerdem wird der Vorteil von rem gegenüber Pixel als Einheit für die Schriftgröße erläutert. Es wird gezeigt, wie man rem leichter berechnen kann, indem man die Standardgröße des HTML-Tags auf 10 Pixel setzt, wodurch die Umrechnung von Pixel zu rem einfacher wird.

10:08

📑 CSS-Zähler und das Order-Property für Flexbox

Hier wird erklärt, wie man mithilfe von CSS einen Zähler erstellt, um nummerierte Überschriften ohne JavaScript zu generieren. Der Sprecher beschreibt die Verwendung von counter-reset, counter-increment und der before-Pseudoklasse, um automatisch Kapitelnummern zu erzeugen. Anschließend wird das order-Property für Flexbox vorgestellt, das es ermöglicht, die Reihenfolge von Elementen innerhalb eines Containers flexibel zu ändern. Der Abschnitt endet mit einem Hinweis auf den Abschluss des Videos und den Verweis auf die vorherigen und kommenden Teile der Serie.

Mindmap

Keywords

💡Universalselektor

Der Universalselektor in CSS ('*') wählt alle Elemente eines Dokuments aus. Im Video wird erklärt, wie dieser Selektor verwendet wird, um globale Stile wie 'margin' und 'padding' auf alle Elemente anzuwenden. Dies ist besonders wichtig, wenn bestimmte HTML-Elemente standardmäßige Stile haben, die nicht vom übergeordneten Element geerbt werden.

💡HTML-Tag

Der HTML-Tag ist das oberste Element jeder HTML-Datei und enthält den gesamten Inhalt der Webseite. Im Video wird gezeigt, wie man ihm eine Hintergrundfarbe gibt und wie sich das auf die Größe des Tags auswirkt, wobei erklärt wird, dass der HTML-Tag nicht das gesamte Viewport abdeckt.

💡Body-Tag

Der Body-Tag enthält den Hauptinhalt einer Webseite. Im Video wird der Unterschied zwischen dem HTML- und dem Body-Tag hinsichtlich der Darstellung von Hintergrundfarben und der Größe im Viewport hervorgehoben, und wie die standardmäßige Höhe des Body-Tags durch den Inhalt bestimmt wird.

💡Box-Sizing

Die 'box-sizing'-Eigenschaft in CSS legt fest, wie die Gesamtgröße eines Elements berechnet wird. Im Video wird erklärt, dass durch die Einstellung auf 'border-box' die Größe eines Elements inklusive Padding und Border gemessen wird, was die Berechnung der Gesamtgröße erleichtert.

💡Scroll-Snap-Effekt

Der Scroll-Snap-Effekt ermöglicht es, dass ein Element beim Scrollen automatisch an einer bestimmten Position einrastet. Das Video zeigt, wie man diesen Effekt mit CSS für Abschnitte einer Webseite erstellt und erklärt den Unterschied zwischen den Modi 'mandatory' und 'proximity'.

💡REM-Einheit

REM ist eine relative Maßeinheit in CSS, die auf der Schriftgröße des HTML-Tags basiert. Im Video wird gezeigt, warum es besser ist, REM anstelle von Pixeln zu verwenden, da sich die Schriftgröße an die Benutzereinstellungen anpasst und so die Barrierefreiheit verbessert wird.

💡Counter-Funktion

Die 'counter'-Funktion in CSS ermöglicht es, automatisch nummerierte Listen oder Abschnitte ohne JavaScript zu erstellen. Das Video demonstriert, wie man mit dieser Funktion Abschnitte automatisch nummeriert, indem ein Zähler definiert und durch CSS erhöht wird.

💡Order-Eigenschaft

Mit der 'order'-Eigenschaft in CSS können Elemente innerhalb eines Flexbox-Containers in einer anderen Reihenfolge dargestellt werden als sie im HTML-Code definiert sind. Im Video wird gezeigt, wie man die Reihenfolge der Boxen mithilfe der 'order'-Eigenschaft ändern kann.

💡Viewport

Der Viewport ist der sichtbare Bereich einer Webseite im Browser. Im Video wird der Zusammenhang zwischen den HTML- und Body-Tags sowie deren Größe im Verhältnis zum Viewport erläutert, insbesondere wenn es um die Anwendung von Hintergrundfarben geht.

💡Overflow

Die 'overflow'-Eigenschaft in CSS steuert, wie der Inhalt angezeigt wird, wenn er den Bereich seines Containers überschreitet. Das Video zeigt, wie man einen Scroll-Container erstellt, indem man 'overflow: scroll' oder 'overflow: auto' verwendet, um einen scrollbaren Bereich zu erzeugen.

Highlights

Explanation of the universal selector, HTML tag, and body tag, and how they differ in defining styles.

Understanding the role of the body element, which covers the viewport, but its height equals the content size.

Clarification of the HTML and body elements' sizes not covering the full viewport by default.

Demonstration of the background color behavior, showing how it's propagated even though the element's size doesn't change.

The importance of the universal selector in resetting default margins and paddings for all elements.

Explaining box-sizing: border-box and how it impacts the total size of an element, including padding and borders.

The importance of carefully using the universal selector depending on the project, particularly for designs with complex text elements.

Introduction of scroll snapping, where each section snaps into place as the user scrolls, using CSS properties like `scroll-snap-align`.

The difference between `scroll-snap-type: mandatory` and `proximity`, affecting the snapping behavior.

The benefits of using rem units over fixed pixel values for font sizes, particularly for responsiveness across different devices.

Simplifying rem unit calculations by setting the global font size to 62.5%, making 1 rem equal to 10 pixels.

Demonstration of CSS counters to automatically number sections without using JavaScript.

How to reorder flexbox items using the `order` property for flexible layout adjustments.

Overview of best practices for applying background colors and ensuring styles don't interfere with nested elements.

Final encouragement to explore CSS more deeply and to continue watching the video series for additional tips.

Transcripts

play00:00

Hello friends. Today, I’ll share some other CSS  tips that every front-end developer should know.

play00:08

Firstly, I want to talk about the universal  selector, the HTML tag, and the body tag.

play00:14

These elements are familiar to most  web developers, and we all use them  

play00:18

to define our default background  color, text color, and font family,  

play00:23

but understanding their roles and  differences can be a bit confusing.

play00:27

So, let’s clarify their  purposes using this HTML file.

play00:32

I’m going to write a text here.

play00:34

And now, I want to give a  background color to my website.

play00:38

But which element should I use?

play00:41

Let’s give a background color here.

play00:44

Okay. It works.

play00:45

Now, I’m going to comment this out and  give a background for the HTML tag.

play00:51

Again, this is what we want.

play00:53

And if I do the same thing for the  body tag, you’ll see it also works.

play00:58

You might think it doesn’t matter  where we use the global styles,  

play01:02

but if you don’t understand the difference,  it might cause some problems in the future.

play01:07

In the example, you can say the body  element covers all the viewport.

play01:12

Let’s give this background again  for the HTML and see what happens.

play01:17

As you realize,  

play01:18

the body height equals the maximum content size.

play01:22

I didn’t change any size here,  but the body tag is smaller now.

play01:26

Well, it’s not actually getting  smaller; it’s always been small.

play01:31

Let’s check the inspector  and pick the body element. 

play01:36

You can see that the height is around 19 pixels.

play01:40

Now, I’ll comment this out, and the  background color is the body’s color,

play01:45

but the body size is still the same.

play01:48

And if you check the html element,

play01:50

Its height is around 35 pixels.

play01:53

So, HTML and body tags do not  actually cover the whole viewport.

play01:59

What happens here is when you give  a background for one of these tags,  

play02:03

even if their sizes are still the same, the  background color is propagated to the viewport.

play02:09

It is the default behavior of CSS.

play02:12

And, this is why we often see people use  100% sizing for the html and body.

play02:19

But in most cases, you won’t even need that.

play02:22

Let’s remove them and give a background color for one of these tags.

play02:26

The recommended way to use a background  color is, to use it inside the body tag.

play02:33

What about this universal selector?

play02:36

Some people think it’s the parent of the HTML tag,  

play02:40

but, it’s a selector that  selects all the elements inside our document.

play02:45

This is important because some HTML elements  

play02:48

have default styles that cannot  be inherited from the parent.

play02:53

What I mean by that?

play02:54

As you can see, the body element has a default  

play02:57

margin here. And I cannot remove this by giving margin 0 here.

play03:02

Or if you have an h1 tag, again, it has a margin.

play03:08

Let’s create a list and add some items inside.

play03:12

And it has a margin and also a padding.

play03:15

And I cannot remove them using the parent.

play03:18

It removes only its own margin here.

play03:22

So, I have to use each element one by  one, and remove the margin and padding.

play03:28

And this is why we need this universal selector.

play03:32

It selects all the elements in our  document, and applies a style for them.

play03:37

So if I say margin and padding 0,  it’s gonna apply for every single element.

play03:43

Another popular property is box sizing.

play03:47

Let’s create a box here,

play03:49

and I’ll give a size, background  color, padding, and a border.

play03:56

And as you can see, the box  size is more than 100 pixels.

play04:00

After adding the padding and the border,  the total size is 140 pixels.

play04:06

To prevent this globally, we  can use box-sizing: border-box.

play04:11

And now, the total size is 100 pixels.

play04:15

Using margin, padding and box-sizing is  a very common usage,

play04:19

But It depends on your project.

play04:22

If you design a blog page with many text  elements inside, like titles, paragraphs,  

play04:28

lists, removing all the margin and  padding might not be the best idea.

play04:33

Also, do not use a background  color or text color here,  

play04:37

because when you use nested  items, it’ll cause some problems.

play04:41

Let’s create a section here  and a container inside.

play04:46

And I’ll give a background color for the section.

play04:49

As you can see, the background is still this  

play04:52

color because we selected our div  element and changed its color.

play04:57

If I move the background color to the body, I can see the section background.

play05:02

Okay, right now, you know the main  purpose of these elements.

play05:06

So let’s see the second CSS tip.

play05:10

I’m gonna show you how to  create a snap scroll effect  

play05:13

and what we should pay attention to when using it.

play05:16

Let’s create 5 sections  and put some texts.

play05:20

Each section is gonna contain the whole  viewport, so I’ll say height 100vh.

play05:27

Let’s increase the font size and give a color.

play05:30

And using nth child selector, I’ll give  different background color for each section.

play05:43

Right now we are ready to give the snap effect.

play05:46

To do that, we need a scroll container.

play05:49

If we create a container,  and give overflow scroll or auto,  

play05:54

it becomes a scroll container.

play05:57

HTML tag is a scroll container by default,  

play06:00

we can use it in the CSS file or we can create a new div and give it an overflow.

play06:07

I'll say overflow.

play06:09

Let's make this hidden first, and the height 100vh.

play06:14

As you can see, it ends at this point and I cannot scroll.

play06:19

But if I say change here, I’ll be able  to scroll down and see my items.

play06:24

Basically this scroll bar belongs  to container, not to HTML.

play06:29

Let me change the size.

play06:30

As you can see, the bar is here.

play06:35

Okay. Let’s remove this and  give a scroll snap type.

play06:38

We’ll scroll vertically, so I’ll use  the y axis, and I’ll add the type.

play06:44

It’s gonna be mandatory.

play06:46

I’ll explain this in a moment.

play06:48

And in the section, I need  to provide the snap position.

play06:53

scroll-snap-align

play06:54

It can be start, center, or end.

play06:58

But in our case, it doesn’t matter  because our sections are full screen.

play07:03

As you can see, it works as expected.

play07:06

But if I change the size, and align to the center,  

play07:11

it’s gonna show item in the  middle of the container.

play07:15

Let’s take this back and change this type.

play07:19

When it’s mandatory, as soon as I release my  fingers, it snaps to other section.

play07:26

But if I make this proximity, I can scroll down, and it snaps  only when it’s close enough to the other section.

play07:34

And it gives a more flexible scrolling experience.

play07:39

Okay. After snap scrolling,  let’s talk about this font size.

play07:43

I’m gonna zoom out and make this 100%.

play07:47

Remember we used here 72 pixels.

play07:51

But as most of you know, the recommended  size unit is rem not pixels.

play07:56

If you don’t know why it’s recommended, let me  explain quickly, and after, I’ll give a small font size trick.

play08:03

I’m gonna open my browser  settings and find the font size.

play08:07

By default it’s medium, but some people including  

play08:11

my mom using their computers and  phones with the large font size.

play08:16

But if I change here to very large nothing happens  because we fixed our font size to 72 pixels.

play08:23

It never changes.

play08:25

To prevent this, instead of pixels, we can use rem.

play08:29

And 1 rem equals 16px.

play08:32

72 divided by 16 equals 4.5rem

play08:37

And now, the font size changes as we expected.

play08:41

But the problem is the calculation of the rem units.

play08:44

If you are not familiar, it can be hard to work with.

play08:47

If you are using 20 pixels for example, it should be 1.25 rem,

play08:53

If you are using 30, it should be 1.875 rem

play08:58

But don’t worry there is an easier way to use it.

play09:02

As you know the default  global font size is 16 pixels.

play09:06

And using html tag I wanna convert it to  10 pixels to make to calculation easier.

play09:13

To do that I’ll say font size 62.5%.

play09:18

And right now the default size  is 10 pixels and it equals 1 rem.

play09:23

Using the body tag, I can set the default  font size to 1.6rem. And it equals 16px again.

play09:31

And now it’s much easier to use rem unit.

play09:35

Instead of this size, I can  use 2rem which is 20 pixels,

play09:40

or 3 rem, which is 30 pixels.

play09:44

Okay. Let me give you another CSS tip.

play09:47

Let's say we have a list here, but instead of a list element, we are using h2 tags.

play09:52

And inside we have some texts.

play09:55

And before each text, I wanna write “Section”  and the and the number of that section.

play10:00

So it’s gonna be: Section 1 - HTML Tutorial,  Section 2 - CSS Tutorial, 3, 4 and 5

play10:07

But how I’m gonna do this  without using JavaScript.

play10:11

I can actually create a counter  using CSS and it’s really easy.

play10:16

Firstly, I’ll define my counter  inside body using counter-reset,  

play10:21

and I’ll write here the name of the counter.

play10:24

It can be anything.

play10:25

And the default value is 0 right now.

play10:28

But how I’m gonna write a text before each item.

play10:31

To do that I can use before selector, and  write anything I want inside the content.

play10:37

Let’s say Section.

play10:39

After that I can show my-counter  using the counter function.

play10:44

As you can see it’s 0 because  we didn’t increment our number.

play10:48

In order to do that, I can use  counter-increment and pass my counter name here.

play10:54

And right now we can see the chapters and we can  add a new item without changing all the titles.

play11:02

And the last tip is order property.

play11:05

I’ll create a container and  inside I’ll add 4 boxes.

play11:10

The first one will be redBox,  blueBox, green and orange

play11:18

Let’s give a size, and a text color

play11:23

And for each box, I'll give their backgrounds.

play11:31

And our container will be a flex box.

play11:35

Let’s give a space between items.

play11:38

And now I can give specific order  for any item using order property.

play11:44

Let’s say order 2, order 1, 4 and 3

play11:51

As you can see, it's that easy to give  different priorities to our flexbox items.

play11:58

Okay, guys. That’s all.

play11:59

If you didn’t watch the first part of this series, the link will be in the description below, and if  

play12:05

you want to see the third part of the CSS tips, please  like the video and let me know in the comments.

play12:12

I really enjoy creating CSS content but  it depends on the video likes and views.

play12:17

Okay.

play12:18

You can support Lama Dev by joining the channel  or using the link in the description below.

play12:23

Don’t forget to follow Lama  Dev’s social media accounts.

play12:26

I hope I’ll see you in the next video. Goodbye.

Rate This

5.0 / 5 (0 votes)

Связанные теги
CSS-TippsWebentwicklungFront-EndUniverseller SelektorBox-SizingFlexboxSnap ScrollRem EinheitenHTML StylingWebdesign
Вам нужно краткое изложение на английском?