Science fiction is a form of speculative fiction. It usually differs from fantasy in that it is possible within the accepted rules of how the universe works. However, this is a shaky definition because of stories that include accepted impossibilities such as time travel. It can be difficult to draw the line between science fiction and fantasy, and many authors of one also write the other, so they are often grouped together in libraries and bookstores.
The setting of science fiction is often another time period than the one that the author is living in or different planets. (Fantasy, on the other hand, often takes place in alternate universes.)
Types of science fiction include dystopian, apocalyptic and post-apocalyptic, and time-travel. "Hard" science fiction refers to a story that relies heavily on scientific principles, while "soft" science fiction focuses on social issues. Alternate history can also be called a type of science fiction.