CSS gradient over multiple elements - html

I was wondering if it is possible to apply a single CSS3 gradient background to multiple elements. In other words, the gradient spans over the parent element but is only visible inside the child elements.
After searching, I found this thread: Applying gradient over multiple views
This is exactly my problem, though I need it as CSS/HTML code.
To visualize the problem, I made two pictures:
This is the basic setting. The two <div>s needing a gradient background are inside a larger <div> element.
As you can see, the gradient in the second image perfecly fades from element A to element B.
This effect is easily doable in most image-editing programs, so I could just use an appropriate image to get the desired effect.
However, since images are probably not the best way to solve this, I hope to find an answer here on how to do this with only CSS. I used gradients before, but I have not found a solution to this problem on my own yet.
Any help is appreciated.
EDIT (06/01/15 13:30 GMT+1):
The Elements A and B should be able to have round corners.
The spanning gradient was originally supposed to be a radial-gradient, but it does not need to be.
Maybe the problem is really not solvable.

(Even if this qestion is quite very old...)
Have a look at Multiple.js - which describes how to apply a gradient to multiple elements without js.
Quoted from the demo page:
.selector {
background-image: linear-gradient(white, black);
background-size: cover;
background-position: center;
background-attachment: fixed; /* <- here it is */
width: 100px;
height: 100px;
}
background-attachment: fixed expands background to viewport's size and displays in every element appropriate chunk, exactly what is needed!
The idea behind this is simple as smart and works for most modern browsers (IE8 too).
If applied it looks like this:

Demo: https://jsfiddle.net/andrewgu/gptbyejt/
One way to accomplish this is that you could always fake it with a solid background color. You display the gradient div in the background and whatever content divs you want, separated with an overlaying div whose color matches the background.
Pros: Flexible, compatible
Cons: Solid background colors only
The other way is to use something called CSS clipping. You can do this using the CSS declarations of clip-path and -webkit-clip-path. This basically makes part of an element transparent. However, you need to specify the size of each element beforehand, and tweak it around a bit to get the items to display correctly. This method works with non-solid backgrounds. Contrary to popular belief, CSS clipping is pretty good with cross-browser compatability.
Pros: Patterned backgrounds, compatible
Cons: Defined child-element sizes, tweaking

Combination of background-attachment: fixed and iframe may do this trick for you.
Check out nexts.github.io/Multiple.js and pay attention to "How it works" section

Related

How to extend background CSS styling indefinitely along the x-axis?

I have a header background that I would like to bleed indefinitely on the x axis. So regardless of the size of someones monitor, the grey background would extend.
How do I do this?
Thanks!
Two answers, depending on what issue you're exactly facing.
First case: The background isn't repeating at all
Assuming you're using a CSS background-image to render your background, use background-repeat: repeat-x;
Little demo: little link.
If you're not using a background-image to render your background, then you should consider using it. There's no direct way of making an <img> repeat.
Second case: The background is repeating, it's just not spanning the whole width
You need to make sure your header has width: 100%;. If you have some padding to your headers parent, use display: block; instead of width: 100%;, otherwise it'll overflow.

Stretching an Image while preserving the corners in HTML5

I want to achieve the effect described in the following question, but using CSS.
I remember seeing somewhere that this now can be done with HTML5, but now can't find the property name.
Stretching an UIImage while preserving the corners
You'll have to use 3 different images.
First, go into photoshop or Gimp or something and break the arrow image you have into 3 parts. The left side with the curve, and the right side with the arrow part. Save them as 3 different images.
Once you've got your images. Create one HTML image element:
<img src="img-middle.jpg" />
In your CSS, apply styling to the before and after pseudo-elements and add the two image bits that you don't want stretched.
img:before {
contents: '';
background: url('img-left.jpg');
height: 50px;
width: 20px;
position: absolute;
left: -20px;
}
img:after {
content: '';
background: url('img-right.jpg');
height: 50px;
width: 40px;
position: absolute;
right: -40px;
}
Make sure you change the width, height, left and right values to match the width and height of your two image files. This CSS allows these bits of the image to be added on to the left and right sides, no matter how wide the element is stretched. It's also cool since it's only one element, so the CSS stays pretty clean except for the requirement of the empty content:''; property.
So then you can stretch your middle image element dynamically. Lets say you want he arrow to stretch, have some jQuery that animates the width of the element, then the middle part will stretch and the corners will stay intact since they're technically not part of the original element, they're just appended.
ETA: As for the method described in the objective-C related post, there's no CSS property that breaks apart images like that unless it's in some obscure webkit nightly build that I've never heard of. Your option here is to break apart the other two sides. You could also combine the left and right portions of your image into a sprite and use the background-position:; CSS property to select bits of the image so that way you'd only have two image file requests, as you want to keep those low to speed up page load time.
you can create an element, assign pseudo elements to it for the left and right side caps, and use a CSS3 transition applied to the width property in order to achieve this effect.
i've set up a working demo on jsFiddle to illustrate how it's done. this demo uses background colors, but one could use images as well (repeating along the X axis for the center element).
check out the HTML5 rocks playground, you'll find some fascinating snippets demonstrating the power of CSS3 and HTML5 (naturally) and can use it as a quick reference as well.
Did you mean CSS3?
I think border-image is pretty much what you're looking for. It lets you take a single image and transform it into the border of an element.
It's kinda hard to work with, so Mozilla made a wonderful WYSIWYG editor:
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Background_and_Borders/Border-image_generator

Getting rid of container div

it seems to be a common practice to use a div with a class of “container” or “content” or something similar around the main content to help with the css-design of the page.
I personally don't like that, because it is (in my opinion) a violation of the rule to seperate presentation from content (because the -element serves no semantic purpose).
So my question is: Is there any way to get a similar result with just CSS? To be more specific, I want the site to have a background-image, but to have a solid white background around the main content.
I already tried something like setting two backgrounds for , one the image, the second a solid white and then size the second one down, but I guess, you can only use background-size on images?
Thanks in advance
You could use the html or body elements, except this is problematic across various browsers, which is why people frequently use a div.

An HTML code for a background image to fit the size of the table

Which code in HTML will allow me to use an image as a background image for a table, but not so that it would be repeated several times vertically and horizontally (in case the table is several times bigger than the image), but in such way that the image height is stretched out to be equal to the height of the table, and its width is stretched out to be equal to the width of the table?
The CSS background options can't handle this reliably across browsers, so you need to put an <img> tag in the table and position it appropriately. As Petr Marek alluded to in the comments, you can do this with the CSS attributes z-index and position, but it's not elegant.
If you set position: relative on the table, you can set position: absolute on the <img> with top: 0; height: 100%; left: 0; width: 100%; to position and size the image, and set z-index: -1 to make it appear behind the other content.
Here's a working example on jsFiddle.
Although it works perfectly for me in Chrome, since you're putting content on top of an image I wouldn't be surprised if it caused some browsers to mess up text selection or something else.
Presentation is the job of CSS, not HTML. You can use background-size in supporting browsers.
HTML stands for Hypertext Markup Language. It is not code. It is intended to provide semantic meaning and structure to a web-accessible document. Entities such as images are purely presentational cruft that was added because we got bored just reading text all day.
That being said, you can use CSS to possibly achieve what you want, but it may be bit tricky. An idea that comes to mind is adding an image element to the page with a height and a width set to be 100% of its container, and then positioning your table, with a transparent background color, over it. You'll have to look into z-indexes, obviously.

Rounded input buttons, absolute positioning, liquid width

I realize there are lots of rounded buttons questions, but my needs are fairly specific, and this hasn't been answered elsewhere.
Here's my requirements:
Works with absolutely positioned buttons
Client side only techniques ( can't change HTML on server side )
Works on input type=button and input type=submit (button element not needed)
Fixed height, liquid width
Supports IE7 or better
The absolute positioning + client side only makes most rounded corner techniques unusable in my opinion.
Images or no images does not matter (either way is fine). JavaScript is allowed.
EDIT: Changed question to reflect actual problem: the one HTML element I thought I needed wasn't really the requirement.
It's not possible for IE. That's why you can't find it anywhere else. The only thing you could do is use a static background image, but that will stretch for different widths.
I ended up using multiple backgrounds for the buttons.
CSS3 multiple backgrounds for browsers that could handle that, and in IE I used the DXTransform filter to add a second image (see here). The actual technique used was a pretty standard sliding door style setup, with some changes to account for the fact that you couldn't position the second image in IE other than at the top left.
For FF 3.5 and lower I used border-radius, since multiple backgrounds only came in 3.6.
Hover/active images worked fine, and it's all in CSS, which was a bonus.
Since javascript is allowed (based on one of your comments), I don't see how it would be a big performance hit to:
wrap the input elements with div
take the positioning properties of the input and copy them to the div wrapper
remove the positioning off the input using an inline position: static
add other elements or styles to get your rounded corners. Being fixed height, then for everything other than IE7, some css like this should work (assumes fixed height of 20px, rounded end images that are 10px wide by 20px high):
Css:
div.inputWrap:before,
div.inputWrap:after {content: ' '; display: inline-block; height: 20px; width: 10px; background: url(/yourRoundedLeftEndImg.png) top left no-repeat;}
div.inputWrap:after {background: url(yourRoundedRightEndImg.png);}
Assuming your javascript gives you this html:
<div class="inputWrap"><input /></div>
You will need to style the input to get rid of borders, and such (I also found that my test in Firefox required me to set vertical-align: top, but not sure if that is necessary. For IE6-7, you would actually have to add extra div's before and after the input since they do not recognize the :before and :after pseudo-classes.