Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to place image inside of a parent div properly

I have a div like this:

<div class="p-5 mb-3 mainInfo">
           <div class="circle"><img src="https://nanoclub.ir/images/cut.png"></div>
</div>

And result goes like this:

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

However, it should be showing inside the div like this:

enter image description here

And here is the CSS:

    .mainInfo{
        background-color: #fff;
        border: .01rem round #e0e1ed;
        border-radius: 20px;
        color: #585CA1;
        height:50px;
    }
    .circle {
        position:absolute;
        width:150px;
        height:170px;
        border-radius:50%;
        background:#fff;
        right:100px;
        top:40%;
        transform:translateY(-50%);
    }

So how can I place the image inside circle div properly like the expected image?

>Solution :

I achieved this by absolute positioning and ::before selector. Also I added border radius to the image with a max width.

HTML:

<div class="mainInfo">
  <div class="circle">
    <img src="https://picsum.photos/200" />
  </div>
</div>

CSS:

.mainInfo{
  background-color: #fff;
  border: .01rem round #e0e1ed;
  border-radius: 20px;
  color: #585CA1;
  width:100%;
  height:5em;
  box-shadow: 0px 0px 17px -5px rgba(0,0,0,0.75);
  margin-top: 3em;
  display: flex;
  align-items: center;
  justify-content: center;
}

.circle {
  position: relative;
  width: 8em;
  height: 8em;
  background: #fff;
  border-radius: 50%;
  box-shadow: 0px 0px 17px -5px rgba(0,0,0,0.65);
}

.circle:before{
    position: absolute;
    content: "";
    width: 15em;
    height: 5em;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: #fff;
}

.circle img {
  position: absolute;
  max-width: 85%;
  border-radius: 50%;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 200;
}

Link to the Demo

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading