Styling transformed elements with a CSS border

After applying a transformation that scales and rotates a HTML node through CSS, I noticed an unexpected issue. A thin additional border in the same color as the node itself appeared outside of the main fat border. It seems like the background extends beyond the border, causing this slim border to show. Is there a way to fix this?

.transform {
  transform: scale(1, .7) rotate(45deg);
}
.container {
  width: 100px;
  height: 100px;
  background-color: chocolate;
  border: 20px solid white;
}
<div class="container">proper white border</div>
<div class="container transform">slim orange border around actual white border</div>

In the example provided above, you can see the difference between a proper border setup and the issue with the additional slim border. The top box has a white border making it less noticeable, while the bottom box reveals the problem with the extra border.

Is there a solution to prevent this strange occurrence from happening?

Answer №1

To prevent this issue, you can modify the background-clip property. By default, it is set to border-box:

border-box

The background stretches up to the outer edge of the border (but beneath the border in z-order).

.transform {
  transform: scale(1, .7) rotate(45deg);
}
.container {
  width: 100px;
  height: 100px;
  background-color: chocolate;
  border: 20px solid white;
  background-clip:content-box; /*OR padding-box*/
}
<div class="container">correct white border</div>
<div class="container transform">narrow orange border around actual white border</div>

By using padding-box or content-box, the background will not stretch up to the border.

Answer №2

Utilize the CSS property background-clip with a value of padding-box;

.transform {
  transform: scale(1, .7) rotate(45deg);
}
.container {
  width: 100px;
  height: 100px;
  background-color: chocolate;
  border: 20px solid white;
  background-clip: padding-box;
}
<div class="container">will have proper white border</div>
<div class="container transform">will display a slim orange border around actual white border</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to stack text boxes vertically in CSS/HTML?

How can I arrange these textboxes so that .textbox appears at the top, followed by textbox1 below? CSS .textbox { border: 1px solid #848484; -moz-border-radius-topleft: 30px; -webkit-border-top-left-radius: 30px; border-top-left-radius: ...

What is the best way to align an image to flex-end?

I recently started using React and ran into issues trying to align my image using CSS. Despite trying various methods like justifyContent, justifySelf, and floating right, I couldn't get the image to align on the right side of the screen without resor ...

I am experiencing issues with my button not responding when I click on the top few pixels

I've created a StackBlitz version to illustrate the issue I'm facing. It seems like the problem might be related to my CSS for buttons... Essentially, when you click on the button at the very top few pixels, it doesn't register the click an ...

Why does the text input value keep disappearing when I click on it?

It recently occurred to me that my plugin's functionality resembles that of an Excel table. The desired behavior is such that when a certain element is clicked, its content should be replaced with an editable input field. While this mechanism does wor ...

Implement a sliding effect for the navigation menu

I'm trying to figure out how to make this menu slide in when the hamburger menu is clicked. The background of the menu is an SVG, not just a shape. Does anyone have any suggestions or ideas on how I can achieve this effect? For reference, here is the ...

Ways to display a pop-up window depending on the user's selection?

I am faced with a challenge concerning my modal and form. I have successfully implemented a feature where clicking on an "Add" button opens the form within the modal. However, I now want to extend this functionality to include editing items created using t ...

Ways to eliminate line breaks and <br> tags in text using only CSS to create a button that trunc

I am currently working on incorporating a truncated text button using only CSS. The issue I'm facing is that the "show more" button doesn't ignore the line breaks or any other relevant HTML within the teaser and text body. This is causing too muc ...

What is the best way to dynamically select and deselect radio buttons based on a list of values provided by the controller?

I am currently working on implementing an edit functionality. When the user initially selects a list of radio buttons during the create process, I store them in a table as a CSV string. Now, during the edit functionality, I retrieve this list as a CSV stri ...

Using the CSS to set the alt attribute and title of an image based on its file name

Is it feasible to dynamically set the ALT and title attributes of an image element to match the file name? It seems like this could potentially be achieved using the CSS content property, although I am not very familiar with the process. For example: < ...

How can I customize the appearance of tab buttons in a QtabWidget using PySide?

Looking for a way to customize the tab buttons on a QtabWidget in PySide. I've managed to style other elements of my custom widget, but can't figure out how to style the QtabWidget tabs. My attempt: self.tabWidget = QtGui.QtabWidget(Form) self. ...

Striving to update only the received field from a form in PHP, while leaving the rest blank

After working on updating a PHP function, I encountered some issues. When receiving input from a form containing a username, email, and password, if the user leaves one or two of these fields blank, I want to update only the non-blank field such as 'p ...

Modify the rows in an HTML table in a Django project to have matching cell values displayed in the same color

I have set up a table in my HTML file for a Django project, with raw data extracted from the provided list sample below (only showing a few lines due to length): mylist= [{'StartDate': '2021-10-02', 'ID': 11773, 'Receiver ...

Why do child elements of a collapsing parent div not collapse with it in CSS transitions?

Just diving into the world of css transitions and experimenting with collapsing a fixed popover div using CSS transition. I managed to collapse the outer div successfully, but the elements inside it did not collapse along. Is this the expected behavior? ...

Unusual behavior in html5 and css

Could you please check out this link: http://jsfiddle.net/MbrJf/ Upon inspecting the element, I noticed that the body of the page is starting 100px from the top, even though there are no margins or any styling in the CSS. The issue seems to arise when the ...

Issue with Fancybox data-source not functioning properly beyond the second page of datatables

Am I overlooking a bug or am I just not getting it? To start off, I am working on some PHP code using jQuery datatables and fancyapps. I'm in the process of creating buttons for datatables rows with their corresponding row IDs. The code is quite leng ...

Obtain the context within the image loading function

I am working with an array of Image objects: let imageArray = []; ... imageArray[i].onload = function () { ... } When the onload function is triggered, I need to determine which specific Image object it is referring to. Is there a method to achieve ...

Craft unique looks for both even and odd Tumblr posts

I recently came up with a unique idea for a Tumblr theme where the design of posts changes based on whether they are even or odd in the sequence. While I have experience with basic HTML coding, delving into Tumblr customization is new to me. Here's wh ...

The Youtube Subscribe Embed feature is causing my image to lose its corners

When I use the official embed code from Google Developers to embed my YouTube channel, it cuts off the corners of my image and leaves white corners. This doesn't look good on my website with a black background. <script src="https://apis.google ...

What benefits does utilizing unordered lists provide in navigation bars?

Throughout my research, I have come across numerous tutorials detailing the process of creating a navigation bar. They often begin by adding an unordered list and then proceed to remove the default styling such as bullet points and margins from both the ...

Hovers and click effects for navigating through images

The website I'm currently working on is stipz.50webs.com. p.s. HOME functionality is not active at the moment. Having successfully implemented the onhover and onmouseout features, my next goal is to enhance the navigation effects for each div/img so ...