I'm trying to create an effect which increases the width of a border up to a certain point, using the thickness of the border to represent a progress bar, and am wondering if there's a nice way to accomplish this while reusing the colours defined in my stylesheets.
Right now I have the element with the border contained within a div, which creates the wider part of the border by using its background color. Then, the outer container's background is styled with a linear gradient, to switch from the color of the inner element's border to the color of the background, like
background: linear-gradient(to right, red 10%, blue 10%)
This works fine, but I'm trying to avoid hard-coding the colors in the background. What I want is to dynamically vary the percentage for where the colors change as a style attribute, but would prefer the colors to be defined as part as a class. So ideally something like:
opacity: linear-gradient(to right, 0 10%, opacity 1 10%)
That way the code which alters the length of the progress bar doesn't need to be aware of the colors, making it easier for the progress bar colors to be kept consistent with the rest of the website. Any other way to accomplish the same thing would work; changing the width could work, if it didn't impact the width of the child element.
Is there a nice way to do this?
It seems you are looking for the mask property. Below is a basic example that you can adjust
.box {
height: 50px;
margin: 10px;
background: linear-gradient(to right, red , blue);
-webkit-mask: linear-gradient(to right,#000 var(--o), #0000 calc(var(--o) + 10%));
}
<div class="box" style="--o: 20%"></div>
<div class="box" style="--o: 50%"></div>
<div class="box" style="--o: 80%"></div>
I have a div with the class "fullheightcolumn" with a height of 100vh and containing an img. I want the top of the page to fade to black, so I added another div with the class "fadedblack" and wrote the following css:
.fadedblack {
height: 50vh;
background: linear-gradient(to top, rgba(0,0,0,0) 0%, rgba(0,0,0,1) 90%);
z-index: 999;
position: absolute;
width: 100%;
}
This works well, but I have a hover effect on the class fullheightcolumn. When I hover over the bottom 50% of the page, the hover effect works. If I go higher, it stops because obviously I'm hovering over the fadedblack element. Is there a way to effectively "lock" the fadedblack element so it's still visible, but when I hover over it, the browser recognizes it as hovering over the element behind it? Is there another way to do this?
Thank you!
Is there a way to position a background image a certain number of pixels from the right of its element?
For example, to position something a certain number of pixels (say, 10) from the left, this is how I'd do it:
#myElement {
background-position: 10px 0;
}
I found this CSS3 feature helpful:
/* to position the element 10px from the right */
background-position: right 10px top;
As far as I know this is not supported in IE8. In latest Chrome/Firefox it works fine.
See Can I use for details on the supported browsers.
Used source: http://tanalin.com/en/blog/2011/09/css3-background-position/
Update:
This feature is now supported in all major browsers, including mobile browsers.
!! Outdated answer, since CSS3 brought this feature
Is there a way to position a background image a certain number of pixels from the right of its element?
Nope.
Popular workarounds include
setting a margin-right on the element instead
adding transparent pixels to the image itself and positioning it top right
or calculating the position using jQuery after the element's width is known.
The easiest solution is to use percentages. This isn't exactly the answer you were looking for since you asked for pixel-precision, but if you just need something to have a little padding between the right edge and the image, giving something a position of 99% usually works well enough.
Code:
/* aligns image to the vertical center and horizontal right of its container with a small amount of padding between the right edge */
div.middleleft {
background: url("/images/source.jpg") 99% center no-repeat;
}
Outdated answer: It is now implemented in major browsers, see the
other answers to this question.
CSS3 has modified the specification of background-position so that it will work with different origin point. Unfortunately, I can't find any evidence that it is implemented yet in any major browsers.
http://www.w3.org/TR/css3-background/#the-background-position
See example 12.
background-position: right 3em bottom 10px;
As proposed here, this is a pretty cross browser solution that works perfectly:
background: url('/img.png') no-repeat right center;
border-right: 10px solid transparent;
I used it since the CSS3 feature of specifying offsets proposed in the answer marked as solving the question is not supported in browsers so well yet. E.g.
The most appropriate answer is the new four-value syntax for background-position, but until all browsers support it your best approach is a combination of earlier responses in the following order:
background: url(image.png) no-repeat 97% center; /* default, Android, Sf < 6 */
background-position: -webkit-calc(100% - 10px) center; /* Sf 6 */
background-position: right 10px center; /* Cr 25+, FF 13+, IE 9+, Op 10.5+ */
A simple but dirty trick is to simply add the offset you want to the image you are using as background. it's not maintainable, but it gets the job done.
This will work on most modern browsers...apart from IE (browser support). Even though that page lists >= IE9 as supported, my tests didn't agree with that.
You can use the calc() css3 property like so;
.class_name {
background-position: calc(100% - 10px) 50%;
}
For me this is the cleanest and most logical way to achieve a margin to the right. I also use a fallback of using border-right: 10px solid transparent; for IE.
Ok If I understand what your asking you would do this;
You have your DIV container called #main-container and .my-element that is within it. Use this to get you started;
#main-container {
position:relative;
}
/*To make the element absolute - floats above all else within the parent container do this.*/
.my-element {
position:absolute;
top:0;
right:10px;
}
/*To make the element apart of elements, something tangible that affects the position of other elements on the same level within the parent then do this;*/
.my-element {
float:right;
margin-right:10px;
}
By the way, it better practice to use classes if you referencing a lower level element within a page (I assume you are hence my name change above.
background-position: calc(100% - 8px);
The CSS3 specification allowing different origins for background-position is now supported in Firefox 14 but still not in Chrome 21 (apparently IE9 partly supports them, but I've not tested it myself)
In addition to the Chrome issue that #MattyF referenced there's a more succinct summary here:
http://code.google.com/p/chromium/issues/detail?id=95085
If you have proportioned elements, you could use:
.valid {
background-position: 98% center;
}
.half .valid {
background-position: 96% center;
}
In this example, .valid would be the class with the picture and .half would be a row with half the size of the standard one.
Dirty, but works as a charm and it's reasonably manageable.
If you would like to use this for adding arrows/other icons to a button for example then you could use css pseudo-elements?
If it's really a background-image for the whole button, I tend to incorporate the spacing into the image, and just use
background-position: right 0;
But if I have to add for example a designed arrow to a button, I tend to have this html:
Read more
And tend to do the following with CSS:
.read-more{
position: relative;
padding: 6px 15px 6px 35px;//to create space on the right
font-size: 13px;
font-family: Arial;
}
.read-more:after{
content: '';
display: block;
width: 10px;
height: 15px;
background-image: url('../images/btn-white-arrow-right.png');
position: absolute;
right: 12px;
top: 10px;
}
By using the :after selector, I add a element using CSS just to contain this small icon. You could do the same by just adding a span or <i> element inside the a-element. But I think this is a cleaner way of adding icons to buttons and it is cross-browser supported.
you can check out the fiddle here:
http://codepen.io/anon/pen/PNzYzZ
use center right as the position then add a transparent border to offset it?
If you have a fixed width element and know the width of your background image, you can simply set the background-position to : the element's width - the image's width - the gap you want on the right.
For example : with a 100px-wide element and a 300px-wide image, to get a gap of 10px on the right, you set it to 100-300-10=-210px :
#myElement {
background:url(my_image.jpg) no-repeat -210px top;
width:100px;
}
And you get the rightmost 80 pixels of your image on the left of your element, and a gap of 20px on the right.
I know it can sound stupid but sometimes it saves the time... I use that much in a vertical manner (gap at bottom) for navigation links with text below image.
Not sure it applies to your case though.
my problem was I needed the background image to stay the same distance from the right border when the window is resized i.e. for tablet / mobile etc
My fix is to use a percenatge like so:
background-position: 98% 6px;
and it sticks in place.
yes! well to position a background image as though 0px from the right-hand side of the browser instead of the left - i use:
background-position: 100% 0px;
I have a web site with a fixed height black header like the one used here:
https://elementsproject.org/posts/
Notice how when a user scrolls the page the black text hits the black header. As the white background page with black text scrolls to the top I would like the black text color to fade away. In other words what I would like to do is to have a header bottom border with a white color where the opaqueness (is that a word) goes from 100% to zero.
Can someone tell me how I can make the opaque property change in this way from top to bottom of the header strip? Note that I'm looking for a modern browser solution only so that might make it a bit easier.
Thanks
you can use a box-shadow or a linear-gradient for this.
/* filled 50/50 */
background-image: linear-gradient(#yourBackgroundColorHere, transparent);
/* just the last 25% */
background-image: linear-gradient(#yourBackgroundColorHere 75%, transparent);
for more information check CSS-Tricks.com https://css-tricks.com/css3-gradients/
/* just a box-shadow under your header */
box-shadow: 0px 0px 20px 20px #yourBackgroundColorHere;
for more information about box-shadow check CSS-Tricks.com https://css-tricks.com/almanac/properties/b/box-shadow/
--
disclaimer: I'm not affiliated with css-tricks.com in any way, it is just a great website about everything css
try this code in style
100%
div { opacity: 0.0; }
50%
div { opacity: 0.5; }
0%
div { opacity: 1.0; }
for animation in jquery
$("#DivID").animate({opacity: 0}, 5000); //5 sec transaction
I'm trying to use multiple background images to obtain this result on a liquid width div:
I have split the image in three parts:
And I'm trying to style the div like this:
height: 14px;
background-image: url(static/img/workspace-pre-hr-l.gif),
url(static/img/workspace-pre-hr-bg.gif),
url(static/img/workspace-pre-hr-r.gif);
background-repeat: no-repeat, repeat-x, no-repeat;
background-position: left, center, right;
But the right margin doesn't work and so I have this instead:
Any help? Thanks
EDIT
Fiddle! http://jsfiddle.net/J5Tsa/
SOLVED
Seems like it is a z-index problem among the images. Declaring the right margin before the repeated one solved the problem.
It is my understanding that images are stacked according to the order in which they are specified in the background-image property. So my theory is that workspace-pre-hr-r.gif is being displayed underneath workspace-pre-hr-bg.gif.
Try this...
background-image: url(static/img/workspace-pre-hr-l.gif),
url(static/img/workspace-pre-hr-r.gif),
url(static/img/workspace-pre-hr-bg.gif);
background-repeat: no-repeat, no-repeat, repeat-x;
background-position: left, right, center;
CSS 3 supports border-image rule, which accomplishes what you want, should you be willing to give up compatibility with earlier CSS versions and user agents. The syntax looks much leaner and easier to read:
<div style="border-width: 25px; border-image: url(http://codebrief.com/old/uploads/2011/11/aqua_bg.png) 25 25 25 25 repeat; background-color: #00e0a0; background-clip: padding-box;">Hello World!</div>
I wrote and saved it at http://jsfiddle.net/Wnq3z/
I simply Googled and found a solution which I credit to http://codebrief.com/2011/11/two-game-changing-css-3-features/ after recalling I read something about this being present in CSS 3.
Waiting for a Fiddle, i can suggest you to use the center image only,
rounding up its borders with something like:
border-radius: 10px 10px 0px 0px;
EDIT: as a note, border-radius is born to bypass the Sliding Doors technique...