How to handle div size without using Hover

Asked

Viewed 669 times

5

Edited -- Despite having asked exclusively in css, I saw that in fact the final result in JS is much more professional and lean. Being so I confirmed the reply of @Renan as the one that actually helped. Yet I remark here that @Fetz’s response was really the best output for using CSS in this case. However the end effect is ugly, moving the whole page of place.

My doubt is simple. I have used much the :hover in CSS and has served me to some extent.

However I’ve been wanting something different. I wish I could click on one div so that it would grow in size, and I would have to click on it in a specific corner so that it would shrink.

The hover is part of the process, but when I take the mouse off the div it returns to previous size.

What I want is for div only change when I click on it. And if possible it only returns to its original form if I click on one X for example or outside the div in question.

I’ve searched a lot, I found some things in JS, but I’m doing my best not to use JS in this project. So I started to imagine that with PHP this is possible. Or some CSS keyword to put in stylesheet in place of hover.

Do you have any word reserved for this as for example a possible onmouseclick of life???

Grateful in advance.

#atredit{
    position:absolute;
    width: 150px;
    height: 75px;
    background-color: #F5F5F5;
    border: 1px solid #111111;
    border-radius: 5px;
    margin-top: 150px;
    margin-left: 200px;
    overflow: hidden;
    cursor:pointer;

}
#atredit:hover{
    width: 150px;
    height: 300px;
    transition: 0.5s ease-in-out;

}
  • Hanzer, even though it seems irrelevant in this case, consider that including a code snippet (just the snippet that when you take your mouse off the div it goes back to its original form) will greatly increase your chances of receiving answers.

  • 1

    thanks @gustavox

  • I edited my previous comment to include the excerpt that I think important you include.

  • Has like, using javascript.

  • 2

    PHP cannot change div sizes because it cannot modify its settings. But Jquery can perfectly solve your problem.

  • Ask me a question @Samuelneiva Is Jquery related to JS? It would be more interesting to use Jquery or JS?

  • 1

    Jquery is done in javascript, only more polished. Better to use jquery because it has classes ready to do what you want.

Show 2 more comments

2 answers

8


You can enter a class responsible for increasing the size of the div when this is clicked, for example, a class that expands the size of the div in 'x' pixels.

How you do this depends on the support you want to give users (actually the browser they use). There is the API classList but Internet Explorer still has partial implementation, such as you can see in this link. However I found that question in Stackoverflowen where there are ways around this "problem" in the IE.

An example using the API classList:

document.querySelector('div').addEventListener('click', function() {
  this.classList.toggle('ativa');
});
div {
  width: 200px;
  height: 100px;
  background: red
}
div.ativa {
  width: 400px
}
<div></div>

document.getElementById('foo').addEventListener('click', function() {
  this.classList.toggle('ativa');
});
#foo {
  width: 200px;
  height: 100px;
  background: red
}
#foo.ativa {
  width: 400px
}
<div id='foo'></div>

  • vlw @Renan! in case this is JS? Thank you anyway!

  • hello again @Renan! Being JS how do I proceed to instead of using div as in the example I use the id of div?

  • I understand it’s probably '#varid

  • document.getElementById("id-div").addEventListener(...); @Hanzerdjalgonn

  • I put it as the answer chosen by me, but I still could not make it work. That’s why I took it out now. I don’t know what to do. Entered with JS I get lost because I haven’t gone deep enough and so I insisted on the question to get an option out of JS. I put the snippet of Document.getElem... code in my html page between the script tags. I don’t know if you need anything else to make it work, because I don’t have enough knowledge in JS. If you can give me more strength I would be grateful

  • 1

    @Hanzerdjalgonn put where? In this case, the code would have to be inserted after the HTML element. If you try calling this function before (in the head, for example) it will not work. It is worth remembering that not only enters the question of Javascript but also CSS, I edited my answer and created a new snippet taking the div by id.

  • 1

    I tested it in several ways. It worked now. I saved the code on a page outside of html .js. I placed the code call above the </body> tag. It worked beautifully! Deeply grateful. Although it’s not only css I really appreciate the effort! vlw

Show 2 more comments

3

With css you can only do so:

If you use the pseudo selector target instead of the hover.

To use target, you need to have an id (eg:<div id="show">) in the growing element and a link pointing to the id of the element you want to grow (ex: show) prefixed # (ex:<a href="#show">)

html:

<a href="#">x</a>
<div id="show">
    <a href="#show">
      <img src="http://placekitten.com/g/300/201" alt="Kitten 2"/>
    </a>
</div>

css:

#show img {
    height: 150px;
}

#show:target img {
    height: 300px;
}

working example: http://jsfiddle.net/zzsLb6g9/

  • Uou!! This came very close! Really increased and I really appreciate it! However the page moved up. Is there an anchor I can place to fix this? Every time the page is called that anchor hold at a certain point?

  • That page-moving reaction really displeases me. so, although his answer was the one that got closer than I asked to accept the brother above because the end effect was really better on JS. However very grateful to you for showing me this possibility.

Browser other questions tagged

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