Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide all text except for the first letter with CSS?

Is it possible to hide all letters after the first letter with CSS?

dt:not(::first-letter) {   display: none; } 
like image 518
Evanss Avatar asked Apr 25 '16 09:04

Evanss


People also ask

How do I select all except first in CSS?

The trick is very easy, in CSS we have the sibling selector ("+"), if i will make selector that choose "li + li" it will select all list-items except the first . That's all!

How do I show only the first letter in CSS?

The ::first-letter selector CSS Pseudo-element is used to apply the style to the first letter of the first line of a block-level element, the condition is it should not be preceded by other content ( such as images or inline tables).

How do you hide the first letter in CSS?

width: 1ch; overflow: hidden; It may not work for every font but it should. It is perfect for monospace as ch is the size of the O letter in a font, so if your first two letters are shorter than O it will work fine, otherwise you may have to tweak it a bit.


2 Answers

I think you can try this:

.twitter{        display: block;        color: transparent;      }        .twitter:first-letter{        color: #000;      }
 <div id="socialMedia">      <a class="twitter">Twitter</a>  </div>  <div id="socialMedia">      <a class="twitter">Google</a>  </div>

See also this fiddle

like image 41
Jainam Avatar answered Sep 28 '22 07:09

Jainam


You can, but your CSS is wrong. The version below works (at least in Chrome). It makes the dt invisible, and defines an overrule for the first letter to make it visible again.

I tried the same with display too, but that doesn't work, as expected. visibility: hidden hides the content, but keeps the element in place, while display: none removes it from the flow, and makes it impossible for sub-elements (the first letter in this case) to become visible again.

I added a hover too, so you can hover the letter to see the rest of the dt.

dt {    visibility: hidden;  }  dt::first-letter {    visibility: visible;  }    /* Hover the first letter to see the rest */  dt:hover {    visibility: visible;  }
Hover to see the rest:  <dt>Lorum ipsum is a weird text</dt>  <dt>Foo bar</dt>

A side effect will be that the area that is covered by the text is still claimed. Maybe that is not an issue, but if it is you will need some other solution. One possibility is to make the font-size of the dt 0 too. That way, the text is so small that is claims no space. Won't help if it also contains images, of course.

Since it doesn't seem to work, here is the alternative using font-size. Less than ideal, but hopefully it will still solve your problem.

dt {    font-size: 0;  }  dt::first-letter {    font-size: 1rem;  }    /* Hover the first letter to see the rest */  dt:hover {    font-size: 1em;  }
Hover to see the rest:  <dt>Lorum ipsum is a weird text</dt>  <dt>Foo bar</dt>
like image 194
GolezTrol Avatar answered Sep 28 '22 07:09

GolezTrol