Repeating pattern on a fluid-width SVG shape? - html

I'm trying to recreate this layout in HTML/CSS (cross-browser compatible back to IE9):
Basically, it's a diagonal line spanning the width of the viewport that divides two background patterns. It will have a fixed height, but it should stretch dynamically to the full width of its container.
I couldn't think of a way to achieve this using purely CSS, so my next thought was to use SVG. This isn't very difficult to do with solid colors:
http://codepen.io/troywarr/pen/EPXVRV?editors=110
But, I'm stumped on how to apply a repeating background pattern to the SVG shape. It will need to line up with the background pattern in the <section>s above and below, and the background fill shouldn't scale with the dimensions of the shape, or it will appear distorted.
Applying some background images in CSS, I'm getting closer:
http://codepen.io/troywarr/pen/OMgyoE?editors=110
I just need the dark background pattern in the filled portion of the SVG.
Using a more visible image as a test, I'm able to stretch it to the dimensions of my <polyline>:
http://codepen.io/troywarr/pen/BjZoEq?editors=110
But, there's that stretching that I don't want. I need to tile my pattern, or at least not distort its native dimensions (so I can use a large swatch of it, if needed), even if the shape itself has a fluid width. I've tried several different combinations of attribute values for the <pattern> element, but I've yet to find anything that works as intended, even following some guidance from related answers:
Fill SVG path element with a background-image
Add a background image (.png) to a SVG circle shape
Fill SVG path element with a background image without tiling or scaling
Any suggestions? I'd love to hear any ideas for non-SVG approaches as well. Thanks!
UPDATE:
Sorry, I just realized that the background patterns in my CodePen examples weren't working. I've updated them with working image URLs.

Considering you need :
the slant width to be relative to viewport
a fixed height for the shape
There is a simple CSS approach you can use with border and viewport units :
body,html{padding:0;margin:0;}
div{
border-bottom:50px solid rgba(0,0,0,.8);
border-top: 50px solid transparent;
background-image:url('http://i.imgur.com/iUhGezx.png');
}
div:before{
content:'';
display:block;
border-right:100vw solid rgba(0,0,0,.8);
border-top:50px solid transparent;
}
<div></div>
The pattern is repeated with the background-image property. The slant is made with the borders on the pseudo element.
The borders on the parent div are there just to make the top and bottom space around the slant.
Viewport related units (vw) are supported by IE9 and over (see canIuse for more info).
Update :
If you need to have a seperate background-image for both areas, there are 2 possible CSS approaches :
With transforms: supported by IE9 and over with vendor prefixes. The slant always has the same angle.
body,
html {
padding: 0;
margin: 0;
overflow:hideden;
}
div{
width:100%;height:150px;
position:absolute;
}
.top {
padding-top:50px;
background: #fff url('http://i.imgur.com/dzFT6wB.png');
}
.bot {
transform-origin:100% 0;
transform:rotate(-5deg);
overflow:hidden;
top:50px; right:0;
width:110%;
}
.bot:after{
content:'';
position:absolute;
right:0;top:0;
width:100%;height:100%;
transform-origin:inherit;
transform:rotate(5deg) translatez(0px);
background: #262729 url('http://i.imgur.com/LxTJ685.png');
}
<div class="top"></div>
<div class="bot"></div>
With the clip-path property: although it has low browser support, it will allow you to control the slant angle better :
body,html{padding:0;margin:0;}
div{
position:relative;
height:150px;
}
div:before, div:after{
content:'';
width:100%; height:100%;
position:absolute;
}
div:before{
background: url('http://i.imgur.com/dzFT6wB.png');
}
div:after{
-webkit-clip-path:polygon(0% 60%, 100% 40%, 100% 100%, 0% 100%);
clip-path:polygon(0% 60%, 100% 40%, 100% 100%, 0% 100%);
background:#262729 url('http://i.imgur.com/LxTJ685.png');
}
<div></div>

This is one way to do it with just CSS. You can use the after pseudo-elements to help you get those angles and just a single png for the diagonal(or an element rotated if you want to avoid the element) and another for the repeating pattern. Since I didn't have your images I just made some real fast, but this should more or less be what you want.
html { height:100%; }
body {
padding:0;
margin:0;
height:100%;
background:#333;
}
.demo {
width:100%;
height:auto;
position:absolute;
}
body:after {
position:absolute;
content:'';
top:0;
bottom:0;
right:0;
left:0;
pointer-events:none;
background: transparent url(http://i.imgur.com/iUhGezx.png) repeat top left;
}
.demo .top {
min-height:100px;
background:#FFF;
position:relative;
}
.demo .bottom { min-height:60px; }
.demo .top:after {
position:absolute;
content:'';
top:100%;
left:0;
right:0;
height:100px;
background: transparent url(http://i.imgur.com/iEubBd5.png) repeat top left;
background-size: 100% 100px;
}
<div class="demo">
<div class="top"></div>
<div class="bottom"></div>
</div>

This is not currently possible with SVG. If you are stretching the SVG with preserveAspectRatio="none" then the entire contents of the SVG are affected. There is no way for some of the contents to opt out of the stretch transform.
In the future, there may be a way to do this in SVG2, but not with the current version of SVG.
Update
If you can live with it not working in IE, you can use a mask or a clip-path to achieve what you want. Below is an example of using clip-path.
body {
margin: 0;
}
section {
height: 50px;
}
main {
background-image: url('http://mass-relevance-all-access.massrel.io/template-static-2e84fe3d1c7dc87710f58b990263ad6c29dacafc/img/bg-pattern-light.png');
}
.dark {
background: #262729 url('http://mass-relevance-all-access.massrel.io/template-static-2e84fe3d1c7dc87710f58b990263ad6c29dacafc/img/bg-pattern-dark.png');
}
.diagonal {
background: url(http://www.boogdesign.com/examples/svg/daisy-grass-repeating-background.jpg);
-webkit-clip-path: url(#diagonalclip);
clip-path: url(#diagonalclip);
}
<svg width="0" height="0">
<defs>
<clipPath id="diagonalclip" clipPathUnits="objectBoundingBox">
<polyline points="0,1 1,0 1,1 0,1"/>
</clipPath>
</defs>
</svg>
<main>
<section class="light"></section>
<section class="diagonal"></section>
<section class="dark"></section>
</main>

Related

Set background-size: contain on an overlay

I'm trying to apply an overlay on top of a background-image, where the image has an attribute background-size: contain. For the overlay that I want to apply on top of it, it doesn't have a background image. All it has is a black background color with an opacity:
.overlay {
background-color: black;
background-size: contain; /* this isn't working */
opacity: 0.5;
}
The image that I want to overlay is 600x600. I want the same to be applied on the overlay too, but I can't because it's not an image; it's simply a black background. How can I get the same square dimensions for the overlay without setting a hard-coded width and without using background-size: contain?
if you want an overlaying transparent div check this out. I usually do it this way. i have a container div containing my background image and black colour div with required opacity to 0.5 (or any other value).
working fiddle https://jsfiddle.net/am2hyf3d/
set the containers width and height to a fixed value
#container{
height:300px;
width:300px;
}
i set the width and height of both the divs inside the container to occupy the containers entire space
#overlay{
height:100%;
width:100%;
background-color:rgba(0,0,0,0.5);
}
If you want simply for the image to come out darker than it really is, you could put it in a container with a black background and give the image itself a 50% opacity.
By making the container an inline-block, it will shape itself to the size of the image inside, so you won't have to specify an explicit size.
.container {
background:black;
display:inline-block;
}
.container img {
opacity:.5;
vertical-align:top;
}
<div class="container">
<img src="https://i.stack.imgur.com/30Bby.jpg">
</div>
For reference, the picture itself looks like this:
a few ways to darken a background-image without an extra element:
div {/* commun css for demo divs */
width:180px;
height:180px;
margin:10px;
background-image:url(http://lorempixel.com/180/180);
color:white
}
.gradient {
background:linear-gradient(rgba(0,0,0,0.5),rgba(0,0,0,0.5)),url(http://lorempixel.com/180/180);
}
.shadow {
box-shadow:inset 0 0 0 180px rgba(0,0,0,0.5);
}
.blend {
background-color:rgba(0,0,0,0.5);
background-blend-mode:multiply;
}
/* extra for layout */
div {
display:inline-flex;
vertical-align:top;
align-items:center;
justify-content:center;
text-align:center;
font-size:2em;
}
<div class="gradient">darken gradient</div>
<div class="shadow">darken shadow</div>
<div>plain normal</div>
<div class="blend">darken blend-mode</div>

How to responsive create circle div with center image/text

I am trying to create responsive circle which fit on every screen size like this:
I tried some codes from but anyone not work properly according to requirement.
You should do it with SVG or 2x res PNG. It will be approximately the same size regarding bandwith but you'll get a better control and much faster render.
Try something like this:
<div class="container">
<div class="circle">
<img class="image" src="http://lorempixel.com/800/800/">
</div>
</div>
.container {
width: 100%;
background: #000;
}
.circle {
border-radius: 50%;
width: 100%;
padding-bottom: 100%;
background: #fff;
position:relative;
overflow:hidden;
z-index:2;
}
.image {
z-index:1;
position:absolute;
top:0;
left:0;
width:auto;
max-width:100%;
height:auto;
max-height:100%;
}
The circle should fit the container..
see on fiddle: http://jsfiddle.net/jimmynewbs/doan8b2f/
You can then create a div inside this for the text / image and set the image to a maximum width of 100% and width auto. this will make sure it doesn't get bigger than the circle. Positioning the image absolute can help keep it within the circle too if you wanted to make it expand out to the edges...

Is it possible to make certain parts of an image transparent in HTML5/CSS

I want to have an image on my page that has certain parts that are transparent, but not all of it. Is it possible to make just certain parts of an image/div transparent? For example, just a circle in the bottom right corner, or the top right portion?
In this wonderful future world of 2022, you can now use mask-image to achieve this type of effect:
img {
mask-image: linear-gradient(to left, transparent 5%, black);
}
https://codepen.io/reynoldsalec/pen/OJOwZmV
Note that, although mask-image should be supported by all modern browsers, I've noticed that sometimes it needs to be prefixed (ex: -webkit-mask-image).
DEMO
Check this Demo, you can do by adding a span tag and give absolute position add opacity. and also you can increase the opacity.
Hope this is the one you are looking for. :)
html :
<div class="imgWrap">
<img src="https://www.google.co.in/images/srpr/logo11w.png" />
<span class="tranparentClass"></span>
</div>
CSS:
.imgWrap img{
width:80%;
height:80%;
position:relative;
border:1px solid #900;
}
.tranparentClass {
opacity:.5;
border:1px solid #f00;
border-radius : 50%;
display:block;
padding:55px;
position:absolute;
top:0;
left:0;
background:#fff;
}
Here is another CSS option. It simulates a transparent area within an image by sharing a fixed background with background-size:cover on both the background and the circle. This technique also creates interesting effects when used for other html elements that can have a background like divs, headers, paragraphs...
JSFiddle
Main CSS
.main-background, .circle-div {
background-image:url(http://i.imgur.com/1aAo20a.jpg);
background-repeat:no-repeat;
background-position:center top;
background-attachment:fixed;
background-size:cover;
-webkit-background-size:cover;
-moz-background-size:cover;
-o-background-size:cover;
}
HTML
<div class="main-background">
<div class="demo-holder">
<img class="img-size" src="http://i.imgur.com/OaE8VAj.jpg" />
<div class="circle-div">Transparent<br />Effect</div>
</div>
</div>
Another suggestion would be to use the inline image as a background on "demo-holder".

How do I create this transparent image overlay on hover with CSS?

I'm looking for advice to reproduce (see this image) effect for my image hovers. My problem is that my images are fluid, and I haven't really been able to find any good tutorials on that subject combined with overlays.
I'm assuming I have to create a transparent png (white area + circle) which overlays the image on hover, and then the text overlaying that? And it all needs to resize accordingly with the image itself.
Also, the top border is not part of the image, it's generated with CSS, and I don't want that to be overlayed if possible.
Could anyone kindly point me in the right direction, or give advice if there's a better implementation? I'm rather lost.
Thank you in advance. :)
If the image is going to be contained in a div with a defined width, you can add an absolutely positioned div to that containing div that'll act as the overlay.
Assuming this snippet and that the opacity of the overlay is set to zero
<div class="picholder">
<img class="fancypics" src=http://placehold.it/500x650></img>
<div class="overlay"><p class="text_box">Hello World!</p></div>
</div>
the css for the hover effect would be
.picholder:hover .overlay{opacity:1;}
.picholder:hover .fancypics{opacity:0.7;}
That should create the hover effect, I believe you're going for. The following css should center the overlay and some other stuff. see here for more on centering divs vertically and horizontally
.overlay {
bottom: 0;left: 0; top: 0; right: 0;
height: 150px;
width: 150px;
margin: auto;
position: absolute;
background-color:#3f3f3f;
border-radius: 50%;
opacity:0;
}
.fancypics{width:100%;}
.text_box{
color:white;
weight:bold;
font-size:2em;
padding:10px;
padding-bottom:50%;
text-align:center;
}
and of course the fiddle
Just use background-color to set a transparent color:
Demo here
HTML
<div class="overlay">
<div>Hello</div>
<span>January 16. 2014</span>
</div>
CSS
.overlay {
position:relative;
width:200px;
height:200px;
border-radius:50%;
}
.overlay:hover {
background:rgba(0,0,0,0.5);
}
.overlay > div {
position:absolute;
color:#fff;
font:50px sans-serif;
width:100%;
top:33%;
text-align:center;
}
.overlay > span {
position:absolute;
color:#fff;
font:12px sans-serif;
width:100%;
top:67%;
text-align:center;
}
The stippled line at the border of the upper text can be achieved using either a border-bottom or a single-line image which you attach as background to the div.
Hope this helps.

modify & design croped image

I cropped an image in html & css . When i am coding a <span> tag the cropped image displayed. But I need to know how can I modify it.
I have the following code:
<style type="text/css">
.design {
padding-left:25px;
background:url('Flings.png') no-repeat top left;
display: inline-block;
height: 17px;
width: 0px;
margin-left: 550px;
}
</style>
<div style="height: 200px;">
<span class="design" style='font-size: 40px;'></span>
</div>
When I am using the span tag, the cropped image displayed. But I want to modify it.
Example:
<span class="desgin" style='color: red;'></span></h3>
I want to color the image itself and change it's size and I am little stuck here.
Hope you understood me well, I will be glad for any help.
Thanks!
So you want to scale and then colorise the image? You can scale the image using background-size but this isn't very well supported. CSS3 filters unfortunately don't have a colorize filter also.
You should do this using an <img> so scaling works without background-size and then use another transparent element on top of the image to provide the tint effect. Unfortunately <img> tags don't support pseudo-elements so need to use a wrapper.
jsFiddle
HTML
<div class="red-tint">
<img src="https://www.google.com.au/images/srpr/logo4w.png" />
</div>
CSS
img {
/* scale the image */
width:200px;
height:auto;
}
.red-tint {
position:relative;
display:inline-block;
}
.red-tint:after {
background: rgba(255, 0, 0, 0.5);
display:block;
content:"";
position:absolute;
left:0;
right:0;
top:0;
bottom:0;
z-index:1;
}
Update
Ah you want to crop, then that's just a matter of using background-position. You will need to give negative left and top positions to background-position which represent the offsets from the top-left corner of the image. For example, this will draw a 200x100 chunk of the image which is 100px in from the left side of the image and 20 px down from the top.
jsFiddle
.design {
width:200px;
height:100px;
background:url(https://www.google.com.au/images/srpr/logo4w.png) no-repeat;
background-position:-100px -20px;
}