Проблема стилизации при отображении поля вне контейнера - PullRequest
0 голосов
/ 08 мая 2020

Я пытаюсь найти решение проблемы, которую я не мог решить самостоятельно. Пожалуйста, посмотрите и скажите мне, что пошло не так в этом коде

.bar {
  position: relative;
  width: 100%;
  height: 4px;
  background: #eee;
}

.bar .progress {
  position: absolute;
  top: 0;
  left: 0;
  width: 0;
  height: 100%;
  background: #4ece99;
  border-radius: 16px;
  transition: width 1s ease-out;
  display: flex;
  overflow: hidden;
  font-size: .75rem;
}

.bar .progress span {
  position: absolute;
  top: -12px;
  left: 100%;
  margin-left: -28px;
  font-size: 10px;
  font-weight: bold;
  text-align: center;
  min-width: 28px;
  line-height: 22px;
  padding: 0;
  border: 2px solid #345;
  border-radius: 8px;
  background: #fff;
}



.bar2 {
  position: relative;
  width: 100%;
  height: 4px;
  background: #eee;
}

.bar2 .progress {
  /*position: absolute;*/
  top: 0;
  left: 0;
  width: 0;
  height: 100%;
  background: #4ece99;
  border-radius: 16px;
  transition: width 1s ease-out;
  display: flex;
  overflow: hidden;
  font-size: .75rem;
}

.bar2 .progress span {
  position: absolute;
  top: -12px;
  left: 100%;
  margin-left: -28px;
  font-size: 10px;
  font-weight: bold;
  text-align: center;
  min-width: 28px;
  line-height: 22px;
  padding: 0;
  border: 2px solid #345;
  border-radius: 8px;
  background: #fff;
}
Current: 
<div class="bar">
  <div class="progress" style="width: 50%">
    <span>50</span>
  </div>
</div>
Expected
<div class="bar2">
  <div class="progress" style="width: 50%">
    <span>50</span>
  </div>
</div>

Если вы запустите фрагмент кода, вы увидите два индикатора выполнения. На первом индикаторе выполнения число внутри поля должно быть видно, как показано в второй индикатор выполнения, но именно в этом месте. Любые предложения будут полезны. Спасибо

Ответы [ 2 ]

1 голос
/ 08 мая 2020
  1. position:relative до .bar .progress {...}.
  2. position:absolute и right:0; до span(.first) это переместится к последнему в зависимости от позиции в родительском элементе, т.е. bar .

скрипка для игры.

.bar {
  position: relative;
  width: 100%;
  height: 4px;
  background: #eee;
}

.bar .progress {
  position: relative;
  width: 70%;
  /* change this make sure it works */
  top: 0;
  left: 0;
  height: 100%;
  background: #4ece99;
  border-radius: 16px;
  transition: width 1s ease-out;
  font-size: .75rem;
}

.first {
  position: absolute;
  right: 0;
  top: -12px;
  font-size: 10px;
  font-weight: bold;
  text-align: center;
  min-width: 28px;
  line-height: 22px;
  border: 2px solid #345;
  border-radius: 8px;
  background: #fff;
}

.bar2 {
  position: relative;
  width: 100%;
  height: 4px;
  background: #eee;
}

.bar2 .progress {
  /*position: absolute;*/
  top: 0;
  left: 0;
  width: 0;
  height: 100%;
  background: #4ece99;
  border-radius: 16px;
  transition: width 1s ease-out;
  display: flex;
  overflow: hidden;
  font-size: .75rem;
}

.bar2 .progress span {
  position: absolute;
  top: -12px;
  left: 100%;
  margin-left: -58px;
  font-size: 10px;
  font-weight: bold;
  text-align: center;
  min-width: 28px;
  line-height: 22px;
  padding: 0;
  border: 2px solid #345;
  border-radius: 8px;
  background: #fff;
}
Current:
<div class="bar">
  <div class="progress">
    <span class="first">50</span>
  </div>
</div>

Expected
<div class="bar2">
  <div class="progress" style="width: 50%;">
    <span>50</span>
  </div>
</div>
1 голос
/ 08 мая 2020

Приведенный ниже код даст ожидаемые результаты. Вы установили абсолютное положение для диапазона bar1, что и центрировало его. Спасибо.

.bar {
  position: relative;
  width: 100%;
  height: 4px;
  background: #eee;
}

.bar .progress {
  /*position: absolute;*/
  top: 0;
  left: 0;
  width: 0;
  height: 100%;
  background: #4ece99;
  border-radius: 16px;
  transition: width 1s ease-out;


  font-size: .75rem;
}

.bar .progress span {
  position: relative;
  top: -12px;
  left: 100%;
  margin-left: -28px;
  font-size: 10px;
  font-weight: bold;
  text-align: center;
  min-width: 28px;
  line-height: 22px;
  padding: 5px;
  padding-left: 9px;
  padding-right: 9px;
  border: 2px solid #345;
  border-radius: 8px;
  background: #fff;
}



.bar2 {
  position: relative;
  width: 100%;
  height: 4px;
  background: #eee;
}

.bar2 .progress {
  /*position: absolute;*/
  top: 0;
  left: 0;
  width: 0;
  height: 100%;
  background: #4ece99;
  border-radius: 16px;
  transition: width 1s ease-out;
  display: flex;
  overflow: hidden;
  font-size: .75rem;
}

.bar2 .progress span {
  position: absolute;
  top: -12px;
  left: 100%;
  margin-left: -28px;
  font-size: 10px;
  font-weight: bold;
  text-align: center;
  min-width: 28px;
  line-height: 22px;
  padding: 0;
  border: 2px solid #345;
  border-radius: 8px;
  background: #fff;
}
Current: 
<div class="bar">
  <div class="progress" style="width: 50%">
    <span>50</span>
  </div>
</div>
Expected
<div class="bar2">
  <div class="progress" style="width: 50%">
    <span>50</span>
  </div>
</div>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...