Forming a space between borders

I'm attempting to create a space within a div's border that can accommodate an image, much like so:

Is there a method to achieve this using only CSS? The only solution I have found is:

.box {
    background: url(img.png) bottom left;
    border-top: 1px solid #eee;
    border-left: 1px solid #eee;
}

However, my issue arises when border-right: 1px solid #eee; places a line above the image, which is not the desired effect.

This setup must be adaptable. While this image serves as an example, it conveys the overall concept effectively.

Answer №1

Is this what you're looking for?

http://jsfiddle.net/6Ufb5/

div {
    border: 1px solid #ccc;
    position: relative;
}
img {
    position: absolute;
    top: 10px;
    left: 10px;
}

To achieve the desired layout, set the container to have a relative position and the image to be absolutely positioned with a 10px shift to the left and 10px down from the top. For responsiveness, consider using percentage widths for the container and/or image. You can see an example here:

http://jsfiddle.net/6Ufb5/2/

Answer №2

To achieve this effect, utilize absolute positioning for the image element within an <img> tag instead of using it as a background image. This way, the image will not overlap the parent border. Even if adjustments are made to the background-position property, the border will still appear on top of the background image - which is the expected behavior.

<div class="box">
    Content goes here
    <img src="http://placehold.it/300x200" />
</div>

Here's the CSS:

.box {
    position: relative;
    border: 1px solid #333;
}
.box img {
    position: absolute;
    bottom: -1px;
    right: -1px;
}

If you require a dynamic or responsive solution, JavaScript might be necessary. For example, resizing the image based on the box dimensions and setting a height for the box considering the image height (since the image is absolutely positioned and removed from the document flow).

Check out the fiddle for a live demo: http://jsfiddle.net/teddyrised/xH6UV/

Answer №3

If you're looking to make your markup more accessible, consider using an actual image instead of a background. This approach is also responsive, though adjustments may be needed for smaller screen sizes using media queries.

Check out this jsFiddle example

.box {
    border: 1px solid #ccc;
    display: inline-block;
    padding: 10px 0 10px 10px;
    width: 40%;
}
.box img {
    margin-right: -10%;
    margin-bottom: -10%;
    width: 105%;
}

<div class="box">
    <img src="http://placehold.it/200x100/f3f3f3" />
</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 right height and width to use in CSS?

I find it challenging to decide when to use rem, em, px, or % in web design. Although I know the definitions of each unit, I struggle with knowing the appropriate situations to utilize them. What guidelines should I follow to determine which one to use? ...

Maintain the div height as the image loads

Is there a way to prevent the collapse of the .img-container while an image is loading? Currently, when the image takes a few seconds to load, the container collapses and only expands to full height once the image has loaded. I would like the container to ...

Having trouble tackling whitespace removal in the DIV? Usual methods seem ineffective?

I am currently working on a vertical-align "3 block" design and trying to eliminate the white space between the divs. I have experimented with setting the font-size of the body to 0px and then assigning individual font sizes to each child, but this approac ...

utilizing symbols from a vector graphic icon库

There are countless icon images to be found on the internet. Recently, I came across this particular set and now I'm stumped on how to actually utilize them. Despite my efforts to find tutorials online, I have come up short in finding any helpful reso ...

How come the window size appears larger than the actual image when using OpenCV to display an image?

Hello, I am just beginning to explore OpenCV version 2.4.2, and I have created a basic program to display an image. The image that I am working with is displayed below: Here is the code I used: include "highgui.h" int main(int argc, char** argv ...

Remove the presence of a black square when selecting elements using CSS

Update: After some investigation, I discovered that the mysterious black square is actually a part of the scroll bar. By adding the following code snippet: select::-webkit-scrollbar { display: none; } The square no longer appears. Initially, my se ...

An HTML table featuring rows and columns that can be adjusted in size separately from the content within each cell

Looking to create an HTML table where the columns and rows can be sized independently of the cell content? If a row or column isn't large enough to display all the content, it should simply overflow behind the cell. A solution was found that worked in ...

aligning two elements within a container to have equal heights

I need to position 2 divs inside a container div. Their height should always be the same, regardless of content. To achieve this, I am using absolute positioning with top and bottom set to 0. Currently, the container div #three collapses and hides the cont ...

When using Vue, the image element may not display the value stored in the object property

Excuse me for being new to Vue and struggling a bit. I'm attempting to display an image using a src that comes from an object property, which is retrieved from an array returned by data() I am not receiving any errors, but the icon is not appearing. ...

Display a <div> element styled using CSS3

Question: I am facing a challenge with CSS3 styling. I have one class and one div. The class is currently set to display:none, but I want it to show when the mouse hovers over it (display:block). I have attempted the following: .1:hover + div#2 { dis ...

Error: The property 'classList' of null is unreachable for HTMLImageElement

I'm facing an issue on my website that's causing the following error message to pop up: Uncaught TypeError: Cannot read property 'classList' of null at HTMLImageElement. Here's the code I've been using: https://jsfiddle. ...

The EJS is causing a problem with linking the style sheet

Currently, I am in the process of familiarizing myself with ejs, express, and node js. Facing an issue while trying to link my style sheet to my header, below is the code snippet and here is a https://i.stack.imgur.com/0BEIn.png. Utilizing include for bo ...

Is there a way to modify or alter the layout specifically for mobile devices?

Currently, my desktop version displays the image first followed by content (h1, p, button), and then repeats in that order. However, I would like to restructure the HTML for the mobile version to make it more user-friendly. How can I achieve this without d ...

Position the div alignment to the top within the table cell

I'm having trouble aligning the div under the header to the right. Here is my HTML code and a photo for reference: HTML Code: <td> <div class="schedule-content"> <div class="timestamp& ...

unable to format radio button list

Having trouble aligning the radiobutton list to the left of the label above. Here is the code snippet: <div style="border-radius: 10px;"> <div style="margin-bottom: 10px"></div> <asp:Panel ID="panel" runat="server"&g ...

What is the reason behind the lack of collapsing in an inline-block container that only contains floated items?

Within this design, there are 3 boxes arranged in a row using the float: left; property for each one. These boxes are contained within 2 other elements. Typically, these containers collapse due to the content being comprised of floated items. However, chan ...

Ways to navigate through a webpage without encountering any overflow issues

My window is too small to scroll, but I still need the ability to do so. Is it possible to scroll even when the height of the container is not large enough to display the scrollbar? Below is the code I am using to achieve scrolling: setTimeout(function() ...

The latest version of npm Boostrap (4.1.0) is missing some important CSS properties for the `background` tag

I am currently working on an Angular 5.2.10 project that utilizes angular-cli and bootstrap version: 4.0.0-beta.2 with a specific css class called .en-icon-24: .en-icon-24 { background: url(../../../../assets/img/icons.png) -370px 0px; width: 24 ...

Creating a container that matches the dimensions of its neighboring container without using relative or absolute positioning

I am seeking a solution to make the ".on-the-left" container have the same size as the ".on-the-right" container, but without relying on relative or absolute positioning. Here is the visual outcome I am aiming for: https://jsfiddle.net/NicoZZZ/osbL16z9/28 ...

Can a website be designed to mimic the style of the current operating system?

Is there a way to apply CSS styling to web site elements, such as buttons, that mimics the appearance of operating system (Windows, macOS, Linux) button styles? This would allow HTML buttons to automatically adjust their CSS style whenever I switch theme ...