Move element up in 50% of its size

Asked

Viewed 68 times

1

How could I do a vertical overlay of Flexbox? I want to make half the circle inside the above item.

html, body {
  height: 100%;
}

body {
  background: #8844ee;
}

.card {
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}

.background-header { 
  width: 400px;
  height: 400px;
  background: white;
  display: flex;
  flex-direction: column;
  
}

.topo {
  background-color: #1b2b34;
  background-size: cover;
  width: auto;
  height: 200px;
}

.foto {
  background-color: red;
  width: 75px;
  height: 75px;
  border: 3px solid white;
  border-radius: 50px;
  align-self: center;
}

.textos > h1,
.textos > h3{
  display:inline;
}

.textos {
  text-align: center;
}
<div class="card">
  <div class="background-header">
    <header class="topo"></header>
    <section class="foto">
    </section>
    <section class="textos">
      <h1>Nome</h1>
      <h3>23</h3>
      <p>Cidade</p>
    </section>
  </div>
</div>

  • Which item above? The class .topo?

  • this is the background item at the top of the card.

2 answers

2


You can use the CSS property transform for move the element up by 50% of its size. For this, use the transformer translateY, that move the element on axis Y.

So:

transform: translateY(-50%);

So that:

  • translateY informs that we want to modify the position of the element in the Y axis;
  • -50% indicates that we want to move the element upward 50% of its size.

In your case:

.foto {
  transform: translateY(-50%);
}

html, body {
  height: 100%;
}

body {
  background: #8844ee;
}

.card {
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}

.background-header { 
  width: 400px;
  height: 400px;
  background: white;
  display: flex;
  flex-direction: column;
  
}

.topo {
  background-color: #1b2b34;
  background-size: cover;
  width: auto;
  height: 200px;
}

.foto {
  background-color: red;
  width: 75px;
  height: 75px;
  border: 3px solid white;
  border-radius: 50px;
  align-self: center;
}

.textos > h1,
.textos > h3{
  display:inline;
}

.textos {
  text-align: center;
}
<div class="card">
  <div class="background-header">
    <header class="topo"></header>
    <section class="foto">
    </section>
    <section class="textos">
      <h1>Nome</h1>
      <h3>23</h3>
      <p>Cidade</p>
    </section>
  </div>
</div>

I put it on top of the style sheet to make it easier to find.

  • Ah, the unexplained negative, what a marvel!

2

Applying the property margin-top inserting -10% of the value that is positioned!

html, body {
  height: 100%;
}

body {
  background: #8844ee;
}

.card {
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}

.background-header { 
  width: 400px;
  height: 400px;
  background: white;
  display: flex;
  flex-direction: column;
  
}

.topo {
  background-color: #1b2b34;
  background-size: cover;
  width: auto;
  height: 200px;
}

.foto {
  background-color: red;
  width: 75px;
  height: 75px;
  border: 3px solid white;
  border-radius: 50px;
  align-self: center;
  margin-top: -10%;
}

.textos > h1,
.textos > h3{
  display:inline;
}

.textos {
  text-align: center;
}
<div class="card">
  <div class="background-header">
    <header class="topo"></header>
    <section class="foto">
    </section>
    <section class="textos">
      <h1>Nome</h1>
      <h3>23</h3>
      <p>Cidade</p>
    </section>
  </div>
</div>

I hope you helped him!

  • 2

    +1 Your answer is very good, but it works if you keep the height of fixed photo in 75px, that is, it does not adapt according to the size of the photo. In this case the answer from @Luizfelipe is the one that fits best :)

  • Yes, yes, I agree, but as it seemed to me a layout that would not necessarily go adjusting according to the height and width of the page, the option would be the most "valid", but consequently for future updates of a tag, the best option is that the others adjust as changes, so there are no headaches! Thanks for the touch @Cmtecardeal

Browser other questions tagged

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