I am given a rectangular picture of width=300px and height=300px.
I want to show this picture inside a circle and came up with the following CSS:
img {
width: 300px;
height: 300px;
background-color: white;
border-radius: 50%;
border: 2px solid black;
}
The issue that I am having is that the picture doesn't really seem to fit nicely inside the circle. It seems way to big and I think that's because the circular form is cropping off parts of the image. So, how could I avoid this? I guess I could simply upload a smaller image. An image of size 200px x 200px, however, would likely be distorted since the circle is too big for it and thus the image would be enlarged.
Any suggestions?
Here is an example of the picture I am adding:
And here it is in a circle:
I would do this with a background image, which gives you more control over how it sits in its container (in this case a <div>).
You can set the background size and position to accommodate a non-symmetric image such as yours.
div {
width: 300px;
height: 300px;
background-color: white;
border-radius: 50%;
border: 2px solid black;
background-image: url(https://i.imgur.com/z7YIJI6.png);
background-size: 80% 80%;
background-position: 50% 70%;
background-repeat: no-repeat;
}
<div></div>
Results:
Suggestion using <div> as wrapper:
img {
z-index: -1;
width: 300px;
height: 300px;
margin: auto;
display: block;
margin-top: 45px;
position: relative;
}
div {
width: 350px;
height: 350px;
display: block;
border-radius: 50%;
border: 2px solid black;
}
<div>
<img src="https://i.stack.imgur.com/oWAqE.png">
</div>
Related
.fotoPerfil{
width: 150px;
position: absolute;
margin-top: -6em;
border: solid;
border-width: 5px;
border-color: white;
border-radius: 50%;
}
<div class="d-flex justify-content-center">
<img class="fotoPerfil" src="./img/manu.png">
</div>
Hello, I have a little problem with this image, idk if you can see it but the border does not fully cover part of the pic, there is a thin space at the top of the image.
I made the border red and added a random photo from google images to help make this snippet a little more visual. This is how I would go about making the photo inside the border and having a container to make the whole thing position absolute if thats still needed for something.
.absolute {
position: absolute;
}
.fotoPerfil {
width: 150px;
height: 150px;
border: 5px solid red;
border-radius: 50%;
overflow: hidden;
}
.fotoPerfil img {
width: 100%;
height: 100%;
object-fit: cover;
}
<div class="absolute">
<div class="d-flex justify-content-center fotoPerfil">
<img src="https://i.natgeofe.com/k/830b5d15-92db-429f-a80a-cc89b5700af5/mt-everest.jpg?w=636&h=437">
</div>
</div>
I am trying to make something like this...
where the blue is the div, and the black is the logo. how is something like this achieved? I was messing around with the transform but this also does one side.
This is a great opportunity to use CSS shapes!
If you think of the blue shape as "a blue rectangle that is immediately followed by a blue downward-pointing triangle, with no gap between them", then we just need to figure out how make that triangle and put it in the right place.
Let's start with your current HTML & CSS (I'm basing this on the screenshot, and assuming the logo element is outside the blue <div>):
.pointy {
background-color: #0086FD;
height: 285px;
}
.logo {
background-color: #000;
height: 200px;
margin: 0 auto;
transform: translateY(-30%);
width: 200px;
}
<div class="pointy"></div>
<div class="logo"></div>
No need to modify your HTML here. We're going to use the :after pseudo element to add the triangle shape after the div.
I used the handy CSS Triangle Generator to get a triangle started using border properties.
A few other details:
adding position: relative to the div, so that...
we can position the triangle at the bottom with position: absolute and top: 100%
we're applying width: 100vw to the div, because...
since the triangle is created using borders, and borders can't be a percentage width, we can set the two relevant border widths to 50vw, and they'll be exactly half the width of the 100vw parent
Let's make the triangle red for the moment, so you can see it clearly.
.pointy {
background-color: #0086FD;
height: 285px;
position: relative;
width: 100vw;
}
.pointy:after {
border-color: #f00 transparent transparent transparent;
border-style: solid;
border-width: 50px 50vw 0 50vw;
content: '';
display: block;
height: 0;
position: absolute;
top: 100%;
}
.logo {
background-color: #000;
height: 200px;
margin: 0 auto;
transform: translateY(-30%);
width: 200px;
}
<div class="pointy"></div>
<div class="logo"></div>
Final solution
Now that we have created and positioned our triangle, let's make it the same color as the div. (I've also tweaked the vertical positioning of .logo to achieve the desired effect.)
Voila: pointy blue div, no extra HTML needed.
.pointy {
background-color: #0086FD;
height: 285px;
position: relative;
width: 100vw;
}
.pointy:after {
border-color: #0086FD transparent transparent transparent;
border-style: solid;
border-width: 50px 50vw 0 50vw;
content: '';
display: block;
height: 0;
position: absolute;
top: 100%;
}
.logo {
background-color: #000;
height: 200px;
margin: 0 auto;
transform: translateY(-20%);
width: 200px;
}
<div class="pointy"></div>
<div class="logo"></div>
I am customising a website in WordPress(CMS). I want to add some elements in my website as background design.
It look something similar like this:
I google and found a way to do it - Using a builder tool in CMS - Elementor.
The good thing is, in Elementor there is a way to add background-img and control background-position.
The bad thing is, I have successfully added and control the element moving around until the place that I want. But The background element seems cannot cross the <section> which mean they will only stay in their own container.
I figured it out another way to do it, which is add the <img> at the current page. Then use position: absolute to position it properly.
But I prefer not to do that way.
Example snippet:
#section-1 {
background-color: #000;
width: 100%;
height: 200px;
color: white;
padding: 20px;
}
#section-2 {
background-color: yellow;
background-image: url(https://temp1.asign.pro/wp-content/uploads/2019/05/element-2.png);
background-position: -150px -223px;
background-repeat: no-repeat;
width: 100%;
height: 200px;
color: black;
padding: 20px;
}
<section class="section" id="section-1"></section>
<section class="section" id="section-2"></section>
The issue with using the triangles as a background-image is that you'll never be able to position them 'out' of the section. The background is a part of the element and can only go as far as the element's dimensions. However, you can make use of the pseudo element and position them absolutely, like so:
#section-1 {
background-color: #000;
width: 100%;
height: 200px;
color: white;
padding: 20px;
overflow: visible;
}
#section-2 {
background-color: yellow;
width: 100%;
height: 200px;
color: black;
padding: 20px;
overflow: visible;
position: relative;
}
#section-2::before{
content: '';
position: absolute;
top: -70px;
left: 0;
width: 200px;
height: 200px;
background: url(https://temp1.asign.pro/wp-content/uploads/2019/05/element-2.png) no-repeat center center/100%
}
<section class="section" id="section-1"></section>
<section class="section" id="section-2"></section>
Im sure this is really easy but i have been looking at this issue for a little while and my brain has gone blank. I have a div that then has an image inside of it. The image seems to just overflow the div border and its driving me mad. I have an image below to show you what is happening along with the css.
#avatar {
float: left;
width: 50%;
height: 300px;
border: 1px solid black;
}
#avatar img {
width: 100%;
height: auto;
}
<div id="avatar">
<img src="http://i.imgur.com/dkzoNCc.png"></div>
I have a border on the main div #avatar just so i can see the whole size of the div. All i want is for the image to scale to the size of the div. If i set the height to 100% it goes into the div just fine but when resizing it it starts to overflow the div. I want the image to resize on the width not height.
Am i missing something really simple here? I dont want to use overflow hidden on the image as that will just cut some of it off i believe.
Thanks everyone
Try below css for img.
use height: 100%; for maximum height
display: block;margin: auto; for centering
max-width: 100%; to fit large images
Please check the example with large and small images.
#avatar {
float: left;
width: 50%;
height: 300px;
border: 1px solid black;
}
#avatar img {
height: 100%;
max-width: 100%;
display: block;
margin: auto;
}
<div id="avatar">
<img src="http://www.baraodasfestas.com.br/Assets/Produtos/SuperZoom/0431_MICKEY_635703672330071491.jpg" alt="">
</div>
<div id="avatar">
<img src="https://upload.wikimedia.org/wikipedia/en/d/d4/Mickey_Mouse.png" alt="">
</div>
Just add:
#avatar img {
max-width: 100%;
}
#avatar {
float: left;
width: 50%;
height: 300px;
border: 1px solid black;
}
#avatar img {
/*width: 100%;*/
height: auto;
max-width: 100%;
height:100%;
}
<div id="avatar">
<img src="http://i.imgur.com/dkzoNCc.png"></div>
It's because of your height:auto for the <img>
Just use :
#avatar img
{
width: 100%;
height: 100%;
}
But this will stretch you image. So if you want full size image inside your container you need to stretch your container instead. Like
#avatar
{
float: left;
display: inline-block;
width: 50%;
height: auto;
border: 1px solid black;
}
#avatar img
{
width: 100%;
height: 100%;
}
I have an image within an image div container. I vertical and horizontal align it. But when i do so using transform - i lose the bottom border of one of my images, and am not quite sure why.
html:
<div class="imageContainer">
<img class="myImage" src='x'>
</div>
css:
.imageContainer {
width: 20px;
max-height: 25px;
height: 25px;
position: relative;
background-color: lightblue;
}
.myImage {
width: auto;
height: auto; /* Set to 9.2px in fiddle example to force the problem. */
max-height: 25px;
max-width: 20px;
border: 1px solid black;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
update:
The reason for this is that the auto sized image land on a decimal value. (fx 9.2px) which is rounded down. So as I understand it, it wraps my 9.2 height element in a border and then rounds it down to 9px which makes the bottom border vanish. (even with overflow: visible)
Anyway to force auto values not to land on a decimal value? or something along those lines.
fiddle example : http://jsfiddle.net/dLLan/24/ (the problem only happens in firefox, so make sure to run the fiddle in firefox.)
Since you updated your question, here is what I did:
1. Added a new div (#imgholder) to hold the myImg div.
2. Added the customized width and height of 20px and 25px on #imgholder.
3. Changed width and height of #myImage to height: auto; & width: 100%; to make it fit comfy on #imgholder.
#imageContainer {
float: left;
position: relative;
background-color: lightblue;
}
#imgholder {
width: 20px;
height: 25px;
}
#myImage {
height: auto;
width: 100%;
border: 1px solid black;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
<div id="imageContainer">
<div id="imgholder">
<img id="myImage" src="http://s7.postimg.org/k8e5116ff/806c278b5a0f77b98f4dcc469f2d0b08.jpg" />
</div>
</div>