0
<header className='header'>
        <div className='header--center'>
          <h1>Title of the website</h1>
        </div>
        <div className='header--right'>
          <p>Some text pushed to the right</p>
        </div>
</header>

I want "Title of the website" to be centered and I want "Some text pushed to the right" to be pushed to the right of the container without messing up the centering of "Title of the website"

Temani Afif
  • 245,468
  • 26
  • 309
  • 415
tyty
  • 1

2 Answers2

0

You can use flex boxes:

.header--right{display: flex;
justify-content: end;}

and:

.header--center{display: flex;
justify-content: center;}

For reference: https://css-tricks.com/snippets/css/a-guide-to-flexbox/ Also, you can use text-align on the p tag and the h1 tag

0

One way is

*,
*::before,
*::after {
  box-sizing: border-box;
  margin:0;
}


body{
  min-height: 100vh;
  overflow: hidden;
  background-color: bisque;
  
}

p{
  font-size: 3rem;
  font-weight: 900;
}

.header-container{
  position: relative;
  display: flex;
  flex-direction: row;
  justify-content: center;
  
}

 .text-right{
  margin-left: auto;
} 

.text-center{
  position: absolute;
  
} 

.container{
  position: absolute;
  width: 50vw;
  height: 100vh;
  border: 5px solid black;
}
<div class="header-container">
      <div class="text-center">
        <p>CENTER</p>
      </div>
      <div class="text-right">
        <p>RIGHT</p>
      </div>
    </div>
<div class="container"></div>

If you don't want to use position:absolute here is another way.

*,
*::before,
*::after {
  box-sizing: border-box;
  margin:0;
}

body{
  min-height: 100vh;
  overflow: hidden;
  background-color: bisque;
}

p{
  font-size: 3rem;
  font-weight: 900;
}

.header-container{
  display: flex;
  flex-direction: row;
  justify-content: center;
}

 .text-right{
  margin-left: auto;
} 

.text-center{
  margin-left: 50%;
  transform: translate(-50%);
} 

.container{
  position: absolute;
  width: 50vw;
  height: 100vh;
  border: 5px solid black;
}
<div class="header-container">
      <div class="text-center">
        <p>CENTER</p>
      </div>
      <div class="text-right">
        <p>RIGHT</p>
      </div>
    </div>
<div class="container"></div>
UPinar
  • 1,067
  • 1
  • 2
  • 16