How CSS Padding and Margin Works

Поделиться
HTML-код
  • Опубликовано: 17 окт 2024

Комментарии • 166

  • @shaikhshafeen
    @shaikhshafeen 10 месяцев назад +3

    I finally understood! Thanks a lot! That 'clock-wise rotation' words immediately cleared out my all confusions!

  • @EdmundAlynJones
    @EdmundAlynJones 4 года назад +41

    You're like the Bob Ross of Web Dev. Thank you for posting this.

  • @devondambrosio4978
    @devondambrosio4978 4 года назад +47

    You just made more sense in seven minutes than the last two hours of me studying in school. Thank you!

  • @emmamachi7822
    @emmamachi7822 2 года назад +2

    i've watched countless tutorials on this only to feel more confused and losing the point. you have a talent for teaching. subscribed!

  • @tonyaanderson
    @tonyaanderson 2 года назад +2

    This was the BEST explanation of margins and padding! THANK YOU!!

  • @rahuls3838
    @rahuls3838 4 года назад +14

    One of the best Tutorials out there! Thank you Steve for helping out millions of students across the globe.

  • @adelowokehinde58
    @adelowokehinde58 2 года назад +2

    I know it’s been years you posted this but you’re so calm, well explanatory and you makes a lot of senses.. thanks Steve 👏🏻

  • @ripcrypto5189
    @ripcrypto5189 2 года назад +1

    I love the fact you actually go straight to the point i know this was 1 year ago but this actually still helped me a LOT i was like “What the hell is margin and padding? But then i saw this thanks a lot ❤

  • @smailsafire9593
    @smailsafire9593 3 года назад +2

    This is the best explanation in the internet for margin and padding, thank you Steve for the lesson.

  • @lamokcm209
    @lamokcm209 3 года назад +1

    i've been trying to get padding and margin for the last 2hours but this video made the understanding much easier in less than 5mn ! thanks a lot

  • @Moarmmoforme
    @Moarmmoforme 2 года назад +1

    Short, concise and to the point. Much obliged kind sir!

  • @nichoalaz6235
    @nichoalaz6235 3 года назад +1

    Hey thanks man! as someone whos only recently started learning HTML and CSS this video was really helpful.

  • @ranaasad5076
    @ranaasad5076 3 года назад

    Hats off to you
    these 7 mins tells everything i want to know

  • @fahadibrahim7268
    @fahadibrahim7268 3 года назад +1

    amazing explanation, opened a locked door for me as a beginner. Thank you

  • @khanayan-v7z
    @khanayan-v7z 2 года назад +1

    Thank you so much steve, the way you explained was so simple and understandable!

  • @UriAlexandrovitz1
    @UriAlexandrovitz1 2 года назад +1

    Excellent video! short yet very coherent

  • @TheDQR
    @TheDQR 3 года назад

    You're great pal, great explanation, your very calmed voice made me put more attention, great video.

  • @freemancity2518
    @freemancity2518 3 года назад +2

    Such a good explanation , it really helped me as visual learner.

  • @sugabts3293
    @sugabts3293 3 года назад +1

    wow you shoud a teacher the way u explain things very smooth

  • @zakhariihusar6975
    @zakhariihusar6975 3 года назад

    Thanks. I"ve just begun and it helped me to adjust text in a list in a centre of the background picture!

  • @sunilchannelvlog3743
    @sunilchannelvlog3743 3 года назад

    you deserve more mate, it was the best lecture i have ever had in my life, hitting sub

  • @shaoyangzhang7974
    @shaoyangzhang7974 2 года назад +1

    Really appreciate this lecture, you are the best👍😃

  • @chum2470
    @chum2470 2 года назад

    Your voice is so calming

  • @shubhsagar6234
    @shubhsagar6234 4 года назад +1

    Now i an actually understanding css coz of you... Hope to complete whole playist thus week :)

  • @Nitroco0l
    @Nitroco0l 4 года назад

    Wow brother, thank you I was having trouble understanding margins and padding. I feel more at ease now.

  • @newworld4252
    @newworld4252 2 года назад

    Woww oh my God.i got the whole of it in minutes.. Thank you bro

  • @ElijahKabambala-jw8my
    @ElijahKabambala-jw8my 3 дня назад

    Thank you, well explained with much calmness

  • @legitsports4808
    @legitsports4808 День назад +1

    Use larger numbers so that people can clearly see the change happening.

  • @jiriorsag6184
    @jiriorsag6184 2 года назад +1

    Best tutorial, totally clear 👌 Thanks!

  • @harshalsurwase2185
    @harshalsurwase2185 3 года назад

    i don't have left any confusion about padding and margin after watched this.

  • @MikixIT
    @MikixIT 4 года назад +4

    Best Tutorial Ever, thank you so much

  • @TonyYakovenko
    @TonyYakovenko 3 года назад

    Thank you, Steve! Short and to the point.

  • @nagavenkateshkolapalli4547
    @nagavenkateshkolapalli4547 4 года назад

    Best lecture I have ever seen👍👍

  • @MuhammadZubair-xw2pf
    @MuhammadZubair-xw2pf 2 года назад

    Wow. Now i understand what is difference b/w them. Thanks

  • @tabrezpathan1588
    @tabrezpathan1588 2 года назад +1

    Love the way you teach. It’s like a master class. Thank you for this.
    Ricky Bubbles Julian Randy! TPB is the best!

  • @haoql.7686
    @haoql.7686 2 года назад

    Very enjoyed watching this video while learning a lot as well

  • @Iamgayathrinagarajan
    @Iamgayathrinagarajan 4 года назад +1

    Just started learning..its so useful..Thank you

  • @notsphinxz
    @notsphinxz 2 года назад

    What's for the Margin when you make another space at the top right bottom and left?
    Like Is it overlapping by its default then you type margin 10px on the screen. And I want
    to know more about margin and padding I am practicing at it.

  • @damo190
    @damo190 3 года назад

    Do margin collapse happen in relatively positioned element? I didn't see such case. MDN only mentions margin collapsing won't happen in absolutely positioned element.

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  3 года назад

      The margin collapse is applied only to the original space for the element. With relative positioning, the original space is saved, the element gets removed from the page while the rest of the page is built, and then put back in the original position with any translations with transform, top, left, etc.

  • @DSureshKumar
    @DSureshKumar 3 года назад

    I subscribe your channel because you made this video very informative. Plz use this teaching skills in all your video. From my side you are 10/10.

  • @damo190
    @damo190 3 года назад

    Nice video Steve.
    I have difficulty in understanding the margin collapsing in nested block elements and with negative margin, I can't understand how the position of blocks change. Example,
    div{border:1px solid blue;}
    .container{background-color:yellow; margin:40px;}
    .box{background-color:orangered; margin:40px; }
    change margin
    When I added "margin-top: -50px;" to .box selector, I can't understand how two boxes position changes than before when I didn't change the margin. I checked without border too for margin collapsing, but couldn't understand.
    Please make a video on this topic.

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  3 года назад +1

      Negative margins are actually quite rare. They used to be used a lot with float to create columns. Now people use flexbox and grid to accomplish this.
      Try this CSS to see if it makes more sense:
      .container {
      background-color: yellow;
      padding: 40px;
      margin: 40px;
      }
      .box {
      background-color: orangered;
      padding: 0;
      margin: -40px 40px 40px 40px;
      }

  • @devT44
    @devT44 Год назад

    While checking in MDN, I found that margin-top and margin-bottom have no effect on non-replaced inline elements. What are these replaced and non-replaced elements, can u explain in your video?

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  Год назад +1

      developer.mozilla.org/en-US/docs/Web/CSS/Replaced_element
      replaced elements are elements whose content is not impacted by your CSS - video, , img, etc.
      inline vs block elements - can be explained through this video - ruclips.net/video/UBWl37Rr9_c/видео.html

    • @devT44
      @devT44 Год назад

      @@SteveGriffith-Prof3ssorSt3v3 thank you.

  • @janaschelle8377
    @janaschelle8377 3 года назад

    Saving my life again and again... Thank you!

  • @Hieucd97
    @Hieucd97 3 года назад

    Great video! What about negative values for margins and padding? Would it just go beyond the space? If I set my padding values to negative, would the words go outside of the blue box?

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  3 года назад

      Negative padding doesn't work but negative margins will pull the box in different directions.

    • @Hieucd97
      @Hieucd97 3 года назад

      @@SteveGriffith-Prof3ssorSt3v3 Thank you! I'm new to HTML and was confused with margins and padding when I put in negative values

  • @saheedabdulsobur5981
    @saheedabdulsobur5981 2 года назад +1

    Can you resize the image you are intending to use for the list-style image?? I will all appreciate your guidance. It gets bigger on my screen. Thanks

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  2 года назад +1

      If you need to style the size of the image that you are using for your list bullets then you should use a background-image and scale it as you want with li::before{ }

    • @saheedabdulsobur5981
      @saheedabdulsobur5981 2 года назад +1

      @@SteveGriffith-Prof3ssorSt3v3 thanks so much steve

  • @bljelena90
    @bljelena90 3 года назад +1

    This makes so much sense now thank you.

  • @spagetti2514
    @spagetti2514 4 года назад

    hello sir i am having an issue compressing them vertically , i have the list of my audio files inside my body tag with margin , but i cannot compress them to become closer .. my example would be the white lines inbetween all of your blue boxes but so much larger , please help me out bro much appreciated

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  4 года назад

      Every tag has the box model box around it. If you have space then you have padding or margin around some element or you have large line height. Use the dev tools in chrome to investigate

  • @armyteguh2649
    @armyteguh2649 5 лет назад +1

    really Love, nice, and what a great explanation...
    but sir, can i fill the value with %, rem, em, or we have to fill it with number only ??
    and thanks in advance sir...

  • @minercreepmc
    @minercreepmc 3 года назад

    Why margin top/bottom do not get add to each other but left/right do, sir.
    Ex:
    If two of this top on each other
    .one{ margin-bottom: 20px}
    .two{margin-top:10px}
    So the margin between these will be 20px
    But if those sit next to each other
    .one{mg-right:10px}
    .two{mg-left:20px}
    Then margin between them will be 30px
    I read that margin top bottom will not be added, but left right they will. But I still a little bit confuse

  • @shankarbaddi4424
    @shankarbaddi4424 3 года назад

    Best explanation ever...

  • @websurferwizard
    @websurferwizard 3 года назад

    Hey steve, do you have a tutorial on how to set up Brackets? Just like you have it. I'm confused.

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  3 года назад

      I switched over to VSCode in 2018 and haven't used Brackets since then. I highly recommend that all my students do the same. Emmet is built into VSCode. Here is a video about VSCode - ruclips.net/video/FxRBjgTgudw/видео.html
      I did make this video about Brackets back in 2017 - ruclips.net/video/yblM56mx2MU/видео.html

  • @topeakanji4942
    @topeakanji4942 2 года назад

    Pls is padding possible without changing the size of the outer div, if not, how can u move the text without changing the outer div size, thanks

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  2 года назад

      The size of the box is the sum of margin, border, padding and width. If you want to make one bigger without changing the size of the box then one of the other values must change.
      The relationship of the width to the other values can also be altered with the box-sizing property.

  • @away6545
    @away6545 3 года назад

    idc how old this video was. But it makes sense

  • @espritcontemplatif7965
    @espritcontemplatif7965 2 года назад

    Uncountable thanks! Extremely Helpful.

  • @Mammad12J
    @Mammad12J 3 года назад +1

    Really good Tutorial. it was awesome.

  • @joemcdonough2510
    @joemcdonough2510 3 года назад

    paragraphs have a default margin on top and bottom of 1em. How come the background color does not have that 1em margin between each of the paragraphs? I wrote the same code out that you have at the beginning of this video in code pen and I see a margin between each of the three paragraphs.

    • @joemcdonough2510
      @joemcdonough2510 3 года назад

      what I mean is I see a small white gap between each of the paragraph's background colors. But at the beginning of your video, there is no small white gap between each of the cornflowerblue backgrounds. Does this make sense?

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  3 года назад

      @@joemcdonough2510 margins always have a transparent background. Padding shows the background colour of the element.

    • @joemcdonough2510
      @joemcdonough2510 3 года назад

      @@SteveGriffith-Prof3ssorSt3v3 Thank you for answering. I'm still confused. Here is a link to the codepen I made: codepen.io/TromboneCode/pen/GRmWXmY
      The only way I can get the background-color of cornflowerblue to not be interrupted by the inherent margin on the top and bottom of paragraphs is if I put that background-color on the element itself. In your example it looks like you're putting the background-color on the elements.
      When I do it the way you have it written, there is an inherent margin of 1em between the top and bottom of each of the backgrounds due to the user-agent stylesheet.

    • @joemcdonough2510
      @joemcdonough2510 3 года назад

      Perhaps the external CSS stylesheet you're linking in has a background-color of cornflowerblue on ? but if that's the case, why would you explicitly add the same background-color to the elements in your internal CSS? Wouldn't that background-color display without needing to add it to the elements? I'm new to CSS so I really appreciate your video and your response.

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  3 года назад +1

      @@joemcdonough2510 I had another attached stylesheet that removed the defaults. Defaults always cause unexpected confusion.
      I tend to use reset stylesheets when building sites because the defaults are different in different browsers.
      I teach my students to zero out all the padding and margin values and then only add them explicitly where they want them. That and to use box-sizing: border-box;
      *,
      *::before,
      *::after{
      box-sizing: border-box;
      padding: 0;
      margin: 0;
      }

  • @ogicacristian4799
    @ogicacristian4799 2 года назад

    Nice, very helpful and very easy to understand! Thank you very much!

  • @soulofsong7518
    @soulofsong7518 3 года назад +2

    you have amazing relaxing sound .

  • @johnniestang
    @johnniestang 5 лет назад

    One question left... If no border, when to use margin versus padding. There seems to be a big debate about the topic. Any pointers or opinion?

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  5 лет назад +2

      They both have a specific purpose. Padding extends the background colour away from the text content. Margin creates space between elements. The difference only disappears if you have no background colour on your element.
      Margins collapse vertically so you should use those to create vertical space between elements.
      Beyond that, the most important thing is to be consistent. Have a standard way that you always use to create your space. And always follow that. Don't randomly pick one of the properties to use on each element.

  • @DSureshKumar
    @DSureshKumar 3 года назад

    Your voice is so brilliant.

  • @OSmith301
    @OSmith301 4 года назад

    great explanation, thank you so much! Makes way more sense now

  • @sarthak1317
    @sarthak1317 4 года назад +7

    2:08 correction
    " 30 pixels "

  • @benzflynn
    @benzflynn 3 года назад

    1. Don't forget defaults and inherits.
    Unless specified otherwise here www.w3schools.com/cssref/css_default_values.asp the default margin and padding will be zero.
    2. Where an element has a default property value, e.g. has a default _padding-left: 40px;_ : here you must override it explicitly, e.g. _nav ul { padding: 0; }_ to establish it otherwise.

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  3 года назад +1

      I do not trust anything from w3schools. They have been found to have erroneous information or be missing key items far too often.
      Yes, elements have default values. The default values are not always the same in all browsers. That is why there have been so many CSS reset and normalize stylesheets created. Many elements will have non-zero default values for padding and/or margin. I will typically add my own reset values for padding and margin to zero them out and then only add the specific values where and when I want them.
      However, this video is only about how margin and padding work as part of the box model not default values or best practices for CSS.
      I have another video on specificity - ruclips.net/video/Kz_S4Nk4qyI/видео.html
      and another about the Cascade where I talk about inheritance - ruclips.net/video/PigxOyVDIQg/видео.html

  • @Maccelerate
    @Maccelerate 6 лет назад

    What IDE do you use? It's very minimalistic.

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  6 лет назад

      Brackets - ruclips.net/video/yblM56mx2MU/видео.html - ruclips.net/video/tTOKcCyWnHE/видео.html

  • @SkeezerMui
    @SkeezerMui 3 года назад

    This tutorial was dope!! Thank you

  • @abdullahamer9282
    @abdullahamer9282 3 года назад

    how can we move a text toward center left .I tried but didnt get it

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  3 года назад

      ruclips.net/video/FMLj-0vGSsI/видео.html
      ruclips.net/video/sLAunIX5RXw/видео.html
      These should help.

  • @deiraraj4547
    @deiraraj4547 3 года назад

    Thanks for posting this.....It was very helpful... : )

  • @jessieboswachter
    @jessieboswachter 4 года назад

    I've always learned that inline CSS Wasn't the way to go. Is there a reason you are using inline CSS?

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  4 года назад +1

      I'm using embedded CSS in most of my tutorials because it keeps everything on the screen at the same time and makes it easier for people to see. Embedded is ok for temporary use and for testing.
      Inline styles are when you use the style attribute inside an HTML element.
      Neither of these things should ever be used in a production setting.

    • @jessieboswachter
      @jessieboswachter 4 года назад +1

      @@SteveGriffith-Prof3ssorSt3v3 thank you for the quick reply Steve. Have a good day

  • @anthony.boyington
    @anthony.boyington 3 года назад

    Thank you so much! nice simple and well explained.

  • @markzait2750
    @markzait2750 2 года назад

    Very useful video. Thank you!

  • @midkort
    @midkort 4 года назад

    Excellent explanation. Thank you!

  • @samirarfaoui8702
    @samirarfaoui8702 4 года назад +3

    dude

  • @itchyaminoacid7251
    @itchyaminoacid7251 4 года назад

    crispy clear. thank you

  • @MobilTemp
    @MobilTemp 4 года назад +1

    Great work

  • @tanvirh7707
    @tanvirh7707 2 года назад

    thank you, sir. that was really helpful

  • @madhumad1397
    @madhumad1397 2 года назад

    great... understood clearly. thanks!

  • @ek5248
    @ek5248 11 месяцев назад

    It is pretty clear. Thanks

  • @thamodaran6998
    @thamodaran6998 4 года назад

    Crisp and clear

  • @ridoanuddin3378
    @ridoanuddin3378 2 года назад

    Thanks a lot. Appreciate it much, Brother.

  • @ersaix0180
    @ersaix0180 2 года назад

    no quetions only RESPECT.
    Thanks a lot

  • @ubahni
    @ubahni 4 года назад

    How do I change the margin/padding on only one element?

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  4 года назад +3

      By targeting that one element with an id, a classname, or something like nth-child(1) or some other selector that identifies it as unique.

  • @patmat.
    @patmat. 4 года назад

    0:14 Paragraphs have margins by default, isn't it the space we see between two paragraphs before you did anything ? But it should be white not blue, margins are transparent... Margins of HTML text containers act weird.

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  4 года назад

      I have zeroed out all the default values in the other CSS value so that I could explain what the padding and margin are without the defaults confusing everything.

  • @ycombinator765
    @ycombinator765 4 года назад

    Awesome bro❤️❤️👍

  • @master-ik9ro
    @master-ik9ro Год назад

    wow i think i understand frontend now haha!!! Thanks!!!

  • @hiroimitsuna7301
    @hiroimitsuna7301 3 года назад

    appreciate your work!

  • @My-video123
    @My-video123 3 года назад

    At last I understood! tnx

  • @nameless7158
    @nameless7158 2 года назад

    whats the name of this coding progrsmme?

  • @aneunjieroine3169
    @aneunjieroine3169 3 года назад

    Thanks Sir it really helpful

  • @salehalnahdy8771
    @salehalnahdy8771 3 года назад

    very much appreciated..thank you for your enlightenment :D

  • @weareeternal3230
    @weareeternal3230 2 года назад

    Thanks! love your voice

  • @vivekhiremath1842
    @vivekhiremath1842 2 года назад

    really helped me a lot

  • @RedBaron19180
    @RedBaron19180 2 года назад

    I actually fucking love you thanks for the video mate

  • @elgunmhrrmov7672
    @elgunmhrrmov7672 Год назад

    thank you so much for details

  • @grounds585
    @grounds585 2 года назад

    got a eloquent voice

  • @IndianGaming0208
    @IndianGaming0208 4 года назад

    Best video 👍👌

  • @leonardmbibi8013
    @leonardmbibi8013 2 года назад

    Great! Aptly put.

  • @kosaygobran
    @kosaygobran 3 года назад

    woooow u made me understand it now

  • @sameerisonvacation
    @sameerisonvacation 3 года назад

    Aww! Awesome 💗 thanks Man

  • @soulofsong7518
    @soulofsong7518 3 года назад

    thank you so much great video

  • @elixr8602
    @elixr8602 4 года назад

    Thanks Steve