Skip to content Skip to sidebar Skip to footer

Display Text Outside Of Containing Element

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. If I then go and add a margin-top: -50px; to the

Solution 1:

Using absolute positioning.

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

.slider {
  width: 100%;
  height: 650px;
  margin-top: 40px;
  background: orange;
  box-shadow: 0078px11px#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;
}
<divclass="slider"><h1>
    Happy Fruit
  </h1><h2>
    HELLO WORLD
  </h2></div>

Solution 2:

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: 0078px11px#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><divclass="slider"><h1>
      Happy Fruit
    </h1><h2>
      HELLO WORLD
    </h2></div></body>

Solution 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: 0078px11px#F3286B;
  /* don't overlap container */z-index: -1;
}

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

h1 {
  font-size: 11vw;
}

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

Post a Comment for "Display Text Outside Of Containing Element"