Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide text, leave icon elements

Tags:

I have a pretty big site now, and it would be time-consuming to change the codes of all of the buttons, so I am wondering if CSS could do something for me with no HTML code change.

Every page has an action bar with some buttons, which all consist of an icon and text. Something like this:

<div class="action-bar">
    <a href="" class="btn btn-primary">
        <i class="fa fa-icon"></i> 
        Button label
    </a>
    <a href="" class="btn btn-primary">
        <i class="fa fa-icon"></i> 
        Button label
    </a>
</div>

What I want to achieve now, is that on the mobile devices, the text inside the button will not display; only the icon should display.

If I had a markup like this one:

<a href="" class="btn btn-primary">
    <i class="fa fa-icon"></i> 
    <span class="label">Button label</label>
</a>

Then I know I could hide all the labels by doing:

@media(max-width:768px) {
    .btn .label {
         display: none;
    }
}

So what I'm wondering is, is there a pure CSS equivalent to this, but with the first HTML markup where there is no span.label?

like image 540
Digital Ninja Avatar asked Nov 30 '15 13:11

Digital Ninja


People also ask

How do I hide text messages without tags?

visibility:hidden , color:transparent and text-indent will do this. the font-size:0 option will work better I think. It should be remembered that some of these solutions will only hide the text. They will still leave a space for it on the screen.

How do I hide text in span?

You can specify either 'hidden' (without value) or 'hidden="hidden"'. Both are valid. A hidden <span> element is not visible, but it maintains its position on the page. Removing the hidden attribute makes it re-appear.


3 Answers

You can set the font size to zero:

.btn { font-size: 0; }

If your icons contain text, overwrite their font size:

.fa { font-size: initial; }
like image 147
Rokin Avatar answered Sep 19 '22 08:09

Rokin


You can use:

@media(max-width:768px) {
    .btn {
        color: transparent;

    }
    .btn > .fa{
        color: black; //or color of your choice
    }
}

This will hide the text, but it doesn't deal with the size of the button.

If you know the size of the icons, you can force that size to the .btns and then use position:absolute;top:0;left:0;z-index:1; on your icons.

like image 40
Szabolcs Páll Avatar answered Sep 21 '22 08:09

Szabolcs Páll


In some cases the best solution to set "invisible" label for interactive element is to use "aria-label" HTML property:

<button onclick="..." aria-label="Send" />

or

<a href="..." aria-label="Next"></a>

Description at MDN Web Docs

like image 1
Dzmitry Kulahin Avatar answered Sep 21 '22 08:09

Dzmitry Kulahin