-1

I have two elements: a back-button and a Headline. Both should be in the same row, the back button should be left aligned and the Headline should be centered.

Let's take this example:

<div class="wrapper">
  <div class="button">
    <button>Back</button>

    <div class="headline">
      <h2>Headline</h2>
    </div>
  </div>
</div>

What do I have to do in CSS to get this result: Image

I tried using Translate X, but of course this solution isn't a responsive one, that's why I am searching one for all view-ports.

Big thanks and have a good rest of the weekend!

Mario Petrovic
  • 7,500
  • 14
  • 42
  • 62
Vogtei
  • 13
  • 5

6 Answers6

3

Center everything in wrapper using flexbox, then move button to the left with absolute positioning.

.wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
}

button {
  position: absolute;
  left: 0;
}
<div class ="wrapper">

  <button>Back</button>

  <h2>Headline</h2>

</div>
Spankied
  • 1,626
  • 13
  • 21
1

Here's an easy way to do this if you know that it'll just be these 2 elements inside of the .wrapper container.

What you do is set the .wrapper to position: relative;, then set .button to position: absolute; this will allow you to position the .button inside of the .wrapper without taking up any space inside of the div. Then you set the .header to width: 100%; and text-align: center;

You'll need to play around with mobile, since the button will span over the header, at that point I would probably stack the elements to make it easier to for the user to click the back button.

.wrapper {
  position:relative;
  background: #dedede;
}

.button {
  position:absolute;
  left:0;
  top:50%;
  transform:translate(0, -50%);
}

.headline {
  width:100%;
  text-align:center;
}
<div class ="wrapper">
  <div class="button">
    <button>Back</button>
  </div>
  <div class ="headline">
    <h2>Headline</h2>
  </div>
</div>
tubstrr
  • 947
  • 2
  • 9
0

This solution is using css flex.

.wrapper{
  display: flex;
  flex-direction:row;
  justify-content: flex-start;
  align-items: baseline;
}
.headline{
  display: flex;
  flex-direction:row;
  justify-content: center;
  width:100%;
}
<div class ="wrapper">
  <div class="button">
    <button>Back</button>
  </div>

  <div class ="headline">
    <h2>Headline</h2>
  </div>
</div>
Daniel Paul
  • 495
  • 1
  • 6
  • 13
0

<div class="wrapper">
  <div class="button" style="float: left;">
    <button>Back</button>
  </div>
  <div class="headline">
    <h2 style="text-align: center;">Headline</h2>
  </div>
</div>

Actually, this is not exactly what you want.

But very simple and tricky way.

Code Plus
  • 150
  • 1
  • 12
0

Another easy way using css flex:

.wrapper {
  display: flex;
  align-items: center;
}

.headline {
   margin: auto;
}
<div class ="wrapper">
  <div class="button">
   <button>Back</button>
  </div>
  <div class ="headline">
   <h2>Headline</h2>
  </div>
</div>
Spankied
  • 1,626
  • 13
  • 21
yatinsingla
  • 434
  • 6
  • 13
0

using CSS GRID

this is responsive! centered,

and all the CSS code is in the parent selector!

  • (with flex, you will write a lot for this simple thing)

explanation

place-items are for centering. (is like writing justify-.. and align-..)

grid-template-column is for specifying how the grid should be (there are many ways to set the grid, but this is the shortest)

in fact, AUTO gets the width of the element and set to it, but 1FR gets

(AllWidthParent-AutoChildWidth)


Code

<div class="wrapper">
    <div class="button">
        <button>Back</button>
    </div>
    <div class="headline">
        <h2>Headline</h2>
    </div>
</div>

<style>
    .wrapper {
        display: grid;
        grid-template-columns: auto 1fr;
        place-items: center;
        width: 100%;
    }
</style>

https://jsfiddle.net/laaouatni/vg5L38am/1/

Laaouatni Anas
  • 4,199
  • 2
  • 7
  • 26