😡 React Destroys Your CSS Layout | Full Screen React Components

Поділитися
Вставка
  • Опубліковано 26 вер 2024
  • Web Dev Roadmap for Beginners (Free!): bit.ly/DaveGra...
    😡 React Destroys Your CSS Layout | Full Screen React Components
    React's nested divs can destroy your otherwise excellent full page app layout.
    🚩 Subscribe ➜ bit.ly/3nGHmNn
    📬 Course Updates ➜ courses.davegr...
    ❓ Questions - Please post them to my Discord ➜ / discord
    ☕ Buy Me A Coffee ➜ www.buymeacoff...
    👇 Follow Me On Social Media:
    Github: github.com/git...
    Twitter: / yesdavidgray
    LinkedIn: / davidagray

КОМЕНТАРІ • 125

  • @saaika5922
    @saaika5922 Рік тому +35

    Note: You should check if everyting looks allright on mobile as vh/dvh/lvh/svh usually dont work good with mobile. + never use it for typography as it will block zooming in/out.

  • @orenfarhi
    @orenfarhi Рік тому +22

    The root can be changed to any other element. Also, you can use css's "display: contents" on the root or any other element to eliminate it's effect on the DOM display layout.

  • @freekicksri4959
    @freekicksri4959 Рік тому +42

    “…React has a 🤠rOoT🤠 div element…”

  • @LopissoErgando-oo3kd
    @LopissoErgando-oo3kd 3 місяці тому

    Tnx. I was literally struggling with this problem in the morning

  • @mbbcriss1779
    @mbbcriss1779 Рік тому +1

    I had a project for uni and i kept running into this every time thank for the tip

  • @frontend9206
    @frontend9206 9 місяців тому

    This helped me alot , I was wondering about this issue since it was working without react

  • @lilyydotdev
    @lilyydotdev Рік тому +2

    I find that using 100vh creates an annoying effect on mobile where the URL bar is counted as part of the viewport leading to you being able to scroll down slightly. You can get around this by just applying a height of 100% to html,body,#root. Though you need to be careful with other container elements that might also wrap your content.

    • @iAmTheWagon
      @iAmTheWagon Рік тому +1

      Have you heard of 100dvh or 100svh? These new properties are supposed to solve this.

    • @lilyydotdev
      @lilyydotdev Рік тому +2

      @@iAmTheWagon last time I checked those properties had bad browser support but that may very well have changed

    • @iAmTheWagon
      @iAmTheWagon Рік тому

      @@lilyydotdev aaaahh they only work on chrome

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Рік тому +1

      A word of caution when using 100% for height as I demonstrate here: ua-cam.com/video/dpuKVjX6BJ8/v-deo.html

    • @ElektrykFlaaj
      @ElektrykFlaaj Рік тому

      @@lilyydotdev you can hack it using css variables and "resize" event on window. Just set --vh css variable to 1/100th of window height in handleResize function and use var(--vh) instead of vh units

  • @joopie46614
    @joopie46614 5 місяців тому

    How does your page not have a scrollbar? Usually the body has a padding/margin by default and doing a height of 100 will cause a scrollbar to appear since the browser does not take into account the size of the padding

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  5 місяців тому

      Use a CSS reset to remove that default padding OR use Tailwind or similar that removes it by default. I also now recommend dvh instead of vh for a full height.

  • @ruantristancarlinsky3851
    @ruantristancarlinsky3851 Рік тому

    My favorite web dev teacher🙏. When is your Next.js series dropping Mr Gray?

  • @Flipping_myFinds
    @Flipping_myFinds Рік тому

    Thank you so much!!! This helps me tons 😁

  • @dailynews7822
    @dailynews7822 5 місяців тому

    You can do it with one div tag. Not two div . I think

  • @techaddictdude
    @techaddictdude Рік тому +1

    Shouldn't you apply height of 100% to all parent elements of App class?

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Рік тому

      No. I don't recommend this. Applying height of 100% can lead to issues as I demonstrate here: ua-cam.com/video/dpuKVjX6BJ8/v-deo.html

  • @Taooflu
    @Taooflu Рік тому +4

    Tailwinds - I’ve been coding (S)CSS for ages and this utility first approach has been amazing.

  • @srsh12345
    @srsh12345 9 місяців тому +1

    thank you for the tip

  • @richardthompson14
    @richardthompson14 Рік тому

    I am new to learning all of this so excuse my lack of understanding. As I learned html, we progressed away from in-line styling and traded that for the organization of having the CSS file to keep everything together. Now we are learning bootstrap, seeing this makes me feel like these new libraries are moving some style back to the html file, more like in-line again. Wouldn’t it be more organized if these libraries worked with the CSS file?

    • @Tester-tv7zz
      @Tester-tv7zz Рік тому

      In React there's a css file that we put our style in called App.css otherwise we can create the file and import it.
      Just focus on what you're learning now step by step you'll know how easy it is with hardwork ofc

    • @BboyKeny
      @BboyKeny Рік тому

      I'd like to share some information on this because I think your observation a very astute and warrants it.
      =History=
      In the olden days, a webpage was made all in 1 html file. If you have only a homepage with a few elements that require custom styling, then you can imagine that simply using the style attribute is fine.
      But the moment the page grows and you have a bunch of styling repeated, then you'd want to use a class. Defining the class in a ... within the html file, is fine since you only have 1 page.
      Now imagine you have multiple pages and want to share those classes with those other html files (remember 1 html file = 1 page). You could copy the ... to every page. But now you lose what you gained from using classes in the first place. Which is that if you boss wants to turn all the cards to round borders, then you have to make sure all the different files are changed.
      This is where importing a separate css file comes in. Which can also be cached in the browser, which saves you from sending data from the server.
      There are some problems with this approach. All the classes are global variable, which means that you can get classes with the same name defined differently. Some styling gets overwritten depending on which class was defined last and some styling just gets merged. For example, you loaded a plugin for your website. But they are using sharp corners for their cards, and now your is angry at you because you removed the rounded corners he asked of you before.
      Another issue is naming things all together. Sometimes you just want to give that specific padding and nothing else. Instead of writing 30 classes with different names only containing exactly the same padding. You might as well make a "padding" class.
      Last problem with the approach of loading in a single css file. Is that you might have css that you don't need on every page. If you have 1k pages with unique styling on your imaginary complex website. You might want to make sure not everything gets loaded in everywhere.
      =Current Day=
      Webdevelopment uses components as opposed to the 1 html file = 1 page (or html templates on the server). So your card is now in its own file. Because the files that requires styling has gotten very small again, it's more sensible to keep the styling near the component.
      This fact makes inline useful again. You can also use the "padding" utility class idea for other of such css styles. (Think of bootstrap and tailwind) That way you don't have to keep track of the exact number of px your normal padding is in every file. So you keep the power of classes and you don't have class collision since you compose the complete styling for the element in the component on the element itself. The name of the component is now how you identify what part of the website you're looking at instead of using the class name.
      Another modern approach is using css modules. Which solves the issue of class collision while still making use of css classes instead of only utility classes. You can now also only load the styling if it's actually used.
      There have also been naming conventions like BEM (block__element--modifier) for css to solve these problems. But this is falling out of fashion.
      =Summary=
      I think I've dumped a bunch if history on you and the main issues that have lead to the changes in convention.
      First structure (html) - design (css) - logic (js) was combined in 1 file. The structure became bigger, which made the rest grow. So it was smart to divide these 3 concepts into their own files. Now we're moving towards components that get combined to make the page, so we can combine the 3 concepts into 1 file (or move the files closer to each other).
      In the end, how you organize your files is mainly opinion. Working on an changing website in production, for half a year to a year, will make you see the problems with any opinion and it's trade-offs.
      Now go young Padawan, center that div and figure out which element is causing that horizontal scrollbar.

  • @عبداللهابورابح-ي6ق

    Thank you please uploaded all explain with translation like this

  • @SanjuKumar-hk8yy
    @SanjuKumar-hk8yy Рік тому

    Thanks for explaining this problem

  • @Aweklin
    @Aweklin Рік тому

    Thanks for this video, I am dying to see the full css used here because I need a layout that always sticks the footer where it belongs without using the position: absolute/fixed and bottom: 0.
    Please help me with this flex layout thing.

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Рік тому +1

      Here's the flexbox chapter from my full CSS course for beginners: ua-cam.com/video/B8BFVzbKmPI/v-deo.html

    • @Aweklin
      @Aweklin Рік тому

      @@DaveGrayTeachesCode Thanks a million

  • @riczonki
    @riczonki Рік тому

    this actually broke me the first time learning react lol

  • @sirdragoon4625
    @sirdragoon4625 Рік тому

    Hi dave , can you please make a tutorial on how react supports server side rendering

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Рік тому +2

      Great request! I will for just React in the future. My current Next.js series just covered SSG and SSR.

  • @mahmoudzakria6946
    @mahmoudzakria6946 5 місяців тому

    Honestly, I don't consider that as a big issue since it's customizable, but I share the same empathy in general that yeah react ruined at least for me my DX as a developer for a long time 😂

  • @advanceringnewholder
    @advanceringnewholder Рік тому

    that's why it's better to use class or per component css

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Рік тому

      Either must account for parent elements. The #root div is often overlooked by beginners.

  • @Jay-kx4jf
    @Jay-kx4jf Рік тому

    Can also apply "display: contents" on root and app

  • @hollycow8171
    @hollycow8171 8 місяців тому

    published video in 2002?

  • @Holfend
    @Holfend Рік тому

    the rut huh

  • @jojo-gg1iz
    @jojo-gg1iz 10 місяців тому +1

    Tailwind, I can’t thank you enough.

  • @karlheinzneugebauer
    @karlheinzneugebauer Рік тому

    What about:
    body > .root,
    body > .root > .App {
    display: contents;
    }

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Рік тому

      That could flatten it nicely if you have applied those classes.

    • @karlheinzneugebauer
      @karlheinzneugebauer Рік тому

      Right, even easier! I looked over id="root".
      So:
      #root, #root > .App
      would be the selector.

  • @itsmaxim01
    @itsmaxim01 Рік тому

    Why do you stack the elements vertically rather than just make the whole video horizontal

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Рік тому

      This is a UA-cam short. They are formatted for mobile phones.

    • @itsmaxim01
      @itsmaxim01 Рік тому

      @@DaveGrayTeachesCode Phones can...rotate? :woah:

  • @Igor-vk8fl
    @Igor-vk8fl 6 місяців тому

    Omg!! Amazing

  • @Chambers18
    @Chambers18 Рік тому

    What icon pack do you use in vscode?

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Рік тому

      I recently made a short about that choice: ua-cam.com/users/shorts-uhzj-kjfV4

  • @anasouardini
    @anasouardini Рік тому +3

    that is not react that is your default of your setup automation app of choice.

  • @MobiusCoin
    @MobiusCoin Рік тому

    The root div is one of the reasons I don't use React for lazy loading blog posts in several websites I maintain.

    • @ElektrykFlaaj
      @ElektrykFlaaj Рік тому

      do you know you can render react app in literally any html tag you want? you can even use a tag

    • @MobiusCoin
      @MobiusCoin Рік тому

      @@ElektrykFlaaj yeah exactly so if I have tiles of blog posts inside of a parent, I can't just append new tiles to the parent. I have to render into a root element, which means the new React rendered tiles will never sit as true siblings to the original blog tiles.

    • @ElektrykFlaaj
      @ElektrykFlaaj Рік тому

      @@MobiusCoin that's a strange use case of react to be honest, why can't you just render all posts using react if you want to use react anyway?

    • @MobiusCoin
      @MobiusCoin Рік тому

      @@ElektrykFlaaj Infra level issues, lots of these sites I manage are on managed hosting platforms that won't allow the install of v8js. Thus I wouldn't be able to SSR the initial blog posts with React. So I have to rely on PHP to load the initial twenty or so. OR I could just have a blank div and have client side React render the blog posts on load, but it's awkward because the blog posts pop in after the rest of the DOM has been loaded.

  • @barneysman9116
    @barneysman9116 Рік тому

    That would be helpful

  • @AbhiAbhi-wu9sb
    @AbhiAbhi-wu9sb Рік тому

    It’s so helpful

  • @abbasebadian6851
    @abbasebadian6851 Рік тому

    Actually we should use 100svh to get same result in mobile devices.

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Рік тому

      Global support is currently only 68% for svh. caniuse.com/?search=svh

  • @aiyn470
    @aiyn470 5 місяців тому

    Use tailwind nextday

  • @Aida-s7f
    @Aida-s7f Рік тому

    What editor is this

  • @larryd9577
    @larryd9577 Рік тому +2

    That's why you use grid for page layout.

  • @devsami
    @devsami Рік тому

    I wonder why devs at React did not use the body, and render everything there?
    Is it something related to how portals are created ?
    Or, that the scripts were enjected and they were meant to be injected at the end of the body?

  • @ramziosta
    @ramziosta Рік тому

    Ok now I can stop banging my head on the wall! 🤷😖

  • @nekotajni394
    @nekotajni394 Рік тому

    What accent is it that makes you say rot instead of root 🤔

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Рік тому

      Kansas / Oklahoma accent. I think I say it more like rut. If I say rot, it has an ahh sound.

    • @iAmTheWagon
      @iAmTheWagon Рік тому

      My father in law from California says it like that.

  • @youhabbo01
    @youhabbo01 Рік тому

    Joe Rogan codes?!

  • @yogyyconst
    @yogyyconst Рік тому

    how about in nextjs?

  • @AnthonyPWhitaker
    @AnthonyPWhitaker Рік тому +5

    This has nothing to do with React. If you apply the right styles to the wrong selector, you won't get the expected output. It's misleading and unfair to new developers to conflate this with anything React related.

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Рік тому +2

      I disagree. It is something those learning React often overlook. The root div is part of React. And it's ok if we disagree.

    • @AnthonyPWhitaker
      @AnthonyPWhitaker Рік тому +3

      @@DaveGrayTeachesCode the root div is not part of React though. It's only there because of the createRoot() call which is passed a DOM element from standard JS's `document.getElementById('root')` which is THEN passed to React's render/createRoot. One could, createRoot on . Anyway, I meant no offense. It's great that you're trying to help others learn - more people should be like you. That said, the problem of applying CSS to the wrong DOM element is a problem, regardless of your front end framework. I personally felt like the language was then click-baity or otherwise misleading and wanted to call that out for those who may also be confused. Thanks for your content and best of luck to you going forward.

    • @carlosgonzalez9668
      @carlosgonzalez9668 Рік тому

      I was looking for this. I think the title is misleading. Thanks

  • @bernardus3289
    @bernardus3289 Рік тому

    That root div messed me up sooo bad the first time I did react. I would just put a: body,#root{ min-height:100vh;} and usually sorts it out

  • @isaacjon
    @isaacjon Рік тому

    Interesting

  • @arxci9402
    @arxci9402 5 місяців тому

    Just use grid...

  • @ChildrenOfDemiurgos
    @ChildrenOfDemiurgos Рік тому

    I get it man but why would you even do this?! I mean, layouting with Flex?! Why?!

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Рік тому

      It's an example, and a full page flex layout is not unusual. The concept of remembering their is a wrapper #root div is in there applies to any layout you may choose.

  • @feeadftth
    @feeadftth Рік тому +12

    Definitely the most irritating thing about React, except maybe not being able to call hooks outside functional components

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Рік тому +7

      I feel your pain. 💯

    • @feeadftth
      @feeadftth Рік тому +6

      @@DaveGrayTeachesCode Related to this, i was curious about best practices for writing reusable components that adapt to the kind of data it needs to show, as conditional statements are out due to the “fixed call order” for hooks. Maybe a video idea if you have time 😁
      Amazing work tho, owe you a LOT!

    • @ChildrenOfDemiurgos
      @ChildrenOfDemiurgos Рік тому

      Use “use” man 🎉

  • @sourabhkulkarni1731
    @sourabhkulkarni1731 Рік тому

    Awesome

  • @pixbug3359
    @pixbug3359 Рік тому

    Yes JSX.

  • @JTCF
    @JTCF 6 місяців тому

    Another reason why react sucks. Adding on elements that break stuff...
    So glad I started frontend web dev in rust.

  • @mac.ignacio
    @mac.ignacio 6 місяців тому

    ReactJS creating again another problem that is not really a problem.

  • @papastiki3181
    @papastiki3181 Рік тому

    I don’t see why this is a problem, that’s how css works

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Рік тому

      It's a reminder for beginners. They often overlook the root div as a parent container.

  • @danielfilat9446
    @danielfilat9446 9 місяців тому

    You're right, you should use jquery only so you can simplify a couple lines of css

  • @ajinkyax
    @ajinkyax Рік тому +1

    Vue JS has no meta or Google backing.. it's actually a better framework than going for Library such as React

  • @alexr7862
    @alexr7862 Рік тому

    Rut

  • @alexmachin1785
    @alexmachin1785 Рік тому

    Woah calling React out specifically really ? No need at all.

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Рік тому

      Chill. No need to get triggered either. Just about the #root div. And titles are about UA-cam.

  • @bradyfractal6653
    @bradyfractal6653 6 місяців тому

    React is a clusterfrick of bad abstractions. Please stop using it. Use one of the many more modern solutions.

  • @adamstuartclark
    @adamstuartclark Рік тому

    Caution, 100vh isn't without its issues.

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Рік тому

      This is true - especially in mobile. But alas, 60 seconds only allows for one quick topic.

  • @onta.nicolae
    @onta.nicolae Рік тому +2

    i never once encountered this

  • @kirksurber7262
    @kirksurber7262 Рік тому +2

    Well, this just saved me a lot of future headaches wondering why they aren't working.

  • @taverneirosilva1591
    @taverneirosilva1591 Рік тому +1

    Nice, thanks for share information

  • @shaikhdanish2793
    @shaikhdanish2793 6 місяців тому

    Its not necessary to use id root you can render the whole react on body making it more legit

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  6 місяців тому

      Nice tip - I wonder why it isn't default?

    • @shaikhdanish2793
      @shaikhdanish2793 6 місяців тому

      ​@@DaveGrayTeachesCode We can have even multiple root elements in react. Creating micro instances
      In case we want to integrate React in existing system we can create instance as
      id="sidebar" and render the sidebar in React
      id="notifications" and make the notification section in React
      Or individual page or parts of the page ✌

  • @Uri.Sab.
    @Uri.Sab. Рік тому

    Amazing exposure Sir

  • @NiceChange
    @NiceChange Рік тому

    Great tip ..this one can become very frustrating..thanks for sharing

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Рік тому

      Glad it was helpful!

    • @NiceChange
      @NiceChange Рік тому

      I am finding an abundance of golden info in your videos. Cheers...and thank you.

  • @iAmTheWagon
    @iAmTheWagon Рік тому

    This solves exactly the problem I've been wrestling with.