Set background-size: contain on an overlay - html

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>

Related

Repeating pattern on a fluid-width SVG shape?

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>

Scale Image to Div With Inherited Height

I want to fit a png image to the height of a div that is inheriting its height from another div. I have seen how this can be done by setting the image's max-height to 100% (in questions such as this: How do I auto-resize an image to fit a div container), but this only works for me when the image is directly in the div thats height is specified in pixels.
I have a topbar, whose height I set explicitly, and a logodiv inside that, which inherits the height from the topbar. However, the logo does not resize to fit the height of logodiv unless I explicitly set the height (the commented code).
It seems like bad coding to have to set the height twice, when it should be inherited. Is there any way to fit the image to the correct height without doing this?
css:
#topbar{
width:100%;
height:45px;
}
#logodiv{
float:left;
/* height:45px; */
}
#logodiv img{
max-height:100%;
}
html:
<div id="topbar">
<div id="logodiv">
<img src="images/project/logo.png" />
</div>
</div>
I want to fit a png image to the height of a div that is inheriting
its height from another div.
Technically, logodiv is not inheriting its height from topbar. Its simply expanding itself according to its content(the image in this case).
Try adding the property height:inherit; to second div and you are good to go.
HTML
<div id="topbar">
<div id="logodiv">
<img src="images/project/logo.png" />
</div>
</div>
CSS
#topbar{
width:100%;
height:45px;
}
#logodiv{
float:left;
height:inherit;
/* height:45px; */
}
#logodiv img{
max-height:100%;
}
Fiddle
Try this css:
#topbar {
width:100%;
height:45px;
border:1px solid red;/* for highlighting*/
}
#logodiv {
float:left;
height:inherit;
}
/*To clear float */
#topbar:after {
clear:both;
display:block;
content:""
}
#logodiv img {
max-height:100%;
}
Demo : http://jsfiddle.net/lotusgodkk/4d4L1g0a/

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;
}

Centering background color via css

I have a centered image in the background, but I need my main background-color to stretch as much as the image and stay at the center.
Here is the css for body background
body{padding:0; margin:0; background:url(/b.jpg) center 10px no-repeat;}
Here is the css for main background
.main{background:#4e4645;}
Ive tried background-position and margin percents with no luck.
You can use margin: 0 auto to center your .main div within the body. Then all you need to do is set the width of .main to be equal to the width of your body's background image.
.main {
margin: 0 auto;
width: 300px;
background: #4e4645;
}
See DEMO.
Is this what you're trying to achieve?
http://jsfiddle.net/aX24s/1/
In the example I'm using two background colours, obviously substitute an image in. I've put 50% opacity on the inner div to show that they're both exactly matched in size (one block is bright green, one is red, so the colours mix).
You have the two backgrounds the same size and overlaid exactly.
HTML:
<body>
<div class="outer_div">
<div class="inner_div">
</div>
</div>
</body>
CSS:
.outer_div {
width:50px;
margin:0 auto;
height:50px;
background-color:#dd0000;
}
.inner_div {
background:#00dd00;
width:100%;
opacity:0.5;
height:100%;
}