As shown below, how to create with css to which if users save the image to his/her machine image should be square and full width and height as original picture.
You can achieve this effect by setting the border radius of the image container, and applying a hidden overflow value. An example is here - http://jsfiddle.net/8jbD5/1
your html would be something like:
<div id="imgCont">
<img src="theimage.jpg" />
</div>
and the css:
#imgCont{border:8px solid #f00;border-radius:50%;overflow:hidden;width:200px;height:200px}
#imgCont img{width:100%;height:100%;}
I hope this helps...
See this example: http://dabblet.com/gist/5450624 (Tested on Firefox 20/Chrome).
I used a 400x400 jpg image and I've adjusted its top/left offset.
relevant CSS
div {
position: relative;
width: 0;
height: 0;
border: 180px silver solid;
-webkit-border-radius: 180px;
-moz-border-radius: 180px;
border-radius: 180px;
}
figure {
position: absolute;
top: -120px;
left: -180px;
width: 200px;
height: 200px;
border: 10px red solid;
overflow: hidden;
-webkit-border-radius: 120px;
-moz-border-radius: 120px;
border-radius: 120px;
}
img {
position: absolute;
z-index: 1;
left: -100px;
top: -100px;
}
Markup
<div>
<figure>
<img src="...">
</figure>
</div>
Sample Output
You have a few choices.
Firstly, you could have an element (say a div) with its background image as your original image. Then inside that div you had an image with part transparency for the inside of the inner circle, so that the original image shows through underneath.
Secondly, you could do similar to above, but instead of using a transparent image you could create circles using a mixture of CSS and HTML. There's some nice demos here.
Thirdly, and probably the most hacky way, would be to just put everything in to one image (like in your question) and use .htaccess to serve a different file on direct access. You may not get very reliable results though. Here's a nice SO answer which explains.
Related
How can I make a div in HTML and apply a gradient background like in the picture:
I have tried some js-fiddle snippets but was unable to do some thing like this in the image it will be used in a pricing table for displaying latest discounts. The other alternate I have is to use the hard quote the image in my pricing table.
http://www.hongkiat.com/blog/css-gradient-border/
I tried the above link and applied border-radius:50% but didn't got the desired result.
Border images are not yet well supported, so I think you need to use two containers with one being styled with background: linear-gradient():
https://jsfiddle.net/nsvn00px/
<div class="circle">
<div class="circle__inner"></div>
</div>
CSS:
.circle {
position: relative;
width: 50px; height: 50px;
border-radius: 50%;
background: linear-gradient(to right, red, green);
}
.circle__inner {
position: absolute;
width: 46px; height: 46px;
top: 2px; left: 2px;
border-radius: 50%;
background-color: white;
}
This can also be done within one container with the use of pseudo classes, but I think this should give you a better idea how it works.
What i'm trying to do here, is to crop an image to replace white spaces of my div:
<div id="profilepicture" style='background:url()'>
<img src="utilisateurs/pp/<?php echo $userinfo['photoP']; ?>">
</div>
And here the stylesheet related to this:
#profilepicture{
height: 200px;
width: 200px;
box-shadow: 1px 1px 2px;
display: flex;
}
#profilepicture img{
width: 100%;
height: auto;
margin: auto;
}
Here is what it gives me :
I just want to crop left and right of the photo and zoom at the center to not have white spaces anymore !
Edit:
If i do
width: auto;
height: 100%;
it gives me this :
Now i just want it to be centered!
Any ideas ?
Using Clip
Use the clip property.
img {
position: absolute;
clip: rect(0, 100px, 100px, 0);
}
One thing to note. clip only works on something position: absolute or position: fixed. I don't know why. This limits its usefulness, but can be dealt with without too much trouble.
The second thing to note is about rect(). Its syntax (like many others in CSS) is rect(top, right, bottom, left);. Now pay attention here because it can be tricky. Both the top and the bottom values define the offset from the top border and the left and right values define the offset from the left border.
So clip: rect(40px, 260px, 150px, 80px); does the following.
Using Background-Image
Probably the easiest option in terms of assembly.
<div id="profilepicture" style='background-image:url('utilisateurs/pp/<?php echo $userinfo['photoP']; ?>')'>
</div>
div {
background-position: center;
}
This, more or less, does what you want, though it does have the side effect of using inline styles.
I suggest a background with "cover" property by moving the image to the background.
html:
<div id="profilepicture" style='background-image:url(utilisateurs/pp/<?php echo $userinfo['photoP']; ?>)'>
</div>
css:
#profilepicture{
height: 200px;
width: 200px;
box-shadow: 1px 1px 2px;
display: flex;
background-size:cover;
background-position:center;
}
Here is one way of doing it.
Set overflow: hidden on the parent container that contains the image.
You can then use absolute positioning to placed the left edge of the image to the center, and then use translateX to shift it to the left by 50% to center the image.
The net effect if to hide/crop the left and right edges of the image by hiding them (hide the overflow) in the parent container.
#profilepicture {
height: 200px;
width: 200px;
box-shadow: 1px 1px 2px;
overflow: hidden;
position: relative;
}
#profilepicture img {
width: auto;
height: 100%;
margin: auto;
display: block;
position: absolute;
top: 0;
left: 50%;
transform: translateX(-50%);
}
<div id="profilepicture">
<img src="http://placehold.it/400x300">
</div>
Center it, add a negative margin and hide the overflow:
#profilepicture{
height: 200px;
width: 200px;
box-shadow: 1px 1px 2px;
display: flex;
text-align:center;
overflow:hidden;
}
#profilepicture img{
width: auto;
height: 100%;
margin: 0 -50%;
}
<div id="profilepicture">
<img src="http://lorempixel.com/400/200/">
</div>
Here's what it looks like without the container:
<img src="http://lorempixel.com/400/200/">
OK, I hear you. I have just something very similar to this. There are a few ways to achieve this.
Use the not so well supported CSS property of object-fit - it does exactly what you want but you will need to use a poyfill if you want to support most browsers.
Set your image wrapper div to overflow:hidden as explained in this guide The guide is for circular images, so just omit the border-radius parts.
If you prefer to see an example I have done one for both options. First option is the one that is commented out and the second option is the active one: http://codepen.io/cactusa/pen/qZrobK
Reduce the height of the container (#profilepicture) because you have a square container with equal height and width but the img is a rectangle and doesn't have same height and width increasing the height of the img to 100% will cause distortion and look funny.
I have a rectangular sprite image that is 120px x 40px. When someone select the image I want the right side of the selected image to turn into an arrow pointing right.
I know how to use border-radius but that gives a curves whereas I want a point.
Using css how would I turn the right side of an image into a arrow?
Thanks
Basically I want to perform a border-radius only on the right side, but instead of curved pointed like an arrow.
.selected {
-webkit-border-radius: 0px 25px 25px 0px;
border-radius: 0px 25px 25px 0px;
}
If you can keep the white background here is a very simple solution:
jsFiddle here
Run the image in the background of the following example.
HTML
<div class="container"></div>
<div class="container"></div>
<div class="container"></div>
CSS
.container {
background: #333;
width: 200px;
height: 60px;
position: relative;
overflow: hidden;
margin-bottom: 10px;
}
.container:hover::after {
content: "";
position: absolute;
width: 70px;
height: 30px;
background: #fff;
top: -20px;
right: -20px;
z-index: 1;
transform: rotate(45deg);
}
.container:hover::before {
content: "";
position: absolute;
width: 70px;
height: 30px;
background: #fff;
bottom: -20px;
right: -20px;
z-index: 1;
transform: rotate(-45deg);
}
I do not know, i understood your question, but i think, what you want to achive, can be done by jQuery and css function with background-position
Basically, if you want to use a CSS Sprite image, background-position will indeed do it.
You may want to have a <div> positionned over your image, that will be displayed on hovering (CSS :hover) or click (jQuery click event) the image, depending on what you meant by "selecting" it.
Here is an example for hovering case (pure CSS) and here is an example for the clicking case (with 3 lines of jQuery).
I'm trying to display any images size as a square thumbnail by using css without cropping it before.
For example I have any size of an images that's x and y dimension if x less than y that means size of square thumbnail is x otherwise is y.
Here's simple picture that i try to explain.
I found the solution by use this CSS conmand
background-image: url(images/1000004_234262146740874_1109821750_n.jpg);
background-size: cover;
But i don't want to do like this because i want to use img html tag, and i don't want to check an image size because i want to use only css.
Here's my jsfiddle http://jsfiddle.net/K7pje/. thanks
Here's an update to your fiddle with a solution for portrait images. Unfortunately there is no way to do the same for landscape without using JavaScript.
http://jsfiddle.net/K7pje/4/
HTML:
<div class="container-working">
<div class="inner-container">
<img src="http://www.pa.msu.edu/people/frenchj/moon/moon-6day-1822.jpg" />
</div>
</div>
CSS:
.container-working {
width: 150px;
height: 150px;
overflow: hidden;
position: relative;
border: 3px solid #03F;
}
.inner-container {
position: relative;
top: -50%;
}
.inner-container img {
margin-top: 25%;
width: 100%;
}
Here is a FIDDLE in which there is a large background image (the full image is the upper one).
The two images below decrease the size of the div, and then reposition the image within the div.
CSS
.clippeddiv {
border: 1px solid black;
width: 200px;
height: 200px;
background: url('http://i.imgur.com/JLEPxT2.jpg');
background-position: 65% 35%
}
let's say I have to place an image RIGHT in a proper spot, but I need its CENTER to be in that spot. I wanted to place an image in the top-left corner of a div, so I placed the image in the div, gave position: relative to the div and position: absolute to the image then set its top and left values to 0. It quite worked but I'd need the CENTER of that image to be right over the top left corner. I'd do it manually setting top: -xpx, left: -ypx BUT I don't have any specific value for the image size (which could vary a lot).
So is there any way to say something like: position: absolute-but-i'm-talking-about-the-center; top: 0px; left: 0px;?
Thank you very much indeed!
Matteo
You could use javascript yo get the size of the image and then set the css left value needed.
Be mindful of the way images are loaded though as they are asynchronous so will not necesserily be available when the document is ready. This means that unless you handle the images correctly you will end up with width and height dimensions of 0.
You should wrap the image in another block element and put a negative left position to the image.
Something like this:
<div id="something">
<div class="imagewrap">
<img>
</div>
</div>
Then give #something a relative position, .imagewrap an absolute, etc... And img should have a relative position with left:-50%. Same for the top.
have you tried;
name_of_div_with_image {
display: block;
margin-left: auto;
margin-right: auto }
give that a go.
No need to use Javascript, this can be done in CSS.
The required HTML: (you must change the div to an img obviously)
<div id="container">
<div id="imgwrapper">
<div id="img">Change this div-tag to an img-tag</div>
</div>
</div>
The required CSS:
#container
{
position: absolute;
left: 200px;
top: 100px;
height: auto;
overflow: visible;
border: 2px dashed green;
}
#imgwrapper
{
position: relative;
margin-left: -50%;
margin-top: -50%;
padding-top: 25%;
border: 2px dashed blue;
}
#img
{
display: block;
width: 200px;
height: 100px;
border: 2px solid red;
}
Click here for a jsFiddle link
The margin-left: 50%; obviously works when using the container div, because the width of the container will be exactly that of the content. (You might need to add width: auto;)
But margin-top: -50%; will not work because the height of the container div will change with it, thus you need yet another wrapper div in which you use this margin-top: -50%; and then you need to fix this error it makes by using a positive percentage based padding. Obviously there may be other solutions to fix this, but the solution should be something like this.
Probably one of the simplest solutions is to place the image in the upper left corner at position
left: 0px; top: 0px; and then use translate to move its center to this position. Here's a working snippet for that:
#theDiv {
position: absolute;
left: 100px;
top: 100px;
background: yellow;
width: 200px;
height: 200px;
}
#theImage {
background: green;
position: absolute;
left: 0px;
top: 0px;
transform: translate(-50%, -50%);
}
<div id="theDiv">
<image width=31.41 height=41.31 id="theImage"></image>
</div>