css

For all your CSS related content...
reefersleep 2018-04-06T19:23:18.000248Z

hey guys

reefersleep 2018-04-06T19:24:20.000292Z

I’m doing a little web app to calculate scores for the board game Lost Cities (hopefully) more swiftly than using pen/paper/calculator 🙂

reefersleep 2018-04-06T19:24:57.000051Z

I’ve developed with Chrome and I’m very satisfied with the result in Chrome and Safari. However, things are not so nice in Firefox.

reefersleep 2018-04-06T19:25:29.000024Z

Are there some resources to help figure out differences in browser CSS implementations?

reefersleep 2018-04-06T19:28:15.000203Z

I looked at caniuse for for example svg, since my svgs render very differently in Chrome and Firefox. There were notes on differences, but nothing about the behaviour I saw. Same for vw.

niamu 2018-04-06T19:30:03.000224Z

@reefersleep Did you start with something like normalize.css (http://necolas.github.io/normalize.css/) as a base?

niamu 2018-04-06T19:31:39.000553Z

I can take a look at what you have if it’s open source somewhere. Typically margin and padding differences are the primary source of layout differences.

reefersleep 2018-04-06T19:31:56.000285Z

@niamu I did not!

reefersleep 2018-04-06T19:32:20.000664Z

I have some negative margins going on, but they’re top margins, and well… You better check for yourself. 😄

reefersleep 2018-04-06T19:32:24.000026Z

just a sec

reefersleep 2018-04-06T19:32:58.000080Z

so this is the app: https://eloquent-curie-577f9e.netlify.com/

reefersleep 2018-04-06T19:33:28.000216Z

and this is the source: https://github.com/Reefersleep/fast-cities

reefersleep 2018-04-06T19:34:03.000547Z

The app works like this: use Q to add handshake cards, A to remove, S D F G H K L Æ to toggle the number cards.

reefersleep 2018-04-06T19:34:24.000285Z

(probably that won’t work for you entirely, unfortunately I’ve hardcoded the keycodes for now)

reefersleep 2018-04-06T19:34:47.000389Z

Enter or space to cycle to the next color, backspace to cycle back

reefersleep 2018-04-06T19:34:49.000314Z

that’s all

reefersleep 2018-04-06T19:38:00.000200Z

@niamu in Firefox, once you’ve added cards to all piles, they start moving towards and apart each other horizontally

reefersleep 2018-04-06T19:38:09.000112Z

also, the SVG is rendered, but weirdly.

reefersleep 2018-04-06T19:38:33.000395Z

The circles are not centered, it seems, and the SVG is not square, regardless of its square height/width.

reefersleep 2018-04-06T19:38:38.000275Z

Possibly more is wrong 😄

niamu 2018-04-06T19:39:19.000401Z

ok, so the main issue is that SVG elements don’t support the “vw” measurement. My advice would be to use a a pixel measurement in your SVGs and then use CSS to scale them.

reefersleep 2018-04-06T19:40:44.000178Z

Lemme try that. So I can use vw for height/width, but not for stuff inside the SVG?

niamu 2018-04-06T19:42:56.000315Z

you can’t use it in the SVG markup itself. But you can use it in CSS to scale the SVGs beyond their width/height attributes.

reefersleep 2018-04-06T19:43:54.000032Z

leans in closer, intrigued

niamu 2018-04-06T19:43:58.000272Z

Imagine it like this; “vw” is viewport width. But a viewport only means something in the context of a browser and SVGs are not solely for the browser so they don’t support that level of measurement.

niamu 2018-04-06T19:45:17.000538Z

So you can scale the SVG elements with CSS because they are vector graphics after all.

reefersleep 2018-04-06T19:45:49.000501Z

Ah, I get it. Thanks for the context, I don’t often get that from gleaning random tips via google. So how do I scale the SVG element in CSS, like you say? Use transform or something?

reefersleep 2018-04-06T19:46:11.000209Z

I dig the scalability of vector graphics 🙂

niamu 2018-04-06T19:46:55.000397Z

You could give all of your SVGs a class or an id to select them with CSS.

reefersleep 2018-04-06T19:47:24.000363Z

hmmmm I calculate the vw height/width in my CLJS code, though

reefersleep 2018-04-06T19:49:12.000053Z

not that I like doing that, btw, but the whole point of me using vw for measurement is to be able to retain the height/width aspect ratio for the cards

reefersleep 2018-04-06T19:49:34.000640Z

I guess I could hardcode the vw values, once I feel good about them.

niamu 2018-04-06T19:49:42.000478Z

I see. I’d recommend just swapping “vw” for “px” though and see how close that gets you to cross-browser compatibility.

niamu 2018-04-06T19:50:57.000525Z

Once you get that far, you can apply some width/height rules in CSS for all your SVGs like so: svg { width: 300%, height: 300% } or something like that.

reefersleep 2018-04-06T19:51:19.000513Z

😕 I feel bad about using px in general, because of screen size differences. But then, none of the relative-size CSS options I’ve tried have been very nice or reliable, either.

reefersleep 2018-04-06T19:51:54.000022Z

Yeah, %’s were my saviour re: the above stuff, I thought. Been trying to do %-based design for a long time.

niamu 2018-04-06T19:51:59.000013Z

It’s a bit of a mental leap, but really all you’re doing is drawing the SVG in absolute units so you can scale them later.

reefersleep 2018-04-06T19:52:53.000328Z

It’s not that big a leap to me, except that I’m not very knowledgable about the precedence order of things in CSS.

reefersleep 2018-04-06T19:53:25.000077Z

(when I say CSS, I count all kinds of stylings - inline, in the header, in sheets and possibly other 😄 )

reefersleep 2018-04-06T19:53:44.000282Z

but that’s not so bad, now that you’ve told me how to do 🙂

reefersleep 2018-04-06T19:53:49.000421Z

I can figure out the order.

niamu 2018-04-06T19:53:55.000397Z

yeah, I have an intuition for these things, but it’s hard to explain how I’d solve the problem most of the time or why I’d solve something a particular way.

reefersleep 2018-04-06T19:54:02.000494Z

hehehe

reefersleep 2018-04-06T19:54:17.000415Z

Too much implicit knowledge

niamu 2018-04-06T19:54:59.000522Z

yeah, CSS is really bad for that. Best advice to most people trying to learn it is spend years doing it. Which isn’t really advice at all.

reefersleep 2018-04-06T19:55:22.000507Z

oh btw, I wrote it wrong; I thought %-based design was my saviour for resizable design, but it was not. It’s really hard to get working for all elements.

reefersleep 2018-04-06T19:55:32.000590Z

hahaha

reefersleep 2018-04-06T19:56:30.000520Z

it’s a trite trope, but CSS really is just terrible, yet I can’t help trying to get it to work. I want to put stuff on the web!

niamu 2018-04-06T19:56:31.000182Z

You’re absolutely on the right track using viewport width/height measurements. Just need to get those values in your CSS rather than directly in your SVG markup.

reefersleep 2018-04-06T19:57:02.000680Z

It annoys me that I can’t interact with the stylesheet from within CLJS/JS

reefersleep 2018-04-06T19:57:15.000534Z

I need programming language capabilities

reefersleep 2018-04-06T19:57:24.000613Z

hm

niamu 2018-04-06T19:57:33.000197Z

https://github.com/noprompt/garden helps

reefersleep 2018-04-06T19:57:35.000408Z

maybe there’s one of the CLJS CSS libs that can save me

reefersleep 2018-04-06T19:57:39.000075Z

oh yeah, like that

reefersleep 2018-04-06T19:57:39.000516Z

😄

niamu 2018-04-06T19:59:22.000289Z

With garden you can define all your CSS in Clojure and then it’s up to you how that final CSS string gets injected into your app. So you could have it inject a new <link rel="stylesheet" ...> in your figwheel reload function for when you alter your CSS.

reefersleep 2018-04-06T19:59:57.000428Z

Oh, I was just wondering about how to make it dynamic.

reefersleep 2018-04-06T20:00:00.000374Z

But still

reefersleep 2018-04-06T20:00:23.000115Z

that’ll have static values, and be static during post-compilation runtime, right?

reefersleep 2018-04-06T20:00:25.000085Z

that is

reefersleep 2018-04-06T20:00:58.000447Z

If I reactively resize a specific width for a class selector and compile that with garden, and put it in a <link rel ...

reefersleep 2018-04-06T20:01:09.000316Z

then the changes won’t propagate for the end user

reefersleep 2018-04-06T20:01:27.000160Z

I need to interact runtime, as well as development-time.

reefersleep 2018-04-06T20:01:34.000112Z

googles for libs

niamu 2018-04-06T20:03:07.000092Z

I’m not sure I follow. Are you altering style attributes dynamically at runtime currently?

reefersleep 2018-04-06T20:04:04.000423Z

yes

niamu 2018-04-06T20:04:10.000053Z

I don’t see anything that would cause a problem with having a single static CSS file for your application currently, but I’ve only glanced at your code.

reefersleep 2018-04-06T20:04:42.000581Z

In the resizable-card component, I’m doing a bunch of arithmetic

reefersleep 2018-04-06T20:04:52.000598Z

nothing complex. just aspect ratios 🙂

reefersleep 2018-04-06T20:06:11.000508Z

Actually, I’m not dynamically altering those values currently. They follow the viewport.

reefersleep 2018-04-06T20:06:30.000660Z

I use one vw value and derive all the other values from that one by multiplying or dividing.

reefersleep 2018-04-06T20:07:32.000093Z

But in a devcard project for my development of these components, I was able to easily scale the card elements - while keeping aspect ratios - by multiplying by a runtime-defined number, controlled via a slider.

reefersleep 2018-04-06T20:07:38.000274Z

That was rather nice 🙂

reefersleep 2018-04-06T20:08:04.000307Z

And something I was considering using for animations in the final app. (I guess I could use transforms, too)

niamu 2018-04-06T20:12:08.000246Z

I see what you mean. You could have the slider affect a wrapping div element around the SVG where the SVG is set to be 100% width filling the div completely as it scales.

niamu 2018-04-06T20:12:50.000626Z

That would accomplish the same effect and not have to re-draw your SVGs each time the size changes.

reefersleep 2018-04-06T20:17:08.000434Z

The resizable-card component is just divs (not true, there’s actually an SVG in there sometimes, but that isn’t a problem)

reefersleep 2018-04-06T20:19:30.000478Z

I just want the arithmetic derived from a dynamic number. That trivial in any programming language, and just not possible in CSS, from what I can tell.

reefersleep 2018-04-06T20:20:18.000067Z

I need the arithmetic to keep aspect ratios regardless of viewport size. The “top” number being dynamic is just a nicety, but one that is so easily accessible from within CLJS.

reefersleep 2018-04-06T20:20:27.000476Z

(And with lovely applications)

reefersleep 2018-04-06T20:21:42.000031Z

I realize that if I gave up top number dynamism, I could just have hard-coded values for everything.

reefersleep 2018-04-06T20:23:00.000318Z

Apart from that being less nice, I just really dislike it… 😄 If I need to change 1 thing somewhere, I need to change a bunch of stuff again, by hand.

niamu 2018-04-06T20:23:29.000537Z

I see what you mean. As long as you’re using percentage values in the children elements, you can have the parents have their width/height attributes be dynamically set on the element themselves and therefore that keeps all of the dynamic sizing features you want while keeping all of the calculations as static percentages in your CSS rather than calculating on each render as you are currently.

reefersleep 2018-04-06T20:23:33.000386Z

Whereas dynamically deriving it is well, what computers are made for 🙂

niamu 2018-04-06T20:24:42.000587Z

But what I’m describing isn’t really necessary. You can still do what you’re doing with no issues as long as you get the “vw” measurements out of your SVGs.

reefersleep 2018-04-06T20:25:07.000293Z

I have realized a couple of times that I’m essentially calculating percentages 😉

reefersleep 2018-04-06T20:25:18.000318Z

ok, let me frame this differently

reefersleep 2018-04-06T20:25:40.000562Z

if I want the height to be width * 1.354, can I do that in CSS?

niamu 2018-04-06T20:27:38.000297Z

No, you’re right that having the aspect ratio fixed but the sizing dynamic would involve having the width/height attributes embedded on the element itself and not in an external stylesheet.

reefersleep 2018-04-06T20:28:21.000512Z

Alrighty.

reefersleep 2018-04-06T20:28:28.000483Z

Thanks for all of your advice 🙂

niamu 2018-04-06T20:29:17.000268Z

Glad I could help.

niamu 2018-04-06T20:29:40.000253Z

Looks like a neat project. I haven’t played Lost Cities yet, but I’ve thought about picking it up a few times before.

reefersleep 2018-04-06T20:32:12.000261Z

I think I have my answer(s); how to render SVGs properly (px, then rescale with CSS, or use percentages, then rescale with css), how to size children dynamically (percentages, of course : ) and finally, aspect ratios; decide on hardcoded values.

reefersleep 2018-04-06T20:33:01.000552Z

Thanks! @niamu thanks a lot for your time 😄

reefersleep 2018-04-06T20:33:08.000353Z

Lost Cities is great btw!

niamu 2018-04-06T20:33:14.000269Z

You’re welcome. 🙂

reefersleep 2018-04-06T20:33:23.000462Z

🙂

reefersleep 2018-04-06T20:33:58.000349Z

It’s cheap, fast to play, very portable. Two drawbacks; the big cards are a bit hard to shuffle, and the calculation in the end can be a bit tedious 😉

reefersleep 2018-04-06T21:29:02.000112Z

BTW @niamu : did you witness the stacks sliding back and forth horizontally when cards have been added to all stacks, and you're adding and removing cards from a pile? Or maybe you addressed that when you talked about the SVG stuff?

niamu 2018-04-06T21:30:41.000122Z

I honestly didn’t understand the interface enough to reproduce that. If the SVG adjustments don’t fix that problem, let me know and I can look at it again.

reefersleep 2018-04-08T14:57:13.000097Z

In case you’re curious; I’ve updated the interface so that it works with the mouse as well. That makes it much easier to use, I think. Take a look if you feel like it: https://eloquent-curie-577f9e.netlify.com/

reefersleep 2018-04-08T14:58:30.000065Z

Thanks a lot for spending time explaining stuff to me. It’s not the first time I’ve butted my head against CSS after developing a perfectly working app in one browser, then looking at it in another. I feel like I’m slowly starting to come to grips with a lot of the idiosyncrasies of CSS 🙂

reefersleep 2018-04-06T21:34:30.000167Z

Type a s d f g h j, then enter, repeat until you've added cards to all five stacks. Now use some of the same keys to toggle and untoggle cards for one stack. Stacks will slide around like Bambi on ice.

reefersleep 2018-04-06T21:36:02.000385Z

It's a more serious problem than the SVG thing. I can do without the fanci SVG drawing, the card stacks are essential.

reefersleep 2018-04-06T21:36:34.000030Z

I can try ripping out the SVG elements entirely and see if the sliding behavior persists.

reefersleep 2018-04-06T21:37:31.000284Z

Tomorrow 😊 it's nighttime here. Have a nice evening!

reefersleep 2018-04-06T21:41:07.000219Z

Couldn’t help giving it a little go before bedtime; without the SVG elements above the stacks, they behave nicely. You hit the nail on the head!

reefersleep 2018-04-06T21:41:28.000362Z

That’s great, I bet I could have scratched my head for long about that!