CSS3 - Turn background image into CD - html

Let's say I have an image of this size:
Using CSS3 and whatnot, is there a way I can make it into like a CD? You know with the whole in the middle (preferably transparent) and round edges and what not.

I'm not sure about a transparent middle, but this will give you the CD shape (http://jsfiddle.net/CkYcN/):
HTML:
<div class="cd">
<div class="hole"></div>
</div>
CSS:
.cd {
-moz-border-radius: 63px;
-webkit-border-radius: 63px;
border-radius: 63px;
background-image: url('http://userserve-ak.last.fm/serve/126/23679395.jpg');
width: 126px;
height: 126px;
position: relative;
}
.cd .hole {
width: 30px;
height: 30px;
position: absolute;
background-color: #fff;
left: 48px;
top: 48px;
-moz-border-radius: 15px;
-webkit-border-radius: 15px;
border-radius: 15px;
}

http://jsfiddle.net/5CDnw/1/
#cd {
background: rgb(255,255,255);
border-radius: 75px;
width:50px;
height:50px;
position:fixed;
z-index: 100;
left: 50px;
top: 50px;
border: 1px solid #aaa;
background: #ebebe9;
}
#cover {
position:fixed;
border-radius: 75px;
width:150px;
height:150px;
background: url(http://userserve-ak.last.fm/serve/126/23679395.jpg);
border: 1px solid #aaa;
}
​

Here's what I'd do (using pure CSS without a mask):
Image on the furthest layer from the screen
A later on top, using a CSS3 circle, that's while (since the div is transparent, the rest is visible). I'd place this vertically and horizontally centered.
A sufficiently larger layer on top that's another CSS3 circle such that it's radius is distance from the center to the edge of the square.
This would mean that a circle would be passing outside the original image, but you can work around this by constraining all of this within another DIV and having the biggest circle with a negative margin.
By CSS3 circle, I'm thinking about using a border-radius like this: http://www.cvwdesign.com/txp/article/413/making-circles-with-css3-border-radius but any implementation of one should do.

You can accomplish that effect with the CSS3 mask property. It doesn't have great browser support at this time, but if you're looking for CSS3, then you probably know that already. It is possible to create an SVG mask (which is nice because it would scale without blurring the edges if you needed to do so), however, that would be a bit tougher to setup with the compound shape (circle in the positive space and then another circle in negative space).
It's easy enough to create a PNG image, which will act as your mask like so:
.cd {
background: url('../jpop.jpg') no-repeat;
-webkit-mask: url('../my_cd_knockout.png') no-repeat;
mask: url('../my_cd_knockout.png') no-repeat;
}
Here's a good writeup with some more details on it:
http://coding.smashingmagazine.com/2011/05/11/the-future-of-css-experimental-css-properties/

Well to round the outside of the image you could use something like
Add following CSS attributes to your element to make its corner round.
img#CD{
background: url('../CD.jpg') no-repeat;
-webkit-mask: url('../hole.png') no-repeat;
mask: url('../hole.png') no-repeat;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
border-radius: 10px;
border: 1px solid;
}
To make only bottom right corner rounded, use following CSS.
img#CD{
background: url('../CD.jpg') no-repeat;
-webkit-mask: url('../hole.png') no-repeat;
mask: url('../hole.png') no-repeat;
-moz-border-radius-bottomright: 10px;
-webkit-border-bottom-right-radius: 10px;
border: 1px solid;
}
You would of course have to change the pixel (px) value to round the image more or less.

Related

How can I fit a square HTML image inside a circle border?

I have a square image, and I'd like to put it inside a circle border. How can I make it so that the entire image fits instead of its corners getting cut?
.circle {
width: 200px;
height: 200px;
border: 1px solid black;
border-radius: 100px;
background-image: url('http://upload.wikimedia.org/wikipedia/commons/thumb/0/0d/Ski_trail_rating_symbol-blue_square.svg/600px-Ski_trail_rating_symbol-blue_square.svg.png');
background-size: contain;
}
Here it is on jsfiddle.
You need to shrink the image slightly to make it fit within the circle. To calculate the exact size, divide the diameter of the circle by sqrt(2). In this case, 200px / sqrt(2) is about 141px.
Thus, add the following properties:
background-size: 141px;
background-repeat: no-repeat;
background-position: 50%;
JSFiddle
Note that the blue block doesn't touch the circle because the image has a transparent border.
UPDATE: As cassiorenan correctly points out, using a percentage allows the image to automatically scale if you change the size of the circle. Since 1 / sqrt(2) = 0.707..., you can use 70.7% instead of 141px:
background-size: 70.7%;
Change the background size to a percentage(So it will still have the same relative size ragardless of you changing the circle's width/height.) and center it. While you're at it, tell it to not repeat.
On your particular case, this code works:
.circle {
width: 200px;
height: 200px;
border: 1px solid black;
border-radius: 100px;
background-image: url('http://upload.wikimedia.org/wikipedia/commons/thumb/0/0d/Ski_trail_rating_symbol-blue_square.svg/600px-Ski_trail_rating_symbol-blue_square.svg.png');
background-size: 90%;
background-repeat: no-repeat;
background-position:center;
}
Edit: Fixed code formatting.
As I said in my comment you must remove transparent border/space around the image or if you don't wanna do that then use this CSS
.circle {
width: 200px;
height: 200px;
border: 1px solid black;
border-radius: 100px;
background: url('http://upload.wikimedia.org/wikipedia/commons/thumb/0/0d/Ski_trail_rating_symbol-blue_square.svg/600px-Ski_trail_rating_symbol-blue_square.svg.png') center;
background-size: 130%;
}
Give the background a size like background-size: 100px; then position it in the center of the div and tell it not to repeat:
background-size: 100px;
background-position:50%;
background-repeat:no-repeat;
The coding should now look like this:
.circle {
width: 200px;
height: 200px;
border: 1px solid black;
border-radius: 100px;
background-image: url('http://upload.wikimedia.org/wikipedia/commons/thumb/0/0d/Ski_trail_rating_symbol-blue_square.svg/600px-Ski_trail_rating_symbol-blue_square.svg.png') ;
background-size: 100px;
background-position:center center;
background-repeat:no-repeat;
}
JSFiddle Demo

Gradient help to create a slanted div

So I've been at it for a while trying to achieve this one shape with CSS with no good solutions. I need this to be an image because this div may resize and I want it to stay intact. I've also attempted to create an SVG which did not work out very well, I've seen some people work with gradient to make shapes but I'm not able to find any good guide to point me in the right direction. Any help is appreciated :)
Using gradients with angles is not fit for your case because (as already pointed out by King King in comments) as the width the increases, the angle of the gradient (or) the color stop percentages need to be modified to maintain the shape. That is very tricky and so this method can be employed only when the shape has fixed dimensions.
However gradients can still be used with the to [side] [side] syntax because gradients defined using this syntax can adapt to variations in container sizes. In this method no pseudo-elements are used.
$(document).ready(function() {
$('#increase').on('click', function() {
$('.gradient').css('width', '300px').css('height', '500px');
})
})
div {
display: inline-block;
vertical-align: top;
height: 300px;
width: 100px;
margin: 10px;
color: beige;
transition: all 1s;
}
.gradient {
padding: 10px;
background: linear-gradient(to top right, transparent 50%, tomato 50%) no-repeat, linear-gradient(to top right, transparent 0.1%, tomato 0.1%) no-repeat;
background-size: 100% 100px, 100% 100%;
background-position: 0% 100%, 0% -100px;
}
/* Just for demo */
body {
background: -webkit-radial-gradient(50% 50%, circle, aliceblue, steelblue);
background: radial-gradient(circle at 50% 50%, aliceblue, steelblue);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="gradient">Some content</div>
<br>
<br>
<button id="increase">Increase Width & Height</button>
Note that it is better to make sure that the text doesn't flow into the slanted section of the shape because wrapping the text around to fit within the shape is not straight-forward.
I have attempted to make that in css as per ur image. http://jsfiddle.net/3zkme/- See if this could help. Thanks.
HTML
<div style="margin:30px">
<div class="trapezoid">
</div>
</div>
CSS
.trapezoid{
top: 150px;
vertical-align: middle;
border-bottom: 120px solid red;
border-left: 200px solid transparent;
border-top-left-radius:0px;
height: 0;
width: 150px;
transform:rotate(270deg);
-ms-transform:rotate(270deg); /* IE 9 */
-webkit-transform:rotate(270deg); /* Opera, Chrome, and Safari */
}
/* ---------- */
.trapezoid {
position:relative;
}
.trapezoid:after {
content:' ';
left:-14px;
top:10px;
position:absolute;
background:red;
border-radius:0px 0 0 0;
width:164px;
height:40px;
display:block;
}
You do not use a gradient for this, you just need to use a pseudo-element like :after.
Sample code:
#bookmark {
width: 50px;
height: 100px;
position: relative;
background: red;
}
#bookmark:after {
content: "";
position: absolute;
left: 0;
bottom: 0;
width: 0;
height: 0;
border-bottom: 35px solid #FFF;
border-right: 50px solid transparent;
}
Live JSFiddle
If you want the shape to be filled in with a gradient, you can do that, too. Just add that to the CSS:
background: linear-gradient(to right, #ff0000 0%,#B00000 100%);

how to position a css sprite as background for another class

Sorry if the layout of the question seems weird, I've wrote this question now for about 10 times over and over again in this editor, resulting always in getting an error message "unformatted code found etc." - so I removed all the code and placed picture examples. (2 hours for a simple question)
Hello folks!
I do have a .png image, containing several icons that works as CSS Sprite.
The creation of each CSS class for that is no problem as I use a generator for that. (works like a charm)
The problem is, that I want to use, for example: The created .iconInfo_32 class, as background property for another css class.
What I want to achive?
Simple said, a custom css - messagebox, with an icon on the left side.
The icon itself is in original a sprite containing multiple icons.
That's where the problem starts.
What I have
The Icons
(thats one PNG)
The Icon I want to use
How the result should look like
How it actually looks
Use another div, in a div
Yes, that would work - but I'd like to have "one" css class, without the need to put always a div, into another div, say where the position should be and so on - also I had problems with the position of the div.
I've provided a source example, hopefully this will help being able to understand my question and my goal.
Excuse me if the layout of my question is unusual and unpleasent, I would have done it in another way, but the editor just won't let me
Source
HTML
<div class="warning_without_sprite">
This is a DIV container<br />
showing an error message with the use of 'close_32.png' as Icon. (No Sprite)
</div><br /><br /><br /><br />
<div class="warning_with_sprite">
This is a DIV container<br />
showing an error message with the use of 'icons.png' as Icon. (Sprite)
</div>
CSS
<style type="text/css">
.iconInfo_32 { width: 32px; height: 32px; background:url(images/icons.png) no-repeat -0px -0px; }
.iconOk_32 { width: 32px; height: 32px; background:url(images/icons.png) no-repeat -32px -0px; }
.iconAdd_32 { width: 32px; height: 32px; background:url(images/icons.png) no-repeat -64px -0px; }
.iconClose_2_32 { width: 32px; height: 32px; background:url(images/icons.png) no-repeat -96px -0px; }
.iconClose_32 { width: 32px; height: 32px; background:url(images/icons.png) no-repeat -128px -0px; }
.iconDelete_32 { width: 32px; height: 32px; background:url(images/icons.png) no-repeat -160px -0px; }
.iconDownload_32 { width: 32px; height: 32px; background:url(images/icons.png) no-repeat -192px -0px; }
.iconHelp_32 { width: 32px; height: 32px; background:url(images/icons.png) no-repeat -224px -0px; }
.warning_without_sprite {
border: 1px solid;
padding: 10px 10px 10px 50px;
background-repeat: no-repeat;
background-position: 10px center;
float:left;
color: #D8000C;
background-color: #FFBABA;
background-image: url('images/close_32.png');
}
.warning_with_sprite {
border: 1px solid;
padding: 10px 10px 10px 50px;
background-repeat: no-repeat;
background-position: 10px center;
float:left;
color: #D8000C;
background: #FFBABA url('images/icons.png') no-repeat -128px -0px;
}
</style>
>> Download as RAR. <<
It's because you've set it as a background-image across the whole <div> element and because the sprite contains multiple images it will show them all. You can't specify how much of that sprite to show.
You'll have to insert a <span> element into your <div>. This will allow you to specify the size of the span and position it relative to your div container.
<div class="warning">
<span class="warning_with_sprite"></span>
This is a DIV container<br />
showing an error message with the use of 'icons.png' as Icon. (Sprite)
</div>
CSS:
.warning_with_sprite {
position:absolute; left:16px; top:16px;
background-repeat: no-repeat;
background-position: 10px center;
width:20px; height:20px;
background: url('http://i5.minus.com/id1CYq.png') no-repeat -133px -2px;
}
.warning {
float:left;
color: #D8000C;
border: 1px solid;
position:relative;
padding: 10px 10px 10px 50px;
background: #FFBABA;
}
See a demo here
Note: you'll have to change the image back to your sprite and the top, left, height and width properties will have to change inline with your requirements

button ignores parent div and won't align

I can't quite get to the bottom of this: it seems that my parent div is being ignored by the browser when I want to align it. Hopefully any of you guys can get it! Thanks!
This is my parent div:
#button-subheading {
height: auto;
width: auto;
text-align: center;
margin-left: auto;
margin-right: auto;
}
and this is my child:
.button, button {
color: #fff !important;
font-size: -63px;
font-family: "CentraleSans-Bold","Helvetica Neue",Helvetica,Arial;
line-height: 265%;
text-decoration: none;
background-color: #167de4;
padding: 0 20px;
position: absolute;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
-ms-border-radius: 3px;
-o-border-radius: 3px;
border-radius: 3px;
-webkit-background-clip: padding;
-moz-background-clip: padding;
border: 1px solid #1d4ea4;
-moz-box-shadow: 0 1px 1px rgba(0,0,0,0.23),inset 0 1px 0 rgba(255,255,255,0.19);
background-image: -webkit-gradient(linear, 50% 100%, 50% 0%, color-stop(0%, #3669ef), color-stop(100%, #4f95f4));
background-image: -webkit-linear-gradient(bottom, #3669ef 0%,#4f95f4 100%);
background-image: -moz-linear-gradient(bottom, #3669ef 0%,#4f95f4 100%);
background-image: -o-linear-gradient(bottom, #3669ef 0%,#4f95f4 100%);
background-image: linear-gradient(bottom, #3669ef 0%,#4f95f4 100%);
text-shadow: 0 1px 2px rgba(0,0,0,0.75);
white-space: nowrap;
text-overflow: ellipsis;
}
when I try to implement it trough this HTML it gets stuck on the left of the screen:
<div id="button-subheading">
<div class="button" href="#" style="opacity: 1;">Get started with a free 30-day trial</div>
</div>
If you want to place only that element into the center, this could be a way to go:
#button-subheading {
position: absolute;
left: 50%;
top: 50%;
height: auto;
width: auto;
text-align: center;
margin-left: -150px; /* half the width of your element */
margin-top: -150px; /* half the height of your element */
}
FIDDLE
There are already a couple answers here, and even a chosen one. But I suppose I'll share my alternate solution anyways, so there are even more options available to you.
The way I solved with was by changing one line of CSS under the .button, button styles, turning position:absolute to display:inline-block. Absolute positioning can be very difficult to properly work into a layout, since it basically removes the element from the "flow" of the HTML.
Also, another minor thing I think might be helpful to you, is to use an anchor (a) element for the button, rather than a div, since clicking on an anchor would actually bring you to the specified href.
Here's a JSfiddle: http://jsfiddle.net/RgGHW/
Regardless though, glad you got an answer to your question!
Absolute positioning can get a bit annoying, i'm assuming you have a container div with a pixel width.
Just set a width on the container (button-sub heading) then set a width and auto margin on the button. The button will center in its container.
You have to have a width set on elements you want to center with margin:auto
Here is rough example: http://jsfiddle.net/hppXY/4/

Rounding the sides of a big image contained in a small division not working in Chrome

I am trying to rounding the sides of a background image with border-radius property.
Here is my scenario:
I placed a big image in a small division as background and put the overflow hidden. Now I need to round the small division. I successfully rounded the corner of small division. But the image's corner is not rounding.
HTML:
<div class="video_thumb">
<div style="background-image: url(http://img.youtube.com/vi/mAYX42saxkI/0.jpg); " class="video-thumbnail"></div>
</div>​
CSS
.video_thumb {
height: 250px;
width: 300px;
overflow:hidden;
margin:20px;
border: 1px solid red;
z-index:100;
position:relative;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
}
.video-thumbnail {
width: 520px;
height: 100%;
position: relative;
background-size: cover;
-moz-background-size: cover;
-webkit-background-size: cover;
background-position: center;
z-index:10;
overflow:hidden;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
}
Here is a demo using jsfiddle
You can see the top left and bottom left border are rounded. But top right and bottom right corner are not rounded. How can we make all the corners of image rounded?
I tried adding z-index, overflow: hidden to both divs, but no luck.
EDIT:
This problem is only with Google Chrome. Working fine on Firefox browser.
This appears to be a Chrome bug and you should consider raising it as such # http://code.google.com/p/chromium/issues/list
For now, you can "work around it" by changing position: relative to position: static
A Hacky Fix
As answered here, you can add a -webkit-mask-image to the parent element to hide the overflowing content:
.video_thumb {
-webkit-mask-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAA5JREFUeNpiYGBgAAgwAAAEAAGbA+oJAAAAAElFTkSuQmCC);
}
Just Updated your Fiddle. Hope this will solve your problem.
DEMO
http://jsfiddle.net/saorabhkr/e94q3/13/
Removed fixed width from this class (.video-thumbnail) and put background-size in your markup where you are adding image/video.
You can try using this piece of code http://jsfiddle.net/shubhanshumishra/e94q3/10/ You don't need to set border radius for both the wrapping div and the div with image.
Here is the code:
.video-thumbnail{
background-image: url(http://img.youtube.com/vi/mAYX42saxkI/0.jpg);;
width: 300px;
height: 250px;
border: 8px solid #666;
border-radius: 10px;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
}
​You can use the background-position property to clip the area you want. Also you can use the background-size property to stretch your background image as you want.
Here is the link to the updated fiddle: http://jsfiddle.net/shubhanshumishra/e94q3/14/