Using pseudo-elements to add borders

I set padding-right directly on my image for a specific reason. However, now I want to add a border style but only to the image itself. I attempted using the box-sizing method without success.

img{
    padding-right: 1em;
    border: 5px solid red;
}


<img src='path.jpg' />

demo

I need to keep the padding but apply the border exactly to the edge of the image. How can I achieve this?


Update:

Is there a way to make it work like an after pseudo-class?

img:after{
    content: " ";
    border: 5px solid red;
    width: 100%;
    height: 100%;
}

Adding margins or placing a span before the image will not work in my situation because the image is set to width: 100%;. Here is an example demo: http://jsfiddle.net/Ab77g/17/

Answer №1

While many suggest using a Margin to achieve your desired outcome, there is another way to solve the problem. By utilizing a span element with a special class and adjusting the layout slightly, you can still use padding instead of margins:

<span class=custom-class><img src="path.jpg" /></span>

In your CSS file:

 span.custom-class{
   padding-right: 1em;

 }
img
{
    border: 5px solid blue;
}

This solution was created based on the provided code snippet in your question.

Answer №2

Sure, we can now start working on improving it using the div element (as per your latest update) in this manner

div{
    width: 200px;
    position:relative;
}
div:after {
    background: #fff;
    border-left: 5px solid #FF0000;
    content: "";
    height: 134px;
    position: absolute;
    right: -46px;
    top: 0;
    width: 41px;
}

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

I am attempting to create basic animations using css, but I'm having some trouble

Currently, I am attempting to add animation to a css div without including the top property in the original div style. I omitted it because I wanted to delay the animation, so I placed it within the @keyframes property. However, the element disappears afte ...

Tips for eliminating any default white space visible between tags:

What is the best way to eliminate white space gaps between div tags and adjust pixel differences between them in HTML code? My current alignment is off and there are noticeable white spaces between div tags. I have tried using a reset link but it didn&apo ...

Place Div in the center of another Div

In my maze creation, I am trying to center an inner div, but no matter what I do, using margin: 0 auto; does not seem to work The inner div displays a sad smiley face when the user enters a wall and loses #highlight_lose { width: 550px; height:55 ...

Chrome isn't showing the Background Image

I am currently working on coding a basic HTML/CSS page and I want to include a background-image within a div element that links to a URL. Interestingly, my code is functioning properly in Safari (Mac) and Firefox but unfortunately not showing the desired r ...

What is the significance of ' */ ' in CSS coding?

I'm unsure if this question is valid, but I have not been able to find answers anywhere. In my code, when using margin-top: -17px; */ and margin-top: -17px;, I notice different results. What does the symbol */ mean and are there other similar symbols ...

Changing Location of Carousel in Bootstrap

Recently, I have been working with Bootstrap and have encountered a problem with the carousel. I have placed carousel images below my navbar, but there seems to be a gap between the navbar and the carousel image. I want the image to be right below the navb ...

JavaScript popup displaying incorrectly

In my experience, I have completed two web-related classes that involved similar projects with popups using HTML and JavaScript. However, I am facing an issue where the popup either does not show at all or appears for only a brief moment before closing its ...

Do my Dash tutorials appear drastically different from the documentation? Is this normal?

As an experienced back-end engineer, I am delving into the world of browser-based GUI design for the first time, specifically with HTML, CSS, and more. My current project involves building a dashboard using Dash, and I am in the early stages of familiarizi ...

Restrict the dimensions of the HTML5 Facebook comment field

I need help limiting the vertical height of Facebook comments generated by the code below. I know how to set the width using the data-width parameter, but I'm not sure how to limit the vertical height of the comment box. Can anyone provide guidance on ...

What is the best way to manage horizontal scrolling using buttons?

I was hoping that when the button is clicked, the scroll would move in the direction of the click while holding down the button. Initially, it worked flawlessly, but suddenly it stopped functioning. export default function initCarousel() { const carous ...

Challenges faced when creating a dynamic HTML and CSS slider

I recently added some code from a CodePen to my website. While the code is functioning correctly, it seems to be causing issues with other content on my page such as links and images. Any idea why this might be happening and how can I fix it? :) HTML &l ...

Unlocking the power of setting dynamic meta content

When using EJS for templating in my node.js webserver, everything has been running smoothly except for one issue. After integrating the head file into a page, I encountered the following error: SyntaxError: Unexpected identifier in /home/runner/superstrap/ ...

A guide on effectively selecting an element with the '@font-face' tag

I'm endeavoring to design a distinctive logo by utilizing a personalized font... and am keen on incorporating it into my CSS As an illustration: the identifier for my div element is "logo" Should I merely employ this code? #logo { font-family: ...

What is the best way to expand the size of the pagination buttons in a primeng table?

My primeng table is quite large: <p-table #dt [value]="artefacts" [columns]="cols" [paginator]="true" [rows]="numberRows" [tableStyle]="{'table-layout':'auto'}"> With so many entries, the table spans over 100 pages. However, the ...

I prefer to have the boxes lined up horizontally, but they seem to be arranged vertically

I'm struggling to get the color boxes to display horizontally with spaces between them. Currently, they are showing up vertically in a column layout. I want 4 smaller color boxes within a larger grey box at the top layer, with margins and padding app ...

Struggling with the functionality of having numerous jQuery UI controls on a single page. Implementing the accordion control in multiple sections

For my current project, I am utilizing the jQuery UI accordion control twice on a single page. The first one functions perfectly fine, but when I attempt to add a second accordion, the original one stops working while the new one works correctly. Does any ...

Crafting a lively piece of content within a written passage

My goal is to create a unique dynamic portion of text within a paragraph. I am struggling to develop an in-line div (or span) that can hold elements like an unordered list or multiple spans which can be vertically adjusted to display. Imagine a window with ...

The color of the SVG is not visible in the PNG rendition

I have applied a style to an svg image and successfully changed the fill color using a random colors function in JavaScript. However, when I convert the svg to a png format after making these color changes, the PNG file retains the original colors instead ...

Adjust dimensions of an image retrieved from a URL

Is there a way to adjust the size of the image displayed here?: var picture = new Image(); picture.src = 'http://www.example.com/images/logo.png'; picture.width = 200; //trying to change the width of the image $('canvas').css({ ...

Styling pagination using CSS and jQuery

I am looking to display 3 sections in a simple, paginated way that mimics tabs. I am trying to implement the logic where when a pagination item is clicked and has the 'active' class, it should show the corresponding block. However, I am strugglin ...