htmlimagecssstylescss-position

Style an image to be at the top-right of a div


I want to style an image to be at the top right corner of a div, how do I achieve this?

I want to style this to reflect this screenshot

<html>
<head>
    <style>
        .first{
            width: 200px;
            height: 200px;
            color: blue;
            border: blue;
            border-style: solid;
            border-color: blue;
            background: blue;
        }
    </style>
</head>

<body>
<div class="first">testing this forum
</div>
</body>
</html>

Solution

  • div {
        height: 175px;
        width: 175px;
        background: blue;
        position: relative;
        /* Additionally padding? */
    }
    
    div > img {
        height: 25px;
        width: 25px;
        position: absolute;
        top: 0px; right: 0px; 
        /* Change top < 0 and right < 0 to be outside */
    }
    <div>
      Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque velit elit, tempus non suscipit id, pretium a mi. Aliquam erat volutpat.
      <img src="http://maciejczyzewski.me/assets/images/prints/head.jpeg" />
    </div>