0

I would like to always keep the "Left" element next to the "Centered" element, taking into account that the "Centered" must always be centered:

enter image description here

Here is my code and a screenshot of how it looks at the moment:

* {
  margin: 0;
  padding: 0;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}

body {
  padding: 10px;
}

.container {
  background-color: #ccc;
  position: relative;
  text-align: center;
  overflow: hidden;
}

.container>div:first-child,
.container>div:nth-child(2) {
  padding: 10px 20px;
  background-color: #fA5858;
  color: #fff;
  text-transform: uppercase;
  font: 15px/1 Sans-Serif;
}

.container>div:first-child {
  position: absolute;
  display: block;
  width: 100px;
}

.container>div:nth-child(2) {
  display: inline-block;
  background-color: #5882FA;
  margin: 0 100px;
  white-space: nowrap;
}
<div class="container">
  <div>left</div>
  <div>Centered</div>
</div>

Code: https://jsfiddle.net/9shor0cb/26/

tacoshy
  • 10,642
  • 5
  • 17
  • 34
iWozzming
  • 13
  • 3

1 Answers1

-1

You could do something like this with flexbox:

body {
  padding: 10px;
}

.container {
  background-color: #ccc;
  display: flex;
  position: relative;
  text-align: center;
  overflow: hidden;
  justify-content: center;
}


.container .inner div:first-child,
.container .inner div:nth-child(2) {
  padding: 10px 20px;
  background-color: #fA5858;
  color: #fff;
  text-transform: uppercase;
  font: 15px/1 Sans-Serif;
}

.container .inner div:first-child {
  position:absolute;
  transform:translatex(-100%)
}

.container .inner div:nth-child(2) {
  background-color: #5882FA;
  white-space: nowrap;
}
<div class="container">
  <div class="inner">
  <div>left</div>
  <div>Centered</div>
  </div>
</div>
ATP
  • 2,939
  • 4
  • 13
  • 34