Position a circular image in the center

Asked

Viewed 181 times

0

I am creating a website and would like to position the image in the center.

inserir a descrição da imagem aqui

.circle {
  background-color: #aaa;
  border-radius: 50%;
  width: 150px;
  height: 150px;
  overflow: hidden;
  position: relative;
}

.circle img {
  position: absolute;
  bottom: 0;
  width: 100%;
}
<div id="conteudo"><!-- Inicio Conteudo -->
			
			<h1>Sobre Edson Luiz</h1>

			<div class="circle">
				<img src="./imgs/foto.jpg">
			</div>

			<p>Profissional ...</p>
</div><!-- Fim Conteudo -->

3 answers

2

To center the circle horizontally, as it is a block element (div), simply add the property:

margin: 0 auto;

The first value 0 represents the margins top/bottom and the second value auto the margins right/left. How is it auto of Automatic (automatic), the side margins will be divided equally and the element will be centered in the container:

.circle {
  background-color: #aaa;
  border-radius: 50%;
  width: 150px;
  height: 150px;
  overflow: hidden;
  position: relative;
  margin: 0 auto;
}

.circle img {
  position: absolute;
  bottom: 0;
  width: 100%;
}
<div id="conteudo"><!-- Inicio Conteudo -->
			
			<h1>Sobre Edson Luiz</h1>

			<div class="circle">
				<img src="./imgs/foto.jpg">
			</div>

			<p>Profissional ...</p>
</div><!-- Fim Conteudo -->

  • Remember that in this case it would also be interesting to center the title, including a css rule ". title { text-align: center; }" and changing the title to "<H1 class="title">About Edson Luiz</H1>".

0


Good morning Edson,

I changed your CSS, see if it meets your need. Follow example of the solution below:

Had considered the attribute left: 50% but the margin: 0 auto leaves better structured

.circle {
  background-color: #aaa;
  border-radius: 50%;
  width: 150px;
  height: 150px;
  overflow: hidden;
  position: relative;
  margin: 0 auto;
}

.circle img {
  position: absolute;
  bottom: 0;
  width: 100%;
}
<div id="conteudo"><!-- Inicio Conteudo -->
    <h1>Sobre Edson Luiz</h1>

    <div class="circle">
        <img src="./imgs/foto.jpg">
    </div>

    <p>Profissional ...</p>
</div><!-- Fim Conteudo -->

Hugs!

  • Hello Gustavo, thanks for the force. The code worked perfectly. I did some tests and even without the part after margin 0 it worked from the same jetio; . Circle img { margin: 0; background: Yellow; position: Absolute; top: 50%; left: 50%; margin-right: -50%; Transform: Translate(-50%, -50%)

  • For nothing Edson, I made an adjustment in response. A stackoverflow colleague showed a better structured way. Hugs

0

Elements <img> are by default inline-block i.e., you can apply block styles (such as width, height, border) and they follow the text flow.

Assuming that the image already in the desired size or proportion (in your case 150px x x 150px), you do not need "hacks" using position, margin or transform

Behold:

.img-circle {
   border-radius: 50%;
   width: 150px;
   height: 150px;
}

.text-center {
   text-align: center;
}
<div id="conteudo"><!-- Inicio Conteudo -->
<h1>Sobre Edson Luiz</h1>
<div class="text-center ">
    <img src="https://dummyimage.com/150x150/000/fff.jpg" class="img-circle">
</div>

<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
</div><!-- Fim Conteudo -->

Browser other questions tagged

You are not signed in. Login or sign up in order to post.