I am trying to crop images in CSS but can't get it work as I wish. Here are three pictures to understand my problem :
The following screenshot represent what the "uncropped" elements look like.
For exemple, let's say I only want to keep the part of the first image going from 20% of its height to 60% of its height.
I tried to apply the following CSS to my red rectangle :
clip-path: polygon(0% 20%, 100% 20%, 100% 60%, 0% 60%);
And the result is
Which is not good because there is still some blank space around the first image (the gray background was added for better visualization.)
The result I am trying to achieve is this
I got that by hardcoding values but this won't be possible.
Is this achievable with CSS only, and knowing the width and height of the original image ?
Solution 1:
using object-fit, object-position :
.col {
margin: 50px auto;
width: 300px;
}
img {
width: 100%;
display: block;
object-fit: cover;
}
<div class="col">
<img style="object-position: 0 80%; height: 100px" src="https://source.unsplash.com/qap1hMjDA-g" />
<img src="https://source.unsplash.com/22CdQfKG8uM" />
</div>
Solution 2:
using background-size, background-position :
.col {
margin: 50px auto;
width: 300px;
}
.img1 {
background: url("https://source.unsplash.com/qap1hMjDA-g");
background-size: cover;
background-position: 0 70%;
height: 100px;
}
.img2 {
background: url("https://source.unsplash.com/22CdQfKG8uM");
background-size: cover;
height: 300px;
}
<div class="col">
<div class="img1"></div>
<div class="img2"></div>
</div>
Note that you need to know the height of the images if you're using
second solution
Related
I'm trying to apply CSS transformations to an image to achieve the following result:
right half of the image - main image - left half of the image
so that after CSS is applied to the image it looks like a "ribbon" with the image repeating after and before but not fully.
Is there a way to do this without changing the structure of the page (that is without using a div and background property)?
Example:
image to apply CSS to
result I want to achieve
I tried using background and it kinda works
background-image: url(/image.png);
background-repeat-x: repeat;
background-size: 50%;
background-position-x: 50%;
background-repeat-y: no-repeat;
height: 50px;
but I would like to get the same effect without using background rules but rules that apply to the image directly.
I tried also to apply the background rules to the img directly and got this result which could be a good middle-ground solution except for the missing image icon overlapping. The removal of this icon could also do the trick.
result by using background on img
Unfortunately, I cannot change the structure of the page (e.g. by wrapping the image into a div)
If you want to show certain parts of an image it's good practice to go with the background-position property in combination with background-size.
Other method is to cut the images to fit your needs and then display them seperatly.
I suggest you to go with my example:
.image {
height: 400px;
}
.image>div {
vertical-align: middle;
display: inline-block;
height: 100%;
}
.left,
.right {
width: 150px;
}
.left {
background: url("https://news.nationalgeographic.com/content/dam/news/2018/05/17/you-can-train-your-cat/02-cat-training-NationalGeographic_1484324.jpg") no-repeat right;
background-size: cover;
}
.middle {
background: url("https://news.nationalgeographic.com/content/dam/news/2018/05/17/you-can-train-your-cat/02-cat-training-NationalGeographic_1484324.jpg") no-repeat center;
background-size: cover;
width: 300px;
}
.right {
background: url("https://news.nationalgeographic.com/content/dam/news/2018/05/17/you-can-train-your-cat/02-cat-training-NationalGeographic_1484324.jpg") no-repeat left;
background-size: cover;
}
<div class="image">
<div class="left">
</div>
<div class="middle">
</div>
<div class="right">
</div>
</div>
I hope you like cats. I do.
I'm trying to solve an issue with my own css stylesheet along with bootstrap. I'm trying to have an image set as a background image on my index page, but can seem to display it.
CSS in my stylesheet:
.main-image{
background-image: url(img/studio.png);
height:100%;
width: 100%;
}
HTML:
<section class="row main-image"></section>
Adding 100% width/height won't work here, because the parent of the element doesn't have static dimensions.
Instead of using height/width: 100%, use 100vh and 100vw.
1vh - Relative to 1% of the height of the viewport.
1vw - Relative to 1% of the width of the viewport.
So your code has to be:
body { margin: 0 } /* Removed the default body margin */
.main-image{
height: 100vh;
width: 100vw;
background-image: url('http://www.superedo.net/wallpapers/wallpapers/Android%20Tablet/Huawei%20Mediapad%2010%20Fhd/huawei_mediapad_10_fhd_005.jpg');
background-size: cover;
}
<section class="row main-image">
<!-- -->
</section>
You need to define what space the image should be in
Fiddle example here
.main-image{
height: 500px;
width: 100%; /* responsive */
background-image: url('https://www.prestigeanimalhospital.com/sites/default/files/08-cat-cancer-4.jpeg');
background-size: cover;
background-position: 100% 50%;
}
<section class="main-image"></section>
You can control what is shown with background-position: 100% 50%;
100% is X - the horizontal value.
50% is Y - the vertical value.
Or do it with background-position-x and background-position-y
I am trying to achieve this with css3, I tried using border-radius with percent values and it's not the same always, I always got a rounded corners and the border will start disappearing on the corners too.
I want it to be exactly the same as the example image:
EDIT:
this is my html code :
<div class='container-fluid'>
<section class='section-1'>
<div class='container'>
</div>
</section>
And this is my css:
.section-1 {
background-image: url('../images/bg.png');
background-size: cover;
background-repeat: no-repeat;
position: relative;
background-position: 50%;
}
If you want, you can accomplish this using only CSS by creating a <div> that would be used only as a mask. You can create the round effect with the border-radius property, but you need to do it bigger than the part that will be visible, then crop the result to show only the curvy part that you want. And you must compensate the imagem position.
Check my example below:
.oval-header {
width: 100%;
height: 150px;
overflow: hidden;
position: relative
}
.oval-header--mask {
width: 200%; /* Mask width 2x the size of the header */
height: 200%; /* Mask height 2x the size of the header */
transform: translate(-25%, -51%); /* This compensates the size of the image and places the curvy part that you want on the certer of the mask, it's a translate that negativates half it's width and half it's height */
border: 6px solid yellow;
border-radius: 0 0 50% 50%;
overflow: hidden;
border-top: 0;
background-image: url('http://www.placecage.com/3000/1500');
background-size: cover
}
<div class="oval-header">
<div class="oval-header--mask">
</div>
</div>
Need some help with CSS background repeat. Below is the wire-frame for the functionality I am trying to achieve.
Current Code:
HTML:
<div class="wrapper">
<div class="container"></div>
</div>
CSS:
.container{
min-height: 10000px;
background-image: url(background1.png), url(background2.png);
background-repeat: no-repeat, repeat-y;
background-position: center top, center 1000px;
}
The current code displays background1 only one time and repeats background2 as I want,but the background2 image starts from the top of the page. I want it to start exactly after the background1 image ends as shown in the wireframe.
NOTE: Both the images background1 and background2 have transparent shapes in them which makes makes the other image visible in the background.
If you set a background to repeat, it can not be limited (AFAIK)
the solution would be to limit it to a pseudo element, and limit this pseudo element to where you want it (with the top property)
.test {
width: 300px;
height: 600px;
border: solid black 1px;
position: relative;
}
.test:after {
content: "";
position: absolute;
width: 100%;
height: 100%;
top: 200px;
background-image: url(http://placekitten.com/g/600/400);
background-repeat-y: repeat;
}
<div class="test"></div>
Note that the height of 100% is not accurate, if you want it to be accurate set it to your dimension
I have this fiddle which generates single country flags from a image sprite. I want to squeeze each flag because the width of flag seems to be too wide.
JSFiddle Demo
For instance the Norwegian flag is too wide in the jsfiddle sample.
How can I do this? Thank you.
#flag1 {
width: 120px;
height: 60px;
background-image: url(http://i.hizliresim.com/e7Y5dm.png);
background-position: -120px 0;
}
#flag2 {
width: 120px;
height: 60px;
background-image: url(http://i.hizliresim.com/e7Y5dm.png);
background-position: -480px 13800px;
}
#flag3 {
width: 120px;
height: 60px;
background-image: url(http://i.hizliresim.com/e7Y5dm.png);
background-position: -1200px 19020px;
}
To get exactly what you wanted I used background-size just to reduce the width of your sprite.
So I reduced the width of the sprite about one sixth and adjusted the width of the element in accordance.
#flag3
{
width: 100px;
height: 60px;
background: url(http://i.hizliresim.com/e7Y5dm.png) 0 0 no-repeat;
background-position: -1000px -480px;
background-size: 1500px 780px;
}
Demo
One solution is to scale (transform:scale(x);) the whole element (the div in this case)
For example transform:scale(0.5); will scale the element to half its size, but keep in mind that it retains the initial space in the DOM flow.
Another way is to use the background-size property to resize your image, but you will have to recalculate the positioning as well..
demo at http://jsfiddle.net/JA97b/5/
Additionally, in your CSS you should group common properties to a single class and apply that instead of repeating tem for each flag..
.flag{
width: 120px;
height: 60px;
background-image: url(http://i.hizliresim.com/e7Y5dm.png);
}
#flag1{background-position: -120px 0;}
#flag2{background-position: -480px 13800px;}
#flag3{background-position: -1200px 19020px;}
and use
<div class="flag" id="flag1"></div>
<div class="flag" id="flag2"></div>
<div class="flag" id="flag3"></div>