How to add a class to an element with CSS

alexander

CSS was introduced to separate the content of a website from it's presentation. I'm a proponent of a strict separation of concerns. The CSS class selector is very usefull to group HTML elements that don't have their own tag (e.g. class: "contact info card" if your page shows many cards). This is a good solution as long as the class selector is also relevant to someone with a text-only browser (to replace missing content tags). But as soon as you start adding class selectors to elements, that have nothing but the desired design-properties in common, you change the presentation from within the content (e.g. class: "background-black" is like using HTML-styling tags).

To keep a strict separation but still be able to reuse CSS code, I came up with this solution: Add a class to a HTML element where it makes sense from a content-point-of-view. If two or more elements that have nothing in common need to use the same complex CSS properties, each of them gets an id selector. Within the CSS file I would like to assign the complex-css-properties-class to the element for each id. How can I add a class to an id-selected element from within the CSS code? Do you know other ways to keep a strict separation of content and presentation?

Josef Engelfrost

For less complex structures, writing your CSS in a smart way might be enough:

/* Cards */
.employee, 
.service {
  font-size: 1.2em; 
  padding: 20px;
  margin: 20px;
  box-shadow: 0 0 .25rem #ccc;
}

.employee {
  color: blue;
  ...
}
.service {
  color: red;
  ...
}
<div>
  <div class="employee">Kim</div>
  <div class="employee">Jim</div>
</div>
<div>
  <div class="service">Ironing</div>  
  <div class="service">Washing up</div>
</div>

For the example above this may be fine, but for larger projects you might want a CSS pre-processor like Sass to keep it maintainable:

@mixin card() {
  font-size: 1.2em; 
  padding: 20px; 
  margin: 20px;
  box-shadow: 0 0 .25rem #ccc;
}

.employee {
  @include card();
  color: blue;
  ...
}

.service {
  @include card();
  color: red;
  ...
}

Edit 2015-11-27 Regarding separation of concerns I revisited Harry Roberts' CSS Guidelines today, and I think he explains a good interpretation of that regarding HTML and CSS. I thought I might post it because it is quite different from the interpretation in the question:

The separation of concerns when applied to front-end code is not about classes-in-HTML-purely-for-styling-hooks-blurring-the-lines-between-concerns; it is about the very fact that we are using different languages for markup and styling at all.

In short: having classes in your markup does not violate the separation of concerns. Classes merely act as an API to link two separate concerns together. The simplest way to separate concerns is to write well formed HTML and well formed CSS, and link to two together with sensible, judicious use of classes.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to add CSS class dynamically to html element

From Dev

How to add | remove css class in angular element?

From Dev

How to add css class to an element inside nav element

From Dev

css add class to selected element

From Dev

How to add class CSS after ng-click to parent element?

From Dev

How Do I Add CSS Class to body Element in Meteor?

From Dev

How to add CSS class to the HTML5 picture element

From Dev

jQuery - How to add css to a parent element if child element has certain css class

From Dev

How to add css class in class

From Java

How to add "class" to host element?

From Dev

How to add a class to a given element?

From Dev

Javascript - add css style to element with class

From Dev

Zend decorator - Add css class on field element?

From Dev

Want to add a CSS class to an element when it is clicked

From Dev

how do you modify the actual css of a class rather than add a style to each element of that class?

From Dev

How add symbol in CSS for element?

From Dev

How to add a class to element if another element is empty?

From Dev

How to add multiple CSS styles to an element with .css()

From Dev

Add a css class to an element if the element has a certain text via javascript

From Dev

How do I add a class to an element and keep it there until the mouse leaves with animate.css?

From Dev

How to add a class to an element when another element gets a class in angular?

From Dev

How to add css class to style.css?

From Dev

How to add class to the third <li> element in javascript

From Dev

How to add a class to parent element when there are several

From Dev

How to add the `active` class on click of `li` element

From Java

How do I add a class to a given element?

From Dev

How to add class to html element with Deface?

From Dev

OM how to add class to html element

From Dev

how to add class to element from its id?

Related Related

  1. 1

    How to add CSS class dynamically to html element

  2. 2

    How to add | remove css class in angular element?

  3. 3

    How to add css class to an element inside nav element

  4. 4

    css add class to selected element

  5. 5

    How to add class CSS after ng-click to parent element?

  6. 6

    How Do I Add CSS Class to body Element in Meteor?

  7. 7

    How to add CSS class to the HTML5 picture element

  8. 8

    jQuery - How to add css to a parent element if child element has certain css class

  9. 9

    How to add css class in class

  10. 10

    How to add "class" to host element?

  11. 11

    How to add a class to a given element?

  12. 12

    Javascript - add css style to element with class

  13. 13

    Zend decorator - Add css class on field element?

  14. 14

    Want to add a CSS class to an element when it is clicked

  15. 15

    how do you modify the actual css of a class rather than add a style to each element of that class?

  16. 16

    How add symbol in CSS for element?

  17. 17

    How to add a class to element if another element is empty?

  18. 18

    How to add multiple CSS styles to an element with .css()

  19. 19

    Add a css class to an element if the element has a certain text via javascript

  20. 20

    How do I add a class to an element and keep it there until the mouse leaves with animate.css?

  21. 21

    How to add a class to an element when another element gets a class in angular?

  22. 22

    How to add css class to style.css?

  23. 23

    How to add class to the third <li> element in javascript

  24. 24

    How to add a class to parent element when there are several

  25. 25

    How to add the `active` class on click of `li` element

  26. 26

    How do I add a class to a given element?

  27. 27

    How to add class to html element with Deface?

  28. 28

    OM how to add class to html element

  29. 29

    how to add class to element from its id?

HotTag

Archive