Can images background be derived from CSS styling? - html

I've an image. Can I derive its background from CSS styling??
Following is the image. I want a similar background in my div.
PS: I can use it as background-image, but I dont want to. So, please dont suggest this solution.

The easiest way will be to use a gradient.
background:-webkit-linear-gradient(left, #743D90, #954792); /*for chrome&safari*/
background:-moz-linear-gradient(left, #743D90, #954792); /*for firefox*/
background:-o-linear-gradient(left, #743D90, #954792); /*for opera*/
background:-ms-linear-gradient(left, #743D90, #954792); /*for IE*/
background:linear-gradient(left, #743D90, #954792); /*standard*/
makes a gradient with the colors of your image. There are so many lines of CSS so it will work in nearly all browsers.
Example:
http://jsfiddle.net/6uMzc/4/
Hope this will work for you.

Absolutely it can be done.
Wrap the image tag inside a div.
<div class="bg">
<img src="xxx.jpg" alt="image">
</div>
Note the image must have transparent background(this should be made sure by image editing tools like photoshop)
Now style the div with background gradient and size as something below.
.bg{
height:100px;
width:200px;
background:-webkit-linear-gradient(left, red, green);
background:-moz-linear-gradient(left, red, green);
background:-o-linear-gradient(left, red, green);
background:-ms-linear-gradient(left, red, green);
background:linear-gradient(left, red, green);
}

If I understand you correctly, you want to create a background similar to that image with css. You have several options for doing this. For starters you can use this Gradient Generator to create gradients. They are relatively simple, I'm sure you can figure out the core concepts, the generator is primarily because the different css prefixes must be added for compatibility.
You said not to consider the lines, but I do think I should point you to this CSS triangle generator, incase you need it. Your image has the lower portion cut off at an angle, and the best way to do this is (IMHO) is to create a white triangle and put it over it.
If you need any help applying these concepts, feel free to ask.
Update: See: http://jsfiddle.net/6uMzc/6/
You need two seperate divs to create this effect. One div is the gradient, the other is the triangle that removes the lower corner. I'll explain what the different parts of the css do:
The background attributes are all defining the gradient for different browsers.
position:relative;
position:absolute;
left:0;
bottom:0;
These lines are postioning the divs. This can get quite complex, but it is incredibly useful for web developers, so you should probably do further research. I actually think the w3schools page on this is pretty good. Elements with relative, fixed, and absolute positions can be modified by the left, right, bottom, and top attributes, which can move them around in different directions. Elements with relative positioning are moved "relative to their normal position." Elements with absolute positioning are moved relative of the next containing element with a relative or absolute positioning, or the body. Since the containing div is positioned relative, the position:absolute moves the triangle relative to the gradient. Left:0 and bottom:0 put the element in the bottom left corner.
border-width: 50px 0 0 500px;
Change the first number to change height and the last one to change width. 500px is the width of the containing div, so this covers up the entire bottom of the containing div.

Related

CSS gradient over multiple elements

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

Inside transparent border

I want to set a transparent border inside around an image by using CSS. I can't find any way to do this.
here is the link for my image with transparent inside border effects.
That's not an easy task, since images are replaced elements and have quite some restrictions (pseudo-elements not working, inset box-shadow not working,...).
You have several options (there are probably more, I just list two):
Solution 1:
Make your image the background-image of an ordinary div and apply a border - this border will automatically overlap the image. Btw., you can use background-clip and background-origin to alter that behaviour.
Problem:
You need to know the dimensions of the image, to adjust the size of the div accordingly.
Solution 2:
Wrap your image into another element, and declare a pseudo-element to which you apply the borders.
Important are the display:block on the img and position:relative on the wrapper element.
Example fiddle
Use box-shadow
This CSS box-shadow: inset 0 0 0px 8px rgba(255, 16, 16, 0.45);
You can do the thing amazingly he he Enjoy It
http://jsfiddle.net/Q8d6V/3/
I can offer use 4 border-blocks with position:absolute. You can change style of each border. For exaple add shadow for left and top border
Example fiddle

Ring shaped div

Actually I have 2 questions here. I'll explain them as much as I can.
I'm trying to create a ring shaped div. Below is what it should look like.
This is what I have done so far. How can I get the slight curve from left bottom corner to the top? And how can I create the inverted curve for the right side?
Thank you.
You could add a second circle that whites-out the portion of the image you don't want to see.
For example, if you add a second div (inside the first) with id cutout and style it as follows:
#cutout {
top:250px;
border-radius: 700px;
border:300px solid #eee;
z-index:1;
left:200px;
height:200px;
position:absolute;
}
Then in your image you'll get a little closer to what you want. You'll also want to add position: relative to your container div to ensure that's what it's positioned and clipped to.
Finally, if you want clip the ring segment as shown in your image, you'll need to add an extra outer div just to clip the innermost two; and you should replace the image with a radial background.
That'd look approximately like: http://dabblet.com/gist/4571882
Image:
Honestly though, I'm not sure contortions like this work very well - you'll usually not get exactly the result you wanted, and it's likely not to work on older browsers very well; nor for that matter necessarily on newer browsers that implement a newer version of these CSS properties.
you could try creating a new div and adding
border-top-right-radius: XXX
for the inverted segment. Making sure they float correctly (e.g. float:left).

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

Get background image from a image file by position in CSS

We suppose that we have the following image (is a single file with 4 radio buttons, seems same but it is for example, the icons could be different):
I saw, a time ago, how to get an image by specifing background-position in CSS style. How to do that ?
Means, how to get the icon from lower-right side in CSS instead splitting that file in four icons separately ?
Sorry if this question is obsolete but I'm beginner in CSS.
Have a look at the background-position attribute.
MDN has an article about it, including examples.
There are also a number of articles available about image-sprites with CSS.
Let's say your image is 100px by 100px and each button takes up 50px by 50px (including some possible whitespace).
Positioning for background images starts in the upper left-hand corner. So 0, 0.
Your CSS might look something like this:
background-image: url( ../images/button-sprite.png);
background-position: 0, 0; // top left
background-position: -50px, 0; // top right
background-position: 0, -50px; // bottom left
background-position: -50px, -50px; // bottom right
Not that the above shouldn't be used all together. You would use a separate background-position for each button.
Sometimes the positioning can seem counter intuitive since you'll usually be using negative values to get to the part of the image you need.
I tend to remind myself that if I were to add padding to an element it would push it down. Basically the same goes for background positioning. If we used 50px (positive) we would be "pushing the image down" instead of pulling it up to where we need it. Make sense?
you can adjust the sprite images in css through background-position in top,left, bottom,right.
In css background position:-
background-position:top,left refers with positive values like:-if need sprite image from top,left so you can give positive values to adjust the image like:-
background-position: 5, 10; // top left
so this mentioned above position will adjust the top & left background position.
And if you need bottom right background position so you can use the method
background-position: 0, -50px; // bottom left
background-position: -50px, -50px; // bottom right
so this mentioned above position will adjust the bottom & right background position.
So you are using Image Sprites.
With CSS, we can show just the part of the image we need.
#navlist{position:relative;}
#navlist li{margin:0;padding:0;list-style:none;position:absolute;top:0;}
#navlist li, #navlist a{height:44px;display:block;}
#home{left:0px;width:46px;}
#home{background:url('img_navsprites.gif') 0 0;}
#prev{left:63px;width:43px;}
#prev{background:url('img_navsprites.gif') -47px 0;}
#next{left:129px;width:43px;}
#next{background:url('img_navsprites.gif') -91px 0;}
Jangid's response is not quite correct.
With CSS, we can show just the part of the image we need.
This is not true; no CSS currently allows "clipping" of an area from a sprite image. All that is possible is to set the positioning as per the standard (left, top, right, bottom or offset).
You must make sure that if your graphics are going to be used as the background to a FLUID element (i.e. with an automatic height/width) that your images are spaced out well enough. If you do not space them out enough, then you may well see more than one image in the background, not just the part you want.
For example, your sprite contains 36 images, all 32px square, spaced neatly on a grid of 40px.
If the element to which you apply the background image is wider and/or taller than 40px, you will see part of other images as the background, which is obviously not satisfactory.
Hope this helps.