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

Mary-Kate Olsen
Release: 2024-10-26 19:42:03
Original
187 people have browsed it

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>
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!