Display text outside of containing element

I want to achieve this. Keep an eye on the top text of "Happy Fruit." I want to be superimposed on the field when it is nested inside it.enter image description here

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>
Run codeHide result

If I then go over and add margin-top: -50px;in h1, the text will remain inside the div, but how can I make it higher / stand on it while it is still nested inside (html)? I tried playing with z-index, but that didn't work.

position: relative; top: -50px;

enter image description here

+4
source share
3 answers

What is wrong with position preference <h1/>? You can compensate for the position by adding indentation to .slider.

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

overflow: hidden; .slider, . overflow: visible;, .

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>
Hide result
+1

.

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>
Hide result
+1

linear-gradient, box-shadow ( ).

, 40px offset background-image: linear-gradient(to bottom, transparent 40px, orange 40px);. top: 40px .

:

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>
Hide result
0

Source: https://habr.com/ru/post/1682672/


All Articles