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

CSS

my grid gaps keep moving i'd like to have them in a fixed state but also flexible when resizing the viewport.

https://w.trhou.se/v37ebfq99h

I am creating html, CSS, & JavaScript to learn from as instructed, however no programmer is an island and I'm well and truly stuck on the bottom three grids, especially when I hover on the text that becomes bold which is ok but the grid gap moves, I did have it in a fixed state but the entire grid layout then refused to shrink when narrowing the viewport, I feel this may be common, but I need help please. (too many hours lost to this)

Also I feel I'm repeating myself on line 53 onwards in my JavaScript file.

Would appreciate someone having a look if you have the time.

Many thanks, Jason Happy coding.

8 Answers

Rohald van Merode
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Rohald van Merode
Treehouse Staff

Hi Jason 👋

Cool practice project you got going on here 😄 The grid resizing has to do with the fact that the size of the content growing when the text becomes bold. You can stop the divs from expanding by adjusting the grid-template-columns property on line 69. Currently it's set up to be auto which shares the left over space between the grid items. I'd suggest to change this to 1fr 1fr 1fr. This way your .grid-item elements will always take the same amount of space.

If you want to change the layout of the grid on smaller viewports you can add the same property to your media query and choose to only have one column for example 🙂

As for the JavaScript you could make it a bit less repetitive by giving all those elements a class to then loop over them to add an eventListener to each of them. This way you'd only write the event Listeners only once instead of 3 times.

Hope this helps to get you going again 🙂

Thanks for your previous response, however I'm quite tangled up and I'm not sure how to implement what you said in my JavaScript file, my attempt is below:-

let xclass = document.getElementsByClassName("carData1");
for(let i = 0; i > xclass.length; i++) {
addEventListener("mouseover", () => {
xclass.style.fontWeight = "bold";
});
}

for (let [key, value] of Object.entries(data1)) { xclass.innerHTML += (<br> ${key}: ${value}); }

Rohald van Merode
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rohald van Merode
Treehouse Staff

Hi jas0n

It's hard to tell without having a look at your complete code so I had a look workspace snapshot you send in your previous response. Given the selector you're using for xclass I assume you've added that classname to each of your labels?

      <div class="grid-container2">
        <div class="grid-item3">
          <label id="carData1" class="carData1"></label><br>
        </div>
        <div class="grid-item4">
          <label id="carData2" class="carData1"></label><br>
        </div>
        <div class="grid-item5">
          <label id="carData3" class="carData1"></label><br>
        </div>
      </div>

If so for the JavaScript you're definitely on the right track. Currently your loop won't be running because the condition i > xclass.length is not true from the start. You'll want to change that to i < xclass.length.

Then inside your loop you'll want to attach the eventListener to each of those elements you're looping over. Also you'll want to make sure to select the right element by using the i as an index.

Your code should then end up something like this:

const xclass = document.getElementsByClassName("carData1");

for(let i=0; i < xclass.length; i++) {
    xclass[i].addEventListener("mouseover", () => {
        xclass[i].style.fontWeight = "bold";
    });
}

Just a few notes though:

  • xclass is not very descriptive. You'll want to name your variables as descriptive as can be for your coworkers and your future self. When coming back to this code in a couple of weeks you'll have to dive into your code to find out what xclass is actually selecting.

  • It's nice for practicing your JS and event listeners with code like this but for these kind of style changes a :hover state in the css would be better for performance.

I hope this has been helpful 😄

Wow thanks. I forgot to submit [i] into both addeventlistener and styles.. yes I do suffer with describing variables :-(

Thanks for your help I did try books and the course helps but nothing like making your own mistakes and actually writing lots of code, I find it makes it stick better in memory. :-)

Oh you've put a class along next to an id? I didn't know i could do that? :-)

This is what I put so far:-

const xclass = document.getElementsByClassName("carData1");

for(let i=0; i < xclass.length; i++) {
xclass[i].addEventListener("mouseover", () => {
xclass[i].style.fontWeight = "bold";
});
}

for(let i=0; i < xclass.length; i++) {
xclass[i].addEventListener("mouseout", () => {
xclass[i].style.fontWeight = "normal";
});
}
Saves me 18 lines of code (shockin)... but cool
On terms of difficulty its a bit hard for me but I'm understanding I think lol, you've been a great help thank you. Just one more question if I may, for me the above I need to keep somewhere may I ask what kind of system would be useful for keeping code snipets would notepad suffice? How do programmers keep track of useful info?

Thanks again.

Rohald van Merode
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree seal-36
Rohald van Merode
Treehouse Staff

Nice one jas0n! You could even reduce it by a couple more lines. Currently you're looping over the same elements twice. You could add both event listeners in the same loop 🙂

As for how to store your snippets there are a whole lot of different tools and it's really a matter of personal preference. I found this article that goes over a couple of popular snippet managers.

I dont see how that's possible? because I need to mouseover and mouseout and change from bold to normal these are two completely separate actions, aren't they?

oh oh oh...
for(let i=0; i < getclass.length; i++) {
getclass[i].addEventListener("mouseover", () => {
getclass[i].style.fontWeight = "bold";
});
getclass[i].addEventListener("mouseout", () => {
getclass[i].style.fontWeight = "normal";
}); }

Yes your right, wow exciting stuff lol

let data1 = {
manufacturer: "Focus",
power: "123 bhp",
color: "Blue",
year: 2013,
FuelType: "petrol"
};

let data2 = {
manufacturer: "Mini",
power: "125 bhp",
color: "Silver",
year: 2018,
FuelType: "petrol"
};

let data3 = {
manufacturer: "Focus",
power: "123 bhp",
color: "Red",
year: 2009,
FuelType: "diesel"
};
for (let [key, value] of Object.entries(data1)) { document.getElementById("carData1").innerHTML += (<br> ${key}: ${value}); }

for (let [key, value] of Object.entries(data2)) { document.getElementById("carData2").innerHTML += (<br> ${key}: ${value}); }

for (let [key, value] of Object.entries(data3)) { document.getElementById("carData3").innerHTML += (<br> ${key}: ${value}); }

I've tried so many times to reduce the above code (not the objects though) but i think it cant be done as each has a different name (I'm not very good at this yet), still haven't found a way to loop through these.
I did use classes and got the class name carData1 but didint know what to do for data1 data2 data3

Rohald van Merode
seal-mask
.a{fill-rule:evenodd;}techdegree
Rohald van Merode
Treehouse Staff

Hi jas0n,

You could create an array that holds all the objects, then loop over the array to do the same thing with each of the objects. I'd suggest to check out this challenge of the JavaScript Objects course that is very similar to what you're doing with this piece of code. In the videos following up the challenge Guil shows his solution 😄

Hope this helps 🙂

Yes I should research more, I will look at that challenge later, sorry to take up more than enough of your time, but thank you so much its been such a great help and has led me to certain ideas and direction that I'm going to study.

Be safe and happy coding. Jason.