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

时间:2019-05-02 10:16:37

标签: html css

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

enter image description here

2)另外,由于某种原因,当我在section.welcome h1,p元素上进行更改时,此更改也将应用于.content部分的.comcome下面的p元素。

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

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>

2 个答案:

答案 0 :(得分:1)

问题之所以出现,是因为您要添加.welcIn的绝对位置,然后对其进行翻译。 父母无法从孩子那里获得身高。

您可以尝试删除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>

答案 1 :(得分:0)

您是否尝试过将overflow: auto;添加到溢出的CSS?可能是媒体或特定部分。