Элементы в контейнере перекрываются - PullRequest
1 голос
/ 02 апреля 2020

На странице я пытаюсь добавить подразделы, где вы можете добавить заголовок с текстовым полем, и он должен быть в центре.

Вот как это выглядит:

enter image description here

HTML

<div id="subsections">
   <div class="subsection">
      <div class="subsection-header"><input type="text" class="subsection-title"></div>
      <div class="subsection-right"><button class="iqsp-button-small red delete-section">X</button></div>
      <div class="subsection-content"></div>
   </div>
</div>

CSS

.subsection {
    position: relative;
    background-color: #006eaf;
    min-height: 50px;
    margin-bottom: 10px;
}
.subsection-header {
    margin: 0;
    position: absolute;
    top: 50%;
    left: 50%;
    -ms-transform: translate(-50%, -50%);
    transform: translate(-50%, -50%);
}
.subsection-right {
    position: absolute;
    right: 0px;
}

Теперь это все здорово, все работает как задумано. Тем не менее, мой следующий шаг - добавить таблицу ниже для создания вопросов:

HTML

.subsection {
  position: relative;
  background-color: #006eaf;
  min-height: 50px;
  margin-bottom: 10px;
}

.subsection-header {
  margin: 0;
  position: absolute;
  top: 50%;
  left: 50%;
  -ms-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
}

.subsection-right {
  position: absolute;
  right: 0px;
}
<div id="subsections">
  <div class="subsection">
    <div class="subsection-header"><input type="text" class="subsection-title"></div>
    <div class="subsection-right"><button class="iqsp-button-small red delete-section">X</button></div>
    <div class="subsection-content">
      <table class="responstable">
        <tbody>
          <tr>
            <th>No.</th>
            <th>Question</th>
            <th>Score</th>
            <th>Mandatory</th>
          </tr>
        </tbody>
      </table>
    </div>
  </div>
</div>

Здесь все запутано и выглядит так: enter image description here

Я вижу что-то происходит, что-то связано с абсолютными позициями, и затем стол начинается с той же позиции, поэтому он просто перекрывается.

Есть ли css, которые я могу добавить к .subsection-content или где-нибудь , чтобы это началось на следующей строке, так сказать?

Ответы [ 2 ]

0 голосов
/ 02 апреля 2020

Вы используете для заголовка подход, который будет центрировать его внутри подраздела как по горизонтали, так и по вертикали.

Последнее не очень удобно, когда таблица также находится внутри подраздела.

Позволяет исправить это , центрируясь только горизонтально, и оставляя немного места сверху

.subsection {
  position: relative;
  background-color: #006eaf;
  min-height: 50px;
  margin-bottom: 10px;
}

.subsection-header {
  margin: 0;
  position: relative;
  left: 50%;
  transform: translateX(-50%);
  width: fit-content;
  top: 10px;
  margin-bottom: 30px;
}

.subsection-right {
  position: absolute;
  right: 0px;
  top: 0px;
}
<div id="subsections">
  <div class="subsection">
    <div class="subsection-header"><input type="text" class="subsection-title"></div>
    <div class="subsection-right"><button class="iqsp-button-small red delete-section">X</button></div>
    <div class="subsection-content">
      <table class="responstable">
        <tbody>
          <tr>
            <th>No.</th>
            <th>Question</th>
            <th>Score</th>
            <th>Mandatory</th>
          </tr>
        </tbody>
      </table>
    </div>
  </div>
</div>
0 голосов
/ 02 апреля 2020

Вот ваши требования

.subsection {
  position: relative;
  background-color: dodgerblue;
  min-height: 50px;
  margin-bottom: 10px;
}

.subsection-header {
  margin: 0;
  position: absolute;
  top: 50%;
  left: 50%;
  -ms-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
}

.subsection-right {
  position: absolute;
  right: 0px;
}

.subsection-content {
  position: absolute;
  left: 0px;
  top: 100%;
}

.responstable {
  width: 100vw;
  text-align: center;
}
<div id="subsections">
  <div class="subsection">
    <div class="subsection-header"><label for="header">Header : </label><input type="text" class="subsection-title" id="header"></div>
    <div class="subsection-right"><button class="iqsp-button-small red delete-section">X</button></div>
    <div class="subsection-content">
      <table class="responstable">
        <tbody>
          <tr>
            <th>No.</th>
            <th>Question</th>
            <th>Score</th>
            <th>Mandatory</th>
          </tr>
          <tr>
            <td>1</td>
            <td>50</td>
            <td>40</td>
            <td>10</td>
          </tr>
        </tbody>
      </table>
    </div>
  </div>
</div>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...