html - 浏览器调整大小时文本溢出

标签 html css

1) 知道如何在浏览器调整大小时阻止该文本溢出吗?我在较宽的屏幕上有一张照片,但当宽度变小时,我给 div 一个背景颜色,并设置浏览器不显示图片。

enter image description here

2) 此外,出于某种原因,当我对 section.welcome h1,p 元素进行更改时,此更改也应用于 .content 部分的 p 元素,该部分位于 .welcome 下方。

我认为问题在于在图像上显示文本的代码。当我将其注释掉时,文本根本不会溢出,但即使屏幕尺寸更大,我也必须完全删除图像。

HTML代码:

html,
body {
  background-color: rgb(250, 250, 250);
  font-family: 'Open Sans', sans-serif;
  margin: 0px;
  min-height: 100%;
}

body {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

.navbar {
  background-color: #1A212B;
  display: flex;
  padding: 15px;
  color: white;
  z-index: 1;
}

.navlink-brand {
  font-size: 1.5em;
  margin-right: auto;
}

.navlink {
  padding-right: 8px;
}

.navlink a {
  font-size: 1.1em;
  color: rgb(255, 255, 255);
  text-decoration: none;
  transition: 0.2s;
  font-weight: 500;
  cursor: pointer;
  font-weight: lighter;
}

.navlink a:hover {
  color: #707A85;
}

.navItems {
  display: flex;
  align-items: center;
}

.navlink-Toggle {
  display: none;
}

@media only screen and (max-width: 768px) {
  .navItems,
  .navbar {
    flex-direction: column;
  }
  .navItems {
    display: none;
  }
  .navToggleShow {
    display: flex;
  }
  .navlink-Toggle {
    align-self: flex-end;
    display: initial;
    position: absolute;
    cursor: pointer;
  }
  .navlink {
    margin: 10px;
  }
  .navlink-brand {
    margin-left: 0;
  }
}

.welcome {
  text-align: center;
  width: 100%;
  min-height: 100%;
  height: auto;
  position: relative;
  background-size: cover;
  background-position: center;
}

@media only screen and (max-width: 800px) {
  .welcome {
    min-height: 25vh;
    background-color: #707A85;
  }
  .welcome img {
    display: none;
  }
}


/*center welcome text,above img*/

.welcome>.welcIn {
  top: 50%;
  left: 50%;
  margin: 0;
  position: absolute;
  transform: translate(-50%, -50%);
}

.welcome img {
  opacity: 0.8;
  max-width: 100%;
  width: auto;
  height: auto;
}

.welcome h1 {
  text-shadow: 6px 6px px #1A212B;
  font-weight: 900;
  font-size: 2em;
}

.welcome p {
  text-shadow: 6px 6px 6px #1A212B;
  font-weight: 900;
  font-size: 1.1em;
}

.welcome h1,
p {
  font-size: 3em;
  color: rgb(255, 255, 255);
  margin: 15px 0;
  line-height: 1;
}


/*for sticky footer*/

.all-wrapper {
  flex: 1 0 auto;
}

.content {
  width: 60%;
  margin: auto;
}

.content h2 {
  color: black;
  text-align: center;
}

.content p {
  color: black;
  font-size: 18px;
  line-height: 20px;
  word-wrap: break-word;
  text-align: left;
}

footer {
  flex-shrink: 0;
  border: solid 1px #1A212B;
  width: 100%;
  height: 50px;
  background-color: #1A212B;
}


/*footer p*/

.last {
  text-align: right;
  color: rgb(255, 255, 255);
  margin: 0 10px;
  line-height: 50px;
}
<!DOCTYPE html>

<head>
  <link rel="stylesheet" type="text/css" href="style.css">
</head>

<body>
  <div class="all-wrapper">
    <div class="navbar"> //nav here
    </div>

    <main>
      <section class="welcome">
        <img src="header.png">
        <div class="welcIn">
          <h1>Welcome to </h1>
          <p class="descr">A website where you can .</p>
        </div>
      </section>

      <section class="content">
        <h2>Text text text text text</h2>
        <p>Text text text textText text text textText text text textText text text textText text text textText text text textText text text textText text text textText text text text</p>
        <p>Text text text textText text text textText text text textText text text textText text text textText text text textText text text textText text text textText text text text</p>
        <p>Text text text textText text text textText text text textText text text textText text text textText text text textText text text textText text text texte</p>
      </section>

    </main>
  </div>
  <footer>
    <div class="last"></div>
  </footer>
  <script src="script.js"></script>
</body>

</html>

最佳答案

问题来了,因为您要添加带有绝对位置的 .welcIn,然后对其进行翻译。 parent 无法从 child 那里得到高度。

您可以尝试删除 position: absolutetransform: translate(-50%, -50%);。然后添加一点填充,这会有所帮助。

html,
body {
  background-color: rgb(250, 250, 250);
  font-family: 'Open Sans', sans-serif;
  margin: 0px;
  min-height: 100%;
}

body {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
  -ms-flex-direction: column;
  flex-direction: column;
  height: 100vh;
}

.navbar {
  background-color: #1A212B;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  padding: 15px;
  color: white;
  z-index: 1;
}

.navlink-brand {
  font-size: 1.5em;
  margin-right: auto;
}

.navlink {
  padding-right: 8px;
}

.navlink a {
  font-size: 1.1em;
  color: rgb(255, 255, 255);
  text-decoration: none;
  transition: 0.2s;
  -webkit-transition: 0.2s;
  -moz-transition: 0.2s;
  -ms-transition: 0.2s;
  font-weight: 500;
  cursor: pointer;
  font-weight: lighter;
}
.navlink a:hover {
  color: #707A85;
}
.navItems {
  display: flex;
  align-items: center;
}
.navlink-Toggle {
  display: none;
}
@media only screen and (max-width: 768px) {
  .navItems,
  .navbar {
    -webkit-box-orient: vertical;
    -webkit-box-direction: normal;
    -ms-flex-direction: column;
    flex-direction: column;
  }
  .navItems {
    display: none;
  }
  .navToggleShow {
    display: flex;
  }
  .navlink-Toggle {
  -ms-flex-item-align: end;
  align-self: flex-end;
  align-self: flex-end;
  display: initial;
  position: absolute;
  cursor: pointer;
  }
  .navlink {
    margin: 10px;
  }
  .navlink-brand {
    margin-left: 0;
  }
}

.welcome {
  text-align: center;
  width: 100%;
  min-height: 100%;
  height: auto;
  position: relative;
  background-size: cover;
  background-position: center;
}

@media only screen and (max-width: 800px) {
  .welcome {
    background-color: #707A85;
  }
  .welcome img {
    display: none;
  }
}


/*center welcome text,above img*/

.welcome>.welcIn {
   padding: 20px 0;
}

.welcome img {
  opacity: 0.8;
  max-width: 100%;
  width: auto;
  height: auto;
}

.welcome h1 {
  text-shadow: 6px 6px px #1A212B;
  font-weight: 900;
  font-size: 2em;
}

.welcome p {
  text-shadow: 6px 6px 6px #1A212B;
  font-weight: 900;
  font-size: 1.1em;
}

.welcome h1,
p {
  font-size: 3em;
  color: rgb(255, 255, 255);
  margin: 15px 0;
  line-height: 1;
}


/*for sticky footer*/

.all-wrapper {
  flex: 1 0 auto;
}

.content {
  width: 60%;
  margin: auto;
}

.content h2 {
  color: black;
  text-align: center;
}

.content p {
  color: black;
  font-size: 18px;
  line-height: 20px;
  word-wrap: break-word;
  text-align: left;
}

footer {
  flex-shrink: 0;
  border: solid 1px #1A212B;
  width: 100%;
  height: 50px;
  background-color: #1A212B;
}


/*footer p*/

.last {
  text-align: right;
  color: rgb(255, 255, 255);
  margin: 0 10px;
  line-height: 50px;
}
<!DOCTYPE html>

<head>
  <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
  <div class="all-wrapper">
    <div class="navbar"> //nav here
    </div>
    <main>
      <section class="welcome">
        <img src="header.png">
        <div class="welcIn">
          <h1>Welcome to </h1>
          <p class="descr">A website where you can .</p>
        </div>
      </section>
      <section class="content">
        <h2>Text text text text text</h2>
        <p>Text text text textText text text textText text text textText text text textText text text textText text text textText text text textText text text textText text text text</p>
        <p>Text text text textText text text textText text text textText text text textText text text textText text text textText text text textText text text textText text text text</p>
        <p>Text text text textText text text textText text text textText text text textText text text textText text text textText text text textText text text texte</p>
      </section>
    </main>
  </div>
  <footer>
    <div class="last"></div>
  </footer>
  <script src="script.js"></script>
</body>
</html>

关于html - 浏览器调整大小时文本溢出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55950247/

相关文章:

javascript - 根据按钮单击移动到特定的 div

html - 在家谱中的单个节点上结束两个节点

python - Brython:每 [...] 微秒移动元素

html - MaterialiseCSS 卡片设计

javascript - 没有元标记的响应式网页?

css - 将 Photoshop 中设置的字母跟踪值转换为 CSS 中的等效字母间距

css - AngularUI Bootstrap Popover 闪烁

css - 为什么这个 svg 不受滤镜影响?

html - 悬停仅在从容器 IE 外部进入时有效

javascript - Adobe animate CC Javascript gotoandplay 延迟