Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display first letter only

Lets say this markup:

<div id="socialMedia">
    <a class="Twitter">Twitter</a>
</div>

What i want is only to be visible the first letter of the text (in this case, just a T)

(Actually I won't end up using it but I am curious about this; sure can be helpfull later)

So this was my a attempt:

#socialMedia .Twitter{
    display:none;
}
#socialMedia .Twitter:first-letter {
    display: block !important;
}

I was able to check that it won't achieve it. Question is why? and is there some work-around this?

-EDIT-

We are looking for IE=+7/8 version capable solutions..

Salut

like image 345
Toni Michel Caubet Avatar asked Jul 16 '12 21:07

Toni Michel Caubet


People also ask

How do I show only the first letter in CSS?

CSS ::first-letter Selector The ::first-letter selector is used to add a style to the first letter of the specified selector. Note: The following properties can be used with ::first-letter: font properties. color properties.

Can I use :: first letter?

::first-letter (:first-letter) The ::first-letter CSS pseudo-element applies styles to the first letter of the first line of a block-level element, but only when not preceded by other content (such as images or inline tables).

How do I color the first letter in HTML?

The ::first-letter selector is used to add a style to the first letter of the specified selector.

How do I make the first letter bold in HTML?

To bold the text in HTML, use either the strong tag or the b (bold) tag. Browsers will bold the text inside both of these tags the same, but the strong tag indicates that the text is of particular importance or urgency. You can also bold text with the CSS font-weight property set to “bold.”


4 Answers

Try something like this:

.Twitter {    font-size: 0;  }    .Twitter:first-letter {    font-size: 12px;  }
<div class="Twitter">Twitter</div>

Maybe this is not the best solution, but it works.

like image 119
Dexter_ns88 Avatar answered Sep 30 '22 18:09

Dexter_ns88


Edit: Disclaimer: this does not work according to comments. Please don't use as-is without checking it fits your needs.

If you check the specification for the :first-letter pseudo-element, you'll notice the following:

The :first-letter pseudo-element must select the first letter of the first line of a block, if it is not preceded by any other content (such as images or inline tables) on its line.

The important word here is "block."

You are trying to use the pseudo-element on an <a/> tag with class of Twitter. By default, anchor tags are inline elements (not block level elements).

For your given markup, one solution to your problem would be to style the anchor this way:

.Twitter {
    display:block;
    visibility:hidden;
}

.Twitter:first-letter {
    visibility:visible;
}​

I'm not sure exactly what you are going for, but that is good enough for experimental purposes. Check out a demo here: http://jsfiddle.net/H7jhF/.

like image 25
TJ Koblentz Avatar answered Sep 30 '22 18:09

TJ Koblentz


Another way is to use color: transparent

.twitter{
  display: block;
  color: transparent;
}

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

JSFiddle


However, this won't work for lte IE8.

References:

  • IE7 IE8 IE9 color:transparent property
  • color: transparent is not working in Internet Explorer
like image 35
Vucko Avatar answered Sep 30 '22 17:09

Vucko


What you're doing is like hiding a parent element and trying to show one of its children, it won't work because the parent's style overrides it. The parent element also has to be a block level element for it to work. Like a div or p tag, or display: block; on the a tag.

Here's something using color:

HTML

<div id="socialMedia">
    <a class="Twitter">Twitter</a>
</div>

CSS

body {
  background-color:#FFF;
}
.Twitter{
    display: block;
    color:#FFF;
}
.Twitter:first-letter {
    color:#000;
}
like image 40
sachleen Avatar answered Sep 30 '22 17:09

sachleen