What is the process for performing a task by simply pushing a button?

I adjusted the length of the box to 0, but now I want it to change to a different value when the button is pressed. However, the current code does not seem to be working as intended:

    .down button:focus .info {
         height: 200px;
    }

For more clarity, please refer to the images and accompanying code below. My goal is to trigger a click action that will adjust the length of the box to another value upon pressing the button. View image here View image here

@charset "utf-8";
* {
  padding: 0px;
  margin: 0px;
  box-sizing: border-box
}

body {
  background-color: #d4d4d4;
}

section {
  width: 90%;
  background-color: #ffffff;
  margin: 100px auto;
  height: 560px;
}

.box {
  width: 80%;
  margin: 0 auto;
}

.title {
  text-align: center;
  padding: 10px 0;
}

.info {
  text-align: center;
  height: 0;
  overflow: hidden;
}

.down {
  margin: 0 auto;
  margin-top: 30px;
  width: 50px;
}

.down button {
  width: 50px;
  padding: 10px 10px 10px 10px;
  background-color: #fff;
  border-radius: 50%;
  box-shadow: 0px 0px 6px #bbb;
  height: 50px;
  border: none;
  position: relative;
}

.down button:focus .info{
  height: 200px;
}

.down img {
  width: 20px;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  margin: auto;
}
<!doctype html>
<html>

<head>
  <meta charset="utf-8">
  <title>COMSEC</title>
  <link href="style.css" rel="stylesheet" type="text/css">
</head>

<body>
  <section>
    <div class="box">
      <div class="title">
        <h1>Title <br>
        </h1>
      </div>
      <div class="info">
        <p>
          TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT<br> TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT<br> TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT<br> TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT
          TEXT TEXT<br> TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT<br> TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT<br> TEXT TEXT ...
        </p>
      </div>
      <div class="down">
        <button><img src="down.png" alt="Button"></button>
      </div>
    </div>
  </section>
</body>

</html>

Answer №1

Solution Explained

The issue in your scenario is that traversing back up (parent) in CSS is not possible. The button is positioned outside the 'info' container, making it challenging to target a child element of the parent on button click.

In your code snippet:

.down button:focus .info {
     height: 200px;
} 

The '.info' element is perceived as a child element by the browser. Ideally, your '.info' element should be within the button element for this approach to work.

If you wish to maintain your HTML structure, employing JavaScript for opening and closing the description is recommended as it offers greater flexibility.

Pure CSS Alternate Solution

A strict CSS-only approach would involve the following implementation. However, this method only functions effectively when the description is placed immediately adjacent to the button. Furthermore, the description collapses upon clicking elsewhere.

Utilizing a JavaScript solution would ensure the description remains open or visible until closed with another button click.

.accordion {
  width: 100%;
  display: block;
}

.accordion-description {
  display: none;
}

.accordion_btn {
  width: 3.2rem;
  height: 3.2rem;
  margin: 0 auto;
  border: 1px solid rgba(0, 0, 0, 0.3);
  overflow: hidden;
  border-radius: 50% 50%;
  background-color: rgba(255, 255, 255, 1);
  box-shadow: 0 0 0.3rem 0 rgba(0, 0, 0, 0.3);
}

.accordion_btn:focus {
    background-color: rgba(200, 200, 255, 1);
}

.accordion_btn:active + .accordion-description,
.accordion_btn:focus + .accordion-description {
    display: block;
}
<div class="accordion">
  <button type="button" class="accordion_btn">Show</button>
  <div class="accordion-description">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus volutpat mauris nec eros sollicitudin sodales vel quis lectus...
  </div>
</div>

Additional Information

To explore various options available for CSS selectors, refer to resources like W3C or Mozilla Developer Network (MDN):

https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors

https://www.w3.org/TR/selectors-3/#selectors

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

On one webpage, IE 11 is correctly styling certain visited hyperlinks while failing to do so for others

Although I acknowledge that others have asked similar questions, none seem to address the exact issue I am facing, nor do they offer practical solutions or clear explanations. Issue I'm encountering a problem where some visited hyperlinks in IE 11 f ...

Adjust the Placement of Images in Relation to Mouse Movements

Is there a way to creatively animate the positions of images or background images based on mouse movement? Let's take Github, for instance: https://github.com/thispagedoesntexist The 404 page on Github is truly impressive. I aim to captivate my use ...

Link not functioning correctly on mobile-responsive CSS

Having some trouble with hyperlinks over a background image. The hyperlinks work fine normally, but in mobile view, nothing happens when clicking on them. Below is the code for the hyperlinks. You can also visit the site and see that the "Post a Project" ...

Creating a dynamic website content in WordPress

I have here my html code featuring two different sections that I need to make dynamic within Wordpress. <section class="contact"> <h1>how it works</h1> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Om ...

Adjust the canvas size to fit its parent element ion-grid

I am currently working on an Ionic 3 component that displays a grid of images connected by a canvas overlay. I have utilized Ionic's ion-grid, but I am facing an issue with resizing the canvas. The problem is that I cannot determine the correct dimens ...

What could be the reason for my select list not showing up?

Hello fellow developers, I am currently working on creating a dynamic tablerow that allows users to fill in input fields and select options from a list for each cell. While the input fields are functioning properly, I am facing an issue with displaying th ...

The left menu icons transform upon mouseover interaction

Is there a way to dynamically change the image displayed in the left menu when hovering over it, similar to the example shown below? https://i.stack.imgur.com/HzGwR.png Currently, the image does not change on hover. We would like it to transition into a ...

I'm struggling to locate the button element in HTML for use with selenium

Currently, I am developing a selenium script to automate browser tasks using the Tor browser, which does not save cookies. Every time the script navigates to a website, a pop-up appears asking for cookie consent. I need the script to automatically click on ...

Materialize CSS is throwing an error: 'velocity is not a function'

Encountering an issue with Materialize CSS. A JavaScript error appears 'I('.velocity is not a function', for certain actions. For instance, clicking the collapse icon on the sidenav results in e.velocity is not a function error. Similarly, u ...

Create a script that ensures my website can be set as the homepage on any internet browser

I am currently in search of a way to prompt users on my website to set it as their homepage. Upon clicking "Yes," I would like to execute a script that will automatically make my website the user's browser homepage. I have come across a Similar Thread ...

Guide on showcasing an array of objects in HTML with the help of the template element

My goal was to populate the HTML page with an array of objects using the template element. However, I made a mistake and only the last object in the array was displayed correctly on the page. Can anyone help me identify my error and suggest a correction? I ...

Stop Sublime Text from automatically calculating Less2Css

Within my workflow using Sublime Text and Less2Css for dealing with less files, I encountered an issue where specifying the max-height of a container as 100% - 20px resulted in minification reducing it to '80%'. Is there a way to work around this ...

What is the best way to align content in the middle of a containing div?

I am struggling with centering text and images inside a div that is 190px x 190px. Despite searching on various platforms, including SO and Google, I have not found a simple solution. Can someone please provide guidance on the easiest way to achieve verti ...

Troubleshooting iFrame Loading Issues with HTML5 PostMessage

Our code is utilizing the newest postMessage feature in HTML 5 to address cross-domain communication challenges. The issue I am facing is figuring out how to determine if the messages posted to an iFrame have been successfully loaded. If the frame fails to ...

The two divs are positioned on top of one another, with the link in the bottom div being unclickable

I am trying to create a unique effect where a tile divides into two on hover, with each tile acting as an individual link. Additionally, I want the background color of the tiles to change on hover. To achieve this, I have stacked two divs (toptile and bot ...

What is the procedure for displaying the complete text for choices on an ionic select button?

On my page, I have included an Ionic select button. The problem is that the options display a long string, and I want them to show the full text without truncating with dot-dot-dot like this: This is how my code looks: <ion-item *ngIf="select == &apos ...

Issue with justify-content in Bootstrap 5 still unresolved

Currently working with Bootstrap 5 and have set up 4 cards. My goal is to position the second card on the left side. I've applied the class justify-content-xl-start to the card, however, it doesn't seem to be functioning as expected. I also attem ...

Altering CSS styles through JavaScript or jQuery

Exploring Options After investigating the use of .css() to manipulate CSS properties of specific elements, I came across a website showcasing the following CSS: body, table td, select { font-family: Arial Unicode MS, Arial, sans-serif; font-size: ...

Interactive pop-up messages created with CSS and JavaScript that appear and fade based on the URL query string

I have a referral form on this page that I want people to use repeatedly. After submitting the form, it reloads the page with the query string ?referralsent=true so users can refer more people through the form. However, I also want to show users a confir ...

Having trouble with named anchor link not functioning in Internet Explorer. I have attempted the recommended fixes from Stack Overflow with no

My issue involves using a named anchor tag: <table id="search_results" name="search_results"> When I try to link to it like this: pagename.php#search_results It works in Firefox and Chrome, but not in IE (8). I have explored using an <a> t ...