Display text outside of containing element

…衆ロ難τιáo~ 提交于 2019-12-10 16:58:31

问题


I want to achieve this. Keep an eye on the top text 'Happy Fruit'. I want to be overlayed of the box while it's nested inside it.

body {
  background: yellow;
  padding: 50px;
}
.slider {
    width: 100%;
    height: 650px;
    margin-top: 40px;
    background: orange;
    box-shadow: 0 0 78px 11px #F3286B;
}
h1, h2 {
  text-transform:uppercase;
  color: red;
}
h1 {
  font-size: 11vw;
}
h2 {
  font-size: 7vw;
}
<body>
<div class="slider">
<h1>
Happy Fruit
</h1>
<h2>
HELLO WORLD
</h2>
</div>
</body>

If I then go and add a margin-top: -50px; to the h1 the text will stay inside the div, but how can I make it going above/standing on it on it while it's still being nested inside (html)? I've tried playing with z-index but that didn't work.

position: relative; top: -50px;


回答1:


What's wrong with adjusting the position of <h1/>? You can offset the position by adding padding to .slider.

.slider {
  position: relative; <!-- necessary in case other position is set in ancestry -->
  padding-top: 10px;
}
h1 {
  position: absolute;
  top: -10px;
}

If overflow: hidden; is set on .slider, your header will be cut off. Otherwise the default is overflow: visible; and you should have no trouble.

body {
  background: yellow;
  padding: 50px;
}

.slider {
  width: 100%;
  height: 650px;
  margin-top: 40px;
  background: orange;
  box-shadow: 0 0 78px 11px #F3286B;
  position: relative;
  padding-top: 10px
}

h1,
h2 {
  text-transform: uppercase;
  color: red;
}

h1 {
  font-size: 11vw;
  position: absolute;
  top: -10px;
}

h2 {
  font-size: 7vw;
}
<body>
  <div class="slider">
    <h1>
      Happy Fruit
    </h1>
    <h2>
      HELLO WORLD
    </h2>
  </div>
</body>



回答2:


Using absolute positioning.

body {
  background: yellow;
  padding: 50px;
}

.slider {
  width: 100%;
  height: 650px;
  margin-top: 40px;
  background: orange;
  box-shadow: 0 0 78px 11px #F3286B;
  position: relative;
  padding-top: 1.2em;
}

h1,
h2 {
  text-transform: uppercase;
  color: red;
}

h1 {
  font-size: 11vw;
  position: absolute;
  top: -1.2em;
}

h2 {
  font-size: 7vw;
}
<div class="slider">
  <h1>
    Happy Fruit
  </h1>
  <h2>
    HELLO WORLD
  </h2>
</div>



回答3:


You can use linear-gradient here and for box-shadow you can use absolutely positioned pseudoelement (with needed offset).

For example, with 40px offset background-image: linear-gradient(to bottom, transparent 40px, orange 40px);. Also we should apply top: 40px for pseudoelement.

Demo:

body {
  background: yellow;
  padding: 50px;
}

.slider {
  width: 100%;
  height: 650px;
  background-image: linear-gradient(to bottom, transparent 40px, orange 40px);
  position: relative;
}

.slider:before {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  top: 40px;
  bottom: 0;
  box-shadow: 0 0 78px 11px #F3286B;
  /* don't overlap container */
  z-index: -1;
}

h1,
h2 {
  text-transform: uppercase;
  color: red;
}

h1 {
  font-size: 11vw;
}

h2 {
  font-size: 7vw;
}
<div class="slider">
  <h1>
    Happy Fruit
  </h1>
  <h2>
    HELLO WORLD
  </h2>
</div>


来源:https://stackoverflow.com/questions/45418991/display-text-outside-of-containing-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!