Codementor Events

5 useful css tips for javascript developers

Published Aug 24, 2018

JavaScript developers are sometimes not much familiar with the nuances of css. And they miss a simple css possibility and go in for a much complex JavaScript workaround.

I mastered JavaScript without mastering css. And I learned these 5 css tips very late and I regret I should have known them much earlier.

Please find the exampe code in this codepen.

1. relatively positioning parent when absolute positioning children

We may want to dynamically place a div on the screen, based on x & y co-ordinates. (In css, we use left and top instead of x and y). By default the co-ordinates are measured by keeping the root document as reference, which may not be ideal for our situation. We may want to position the div based on the parent container. In such cases, use the below one.

.parent {
  width: 400px;
  height: 300px;
  position: relative;
}

.child {
  position: absolute;
  width: 40px;
  height: 40px;
  left: 100px;
  top: 100px;
}

When we use absolute positioning — it is based on the nearest parent with relative positioning. If there is no relative positioning in the parent hierarchy, then the ‘child’ will be positioned based on the root document.

2. Ignoring mouse event on nodes

Frequently, we add the same click event handler to multiple ‘divs’, and we will identify the target using ‘event.target.id’. But the problem is, if the particular div has child nodes, then ‘event.target’ will refer to the child node and not the original node on which we added the listener. We need to tell that the children should not receive any mouse events, and any event on it should be considered as the event on the parent node.

.child1 {
  pointer-events: none;
}

3. Making text non selectable

There are two reasons why we may want to make the text non selectable.

  1. We dont want the user to copy the text easily.
  2. The UI will not look good, if text selection happens.

We can use a simple css property to handle this.

.non-selectable {
  user-select: none;
}

4. Overflowing Text

Sometimes we load dynamic text on which we have little control, (say user entered text) on a fixed width div. Here we should not allow the text to flow outside the box, at the same time we cannot simply hide the overflowing text. It may give a false impression that the shown text is the full text. Ideally we need to fit the text within the box, and it should end with ellipses (three dots) to indicate that there are more text, which are not displayed due to space constraints. Ideally something like below.

.overflow {
  width: 70px;
  white-space: nowrap; 
  overflow: hidden;
  text-overflow: ellipsis;
}

Apart from text-overflow, we should also define width, white-space, and overflow. Otherwise it will not work for obvious reasons.

5. Force Wrap Text

There will be occasions, when we want to make the whole text visible, by text wrapping. By default text will wrap around whitespace characters. If we want to break at the middle of the word, it is still possible.

.forcewrap {
  border: 1px solid red;
  width: 50px;
  margin-left: 200px;
  overflow: hidden;
  word-break: break-all;
}

Please find the exampe code in this codepen.

Thanks for reading this. 😃

Discover and read more posts from Rethna
get started
post commentsBe the first to share your opinion
Daniel Tonon
6 years ago

word-break: break-all;

Often the more useful setting is word-wrap: break-word;. When you use that it will only break words that are too long to fit inside their container. Typically that would be a long url that someone has posted.
(There are 3 variants of this that all do the same thing. word-wrap: break-word; is the variant with the best browser support. It even works in IE)

word-break: break-all; will break words regardless of whether there is enough room for the word or not.

Daniel Tonon
6 years ago

Frequently, we add the same click event handler to multiple ‘divs’

That makes me cringe. Use semantic HTML. Button elements are elements that you click. Not div elements.

Keith Lewis
6 years ago

Absolute never has made sense to me. It means basically the polar opposite of its dictionary definition.

For example:

Absolute - viewed or existing independently and not in relation to other things; not relative or comparative.

Css Absolute - positioning based relative to a parent element.

And people wonder why some struggle with css. There are a number of these syntactic “gotchas” that can easily leave someone confused. 😕

Rethna
6 years ago

Ofcourse, absolute and relative are opposite to each other in meaning. When we want to ‘absolute’ position a div, we want to do it with respect to its parent node and not the root node. And it can be done by setting ‘relative’ position to parent and ‘absolute’ position to children node. It is not very intuitive, and that is why it is difficult to understand.

Show more replies