Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

General Discussion

Alan Steffens
Alan Steffens
2,558 Points

Any advice using Bootstrap to make an existing website responsive? Adding Bootstrap messes up all existing formatting

After downloading the Bootstrap alpha build a few weeks ago, I decided to use my notes from Guil's Bootstrap 4 Basics class to Bootstrap up a website I have been tasked with making responsive. Based on Guil's Bootstrap classes my site is not rendering anything like it should. I pulled over a section of Guil's Full Stack Conf code and put it in my web dev environment and it isn't rendering the way it did for Guil in the class. So much for class notes!

From the class, one would think that if you cascade your CSS properly with Bootstrap CSS first followed by your site specific CSS file includes, that the addition of <div class="container"> at the top and <div class="row"><class="col-xs-12"> in front of a page section of pre-existing code and following it with the closing col and row </div>s would leave your CSS formatting alone. Not so ... it totally screws all your previous formatting up, way more than just the Bootstrap margin the "container" div adds.

At this point I don't see how the addition of Bootstrap is useful to making an existing website responsive. Bootstrap may have its place when you build a website from scratch with it, but with the format changes Bootstrap's addition makes to pre-existing code, you seem to spend more time reworking all your pre-existing CSS to undo what Bootstrap does to your previous formatting, than it is worth. At this point just adding a few media queries into pre-existing code would seem way more controlled then sucking in Bootstrap and having it completely unravel your existing website formatting.

Any sage words of advice on this from someone who has fought the Bootstrap addition to an existing website's battles?

9 Answers

mathisvester
mathisvester
13,451 Points

Hi Alan, do you have an example? generally Bootstrap is not a Plug-In to make "Sites" responsive. Rather it is a CSS-Framework which provides a Grid-System and different HTML Components which are tested on several Browsers and Devices. If you want to use the advantages of Bootstrap, you should consider to rebuild your Site using the Bootstrap Components. There a lot of great features for example the Variables File of Bootstrap where your are able to customize your styles very quickly. Furthermore you should avoid overwriting when mixing Styles of Bootstrap and an exisiting Site to keep your CSS simple and small.

For adding fluid-widths to a static page it can be faster to code a Grid-System on your own which fits only your "Custom-Side-Needs".

Alan Steffens
Alan Steffens
2,558 Points

Thanks for your response. I understand the concept of Bootstrap and the grid system taking Guil's course on Bootstrap ... several times now:) It is doing some very bizarre stuff to my layout of my current website, as if the Bootstrap course was not teaching the correct use of the col- statements. I made sure to remove all my position CSS and anything else that I thought that Bootstrap would have issues with. I pulled Guil's Full Stack Conf (Bootstrap course) code into my Bootstrap Atom environment and it did not render the same in my environment as it did in his. I find that some of the statements are no longer available in the latest alpha version of Bootstrap and that statements that were still there render different formatting results now. I will dig out some examples from my code and see if I can figure out how to post that in this forum along with before and after bootstrap statement additions screen shots. Stay tuned for that ... will need to do that tomorrow at this point.

One problem I am currently having is that with the Bootstrap row and col- statements in my code, is that in resizing my browser window down from 1200 down to 1199, I get a 100 pixel right margin or padding being applied that I can't explain and inspect does not tell me anything when I click on the element the 100 pixel from the right is being applied to. I have a col-md-6 and col-md-6 on the 2 columns in the code, so any reformatting shouldn't happen until < 768px, not <1200px. Again I will post the code and before and after screen shots tomorrow. I would appreciate any words of wisdom on the right margin/padding being applied if you or anyone else can explain that. It's driving me crazy ...

mathisvester
mathisvester
13,451 Points

The Bootstap Customizer will be removed in Bootstrap 4. Read under Misc.

Dropped the online Customizer in favor of more extensive setup documentation and customized builds.

Hey Alan, it sounds like there is badly written custom CSS code on your website with some dependencies getting broken (in programming languages you would refer to something like this as 'spaghetti code', maybe you've heard of this), since you took care of the correct cascading.

For example, when you have a <section> with an <h1> in it as a direct child and your CSS looks something like this:

section > h1 {...}

and after adding some Bootstrap elements to your HTML you end up with something like

<section> <div class="container"> <h1>Lorem ipsum</h1> </div> </section>

, then your previously written CSS doesn't affect your h1 anymore. Now imagine the chaos when using position- and display-attributes this way.

Also think of "rebuilding" your website with Bootstrap instead of "adding responsiveness via adding Bootstrap".

Alan Steffens
Alan Steffens
2,558 Points

I was a software engineer for many years, so I fully understand spaghetti code. I designed and coded the website in question that I'm trying to make responsive, so the only spaghetti code in there is the side panel collapsible menu button widget I dropped into it from a third party widget shop. I suppose it could be a culprit here, so I'll look further into that. When I "inspect" on the funky formatting being created by Bootstrap, I'm not seeing the elements causing the formatting issues, so it's very frustrating having no way to debug it. I have resorted to your "rebuilding" suggestion, but even there I am getting unexplained Bootstrap formatting anomalies. I'll post some code tomorrow and screen shots tomorrow. Maybe between you and mathisvester someone can figure out what is causing them. Thanks for your response!

Alan Steffens
Alan Steffens
2,558 Points

I can't figure out how to post screen shots. I have seen them in other posts. There is about 60 lines of HTML and an equal amount of CSS to post. A scrren shot of my Atom environment with both viewed at the same time is what I am trying to do. I was also planning to post a browser screen shot of the site before > 1200px and after < 1200px to show the 100px right and left padding/margin that pops in. Any ideas how to post an image here?

Alan Steffens
Alan Steffens
2,558 Points

OK, got it. The first screen shot below is of my website code in my Atom environment with 1 Bootstrap row with 3 columns. Logo column is 2 columns wide (col-md-2, Top Menu and EMS-Tagline graphic column is 8 columns wide, and Menu Button column is 2 columns wide.

! code

The next screenshot is of the website with the proper layout at greater than 1200 pixels. There is a 7px pixel padding on the left for the Logo and a 25px padding on the right for the menu button.

! gt1200px

The last screenshot is of the website with the browser resized to be just less than 1200 pixels. Note the extra 100 pixels added to the right and left, and having it shove the top menu also to the left. I do not understand using the Bootstrap code I have where that is coming from. Google "inspect" doesn't tell me anything on this. Can anyone help me to understand this?

! lt1200px

mathisvester
mathisvester
13,451 Points

Hi Alan,

you got a spacing on the left and right side, because the default Bootstrap .container class contains a width which will be decreased on each breakpoint and margin: 0 auto which centers the container. For example if your viewport width is 1400px and the container width is 1170px you got a spacing of 115px on each side because the container width will not be increased by larger breakpoints than ~1200px. If you don't want to use a containered layout try using the .container-fluid class which doesn't contain a specific width. Remember that your content is more difficult to read if you have sentences going over the whole viewport width on large devices. So a container can be usefull to set a max-width to your content.

Alan Steffens
Alan Steffens
2,558 Points

Thank you! I didn't see any mention of the added spacing in the Bootstrap documentation, but I don't think I read any "container specific" docs. I'll review those today. I can't use container-fluid because I wanted a max-width of 1200px with my page design and the docs said that you can't use max-width with container-fluid. Is there a way to counteract the spacing by the container class? I tried everything I could think of, and suggestions from several posts, even adding !important on local page CSS to set margin and padding to 0 ... nothing worked. I like the idea of Bootstrap, it just seems that you spend a lot of time undoing what they consider to be desirable formatting for certain widths. Any ideas on that?

Alan Steffens
Alan Steffens
2,558 Points

I see from the Bootstrap docs, that I had written down the media query widths wrong, but that is what they taught in the Bootstrap Basics course ... argghh! The breakpoint sizes should be xs < 543px, sm 544-767px, md 768-991px, lg 992-1199px, and xl 1200-up. Now the Bootstrap margin resizing on my code makes sense ... I should have used col-lg- instead of col-md-. I still need to understand how to counteract the Bootstrap margin/padding increase, if anyone could help me with that. Thanks!

mathisvester
mathisvester
13,451 Points

Going further you should take a look at the Source code or Sass Download of Bootstrap. Compiling .less and .sass files on your own gives you the opportunity to have maximum customizing options and smallest file size while only adding the files and modules of bootstrap your project needs.

For example the variables.less contains values as @grid-gutter-width and @container-lg-width which allow you to increase the gutter-width to 40px or your container-width on large screens up to 1300px for example. The padding stored in each column (15px) is half of the default gutter-width (30px) because that is the space between two columns. For more information of the Grid-System read here.

There is a Sass Basics Course here on Treehouse to explore the idea of CSS Preprocessors.

If your are not interested in using the Source code edition of Bootstrap, you have to overwrite the exact CSS properties set by Bootstrap on each Breakpoint to style your layout.

Alan Steffens
Alan Steffens
2,558 Points

Thanks, Mathis! I saw the Sass write-up in the docs. Was trying to make the Bootstrap responsive additions to my website as painless as possible just using the Grid System for my format changes. Now I see it's not as simple as the Bootstrap Basics course makes it out to be. Not into piling another new skill (Sass) to learn in my portfolio at this time and work is pressuring me into showing some results on my responsive redesign. Based on my previous unsuccessful attempts to "inspect" to find the exact Bootstrap CSS causing the left and right breakpoint spacing being added, can you recommend a good way to figure out what Bootstrap CSS I should be counteracting? Thanks again for your help!

mathisvester
mathisvester
13,451 Points

can you recommend a good way to figure out what Bootstrap CSS I should be counteracting?

I'm not sure what you mean. If you want to understand Bootstrap's way of writing styles I recommend reading the Documentation as well as the Source Code of Bootstrap. A direct method would be to inspect the Bootstrap Elements with Firebug or Developer Tools to see which styles are applied.

Alan Steffens
Alan Steffens
2,558 Points

I tried Google-Dev Tools inspection on the menu button and logo (right and left side elements) and could see no padding or margin applied to either, however the spacing got applied when browser width was less than 1200px ... regardless of what my col-xx-nn statements were. It makes no sense to me. As far as reading documentation, when there is so much of it one is never sure where to start. I didn't want to spend weeks figuring Bootstrap out, I was just hoping to take a few courses and play around with it, reading specific documentation to do what I was trying to do. When inspect doesn't work, how do I determine what Bootstrap CSS is being applied to insert the left and right spacing? My plan would then be to counteract their CSS with my own local CSS with the !important tag. Does that make more sense now?