Recent declines in the stock market may not put you in the mood to invest. After all, what if you buy a stock -- and in a few days it drops even further? You might ask yourself whether negative sentiment will last for a while -- or if we're in the midst of a short trend. It's nearly impossible to buy a stock at its very lowest point. And no one can accurately predict how long a market slowdown will last. If you're a short-term investor, those issues may heavily impact the value of your investment.

But here's some better news: If you're a long-term investor, they won't. If you plan to buy shares and hold them for at least a few years, your companies will have time to recover from today's difficulties -- and grow. Now is a great time to invest. But let's be sure to do it safely. Here's how.

An investor studies a stock chart showing declines.

Image source: Getty Images.

Your investment horizon

First, it's essential to consider your investment horizon and comfort with risk. When I refer to long-term investing, I mean buying a stock and holding on to it for at least five years. But some long-term investors know that if they've got a stock they like, they'll hold on to it for much longer. Why is it key to invest over time rather than jump in and out of stocks more frequently? This gives the company time to grow, evolve, and deliver share performance accordingly. And that offers you a greater chance of benefiting from that company's progress.

Consider a company like Amazon (AMZN -1.64%). If you bought shares around the time the online retail giant launched its Prime membership program -- in February 2005 -- and then sold two years later, you would have lost money on your investment.

AMZN Chart

AMZN data by YCharts

But, if you bought then and sold after five years, you would have pocketed a decent profit.

AMZN Chart

AMZN data by YCharts

And if you held on through today, you really would have hit the jackpot. The stock has climbed about 5,000% since the month of Prime's launch.

Of course, there are times when stocks will decline over time. But if you carefully select companies (more about that below), you increase your chances of success.

The idea of risk

Now, let's talk comfort with risk. In some cases, this may go along with the idea of investment horizon. If you're a new investor and you're in your 20s, for example, you may be more comfortable with taking a few risky bets. You know that, if you lose, you might make up for those losses when investing down the road. If you've been investing for many years and hope to lock in gains in a few years, you probably shouldn't load up on risky stocks.

It's key to determine your investment horizon and style right away. Now, let's move on to the most exciting part: the stocks themselves.

If you're an aggressive investor, you might consider a few higher risk names. For instance, a biotech company with a potentially game-changing program. An example is Vaxart, a clinical stage company developing oral vaccines. The shares have dropped more than 40% year to date.

But in these times of volatility, the safest bet -- for most everyone -- is to focus on companies with a strong earnings track record and/or solid future earnings prospects. Here, I'm thinking of companies that now are trading at a low valuation in relation to their earnings. And companies that have wisely invested in areas that will support growth in the future. Target is a good example. Today, Target is trading for about 10.8 times trailing 12-month earnings.

Also, Target has and is investing in its contactless pickup and delivery services. And it's adding sortation centers to speed up package delivery and cut down on costs. This will support growth down the road.

Healthcare companies and dividend stocks

You might also turn to companies that are less sensitive to weakness in consumer spending. Healthcare companies are a good option because patients truly need their products -- regardless of the economic situation. One of my favorites is Vertex Pharmaceuticals. It's the worldwide leader in cystic fibrosis treatment and generates billions of dollars in annual revenue and profit.

And now also is a perfect time to add some dividend stocks to your portfolio. You could go for Dividend Kings -- those that have increased their dividends for at least 50 straight years. Dividend stocks are interesting because in addition to the potential gains the stock may provide, you also generate annual income from your investment.

So, yes, it's never fun to watch markets decline. But that doesn't mean the environment is dangerous and you should give up on investing. Instead, use this time to safely shop for shares of quality companies. Over time, they're likely to thrive.