How to fix a content or a background to a div

Asked

Viewed 211 times

1

I’d like to do with that div roll along with the page but its contents or the contents of a before that would be in the case of the image (if applicable), it would be fixed so that the content (or background) appears to disappear when scrolling the page.

In fact, as I did the fund was fixed but it was not disappearing according to what I scroll down the page. The idea is to make like one overflow: hidden.

#teste {
  width: 100%;
  height: 100%;
  position: relative;
}

#teste:before {
  content: '';
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  background-image: url("https://upload.wikimedia.org/wikipedia/commons/8/8f/%24700%2C000_Home.jpg");
  background-size: cover;
  position: fixed;
  overflow: hidden;
}
<div id=teste></div>

eu <br />
eu <br />
eu <br />
eu <br />
eu <br />
eu <br />
eu <br />
eu <br />
eu <br />
eu <br />
eu <br />
eu <br />

  • I’m not sure I understand, but is that what you want? https://repl.it/@Costamilam/Hurtfulhilariouskeygens

  • No! What I want to do is what happens on the banner at the top of this site: https://www.banesecard.com.br/Home/

  • But could you change this example to just div.imagem and a block of me <br /> as in the question to make the effect that occurs in div.imagem continue to occur? However, instead of content before ter 'Imegem', I would like there to be an image, it can be google, that had the dimensions of the div.

  • I don’t quite understand what you want, it seems you already have what you want, just pack the z-index how hugocsl responded. You can inspect element and see how it was done on the site to ask any questions

  • Carlos I completely edited the answer, I think now is more in agreement with the effect you wanted

1 answer

1


This technique is actually not with an element with position:fixed, but rather an element with background-attachment: fixed. That way the element rolls with the screen, but the background of it no, woe to that effect kind of parallax.

inserir a descrição da imagem aqui

Follow the image code above:

html, body {
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
    font-size: 70px;
    color: red;
}

.fixo {
    width: 100%;
    height: 100px;
	background-image: url("https://upload.wikimedia.org/wikipedia/commons/8/8f/%24700%2C000_Home.jpg");
    background-size: cover;
    background-attachment: fixed;
        background-position: center center;

}
.con {
/* essa altura é só para vc ver melhor o exemplo, mas vc não precisa definir uma altura aqui*/
    height: 100%; 
}
<div class="fixo">
    conteúdo qq
</div>
<div class="con">
    resto do content
</div>

OBS: The only caveat left is about the background-attachment: fixed on iOS of older iPhones as you can see here Background problem with Iphone 6, 7 and 8

  • Hi Hugo, that’s not it! What I want to do is what happens on the banner at the top of this site: https://www.banesecard.com.br/Home/

  • @Carlosrocha now understood better, I made a complete edition of the answer, including the technique is different, different than what we initially thought... ;)

  • Um, I get it. But the image needs to be the size of the div?

  • @Carlosrocha not young, here .fixo { ... height: 100px; you can put the height you think is best, even in pixels, in the other div I put a height just to have scroll on the page and you can see and test the effect. https://imgur.com/IALMY03 with 100px height in . fixed will be like this

  • hum. But this height: 100px is the size of the div. What I need is for the image to have the size of the div. I put background-size: cover; and it didn’t work. Don’t need to use a :before?

  • Carlos why ::before if the image is the background of the main div? You are complicating things for no reason...

  • should be. But here for me, the background image is much larger than the div. And the goal is for the image to remain the height of the div.

  • @Carlosrocha I edited the answer and put a background-position: center center; to center the image in the size of the div, you can change to type top left if you want to see if it’s more to your liking... I changed the height so you see that the image respects the size of the div, because it is the background of the div itself and there is no way to be bigger than it....

  • Now only this doubt: Knowing that the height of the image will always be 32% of the width and knowing that the width of the image will always be the width of a div container. How do I calculate the height of that div containner so that it never pushes the Divs down? I tried height: Calc (100vw * 0.3251622); but it didn’t work -

  • @Carlosrocha in the good young lady, that’s another question... if you have doubt with something else opens another question, poses your problem, image of how this one and how you need it etc, then I or someone else can help you with this problem. About the effect you wanted I think you already solved

  • of agreement. Thank you. I will post another!

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.