Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Offset a border over an image

Tags:

html

css

border

I'm trying to create exactly like what is shown in the picture below. I have tried other methods using outline and offset method too but I could't figure out how to go on about doing this.

image with an offset border

Here's the JSFiddle:

img {
  border: 4px solid green;
}
<img src="https://image.ibb.co/cxwWPa/services_gas_oil.jpg" alt="services_gas_oil" border="0">

How do I do to get this offset border over the image?

like image 919
Elaine Byene Avatar asked Jul 10 '17 06:07

Elaine Byene


2 Answers

Wrap the image with an inline block, and set an absolutely positioned pseudo-element as the border:

body {
  padding: 50px 0 0 80px;
}

.imageContainer {
  display: inline-block;
  position: relative;
}

.imageContainer::before {
  position: absolute;
  top: -5%;
  left: -15%;
  width: 100%;
  height: 100%;
  border: 4px solid #77B244;
  content: '';
}
<div class="imageContainer">
  <img src="https://cdn.pixabay.com/photo/2013/07/31/12/25/cat-169095_960_720.jpg" alt="services_gas_oil" border="0">
</div>
like image 75
Ori Drori Avatar answered Oct 16 '22 05:10

Ori Drori


A simpler way would be to use a combination of border, outline and a negative outline-offset. Here is an example :

img{
  outline:4px solid #77B244;
  outline-offset:-100px;
  border:50px solid transparent;
  border-width:150px 50px 50px 150px;
  margin:-75px 0 0 -75px;
}
<img src="https://image.ibb.co/cxwWPa/services_gas_oil.jpg" alt="services_gas_oil" border="0">

This prevents the use of a parent element and a pseudo element.

like image 45
web-tiki Avatar answered Oct 16 '22 05:10

web-tiki