Although it is often said that nothing is certain except death and taxes, the one tax you may be able to avoid or minimize most through planning is the tax on capital gains. Here’s what you need to know to do such planning:

What is capital gain? Capital gain is the difference between the “basis” in property — usually real estate or stocks, but also including artwork and collectibles — and its selling price. The basis is usually the purchase price of property. So, if you purchased a house for $250,000 and sold it for $450,000 you would have $200,000 of gain ($450,000 – $250,000 = $200,000). However, the basis can be adjusted if you spend money on capital improvements. For instance, if after buying your house you spent $50,000 updating the kitchen, the basis would now be $300,000 and the gain on its sale for $450,000 would be $150,000 ($450,000 – ($250,000 + $50,000) = $150,000). Just make sure you keep good records of any capital improvements in order to prove them in the event of an audit. (The residence exclusion and the step-up in basis are discussed below.)

How much is the tax? It depends, but assume 15 percent federally unless you have either very low or very high income, and whatever your state’s tax is (let’s assume 5 percent, for a total of about 20 percent). Using those assumptions, the tax on $200,000 of gain would be about $40,000. There are three exceptions. First, if you owned the property for less than a year, you would be subject to short-term capital gains tax rates, which are essentially the same rates as for income tax. Second, if your taxable income, including the capital gains, is less than $37,650 for a single person and $75,300 for a married couple (in 2016), there’s no federal tax on capital gain. But beware that the capital gains will be included in the calculation and could put you over the threshold. Third, if your income is more than $415,050 for a single person and $466,950 for a married couple (in 2016), the federal capital gains tax rate is 20 percent, bringing the combined federal and assumed state rate up to just over 25 percent.

The personal residence exclusion. You may exclude up to $250,000 of gain on the sale of your personal residence and if you’re married you can exclude $500,000. To qualify, you (or your spouse) must have lived in and owned the house for at least two out of the five years prior to the sale. Those two years don’t have to be the same. For instance, if you lived in the house from 2012 to 2014 and owned it from 2014 to 2016, but rented it out, you could still qualify for the exclusion. If you are a nursing home resident, the two-year requirement is reduced to one year.

Carry-over basis. If you give property such as a family heirloom or real estate to someone else, they receive it with your basis. So, if your parents bought a vacation home many years ago for $25,000 and now its fair market value is $500,000, if they give it to you, your basis will also be $25,000. If you sell it, you’ll have a gain of $475,000 and no personal residence exclusion, unless you move in for two years first. The combined state and federal tax would be $118,750.

Step-up in basis. On the other hand, the basis in inherited property gets adjusted to the value on the date of death. In the example of the vacation home, if your parents passed it on to you at death rather than giving it to you during life, the basis would be adjusted to $500,000, potentially saving you $118,750 on its sale. On the other hand, depending on the size of your parents’ estate, it may be subject to estate tax, which would be payable within nine months of their death, while the tax on capital gain would not be due until you sold the property, perhaps decades in the future. President Obama has proposed getting rid of this so-called “step-up” in basis. His reasoning is that it is regressive, benefiting people with property, and the more property they have, the more tax they save. But an argument for retaining the step-up rules is that they can save a tremendous amount of administrative hassle. If you inherited stock from your father that he inherited from his mother, it may be impossible to establish what it was she paid for it. It’s much easier to determine what it was worth at your father’s death.

Offsetting losses. If during the tax year you realized capital gain through the sale of property, you can offset it with capital losses. Say, for example, you sell your home and realize a lot of gain. You could also sell some stock that has gone down in value, creating a loss that offsets some of the gain on the house sale. In some instances, you can carry over loss from one tax year to the next to offset future gains.

By understanding and considering these rules, you can save on capital gains taxes and avoid a number of possibly expensive mistakes.