Home  >  Article  >  Web Front-end  >  How to Prevent Double Borders in CSS: Outline vs. Negative Margins?

How to Prevent Double Borders in CSS: Outline vs. Negative Margins?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-10-26 19:42:03188browse

How to Prevent Double Borders in CSS: Outline vs. Negative Margins?

Avoiding "Double" Borders in CSS

When two adjacent elements with borders are placed side by side, it may appear as if they have a double border at the junction where they meet. To prevent this visual artifact, consider the following CSS techniques:

Using Outline Instead of Borders

For elements that may appear in any order, the outline property provides a reliable solution:

<code class="css">.collection {
  /* Optional styles if needed */
  margin-top: -1px;
  margin-left: -1px;
}

.collection .child {
  outline: 1px solid;
  margin-top: 1px;
  margin-left: 1px;
}</code>

Negative Margins

Alternatively, using negative margins on the child elements will effectively "erase" the double border:

<code class="css">.collection {
  /* Optional styles if needed */
  margin-top: -1px;
  margin-left: -1px;
}

.collection .child {
  margin-top: -1px;
  margin-left: -1px;
}</code>

Note for Legacy Browsers

The outline property is not supported in older browsers such as IE7 and earlier. In these cases, the negative margin approach is recommended.

The above is the detailed content of How to Prevent Double Borders in CSS: Outline vs. Negative Margins?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn