I'm trying to fade a div's content out at the edges using a couple of absolutely positioned overlay divs at either side. However, I need the background of the page to be viewable once the fade is finished, and throughout - effectively masking the content of one div using 2 others with linear 'fade' gradients. See the diagram below for a better explanation...
I have tried the following:
Use -webkit-mask property with a linear gradient. This works in Webkit, but nothing else. Also, the linear gradient is rather choppy and stacatto when used with the mask property. Not ideal.
Use SVG gradient mask (e.g. in the Firefox / MDN demo). Works, but only in Firefox. Not anywhere near as poor a gradient as -webkit-mask / linear-gradient in Chrome though
Using a transparent masking GIF or PNG. However, in the example I used the masking colour shows through (see this SO question).
I'm hoping there might be another way that I've not thought of, or perhaps an alternative layout I could use to achieve the same aim. Any thoughts?
I would think that the transparent PNG would be the best bet, make it absolute with a higher z index and make it inside a container div. This container div would float over the background sliding image? I would use a very small 2 px slice and just repeat it along the y axis, but I might not be seeing your problem correctly.
I tried it with a very small slice of the radiant that I repeated down (y) and the underlying image did scroll through the top transparent images. If I follow what you are trying to do. It worked in chrome, firefox and safari: here is the css
#container {
background-attachment: scroll;
background-image: url(Untitled-1.jpg);
background-repeat: repeat-x;
clear: both;
float: left;
height: 768px;
width: 80000px;
position: relative;
}
#container #info {
float: left;
width: 630px;
margin-right: 20%;
margin-left: 20%;
position: fixed;
margin-top: 100px;
height: 519px;
clear: both;
clip: rect(100px,auto,auto,auto);
}
#container #info #l_side {
background-image: url(left_.png);
background-repeat: repeat-y;
float: left;
height: 519px;
width: 165px;
position: relative;
margin-right: -2px;
}
#container #info #content {
background-color: rgba(0, 0, 255, 0.56);
color: rgba(0, 0, 255, 0.56);
float: left;
height: 519px;
width: 300px;
position: relative;
}
#container #info #r_side {
background-image: url(Right.png);
background-repeat: repeat-y;
float: left;
height: 519px;
width: 165px;
position: relative;
margin-left: -1px;
}
Related
Currently, I am trying to fit (crop) image on the mobile version of the website, but, with no results...
How it looks on a desktop -
How it looks on a mobile -
Image crops on mobile because position: absolute; margin-left: 50%;, but..., this page has a scroll to the left, like this -
I've tried object-fit, different positions, so on, and no success.
How should it be done?
No image is going to be able to cope with all random viewport aspect ratios.
In this particular design it seems important that the figure is looking at/moving towards the text on the left, so the common method of dealing with narrow portrait viewports by putting one half of the 'header' below the other won't make sense in this case.
One suggestion therefore is to separate out the figure from the background and position and size it in relation to the aspect ratio/size of the viewport. Bearing in mind that the text on the left has to have a certain minimal physical size in order to be readable, this snippet sets a minimum absolute width and also the percentage across the viewport that the figure will stand.
Obviously you will want to alter the actual numbers here to get the layout that is required for narrow as well as wide devices so treat the numbers used here as being just for the demo.
Also, the colored background could be a real image as stretching to use cover would work given its design, but in this snippet it is roughly drawn using CSS just to give the idea. The figure has been roughly cut out of the given image in the question and of course would need to be a better done for a published environment.
* {
padding: 0;
margin: 0;
}
.bg {
--minw: 256px;
/* minimum width we allow the left hand side (with the text) to go */
--textw: max(var(--minw), 50vw);
position: fixed;
top: 0;
left: 0;
height: 100vh;
width: 100vw;
display: inline-block;
}
.bg::before,
.bg::after {
display: inline-block;
position: absolute;
top: 0;
left: 0;
content: '';
background-repeat: no-repeat no-repeat;
}
.bg::before {
width: 100vw;
height: 100vh;
background-image: radial-gradient(circle, rgba(160, 32, 240, .8), transparent), linear-gradient(to right, rgba(0, 0, 0, 0), #00B7EB), linear-gradient(to right, rgba(255, 0, 255, .6), rgba(160, 32, 240, .8));
background-position: center center;
}
.bg::after {
background-image: url(https://i.stack.imgur.com/655zI.png);
background-size: contain;
--pc: 80%;
background-position: var(--pc) center;
--w: calc(100vw - var(--textw));
width: var(--w);
left: calc(100vw - var(--w));
margin: 5% 0;
height: calc(100vh - 10%);
}
.text {
height: 200px;
width: var(--textw);
position: relative;
top: 0;
left: 0;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
.innertext {
border: solid white 2px;
color: white;
padding: 5%;
}
#media screen and (max-aspect-ratio: 1 /1) {
body::after {
--pc: 90%;
}
}
<div class="bg">
<div class="text">
<div class="innertext">HERE IS SOME TEXT</div>
</div>
</div>
Additional note: background-attachment: fixed is not fully implemented on all browsers at the moment so the backgrounds are added to pseudo elements on a div which is given position fixed.
Use two different versions based upon the screen size in picture element, or use object-fit in the css for mobile media query.
You are getting a horizontal scroll bar because of the absolute position plus the margin, this would push the image to the right of the screen(assuming 100% wide image). If you wanna keep it this way, use overflow-x: hidden on the html and body tags.
I have a div with a background image. I want the image to always have at least a 1% left and bottom margin/padding. The container of the div is a dynamic absolutely positioned box which can have a size of 5% or 95% (and everything in between with CSS transition).
I chose to achieve this by putting the background-image on that div which has min-height of 5% and width of 100%. The background is not repeating, centred and set to be contained within the area (background-size: contain). I decided to go with a 1% padding and background-clip CSS property to content-box, which should mean that the background covers only the content which starts at 1% away from the border. I chose padding and not margin, because box-sizing is set to border-box, therefore a width 100% with additional padding would not increase the size of the div which is not the case with margin.
However this did not work as expected:
When using background-clip: content-box together with background-size: contain, the background is contained within the border-box and not content-box and the padding cuts away the areas between the content and border.
Example:
div {
background-size: contain;
background-repeat: no-repeat;
background-position: 50% 50%;
background-image: url(http://www.ghacks.net/wp-content/uploads/2011/04/standard-google-image-search.jpg);
float: left;
width: 200px;
height: 200px;
}
.clipped {
border: 1px solid blue;
padding: 20px;
background-clip: content-box;
}
.normal {
border: 1px solid green;
padding: 20px;
background-size: contain;
}
<div class="clipped">
</div>
<div class="normal">
</div>
So the questions are:
Is this the normal behaviour?
Where is this documented?
What would be the solution to achieve what I need?
p.s. I am not English so apologies for possible mistakes or misconceptions. Also I will try to explain better in case you did not understand the issue.
Yes, this is normal behavior. The content-box does not mean the image is sized within it, it means it gets clipped by it.
https://developer.mozilla.org/en-US/docs/Web/CSS/background-clip
In below sample I used a pseudo class to achieve it
div {
position: relative;
float: left;
width: 200px;
height: 200px;
border: 1px solid blue;
}
div::before {
content: '';
position: absolute;
left: 20px;
top: 20px;
right: 20px;
bottom: 20px;
background-size: contain;
background-repeat: no-repeat;
background-position: 50% 50%;
background-image: url(http://www.ghacks.net/wp-content/uploads/2011/04/standard-google-image-search.jpg);
}
<div>
</div>
In my css file i have one rule: two add two background images before and after text element. Before i used two images and all was ok. But now i use sprites: so i need to get area of big image and post it to element (background-position) but i have one trouble: if i set background position: i could not stuck to it position like left center and right center:
background: url(../images/png/elements.png) no-repeat -5px -152px, url(../images/png/elements.png) no-repeat -5px -104px;
how could i float first part to left and second to right of the element?
before was:
background: url(../images/png/mail.png) no-repeat left center, url(../images/png/edit.png) no-repeat right center;
is it real to do?
also: i use it with :hover
I'm afraid that it is not possible to limit the visible area of sprite images unless the size of the element itself is limited.
However, perhaps you could assign the background images to ::before and ::after pseudo-elements which are positioned to the left/right side of the parent box properly (either by float or absolute positioning).
So that you could handle the position of each icon interdependently.
For instance:
.box:before, .box:after {
content: "";
display: inline-block; /* or position these elements by floats, etc. */
width: 48px; /* for instance */
height: 48px; /* for instance */
}
.box:before {
background: url(../images/png/elements.png) no-repeat -5px -152px;
}
.box:after {
background: url(../images/png/elements.png) no-repeat -5px -104px;
}
<div class="box"></div>
The left and right you are using belong to background-position. The pixel definitions are overriding them.
You should separate the images to two different elements.
Don't use Shorthand for this (especially in the position property):
try with something like:
div {
width: 100%;
height: 190px;
border: 1px solid red;
background: url(http://alt-web.com/Images/CSSSprite.jpg), url(http://alt-web.com/Images/CSSSprite.jpg);
background-position: left top, right bottom;
background-repeat: no-repeat;
}
<div></div>
Edit: Example with pixels, zero and %.
div {
width: 100%;
height: 190px;
border: 1px solid red;
background-image: url(http://alt-web.com/Images/CSSSprite.jpg), url(http://alt-web.com/Images/CSSSprite.jpg);
background-position: 0 0, 100% -1215px;
background-repeat: no-repeat;
}
<div></div>
I am using a gradient background to display an alternating background for absolute positioned rows.
When zooming out in Chrome the layout gets messed up.
The calculation of the gradient background size seems to be different to the calculation of the top margins.
I have created a JSFiddle to illustrate the problem: http://jsfiddle.net/4y3k2/4/. When zooming out to e.g. 75% an offset appears between the foreground and background. The offset sums up more and more so that the layout looks completely broken for the last rows.
Here is my code:
#container {
position: absolute;
height: 2000px;
width: 100px;
background: linear-gradient(red 50%, green 50%, green);
background-size: 40px 40px;
}
.row {
position: absolute;
}
<div id="container">
<div class="row" style="top: 920px;"></div>
</div>
Everything works fine on IE and Firefox.
You can do this without calculating top every single time
for each row.
Instead set the parent div to be a block and use
predefined height and width for each row while floating them to the left:
#container {
position: absolute;
height: 2000px;
width: 100px;
background: linear-gradient(red 50%, green 50%, green);
background-size: 40px 40px;
display: block;
}
.row {
float: left;
width: 100px;
height: 20px;
}
http://jsfiddle.net/4y3k2/11/
I have following markup
<body>
<div class="holder">
<div class="circle"></div>
</div>
</body>
and i have applied a fixed background to body element and white background applied to class holder
body {
background: url(image.png);
background-attachment: fixed;
}
.holder {
width: 500px;
height: 500px;
background: #fff;
}
.circle {
width: 100px;
height: 100px;
border-radius: 50%;
background: rgba(0, 0, 0, 0);
}
what i have tried to do is to make the circle transparent to see the body background. Simply, what i am trying is, making the circle transparent to see the body background image while the white background around the circle still exist. please excuse my English. Guys please help me.
What you are asking to do will not work using transparency.
However, there is a work around that is quite acceptable:
body {
background: url(http://placekitten.com/g/400/500);
background-attachment: fixed;
}
.holder {
width: 500px;
height: 700px;
background: rgba(255,255,255,0.8);
border: 1px dotted blue;
}
.circle {
width: 100px;
height: 100px;
border-radius: 50%;
background: url(http://placekitten.com/g/400/500);
background-attachment: fixed;
}
see demo at: http://jsfiddle.net/audetwebdesign/FqMXz/
Just apply the same background image to the .circle div.
This trick is taken from one of the CSS books by Eric Meyer.
The 4th number in rgba() is the alpha transparency. You've set it to 0, which is completely transparent. 1 would be completely opaque. You need to set that to some value between 0 and 1.
That said, if you are trying to create the effect of a hole, then what you need to do is create a background image that is white and has a transparent circle cut in it and make that the background to .holder. It doesn't matter how transparent you make .circle if .holder is completely opaque!
may be you should try it by adding opacity: value attribute or by setting its background-color: rgba(0,0,0,value)
Value must be between 0 to 1.
I'm about to just make 5 divs with 1 in the center all inside of a parent. Parent is transparent and your circle would be too. Surrounded on all 4 sides with ::before & ::after elements that aren't transparent to tighten up the seams... hope that helps.