November 27, 201510 yr Following the General JS thread, why not start a general CSS topic, where we can cover everything recent and possibly answer questions? I will start with a question about the use of id selectors in CSS. I was just looking at Harry Robert's Nudge https://github.com/csswizardry/nudge and noticed the following: No IDs We shouldn’t use IDs in CSS, so display a warning any time we find one. Is that becoming a standard now (no id selectors at all) and in which cases do you think we should use ids in CSS? Or does that relate to large scale UIs and not smaller projects?
November 27, 201510 yr Following the General JS thread, why not start a general CSS topic, where we can cover everything recent and possibly answer questions? I will start with a question about the use of id selectors in CSS. I was just looking at Harry Robert's Nudge https://github.com/csswizardry/nudge and noticed the following: No IDs We shouldn’t use IDs in CSS, so display a warning any time we find one. Is that becoming a standard now (no id selectors at all) and in which cases do you think we should use ids in CSS? Or does that relate to large scale UIs and not smaller projects? I use ID's for JS only, no exceptions. There's no use case where an ID is more appropriate in CSS, it raises the specificity, and prevents any kind of re-use.
November 27, 201510 yr Agree with Jack, a class can do everything an ID can do and with increased specificity you have a higher probability of specificity issues down the line. I think this quote from Crockford is true in so many areas of software engineering and especially so here: “If a feature is sometimes dangerous, and there is a better option, then always use the better option.” So no IDs. Ever. I even use them rarely as JS hooks because we should be thinking in terms of components and using IDs means we cannot have duplicates. Sometimes we know for definite this will be the case so an ID is safe to use, but always bear that in mind, if there's any doubt use a class hook. Class hooks should always be prefixed with `.js-` and no styling should ever be attached to that class, it's a single responsibility class and that responsibility is to be a javascript hook. Having classes that describe what a component does rather than what it is is something to bear in mind when creating components. If there's some situation when you absolutely must style something via an ID - perhaps you don't have direct access to the HTML then style it using the attribute selector. This way you avoid specificity issues. [id="myID"] { [yourstyles] } Edited November 27, 201510 yr by rbrtsmith
November 28, 201510 yr I'm on the fence here. I've been using CSS since it first became available in browsers. Back then the rule was ID's were perfectly normal to use if they were only used once per document, which was logical, and still the case today. I still use ID's for styling, mainly for top level layout where I know they will ever only be used once in any document (and of course JS hooks). If you write CSS correctly and logically there should be no specificity issues at all. As for people saying you shouldn't use them at all for styling, ever, then I will disagree...
November 28, 201510 yr Then you are breaking the single responsibility principle. IDs are often used as JS hooks, if you then or other devs then attach styling to them you have no separation of concerns. It's not just about specificity. The is fact a class can do everything an ID can in terms of styling there simply is no good argument for their usage. We shouldn't use bad practices just because we used to do it that way in the dark ages, back then the web was a huge mess of confusion. The top CSS devs in our industry are all in agreement that IDs should be avoided in CSS. Nicole Sullivan, Harry Roberts to name two. And to give an indication of the size of clients they advise: Google, Facebook, BBC... safe to say they know what they're talking about. http://csswizardry.com/2012/11/code-smells-in-css/#ids Edited November 28, 201510 yr by rbrtsmith
December 11, 201510 yr I have a question for those of you who uses CSS preprocessors: What are your views on the level of abstraction for variable names especially for colours? Variable names like "$dark-red" or "$silver" does not seem to be a good idea, If you later have to change colours in a theme. Names like "$main-bg-color" also feels wrong if you were to use this variable as a foreground- or border-colour somewhere. On the other hand abstract names like "$primary2" might be hard to remember compared to say "$light-blue". Edited December 11, 201510 yr by Nillervision
December 11, 201510 yr I have a question for those of you who uses CSS preprocessors: What are your views on the level of abstraction for variable names especially for colours? Variable names like "$dark-red" or "$silver" does not seem to be a good idea, If you later have to change colours in a theme. Names like "$main-bg-color" also feels wrong if you were to use this variable as a foreground- or border-colour somewhere. On the other hand abstract names like "$primary2" might be hard to remember compared to say "$light-blue". I use the greek alphabet plus brand color + a comment $brand-color: #XXXXXX /* Red */ $alpha-color: #XXXXXX /* Green */ $beta-color: #XXXXXX /* Blue */ $gamma-color: #XXXXXX /* Black */ $delta-color: #XXXXXX /* Medium gray */ $epsilon-color: #XXXXXX /* light gray */ $zeta-color: #XXXXXX /* light yellow */ $base-text-color: $gamma-color; $base-border-color: $delta-color; $base-link-color: $brand-color; $site-background: $epsilon-color; The vars underneath just tie the global colours to specifics, I add more of these where necessary. This means I don't have to use $site-background color if I want a matching color elsewhere, I can just use $epsilon-color. The comment just tells me what the color is at a glance. Edited December 11, 201510 yr by rbrtsmith
December 11, 201510 yr For anybody that's interested in how I set up my global variables this is how my _vars file looks like on my boileplate framework. /*------------------------------------*\ SETTINGS -> VARIABLES \*------------------------------------*/ /*------------------------------------*\ $COLORS \*------------------------------------*/ /** * Theme specific coloring, in general order of precedence. */ $brand-color: #f43338; $alpha-color: #bbb; $beta-color: #000; $gamma-color: #00f; $delta-color: #fff; $epsilon-color: #fff; $zeta-color: #fff; /** * Set base colors for common objects. */ $site-background-color: #FFF; $base-text-color: #111; $base-link-color: $brand-color; $base-border-color: #ddd; /*------------------------------------*\ $FONTS \*------------------------------------*/ /** * 1. Font-size % value on the root HTML element. At the ´screen-sm´ * and `screen-xs` breakpoints it reduces by the coefficient. * Text and the `$spacing-unit` are all influenced by the base * font size. * 2. At descending breakpoints the $root-sizing unit decreases, * This coefficient gives control over the rate of this decrease. * 3. Set the font-size, font-size-coefficient, and line-height normalizer. * 4. This value should be equal to the font-size captured from the * PSD / Design tool. It will be converted by an internal function into * a REM equivalent. * 5. This value is a coefficient similar to `$root-sizing-coefficient` * As the root font-size decreases at `$screen-sm` and `screen-xs` breakpoints * these declared fonts will also decrease. So for text to maintain it's * size (roughly) it will require a negative number, to increase the rate * of the decrease (For larger fonts) put in a positive value. It generally * aids readability on smaller devices if the contrast between large and * small fonts is less than that of larger devices. * 6. This value is to set whether or not the font-size uses the line-height * mixin to reduce the line-height on larger fonts. This is optional because * on font-sizes close to the `$initial-line-height` value may end up with a * line height that is a little less than desirable. I would recommend you * test this for each font size and toggle accordingly. * 7. Base-font-size. The default font size for text in the project. * 8. Global headline font-sizes, takes the font-size variables from [3]. * 9. Brand-font. The primary font-family for the project. You can declare * additional font-families via $alpha-font, $beta-font... */ $root-sizing-unit: 100%; /* [1] */ $root-sizing-coefficient: 18; /* [2] */ /* [3] [4] [5] [6] */ $txt-xxl: 50px, 1, true; $txt-xl: 40px, 0, true; $txt-lg: 30px, -1, true; $txt-md: 20px, -2.5, false; $txt-sm: 16px, -3.6, false; $txt-xs: 14px, -3.6, false; $txt-xxs: 12px, -3.6, false; $base-font-size: $txt-sm; /* [7] */ $h1-font-size: $txt-xl; /* [8] */ $h2-font-size: $txt-lg; $h3-font-size: $txt-md; $h4-font-size: $txt-sm; $base-font-family: Source Sans Pro, arial, sans-serif; /* [9] */ /*------------------------------------*\ $SPACING & LINE-HEIGHT \*------------------------------------*/ /** * 1. `$initial-line-height` helps set the vertical rhythm of the site. * `$spacing-unit` typically uses the same value. These values * will get converted to a unitless number as per the best * practices regarding line-height. It should be measured in the PSD * or design tool when selecting text that matches the `$base-font-size` * 2. The maximum width of the site-wrapper, the site wrapper wraps * general content. The pixel unit will get converted into REMs * at point [6] * 3. `$base-line-height`is a unitless number that is calculated by: * `$initial-line-height` / `$base-font-size`. This will be the value * of our line-height property on the `<body>` element. * 4. spacing-unit is a conversion of the `$base-line-height` into REMs. * 5. Half of the `spacing-unit`. used for small variants of abstractions like * inline-list, ui-list push, soft. * 6. site-wrap-width is a conversion of the `$base-site-wrap-width` into REMs * by diving `$base-site-wrap-width` by `$base-font-size`. */ $initial-line-height: 24px; /* [1] AKA MAGIC NUMBER */ $base-site-wrap-width: 1170px; /* [2] */ /** * NOTICE........... DO NOT MODIFY THIS GROUP! */ $base-line-height: ($initial-line-height / nth($base-font-size, 1)); /* [3] */ $spacing-unit: ($base-line-height * 1rem); /* [4] */ $half-spacing-unit: ($spacing-unit / 2); /* [5] */ $site-wrap-width: (($base-site-wrap-width / nth($base-font-size, 1)) * 1rem); /* [6] */ $grid-gutter-width: $spacing-unit; /*------------------------------------*\ $MEDIA QUERY BREAKPOINTS \*------------------------------------*/ /** * It's considered bad practice to define device specific breakpoints, * here are some defined broad breakpoints, that are used within the * media-querys mixin. * * 1. Denotes the breakpoint of the main navigation. */ $screen-lg: 1200px; $screen-md: 992px; $screen-sm: 768px; $screen-xs: 480px; $screen-xxs: 400px; $navbar-bp: 800px; /* [1] */ /** * Define breakpoints for the grid. Comment out or remove unused * breakpoints from the list below to reduce bloat in your compiled * CSS. * * 1. Classname, can use any you wish, just ensure your html grid classnames * match up. i.e. if you use `large` your html should look something like * `<div class="1/2@large>` * 2. The breakpoint value assigned to the classname. */ $grid-breakpoints: ( /* [1] [2] */ ( xxs-up, $screen-xxs ), ( xs-up, $screen-xs ), ( sm-up, $screen-sm ), ( md-up, $screen-md ), ( lg-up, $screen-lg ) ); /*------------------------------------*\ $MISC \*------------------------------------*/ /** * Any miscellaneous global variables. */ $brand-radius: 4px; $brand-transition: 0.2s; Edited December 11, 201510 yr by rbrtsmith
May 7, 20179 yr position:relative If you specify position:relative, then you can use top or bottom, and left or right to move the element relative to where it would normally occur in the document. position:absolute When you specify position:absolute, the element is removed from the document and placed exactly where you tell it to go. see examples and more at Learn More at ammanu
May 7, 20179 yr position:absolute When you specify position:absolute, the element is removed from the document and placed exactly where you tell it to go. see examples and more at Learn More at ammanu It will be placed relative to the closest ancestor that has a position property that is not 'static' else it defaults to the document. It's also not technically removed from the document but it is removed from the layout flow. Edited May 7, 20179 yr by rbrtsmith
May 8, 20179 yr position:relative If you specify position:relative, then you can use top or bottom, and left or right to move the element relative to where it would normally occur in the document. position:absolute When you specify position:absolute, the element is removed from the document and placed exactly where you tell it to go. see examples and more at Learn More at ammanu thanks for info. very helpful.
Create an account or sign in to comment