fyngyrz
fyngyrz

Reputation: 2658

Creating a styled button representation (not an actual button) with css

I have an application that has a lot of buttons in the window. In writing the HTML documentation pages for this, I've been successful in creating a bordered, sorta-shadowed CSS <span> with text within that represent the buttons that just have legends on them.

This allows me to show "momentary" buttons like these...

enter image description here

...that just have a legend on them in such a way that it's reasonably obvious what I'm describing by simply putting...

<span id="button">LAP</span>

...in line with the associated description (and my custom documentation system makes it even easier by letting me invoke the style inline with [s button LAP]. Fun. :) Here's the style I built for that:

span#button
{
    font-family: Courier;
    font-weight: bold;
    white-space: pre;
    border: 1px solid #000000;
    background: #ddddee;
    padding-left: 2px;
    padding-right: 2px;
    color: #000000; 
}

Here's screen clip of part of the documentation that uses that technique:

doc screen shot

Also within the application, I have buttons that have "LED" indicators on them. A typical one might display a green LED when on, and a dark LED when off. Screen clip from the application (with a dark style sheet, so the buttons are dark) showing some of these:

screen clip of some app controls

I already have nice little .jpg images that show all the "LED" colors I use, conversely, an embedded CCSS box filled with the right color would be fine too.

What I would like to do, and am having no luck at all doing, is create a <span> within the text that looks as least somewhat like one of those buttons -- without going to specific images for each button, or in other words, using CSS. Since the only things that vary are the LEDs and the text, I want to can the LEDs and feed in the text. Something like...

<span id="greenbutton">Run</span>

In order to do that, I need the LED to appear above the text, and size the text small enough to land underneath it, and center them both within a bordered box as the text-only version above does. I would like an output like this (button built in an image processor)...

press putative LED-button from CSS to start

...from this:

press <span id="greenbutton">RUN</span> to start

It seems like it ought to be easy enough; and I can add quite a bit of complexity within my documentation system if required to make it all work -- multiple nested spans, divs, images, test, whatever it takes -- but I keep running into these two showstoppers:

I can't seem to get a <div> to just land in the text where I put it in the first place, although I've been able to make them look just like I want them to because they understand vertical alignment and positioning withing their own context.

I was also thinking of some actual images of buttons with the text removed from them in each LED state, used as background to a span, where the text is overlaid on that background, thereby looking like a specific button. I've not tried this, as I can't seem to find how to make a span have a background and <div>... a <div> won't stay where I want it (not left or right, but right there, or else refrain from breaking the lines if it's not floated.

I'm not opposed to putting a table inline, either. If I knew how...

I hope I'm missing something. In which case, help! Or is this impossible, and the only solution is to screen-cap the many, many buttons in each of their various states (some actually display multiple LED colors for various settings, worse yet) and then drop the images in where I want them? Because although I could do that, it's awfully clumsy and effort intensive. :(

Upvotes: 0

Views: 738

Answers (1)

maguijo
maguijo

Reputation: 202

Introducing the pseudo element "before"! Ta-da!

<p>Green button</p>
<span class="myButton greenbutton">RUN</span>
<p>Red button</p>
<span class="myButton redbutton">RUN</span>
<p>Click this purple button <span class="myButton purplebutton">RUN</span> here.</p>

<style>    
span.myButton {
    display:inline-block;
    border-top: 2px solid #eee;
    border-left: 2px solid #eee;
    border-right: 2px solid #000;
    border-bottom: 2px solid #000;
    padding:1px 2px 0;
    background: #dde;
    width:20px;
    height:auto;
    font-size:10px;
    font-family:monospace;
    text-align:center;
}
span.myButton:before {
    display:block;
    margin:2px auto 0;
    width: 16px;
    height: 5px;
  border: 1px solid #000;
  content: "";
}

span.greenbutton:before {background:#99FF00;}
span.redbutton:before {background:#FF0043;}
span.purplebutton:before {background:#A200C1;}

</style>

Updated answer: I changed the display on the span to inline-block, so it will go inside a paragraph. I missed that requirement on my previous answer.

I added a class to each span, so that all spans in your document won't be affected, just the ones with that class.

Technically, if you are going to have more than one green button, you shouldn't use an ID for it. ID's are supposed to be unique and therefore only used once in a document. So I've also converted that to a class.

in CSS, the period denotes a class, as opposed to the # sign denoting an id. Ergo: span.myButton targets the span with class "myButton". span.greenbutton targets a span with the class greenbutton. You can have more than one class on an element.

I took the background-color property out of the span:before style, and put it in a class specific style -> span.greenbutton:before. Basically, the classes for the span.myButton and the pseudo element span.myButton:before are the same for all these buttons. But for each color, put an additional class on the span, and create a style with that class for it, using the background color you want. Hope that's clear. Fiddle updated too.

https://jsfiddle.net/maguijo/05zwwjy6/

Upvotes: 1

Related Questions