Curved border with stroke in pure CSS? - css

I'm wondering if it is at all possible to achieve a curved border (with a stroke) using only CSS? At the moment I'm creating curved borders for the header of my website using images:
I'd like to change this to a CSS solution so that I'm not having to alter images when the amount of content within changes - I need these to be dynamic and responsive, I've managed to draw a curve using border-radius:
This works much better for me, but I'm wondering if it is possible to add a stroke to it to make it look a more like the image representation? Any help is greatly appreciated. Here's the code I've written to achieve this:
<div class="slider">
<div class="slide">
<!-- Content -->
</div>
</div>
CSS:
.slider {
background: #21639e;
}
.slider .slide {
background: url("image.jpg") no-repeat #21639e;
border-radius: 100%/0 0 30px 30px;
}
I did try adding border-bottom: 5px solid #fff; to the .slide class, but it ended up looking like this:
I've created a jsfiddle for you to test what I'm trying to achieve.

Yes, you can try and use box shadows to create this kind of border. Applying a white box shadow on the outer side of the element will make it look like a stroke/border.
This - border-bottom: 5px solid #fff; produces a different kind of effect because we are applying only the bottom border to the element. The borders on the left and right are non existent (zero width) and so the line thins out as you go nearer to the edges.
.slider {
height: 500px;
background: #21639e;
}
.slider .slide {
height: 200px;
background: url("http://placehold.it/800x800/FF00FF") no-repeat #21639e;
border-radius: 100%/0 0 30px 30px;
box-shadow: 0px 6px 0px white;
}
<div class="slider">
<div class="slide">
Some content
</div>
</div>
Below is an updated version of your Fiddle.
For a more graceful looking curve then you can also try the below approach. It uses a pseudo element which is wider than the .slide and then centers the image within it. (I feel that this approach makes it look closer to the original image but the choice is yours)
.slider {
height: 500px;
background: #21639e;
}
.slider .slide {
position: relative;
height: 200px;
width: 100%;
overflow: hidden;
}
.slider .slide:before {
position: absolute;
content: '';
left: -2%;
top: -6px;
width: 104%;
height: 100%;
background: url("http://placehold.it/800x800/FF00FF") no-repeat center center #21639e;
border-radius: 100%/0 0 30px 30px;
box-shadow: 0px 6px 0px white;
}
<div class="slider">
<div class="slide">
Some content
</div>
</div>

Related

Is there a way to reduce the radius of an image?

I've got an image, with properties defined as follows:
.icon {
background-color: white;
border-radius: 50%;
width: 50px;
height: 50px;
}
Due to the border-radius, the image is in a circle. Is there a way to tighten this circle by some number of pixels, such that some "outer layers" of the circle are shaved off, without scaling the image down with it?
A combination of background-size and background-position properties allow you to resize an image as a background relative to the element it is a background of. Sorry if that is a mouthful, in other words, if this is your initial approach:
.icon {
background-color: white;
border-radius: 50%;
width: 50px;
height: 50px;
}
<img src="https://barkpost-assets.s3.amazonaws.com/wp-content/uploads/2013/11/grumpy-dog-11.jpg" class="icon" />
Instead if the image is applied as a background image you can control the size relative to the element:
.icon {
background-color: white;
background-image: url('https://barkpost-assets.s3.amazonaws.com/wp-content/uploads/2013/11/grumpy-dog-11.jpg');
background-position: center center; /* two values for horizontal and vertical positioning, you can use px or other units to configure distance too. */
background-size: 180% 180%; /* also two values for height and width, here I am using greater than 100% to make the image larger than the element, achiving the effect you are looking for */
border-radius: 50%;
width: 50px;
height: 50px;
}
<div class="icon" role="img" aria-label="this puppy looks a little closer, right?"></div>
I can think of a few different ways to do this, depending on how "clean" you need this to be. Read the comments in-line for a description of what is going on.
/* Your code, as is: */
img {
background-color: white;
border-radius: 50%;
width: 150px;
height: 150px;
}
/* Using clip-path (not supported in IE/Edge): */
img.clipped {
clip-path: circle(28.6% at 50% 50%);
}
/* Using a background image: */
span.image {
width: 150px;
height: 150px;
display: inline-block;
background: url(https://via.placeholder.com/150);
border-radius: 50%;
box-shadow: inset 0px 0px 0px 25px #fff;
}
/* Using a span as a 'wrapper': */
span.image_wrapper {
width: 150px;
height: 150px;
display: inline-block;
border-radius: 50%;
box-shadow: inset 0px 0px 0px 25px #fff;
}
<!-- Your code, as is: -->
<img src="https://via.placeholder.com/150" />
<!-- Using clip-path (not supported in IE/Edge) -->
<img src="https://via.placeholder.com/150" class="clipped" />
<!-- Using a background image: -->
<span class="image"></span>
<!-- Using a span as a 'wrapper' -->
<span class="image_wrapper">
<img src="https://via.placeholder.com/150" class="clipped" />
</span>
For clip-path see: https://caniuse.com/#search=css%20clip

how to achieve following overlay while keeping it responsive

I am trying to achieve white overlay that can be seen above. image behind it is a slideshow and I need to show little bit of it with that triangle cut out in the middle, I'm trying to figure out a way of how to achieve this with pseudo elements while keeping solution responsive at the same time, but cant seem to find a way. I was also thinking about using multiple backgrounds, but am not sure how to make one of them in the centre and other two on the sides.
<div id="slideshow"></div>
Above is markup for slideshow at the moment (slides appear as background images of this div, it is absolutely positioned).
Utilizing some pseudo class before and afters on the center container, you can create CSS triangles using CSS borders.
See this for CSS triangles : https://css-tricks.com/snippets/css/css-triangle/
Technically you wouldn't need to use the before/after for the containers but I used it here anyways to keep the mark-up clean.
Next using some positioning and some width calculations, I set the two containers (left and right) to width 40% which leaves me 20% to play around with for the center section.
Lastly using, vw units, I set the border-left and border-right sizes to be 10vw. This is important because it basically allows the borders to be responsive based on the viewport width as long as the slideshow is full width.
See the JSFiddle here (Updated) : https://jsfiddle.net/x117ss0q/4/
<div id="slideshow">
<div class="slideshow-overlay-wrapper">
<div class="slideshow-overlay left"></div>
<div class="slideshow-overlay-center"></div>
<div class="slideshow-overlay right"></div>
</div>
</div>
#slideshow{
background-color: #333;
position: relative;
height: 200px;
}
.slideshow-overlay-wrapper {
bottom: 0;
display: table;
left: 0;
position: absolute;
width: 100%;
table-layout: fixed;
}
.slideshow-overlay {
background-color: #fff;
display: table-cell;
height: 50px;
}
.slideshow-overlay-center {
display: table-cell;
height: 50px;
width: 200px;
position: relative;
}
.slideshow-overlay-center:after {
content: '';
left: 0;
position: absolute;
border-bottom: 0px solid transparent;
border-top: 50px solid transparent;
border-left: 100px solid #FFF;
width: 0;
height: 0;
}
.slideshow-overlay-center:before {
content: '';
right: 0;
position: absolute;
border-bottom: 0px solid transparent;
border-top: 50px solid transparent;
border-right: 100px solid #FFF;
width: 0;
height: 0;
}

Rectangle image in transparent circle frame

I have just started working with CSS.
I have a rectangle image. I want to put it on a background and view it as a circle with light transparency as the example.
Is this what you looking for?
When you apply: border-radius: 50%; to your img it gets a circle as you you want.
.bg {
background-color: mediumaquamarine;
width: 500px;
height: 500px;
margin: auto
}
img {
border-radius: 50%;
/* Safari 3-4, iOS 1-3.2, Android 1.6- */
-webkit-border-radius: 50%;
/* Firefox 1-3.6 */
-moz-border-radius: 50%;
border: 1px solid red;
margin-left: 25%;
margin-top: 25%;
z-index: 1;
position: relative;
opacity: 0.8
}
<div class="bg">
<img src="http://placehold.it/250x250&text=Image" />
</div>
here's a rough demo of how to do it:
http://jsfiddle.net/jalbertbowdenii/vfac6L4x/
using your pix, just simply add the correct url for the img element and change the backgroudn color of the mask container div as well as the border color of the image.
if you want more info, search for css masks
because stackoverflow requires this:
.mask{background-color:#000}
img{display:block; margin-left:auto; margin-right:auto;
border-radius:25px; border:solid #000}
and the markup
<div class="mask">
<img src="https://photos-6.dropbox.com/t/1/AAASULb1odiWJlk3dyEG-rF4B0baCCQ2D9aoTqXZiYZW6w/12/107220852/jpeg/1024x768/3/1416250800/0/2/trans-cirecle.jpg/VFul9uUE7QKOIrYKVNy58z9JzoOHj9UK3AGRUsSFbgY" />
</div>
Add border-radius:50% to the image; img{}
Change values of border-radius to various pixel values and percentage values to get more effects.

CSS Dual Image Border

I am trying to replicate the following image border using only CSS.
http://fish.websitedesignsflorida.com/wp-content/uploads/2014/05/demoSlide1.png
So far I am only able to replicate it using an image border. Is there was a way to do this strictly in CSS?
The two effects you need are multiple borders and rounded corners.
You can simulate as many borders as you like using box-shadow, which is well supported today. Each box shadow overlaps, so to get more box shadows make them bigger and bigger. To get the rounded corners just use border-radius.
http://caniuse.com/css-boxshadow
html
<img>
css
img {
box-shadow:
0 0 0 10px #921808,
0 0 0 20px #163459;
border-radius: 20px;
}
You can also use padding and a background color to get an extra border. Note this will only appear to be a border because the image is covering up the inside of the shape.
html
<img>
css
img {
padding: 5px;
background: #921808;
border: 5px solid #163459;
}
There are even more ways to do this with CSS, check out this CSS-Tricks Link:
http://css-tricks.com/snippets/css/rounded-corners/
If your image position is fixed then you can use the property position: absolute for the borders of your image. Try this:
<html>
<head>
<style>
.d1
{
width: 200px;
height: 300px;
border: 10px solid #ff0000;
border-radius: 6px;
position: absolute;
top: 0px;
left: 0px;
}
.d2
{
width: 200px;
height: 300px;
border: 10px solid #000000;
border-radius: 6px;
position: absolute;
top: 5px;
left: 5px;
}
.i
{
width:200px;
height: 300px;
}
</style>
</head>
<body>
<img src="first.png" class="i">
<div class="d1"></div><div class="d2"></div></body>
</html>
Hope this helps

Why doesn't inset box-shadow work over images?

I have a container that uses inset box shadow. The container contains images and text. The inset shadow apparently does not work on images:
The white section here is the container. It contains a white image, and there is inset box shadow applied to it.
body {
background-color: #000000;
}
main {
position: absolute;
bottom: 0;
right: 0;
width: 90%;
height: 90%;
background-color: #FFFFFF;
box-shadow: inset 3px 3px 10px 0 #000000;
}
<main>
<img src="https://upload.wikimedia.org/wikipedia/commons/d/d2/Solid_white.png">
</main>
Is there a way to make the inset box shadow overlap images?
Just to chime in on this, because I was just creating something similar...
I hate polluting my markup with extra elements for the sake of styling, so the CSS solution is to use the :after pseudo element:
main::after {
box-shadow: inset 3px 3px 10px 0 #000000;
content: '';
display: block;
height: 100%;
position: absolute;
top: 0;
width: 100%;
}
<main>
<img src="https://upload.wikimedia.org/wikipedia/commons/d/d2/Solid_white.png">
</main>
It's probably too late for what you were trying to do, but is the better solution in my estimation.
Because the shadow is part of the parent container it renders below the image. One alternative is to have a div which places a shadow overtop the image like so:
body {
background-color: #BBB;
}
main {
position: absolute;
bottom: 0;
right: 0;
width: 90%;
height: 90%;
background-color: #FFFFFF;
border-radius: 20px;
}
main img {
border-radius: 20px;
}
.shadow {
position: absolute;
width: 100%;
height: 100%;
box-shadow: inset 3px 3px 10px 0 #000000;
border-radius: 20px;
top: 0;
left: 0;
}
<main>
<img src="https://upload.wikimedia.org/wikipedia/commons/d/d2/Solid_white.png" />
<div class="shadow"></div>
</main>
Edit: I've updated the fiddle to include border radius on the shadow and on the img which solves the issue identified in the comments.
The reason it's not overlapping is because the image is inside the div, so the image is on top of it. The image is higher (closer to the user) than the div.
You can change the image to use position: relative; z-index: -1, and have the containing div use a border instead of setting background color on the body. You'll need to use box-sizing: border-box to include the border in the width of the div.
DEMO
body {
background-color: #FFF;
}
main {
position: absolute;
bottom: 0;
right: 0;
width: 100%;
height: 100%;
border: 60px solid black;
box-shadow: inset 3px 3px 10px 0 #000000;
box-sizing: border-box;
}
img {
z-index:-1;
position: relative;
}
For those, who're using absolute-positioned, full-size :before/:after pseudo elements, consider using pointer-events: none on the pseudo-element so the original elements remain clickable.
The best way to achieve this in 2020 would be to use mix blend mode on the image. use the box-shadow on the parent element of the img and use mix-blend-mode: multiply.
You could set the image as the div's background instead:
background-image:url(http://www.placehold.it/500x500)
jsFiddle example
https://stackoverflow.com/a/21415060/6235358
that's a great way to do it but we can do it in a better way using the ::after pseudo-class so you'll not have to add an empty <div> to your HTML
As Rilus mentioned we could use a pseudo class. Unfortunately this does not seem to work on an img tag for some reason however we can use a combination of inner and outer containers to achieve the affect we need.
.outer:hover .inner:after {
position: absolute;
content: '';
color: white;
display:block;
bottom: -0px;
right: -0px;
width: 100%;
height: 100%;
z-index: 11;
border: solid 10px red;
}
http://jsbin.com/kabiwidego/1/
not sure about ie 10 though as it seems to handle pseudo classes that are absolutely positioned slightly differently to most browsers.
One simple fix if you are clever with your decimals is to store your content in a separate div which you then select and implement a certain number of pixels from the top.
For example, let's say your header has a height of 50px. You could begin your #content div id 53.45px from the top (or whatever height your drop shadow is) and then your shadow would appear above the images.
One issue with this is that if you are using a rather transparent shadow, the more transarent it is the more tacky it may look by implementing this css.
In practice the code would be as follows:
HTML:
<header>
Whatever's in your header
</header>
<div id="content>
Page content
</div>
CSS:
header {
height: 50px;
box-shadow: 0 5px 5px rgba(0,0,0,1);
}
#content {
top: 55px;
}
Even if i'm late for the party, I had the same issue these days and worked on a solution. For me, the best solution (mobile friendly) is this one:
JSFiddle:
.image-inset-container {
margin-bottom: 30px;
}
.image-inset-shadow {
position: relative;
}
.image-inset-shadow img {
border-radius: 20px;
}
.image-shadow {
position: absolute;
width: 100%;
height: 100%;
box-shadow: inset 3px 3px 10px 0 #000;
border-radius: 20px;
top: 0;
left: 0;
}
<body>
<h4>Reimagined Web Design</h4>
<p>With your input and business goals in mind, we bring your brand to life through custom human-facing graphics and
visual elements targeted toward your audience for good user experience and created in future-forward technology,
guaranteeing a successful new web design.</p>
<div class="image-inset-container">
<div class="image-inset-shadow"><img src="https://upload.wikimedia.org/wikipedia/commons/d/d2/Solid_white.png" alt="img1" />
<div class="image-shadow"></div>
</div>
</div>
<p>We initiate a collaborative process where your team is involved in every step to create a frictionless and
delightful
experience for your customers. Our designers immerse themselves in your industry and your brand aesthetic to
deliver
a website that represents your business while achieving your goals for a connected future.</p>
</body>

Resources