将文本设置为始终位于另一个元素之上

时间:2018-04-07 22:46:28

标签: html css

我有一个常规框,我希望文本浮动在它上面,并适当调整文本的字体大小。

这就是我所做的:https://jsfiddle.net/a87uo3t2/

@import url('https://fonts.googleapis.com/css?family=Share+Tech+Mono');
body {
  margin: 0 auto;
  overflow: hidden;
}

#content {
  width: 100%;
  height: 2527px;
  background-image: url('../images/wallpaper.jpg');
}

#menu-container,
#button-container {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  margin: auto;
  width: 30%;
  height: 40%;
  border: 1px line #ccc;
  background-color: #ffffff;
  background-image: url('../images/brick-wall.png');
  box-shadow: 0 0 3em #ccc;
}

#button-container {
  border: none;
  box-shadow: none;
}

.button-sub-container {
  display: inline-flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

h1 {
  position: relative;
  bottom: 120px;
  left: 6px;
  text-align: center;
  font-family: 'Share Tech Mono', monospace;
  font-size: 5vw;
  text-transform: uppercase;
  text-decoration: none;
  letter-spacing: 15px;
}


/*BUTTONS*/

.main-button {
  text-decoration: none;
  font-family: 'Share Tech Mono', monospace;
  font-size: 35px;
  margin: 15px;
  color: #000;
  letter-spacing: 5px;
  text-transform: uppercase;
  transition: letter-spacing 0.5s, color 0.8s;
}

.main-button:hover {
  letter-spacing: 15px;
  color: red;
}

.main-button:active {
  letter-spacing: 2px;
  color: blue;
}
<div id="content">
  <div id="menu-container">
    <h1>welcome</h1>
    <div id="button-container" class="button-sub-container">
      <a class="main-button" href="about.html">about</a>
      <a class="main-button" href="projects.html">projects</a>
      <a class="main-button" href="contact.html">contact</a>
    </div>
  </div>

正如您所看到的,我已尝试通过定位relatively来设置它,但这意味着在调整大小时,我可以通过使用vw作为字体大小来解决这个问题,因为如果有一个非常宽的监视器,它将超过我的小menu-container

侧面注意:不要注意按钮 - 文本突破开箱即用,我有一个媒体键来解决这个问题

1 个答案:

答案 0 :(得分:0)

使用flexbox

的解决方案

并仅更改框的font-size

&#13;
&#13;
@import url('https://fonts.googleapis.com/css?family=Share+Tech+Mono');
html,
body {
  height: 100%;
}

body {
  margin: 0;
  display: flex;
  justify-content: center;
  align-items: center;
}

.box {
  font-family: 'Share Tech Mono', monospace;
  max-width: 50%;
  text-align: center;
  /* Change to your needs */
  font-size: 20px;
}

.box h1 {
  text-transform: uppercase;
  margin-top: 0;
}

.box ul {
  list-style: none;
  margin: 0;
  padding: 15px;
  border: 1px line #ccc;
  background-color: #ffffff;
  background-image: url('../images/brick-wall.png');
  box-shadow: 0 0 3em #ccc;
}

.box a {
  text-decoration: none;
  color: #000;
  text-transform: uppercase;
  white-space: nowrap;
  transition: letter-spacing 0.5s, color 0.8s;
}

.box a:hover {
  letter-spacing: 3px;
  color: red;
}


/* Change to your needs */

@media screen and (min-width: 600px) {
  .box {
    font-size: 20px;
  }
}


/* Change to your needs */

@media screen and (min-width: 1200px) {
  .box {
    font-size: 30px;
  }
}
&#13;
<div class="box">
  <h1>Heading</h1>
  <!-- It's a list -->
  <ul>
    <li><a href="#">Site 1</a></li>
    <li><a href="">Site 2</a></li>
    <li><a href="">Site 3</a></li>
  </ul>
</div>
&#13;
&#13;
&#13;