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

Curiosity...

I apologize in advance if this question presents an excessive amount of naïveté... I am re-visiting some basics, and while working with some JavaScript functions, I got to thinking about something...

In the main style-sheet for our website, there are some max-width classes, used for images. The only problem, though, is that, the class definitions are all pretty much the same, with exception to the max-width property for each one. The code is as follows:

/* Added some "Max Width" classes for some items that have
the width set to 100%, but can get too big on some devices */

.max-103 {
    width: 100%;
    height: auto; 
    max-width: 103px;   /* This one is a bit of a special case, on the Media Relations / Publications page */
}

.max-150 {
    width: 100%;
    height: auto; 
    max-width: 150px;
}

.max-175 {
    width: 100%;
    height: auto; 
    max-width: 175px;
}

.max-200 {
    width: 100%;
    height: auto; 
    max-width: 200px;
}

.max-250 {
    width: 100%;
    height: auto; 
    max-width: 250px;
}

.max-300 {
    width: 100%;
    height: auto; 
    max-width: 300px;
}


.max-320 {
    width: 100%;
    height: auto; 
    max-width: 320px;
}

.max-325 {
    width: 100%;
    height: auto; 
    max-width: 325px;
}

.max-350 {
    width: 100%;
    height: auto; 
    max-width: 350px;
}

.max-400 {
    width: 100%;
    height: auto; 
    max-width: 400px;
}

.max-425 {
    width: 100%;
    height: auto; 
    max-width: 425px;
}

.max-450 {
    width: 100%;
    height: auto; 
    max-width: 450px;
}

.max-500 {
    width: 100%;
    height: auto; 
    max-width: 500px;
}


/* End Max-Width Classes */

Basically, in order to "DRY things up", I was wondering if there would be some way to use jQuery or JavaScript to apply the correct max-width to an item depending on which "max-###" class was assigned to it in the HTML. Something like...

  1. Get the class name of an object (for the sake of example, store it in a variable called className).
  2. Continue if className.substring(0,3) == "max"
  3. Apply appropriate styling

It sounds doable, but when I go to actually write the code, that's when the gears lock up ^^; At the same time, I'm also thinking that, by nature, it might not be possible at all; maybe something having to do with the separation of client-side/server-side or something...

If it's implausible, then no worries. I just thought I would ask either way :)

Any insight, though, would be appreciated. Always good to hear from those who are more experienced than me (I might not fully comprehend the information shared by said more-experienced-person at the moment, but it's nice to have a reference-point to revisit once I get a bit closer to understanding it.) :)

Thanks!

1 Answer

Kevin Korte
Kevin Korte
28,149 Points

I would think that might be slow, I'm not sure.

When I saw this, I first though Sass (or Less, Stylus, etc) I'm familiar with sass so I might write it like

@each $width in 103, 150, 175, 200, 250, 300, 320, 325, 350, 400, 450, 500 {
  .max-#{$width} {
    width: 100%;
    height: auto;
    max-width: #{$width}px;
  }
}

Now, when that compiles, it'll make a css file that has that grid, but I didn't have to write anything more than once. It's easy to add more widths, take them away, or adjust styling to all elements in that grid.

That's how I'd do it.

That's possible, but I was wondering if there was some way (and if it would be a good idea or not) to use JS or jQuery to apply the styles...

The more I think about it though, the more I think that the only way to really do it would be to use jQuery's something.css() or attribute.val() to change the CSS class being applied to the element, but that would still mean that it would need to be defined in the CSS.

Just the same, it's not that big of a deal, really, having those multiple classes defined in the CSS. I was just curios as to whether or not it could be "robo-coded" :D