To see all of this on video visit the Drywall section of the Learning Curve.
When you need to install an electrical box in a specific location and there is no stud to attach it to, we call that ReWork -- getting a box in where it is impossible or difficult to attach to a stud. Most electrical boxes are installed before the drywall during construction, so they are designed to be nailed or screwed straight across the box to the stud. When the drywall is already there, that simply doesn't work. You do have several choices. All of them depend on the wall tabs shown in the first photo being secured into place because these tabs will sit on the front side of the drywall as one half of the clamping action of holding the box in place. In addition the hole in the wall have to be carefully cut out to be sure that these tabs have something to sit on.
The most common box attachment is a pair of strange tabs that are put through the wall and then pulled forward. Then you work carefully to slide a regular electrical box into place, although choosing a box with the fewest protrosions possible that will make it hard to slide into a snug hole. Always buy a couple of extra tabs because you may drop one or more inside the wall. Once the box is snug on the wall, you bend the tabs over into the inside of the box. This catches the drywall between the end tabs and the ears of these little wings. Watching the video mentioned above certainly helps to understand this.
If you want to purchase special no-stud boxes rather than use the tabs with standard boxes, you can get one that has a clamping arrangement on each end. This one requires that you open the hole just a little larger top to bottom to get it in the wall, but not so large as to loose your grip on the drywall. Screws on the front will pull the back part of the clamp into place.
The last photo shows what is probably the easiest box to use. Simply push it into the wall and a spring like piece of metal will pop open on the back side. Tighten a screw in the bottom of the box and it clamps onto the drywall. Be careful not to tighten too much or the clamp could cut into the drywall on the backside and you would loose some of the stability of the clamping action. Click here for information on the LEGALITY OF DIY ELECTRICAL WORK.