the image flow out the div, I expect it fit within the wrap because I've set width to the div wrapper. The image maybe not the same size for all, so I didn't set the img to a certain fix width to maintain it aspect ratio.
demo link
profile-pic-wrap {
width:200px;
height:200px;
border: 5px solid black;
}
img {
width:100%;
}
You left the . out of your class. But event with that (http://fiddle.jshell.net/293mW/1/) the image will pop out of the div. You could add overflow:hidden; to the div, but that will crop the image (http://fiddle.jshell.net/pzDVd/).
You probably want to use a background image instead and the background-size: cover; or background-size: contain; rule. See also https://developer.mozilla.org/en-US/docs/Web/CSS/background-size
jsFiddle example (or http://fiddle.jshell.net/Fhnk8/)
.profile-pic-wrap {
width:200px;
height:200px;
border: 5px solid black;
background-image: url(http://naijaparrot.com/wp-content/uploads/2013/10/mark-zuckerberg-le-fondateur-de-facebook.jpg);
background-size: cover;
}
Here is the correct code to solve your problem. This will resize any image into the container while maintaing the correct aspect ration.
Also, you should try to avoid background-size: cover because browser support is poor for older browsers.
http://fiddle.jshell.net/293mW/4/
.profile-pic-wrap {
width:200px;
height:200px;
border: 5px solid black;
}
img {
width:100%;
max-width: 100%
height: auto;
}
Related
I am trying to set an image as a background but the zoom version of image is coming.
My code
<!DOCTYPE html>
<html>
<head>
<style>
body, html {
height: 100%;
}
.bg {
/* The image used */
background-image: url("../../media/image1.jpeg");
/* Full height */
height: 100%;
/* Center and scale the image nicely */
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
</style>
</head>
<body class="bg">
</body>
</html>
How can I fix it?
You can use the propertie background-size, here is a following guide:
background-size: auto
Default value. The background image is displayed in its original size:
div{
width:100px;
height:100px;
border:5px solid blue;
background-image:url("https://googlechrome.github.io/samples/picture-element/images/kitten-small.png");
background-size:auto;
}
<div></div>
background-size: lenght
Sets the width and height of the background image. The first value sets the width, the second value sets the height. If only one value is given, the second is set to "auto". Read about length units
div{
width:100px;
height:100px;
border:5px solid blue;
background-image:url("https://googlechrome.github.io/samples/picture-element/images/kitten-small.png");
background-size:10px;
}
<div></div>
background-size: percentage
Sets the width and height of the background image in percent of the parent element. The first value sets the width, the second value sets the height. If only one value is given, the second is set to "auto"
div{
width:100px;
height:100px;
border:5px solid blue;
background-image:url("https://googlechrome.github.io/samples/picture-element/images/kitten-small.png");
background-size: 10%;
}
<div></div>
background-size: cover >good answer for this question<
Resize the background image to cover the entire container, even if it has to stretch the image or cut a little bit off one of the edges
div{
width:100px;
height:100px;
border:5px solid blue;
background-image:url("https://googlechrome.github.io/samples/picture-element/images/kitten-small.png");
background-size:cover;
}
<div></div>
background-size: contain >the best answer for this question<
Resize the background image to make sure the image is fully visible
div{
width:100px;
height:100px;
border:5px solid blue;
background-image:url("https://googlechrome.github.io/samples/picture-element/images/kitten-small.png");
background-size:contain;
}
<div></div>
background-size: initial
Sets this property to its default value.
background-size: inherit
Inherits this property from its parent element.
Source on background-size
Suggestion on following up read
am making a card where i have the image and description below but the image is too zoomed and doesnt look attractive i've tried to adjust the height and image but it doesnt work
HTML
<div id="event-card">
<div id="card-image">
<img src="{{ URL::to('/assets/photos/event3.jpg') }}">
</div>
<div class="container" id="card-details">
{{$event->eventName}}
</div>
</div>
This is the CSS
#event-card{
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2);
transition: 0.3s;
display: inline-block;
width:250px;
height:250px;
overflow: hidden;
margin-right:10px;
margin-bottom:10px;
border-radius: 8px;
margin-top:40px;
}
#card-image {
background-image:url('/churchill/public/assets/photos/event3.jpg');
height:60%;
width: 100%;
background-size:cover;
overflow:hidden;
background-repeat: no-repeat;
}
#event-cards{
width:80%;
margin-left:156px;
}
All well.. images.. biggest problem ever :D
Well you actually have few options.
I will be straightforward
img {
width: 100%;
height: 100%;
object-fit: cover;
}
This will make image look natural and not stretched but it might cut it on sides for that
img {
max-width: 100%;
height: auto;
}
This might be best solution for you. Image won't go over parent in width and it will go in height big enough to keep its aspect ratio and it will look natural. Play with it and see what looks best for you
PS: You also have
object-fit: fill;
object-fit: contain;
object-fit: cover;
object-fit: scale-down;
object-fit: none;
What is the original width and height of the image?
if the image height 500px and the width 500px and you set it width 500px and height 200px it will cause a problem like you facing, to avoid this issue you can set the image as a background you can create another div with the height and width you want and set this image as a background and you can control it using background-size:cover and background-position
I want to give background size as 636px and 1140px to background image instead of div. as I don't want the scroll of div because of its height.
If I give height and width to the parent div then I get background-image but when i give to background-size then it doesn't work.
.custom_app_phone_div {
margin: 5% auto 0 auto;
height:auto;
width:auto;
}
.custom_app_phone_div .custom_app_phone_bg_image_div {
background: url("http://i.imgur.com/jIE5Bf7.png") no-repeat;
background-position-x: center;
background-size:636px 1140px;;
width: 100%;
height: 100%;
padding: 10px;
}
<div class="custom_app_phone_div">
<div class="custom_app_phone_bg_image_div">
</div>
</div>
Any help would be great.
Thank you.
If not defined the div's height will be determined by the content's height.
What you could do is set the min-heigh property.
I'm not sure what you are asking for. But if you want to change the background image size without changing the div size you should either use an image <img/> or use a helper div as background (see example below).
Now, if you want to get rid of the scrolling bar you can set the parent container .custom_app_phone_div a height/width and set overflow: hidden.
Can you try this and let us know if this works for you?
.custom_app_phone_div {
margin: 5% auto 0 auto;
height:auto;
width:auto;
}
.custom_app_phone_div .bgHelper{
background: url("http://i.imgur.com/jIE5Bf7.png") no-repeat;
background-position-x: center;
background-size: contain;
height: 1140px;
width: 636px;
}
.custom_app_phone_div .custom_app_phone_bg_image_div {
}
<div class="custom_app_phone_div">
<div class="bgHelper"></div>
<div class="custom_app_phone_bg_image_div">
</div>
</div>
Set the size of background images to 636px x 1140px using any image editor and then use:
background: url("http://i.imgur.com/jIE5Bf7.png") center no-repeat;
Working fiddle here
How could I use img-responsive of bootstrap to a div as follows:
<div class="fill" style="background-image:url
('./images/abc.jpg');">
</div>
While I am trying to add img-responsive class inside the div along fill it doesn't work. How could I make the background image for the above div responsive?
div {
background-image:url('./images/abc.jpg');
background-repeat:no-repeat;
background-size:contain;
}
If the background-size property is set to "contain", the background image will scale, and try to fit the content area. However, the image will keep its aspect ratio (the proportional relationship between the image's width and height):
div {
width: 100%;
height: 400px;
background-image: url('img_flowers.jpg');
background-repeat: no-repeat;
background-size: contain;
border: 1px solid red;
}
If the background-size property is set to "100% 100%", the background image will stretch to cover the entire content area:
div {
width: 100%;
height: 400px;
background-image: url('img_flowers.jpg');
background-size: 100% 100%;
border: 1px solid red;
}
If the background-size property is set to "cover", the background image will scale to cover the entire content area. Notice that the "cover" value keeps the aspect ratio, and some part of the background image may be clipped:
div {
width: 100%;
height: 400px;
background-image: url('img_flowers.jpg');
background-size: cover;
border: 1px solid red;
}
Look, there is a nice technique using background-size property
.fill {
background: url(path/to/img.jpg) center center no-repeat;
-webkit-background-size: cover;
background-size: cover;
}
This rule will make your background image cover all container space and adjust when scaling the window.
Use background-size property to the value of 100% auto to achieve what you are looking for.
For instance,
.fill{
background-size:100% auto;
}
I have the following code:
.container {
width: 1rem;
height: 2rem;
background-image: url(https://dl.dropboxusercontent.com/u/1142760/static/svg/triangle.svg);
background-size: 100% 100%;
border: 1px solid black;
}
<div class="container"></div>
I expect the background image of the div to be stretched to the full width and height of said div.
Firefox behaves as expected:
But Chrome appears to have a bug that makes it interpret background-size differently:
You can see the live result in this fiddle
Re-creating the background image would not be a solution, as the div in question is of variable height.
Is there a work-around for this Chrome bug?
Maybe someone will find this post and not find the right answer.
I found out that in my case, i had to add an attribute to my svg File:
<svg preserveAspectRatio="none" ...
Well, maybe not the best solution, but at least it works fine for me in Chrome.
But you have to set the height (transform-scale) of the inner div with JQuery, according to your browser on load. Not really nice, but it works.
Also watch out for the -webkit-background-size.
As you can see, a lot of problems.
HTML:
<div class="container"><div id="picture"></div></div>
CSS:
.container{
width:1rem;
height:2rem;
border:1px solid black;
}
#picture {
width: 100%;
height: 100%;
background-image:
url(https://dl.dropboxusercontent.com/u/1142760/static/svg/triangle.svg);
-webkit-background-size: 100%;
-webkit-transform: scale(1,2);
}
http://jsfiddle.net/y0j5tejw/3/
Try this css for your code, it will definitely resolve your query.
.container{
background-image: url(https://dl.dropboxusercontent.com/u/1142760/static/svg/triangle.svg);
background-size: 100% 100%;
border:1px solid black;
float: left;
background-repeat: no-repeat;
width: 5%;
height: 30px;
}