为什么 CSS text-align:left 不能与 Simplebar 一起使用?

Why doesn't CSS text-align:left work with Simplebar?

我遇到了 text-align:left 的问题:我的消息(文本)没有左对齐。到目前为止,我已经尝试了几种方法:添加 width:100%、float:left 以及 display:block。不幸的是,其中 none 有效。我当时在想 simplebar 可能会影响它,尽管我不知道当时是如何影响的。如果您能以某种方式帮助我,将不胜感激! 这是我的 CSS 和 HTML:

<h2 id="receiver" name="{{receiver}}">Chat with {{receiver}}</h2>
    <div data-simplebar id="my-element" class="history">
      {% for message in hist_list %}
        {{message}}<br>
      {% endfor %}
    </div>

.history {
  position: absolute;
  top: 210px;
  left: 249px;
  transform: translate(-50%, -50%);
  height: 416px;
  text-align:left;
  width:100%;
}
* {
  box-sizing: border-box;
  margin:0;
  padding:0;
  text-align: center;
  font-family: 'Exo', sans-serif;
  color: black;
}

这是 Simpebar,可能会干扰我的 CSS:

[data-simplebar] {
    position: relative;
    flex-direction: column;
    flex-wrap: wrap;
    justify-content: flex-start;
    align-content: flex-start;
    align-items: flex-start;
}

我认为您应该从通用选择器中删除 text-align:center;。希望下面的代码片段对您有所帮助。

* {
  box-sizing: border-box;
  margin:0;
  padding:0;
/*   text-align: center; */
  font-family: 'Exo', sans-serif;
  color: black;
}
#receiver{
  text-align:center;
}

.history {
  position: absolute;
  height: 416px;
  text-align:left;
  width:100%;
}


[data-simplebar] {
    position: relative;
    flex-direction: column;
    flex-wrap: wrap;
    justify-content: flex-start;
    align-content: flex-start;
    align-items: flex-start;
}
<h2 id="receiver" name="">Chat with Name</h2>
<div data-simplebar id="my-element" class="history">
  <p>This is a message</p><br>
  <p>This is a message</p><br>
</div>

所以您可以做一件小事来解决您的问题,只需在所有事情之上调用您的通用选择器代码,

就这样,

* {
  box-sizing: border-box;
  margin:0;
  padding:0;
  text-align: center;
  font-family: 'Exo', sans-serif;
  color: black;
}
.history {
  position: absolute;
  top: 210px;
  left: 249px;
  transform: translate(-50%, -50%);
  height: 416px;
  text-align:left;
  width:100%;
}

CSS是级联的,优先写在后面的代码,看这个小例子

p {color: red; font-size: 100px;}
p {color: green; font-size: 50px;}
<p>Test</p>

所以在给定的示例中,下面编写的代码,即绿色的代码,将获得优先权,因此最好在代码顶部编写通用选择器,然后使用所有其他东西你想在下面覆盖。

只需删除转换和绝对

.history {
    /* position: absolute; */
    /* top: 210px; */
    /* left: 249px; */
    /* transform: translate(-50%, -50%); */
    height: 416px;
    text-align: left;
    width: 100%;
}