36. Playing with the title's position and negative margins - Responsive CSS Tutorial

Scrimba · Beginner ·🌐 Frontend Engineering ·6y ago

Key Takeaways

This video tutorial explores the use of negative margins in CSS to create a responsive layout, specifically for positioning titles and handling overlapping content. The instructor demonstrates how to use media queries to apply negative margins and achieve a desired layout, while also discussing potential issues and best practices.

Full Transcript

consequence and note so there was one unintended consequence didn't notice before which was we got rid of the spacing on our titles and that got this stuck on this we'll fix this in this video as well but the main focus will be here like I mentioned getting it to pop on over to this side it certain sizes the way I'm going to do it now the reason I'm saying it's not a best practice video as much as a exploration video is because if you had a really long title this would cause some problems there's the potential for overlapping but I just want to show you something that we haven't looked at yet it's something that I try and avoid when I can but I want to show it to you anyway and I think it is fun for the layout purposes on this one and something is worth exploring because while you try and avoid it it's good to know that it is a possibility and that its negative margins so I move all the way down to my layout area we're now all the way down till we find my articles there we go and I'm going to create a new media query because we're going to be dealing with these two pretty much so at media as far as the size of where do I create this media query for don't create media queries based on the size of certain devices or set devices because then you can always do things that you want to do or those devices you know people will make too many media queries because they're trying to match certain devices when they don't need one or in this case I'm trying to really base it on my layout I'm sort of just guessing it the size we're going to tweak it a little bit as we go so I'm going to do 500 pixels for now and let's see if you see how it goes from there so inside my media query what I want to do is select my article recent main and I'm gonna give this a margin top of negative I don't know that's art with negative 1m and to see how that affects things so you can see this sort of gotten a little close together let's just try changing that maybe two negative two you can start seeing these are starting to overlap and we can bring that maybe up to a whole negative three and they're starting to line up how we want them to I'm going to switch that to a 2.5 because I think it's gonna be bang on how I want the top of the text is sort of lining up now as I mentioned this can create text overlap so it's probably not a best practice overall here yeah so this is my longest one so I'm going to base it on this title but what would happen if you have a really long title it would break this whole layout so I'm gonna show you how we can do it with a negative margin so you know if I bring this up really big you can see it's pulling it up on top of other things and it's one of the problems with negative margins but it is important to know you can have a negative margin because sometimes it does come into use and it is useful to have so that is perfect now the other thing I'm going to do in this media query is do my article info and so there's a text aligned to the right and there we go we have the layout that I wanted where we have those going like that now the problem is where's the media query Canyon where is 550 pixels so we're gonna shrink and it's really really close but on this one it's looking perfect so you know it's one of those ones where you have to judge it by eye a little bit I'm gonna bump it up to 550 just because I don't want those to get so close to each other so it gives us this small little range where we have two columns and then where it pops over back to the big layout where we're gonna have to change what that looks like anyway so that is how we can go and do something like that with a little bit of hackery to give ourself this fake two column look based on the way our markup is but again make sure you keep this really really simple because it's keeping cooking simple but if this was how to keep your cooking simple all of a sudden I'm a little worried we have overlap and it's causing problems so while negative margins can be fun for creating cool layouts or cool effects sometimes and sometimes you want overlapping content it's always possible you want two things to sort of overlap and there's other ways of doing it with positioning as well which we're going to explore later but just when you use the negative margin be very very aware that this is a possibility so I think what I'm going to do is while we've explored it while we've seen it you want to sort of rebuild this on your own at one point and keep it in your code because I do think it looks really nice with how I've set this up it's just not something we can do in a responsible way so we've looked at how to do it but now we're just going to take it all off because I think it's going to cause more problems in the long run and it's really important that you keep your code as functional as possible and that it can accommodate for all these situations so if you're ever putting together a demo site make sure you don't only use short text it's real like if you're doing a business card template don't is a really short name Kevin Powell is not a good name to use you want to get a really really long name on the template to make sure that it can work with super long names and it's not running into issues so the same thing here we you want to test short titles and long titles to make sure everything will work with your code

Original Description

This tutorial is a part of "The Responsive Web Design Bootcamp" on Scrimba. Explore the full course here: https://rebrand.ly/responsivebootcamp_yt We can use negative margins for some pretty creative stuff, and in the right situation it might be the right thing to do, but we have to be really careful with them and make sure that it doesn't cause any problems.
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Scrimba · Scrimba · 15 of 60

1 CSS Grid Course: Learn the Basics in 3 Minutes
CSS Grid Course: Learn the Basics in 3 Minutes
Scrimba
2 CSS Grid Course: Positioning Items
CSS Grid Course: Positioning Items
Scrimba
3 CSS Grid Course: Why Learn It And How It Compares To Bootstrap
CSS Grid Course: Why Learn It And How It Compares To Bootstrap
Scrimba
4 CSS Grid Course: auto-fit & minmax
CSS Grid Course: auto-fit & minmax
Scrimba
5 CSS Grid Course: Implicit Rows
CSS Grid Course: Implicit Rows
Scrimba
6 CSS Grid Course: Fraction Units And Repeat
CSS Grid Course: Fraction Units And Repeat
Scrimba
7 CSS Grid Course: Justify Items and Align Items
CSS Grid Course: Justify Items and Align Items
Scrimba
8 CSS Grid Course: An Awesome Image Grid
CSS Grid Course: An Awesome Image Grid
Scrimba
9 CSS Grid Course: Named Lines
CSS Grid Course: Named Lines
Scrimba
10 CSS Grid Course: auto-fit vs auto-fill
CSS Grid Course: auto-fit vs auto-fill
Scrimba
11 CSS Grid Course: Justify Content and Align Content
CSS Grid Course: Justify Content and Align Content
Scrimba
12 CSS Grid Course: Template areas
CSS Grid Course: Template areas
Scrimba
13 27. Setting up the structure - Responsive CSS Tutorial
27. Setting up the structure - Responsive CSS Tutorial
Scrimba
14 25. Making the navigation responsive - Responsive CSS Tutorial
25. Making the navigation responsive - Responsive CSS Tutorial
Scrimba
36. Playing with the title's position and negative margins - Responsive CSS Tutorial
36. Playing with the title's position and negative margins - Responsive CSS Tutorial
Scrimba
16 31. Starting the CSS for our page - Responsive CSS Tutorial
31. Starting the CSS for our page - Responsive CSS Tutorial
Scrimba
17 26. Taking a look at the rest of the project - Responsive CSS Tutorial
26. Taking a look at the rest of the project - Responsive CSS Tutorial
Scrimba
18 15. Spacing out the columns - Responsive CSS Tutorial
15. Spacing out the columns - Responsive CSS Tutorial
Scrimba
19 33. Starting to think mobile first - Responsive CSS Tutorial
33. Starting to think mobile first - Responsive CSS Tutorial
Scrimba
20 22. Making our navigation look good - Responsive CSS Tutorial
22. Making our navigation look good - Responsive CSS Tutorial
Scrimba
21 37. Changing image size with object-fit - Responsive CSS Tutorial
37. Changing image size with object-fit - Responsive CSS Tutorial
Scrimba
22 44. Module Wrap up - Responsive CSS Tutorial
44. Module Wrap up - Responsive CSS Tutorial
Scrimba
23 16. Controlling the vertical position of flex items - Responsive CSS Tutorial
16. Controlling the vertical position of flex items - Responsive CSS Tutorial
Scrimba
24 39. Setting up the widgets and talking breakpoints - Responsive CSS Tutorial
39. Setting up the widgets and talking breakpoints - Responsive CSS Tutorial
Scrimba
25 42. Setting up the About Me page - Responsive CSS Tutorial
42. Setting up the About Me page - Responsive CSS Tutorial
Scrimba
26 35. Changing the visual order with flexbox - Responsive CSS Tutorial
35. Changing the visual order with flexbox - Responsive CSS Tutorial
Scrimba
27 23. Adding the underline - Responsive CSS Tutorial
23. Adding the underline - Responsive CSS Tutorial
Scrimba
28 21. Using flexbox to start styling our navigation - Responsive CSS Tutorial
21. Using flexbox to start styling our navigation - Responsive CSS Tutorial
Scrimba
29 20. Creating a navigation - Responsive CSS Tutorial
20. Creating a navigation - Responsive CSS Tutorial
Scrimba
30 40. Using a new pseudo class to wrap up the homepage - Responsive CSS Tutorial
40. Using a new pseudo class to wrap up the homepage - Responsive CSS Tutorial
Scrimba
31 43. Fixing up some loose ends - Responsive CSS Tutorial
43. Fixing up some loose ends - Responsive CSS Tutorial
Scrimba
32 32. Starting the layout. Looking at the big picture - Responsive CSS Tutorial
32. Starting the layout. Looking at the big picture - Responsive CSS Tutorial
Scrimba
33 24. A more complicated navigation - Responsive CSS Tutorial
24. A more complicated navigation - Responsive CSS Tutorial
Scrimba
34 28. Feature article structure - Responsive CSS Tutorial
28. Feature article structure - Responsive CSS Tutorial
Scrimba
35 34. Styling the featured article - Responsive CSS Tutorial
34. Styling the featured article - Responsive CSS Tutorial
Scrimba
36 18. Making layout responsive with flex direction - Responsive CSS Tutorial
18. Making layout responsive with flex direction - Responsive CSS Tutorial
Scrimba
37 19. flex direction explained - Responsive CSS Tutorial
19. flex direction explained - Responsive CSS Tutorial
Scrimba
38 41. Creating the recent posts page - Responsive CSS Tutorial
41. Creating the recent posts page - Responsive CSS Tutorial
Scrimba
39 17. Media Query basics - Responsive CSS Tutorial
17. Media Query basics - Responsive CSS Tutorial
Scrimba
40 30. Home Page. HTML for the aside - Responsive CSS Tutorial
30. Home Page. HTML for the aside - Responsive CSS Tutorial
Scrimba
41 38. Styling recent articles for large screens - Responsive CSS Tutorial
38. Styling recent articles for large screens - Responsive CSS Tutorial
Scrimba
42 29. The home page.  HTML for the recent articles - Responsive CSS Tutorial
29. The home page. HTML for the recent articles - Responsive CSS Tutorial
Scrimba
43 10. ems and rems   an example - Responsive CSS Tutorial
10. ems and rems an example - Responsive CSS Tutorial
Scrimba
44 1. Starting to think responsively - Responsive CSS Tutorial
1. Starting to think responsively - Responsive CSS Tutorial
Scrimba
45 4. Controlling the width of images - Responsive CSS Tutorial
4. Controlling the width of images - Responsive CSS Tutorial
Scrimba
46 5. min width and max width - Responsive CSS Tutorial
5. min width and max width - Responsive CSS Tutorial
Scrimba
47 3  CSS Units.  Percentage - Responsive CSS Tutorial
3 CSS Units. Percentage - Responsive CSS Tutorial
Scrimba
48 11. Flexbox  refresher and setting up some HTML - Responsive CSS Tutorial
11. Flexbox refresher and setting up some HTML - Responsive CSS Tutorial
Scrimba
49 12. Basic Styles and setting up the columns - Responsive CSS Tutorial
12. Basic Styles and setting up the columns - Responsive CSS Tutorial
Scrimba
50 8. The Solution Rems - Responsive CSS Tutorial
8. The Solution Rems - Responsive CSS Tutorial
Scrimba
51 14. Setting the columns widths - Responsive CSS Tutorial
14. Setting the columns widths - Responsive CSS Tutorial
Scrimba
52 2  CSS Units - Responsive CSS Tutorial
2 CSS Units - Responsive CSS Tutorial
Scrimba
53 7. The problem with ems - Responsive CSS Tutorial
7. The problem with ems - Responsive CSS Tutorial
Scrimba
54 6. CSS Units. The em unit - Responsive CSS Tutorial
6. CSS Units. The em unit - Responsive CSS Tutorial
Scrimba
55 13. Adding the background color - Responsive CSS Tutorial
13. Adding the background color - Responsive CSS Tutorial
Scrimba
56 9. Picking which unit to use - Responsive CSS Tutorial
9. Picking which unit to use - Responsive CSS Tutorial
Scrimba
57 Tutorial to Learn Alpine JS - Full Course for Beginners
Tutorial to Learn Alpine JS - Full Course for Beginners
Scrimba
58 Guide To Algorithms in Javascript [Binary Search] - Full Course / Tutorial
Guide To Algorithms in Javascript [Binary Search] - Full Course / Tutorial
Scrimba
59 Learn UI Design [7 Fundamentals Tutorial] - Full Course for Beginners
Learn UI Design [7 Fundamentals Tutorial] - Full Course for Beginners
Scrimba
60 Javascript Tutorial for Beginners [From 0 to ES6+] - Full Course
Javascript Tutorial for Beginners [From 0 to ES6+] - Full Course
Scrimba

This video tutorial shows how to use negative margins in CSS to create a responsive layout, handling title positioning and overlapping content. The instructor demonstrates how to apply negative margins using media queries and discusses potential issues and best practices.

Key Takeaways
  1. Create a new media query to handle different screen sizes
  2. Apply a negative margin to the title element to position it correctly
  3. Adjust the negative margin value to achieve the desired layout
  4. Test the layout with different title lengths to ensure it works as expected
💡 Negative margins can be used to create a responsive layout, but it's essential to test the layout with different content lengths to ensure it works as expected.

Related Reads

📰
Stop installing giant icon packages — write SVGs as source with a CLI + MCP
Learn to create SVG icons from scratch using a CLI and MCP, reducing dependency on giant icon packages
Dev.to · arialus
📰
Frontend Engineers Are Quietly Becoming the New Overhead.
Frontend engineers are being bypassed in the development process, with features being shipped without their involvement, highlighting a potential shift in their role
Medium · AI
📰
Building an Interactive Fashion Hero with HTML, CSS & GSAP
Learn to build an interactive fashion hero using HTML, CSS, and GSAP, and discover how to create immersive digital experiences
Dev.to AI
📰
Out-of-Order HTML Streaming Moves from JS Frameworks into the Browser
Out-of-order HTML streaming is moving from JS frameworks to browsers, enabling faster page loading and interaction
InfoQ AI/ML
Up next
Sports on the Go: Africa makes history at 2026 FIFA World Cup
CGTN Africa
Watch →