Is there a way to create a CSS-based popup or tooltip without using any other coding languages

I've created code to enable scrolling for a paragraph within a popup. My goal is to hide all content outside of the popup <div> using only HTML and CSS. Is this possible?

<div style="position:relative;">
  <div id="popup" style=" overflow: hidden;  position: relative; width: 100px; height: 100px;">This content should be visible. Anything outside of this div should be hidden</div>

  <div style="position:absolute;left:-100px;top:-50px;">the tooltip thing</div>
</div>;

<p>These should be hidden.</p>
<p>These should be hidden.</p>
<p> And These as well</p>
Here is the pop-up code(noscript):

<style>
div.po
   {
   display: none;
   } 
div.p
   {
   display: block;
   } 

button:hover + div.p {
    display: none;
    }
button:hover + div.po {
    display:block;
    }
</style>
<button>Show</button>

<div class="p">Other contents</div>
<div class="po">Popup</div>

The issue with the css button hover effect is that it only changes the style of one element at a time. When the button is clicked, I aim to set both 'div.po - style' to display: block and 'div.p - style' to display: none.

Answer №1

After thoroughly reviewing your post and comments, it appears that you are attempting to transform a specific paragraph into a popup by enabling it to be scrollable while concealing all other content on the page.

Additionally, it seems that you are utilizing in-line styling rather than a CSS StyleSheet, and there is no mention of JavaScript in your approach.

Solution:

In response to your inquiry about achieving this solely with HTML and CSS, it is unlikely due to the inability to dynamically manipulate one HTML element directly from another. While inheritance could potentially serve as a workaround, if successful, it would likely result in a messy solution unsuitable for modern web development practices.

Furthermore, the use of Pseudo-selectors mentioned in your comments cannot be applied with inline styling. These selectors alter the displayed HTML of a specific element and its children but do not impact all elements on the page unless the said element serves as the top-level element.

To propose a solution, I recommend incorporating HTML5, CSS, and JavaScript to implement a "Burn and Re-build" structure commonly found in Single-Page Applications, directing users through different website sections via hidden content with selective display.

Various approaches exist to achieve this goal, typically beginning with JavaScript EventListeners and progressing to advanced tools like jQuery, Angular, or React for efficient updates to the DOM. While these methods provide the necessary resources, accomplishing your objective purely with HTML and inline styling may prove challenging.

Note:

One effective method to achieve your desired outcome involves utilizing HTML5, CSS, and JavaScript EventListeners. Start by defining two distinct styling classes in your CSS: '.show' and '.hide'.

.show {
   visibility: visible;
}
.hide {
   visibility: hidden;
}
  • Instead of using display properties, favor visibility to control an element's visibility without affecting layout integrity, ensuring consistent formatting within your classes. Applying these classes to HTML tags will toggle their visibility alongside child elements.

  • Although eliminating the .hide class rather than toggling to .show alone can automatically reset visibility, employing both ensures clearer commands and differentiation when modifying previously concealed elements. Prioritize visibility over display to preserve positioning stability during manipulation.

Subsequently, craft a function triggered upon clicking a designated button to handle essential operations:

  • Retrieve a collection of target elements for hide/show actions.
  • Cycle through the collection to appropriately manage element visibility.

Once the function is established, attach an EventListener to the button for seamless function invocation upon clicks!

The following code snippet offers guidance and insight, hoping to facilitate your implementation!

JavaScript, CSS, and HTML5 code:

// JavaScript function for handling popup visibility
'use strict'

// Boolean tracker for popup status
let hidden = false

// Function called on button click to manage popup behavior
let showPopup = function() {

  // Retrieving child elements under 'main' tag
  let main = document.getElementById('main').children

  if (!hidden) { // Hides everything except popup

    for (let i = 0; i < main.length; i++) {
      if (main[i].id !== "popup") {
        main[i].classList.remove('show')
        main[i].classList.add('hide')
      } else {
        main[i].classList.add('popup-para') 
      }
    }

    hidden = !hidden 

  } else { // Reveals all hidden elements

    for (let i = 0; i < main.length; i++) {
      main[i].classList.remove('hide')
      main[i].classList.remove('popup-para')
      main[i].classList.add('show')
    }

    hidden = !hidden 
  }

  console.log(document.getElementById('test-para').classList)
}

document.getElementById('popup-button').onclick = showPopup
.show {
  visibility: visible;
}

.hide {
  visibility: hidden;
}

.class-1 {
  color: purple;
}

.c1 {
  height: 10%;
  width: 60%;
}

.popup-para {
  max-height: 10em;
  font-family: Verdana;
  font-size: 32px;
  overflow: scroll;
  color: darkblue;
}
<!DOCTYPE html>
<html>

<head>
  <link href="index.css" rel="stylesheet" type="text/css" />
</head>

<body>
  <main id="main">
    <h1> This is the main page content </h1>
    <ol>
      <li> Here's a list item. </li>
      <li> Here's anotha! </li>
    </ol>
    <p> Your button is just after this paragraph </p>
    <div id="popup">
      <button id="popup-button" class="c1 c2 c3 c4">Click to hide or show all other sections</button>
      <p> This is the paragarah that you want to look like it has popped up. I also have it changing it's text color let it stand out more! Also notice that it has kept the same position on the page and didn't jump up to the top. This is because we changed
        the visibility of all the other elements as opposed to the display! They still maintain their positioning and flow!

        <p> This paragrah shows up too because it is inside the popup div! Trying to fill space here, too lazy to get the Ipsum. Let's breakdown the JavaScript document object a bit. When we say document.getElementById('main') we are saying, "Hey, go look
          at the entire HTML5 file, document, and get me each element that has an id of 'main', .getElementById('main'), then store those children into the variable 'main' as an array!" Since each id is unique, there can only be one thing that is returned
          when we call .getElementById(). If you happen to have two elements in your HTML that have the same id, then only the first one will get returned. What is returned is actually called an Element object. Since it is an object, it also has properties!
          One of those properties is called children and since an element can possibly have multiple children, it will return an array! Each item in that array is another Element object that references that specific element in the HTML file! Should have
          got the lorem... </p>
    </div>
    <p id="test-para" class="class-1 class-2 class-3">This color stays purple because of precedence! </p>
    <p> This doesn't though..</p>
    <h1> "DON'T FORGET ABOUT ME!!"</h1>
    <ul>
      <li> These list elements are still in position</li>
      <li> tada</li>
    </ul>
  </main>
</body>
<script src="index.js"></script>

</html>

Answer №2

This edit section is crucial. Give it a thorough read.

If you opt not to use inline CSS, you have the option to give hidden items a class with display: none or hide all children of the parent class and create a specific scenario for popups. Keep in mind that CSS stands for Cascading Style Sheets, so priority is given to popups.

.parent * {
    display: none;
}

.parent popup {
    display: initial;
    /* Alternatively, if you prefer block, as mentioned in your comment: */
    display: block;
}

If it was already set as a block, then 'initial' will suffice.

Edit: After considering the comments below (highly recommended reading), this is the revised version.

.tooltip-content {
  display: none;
}

.show-tooltip:hover + .tooltip-content {
  display: block;
}

.show-tooltip:hover ~ * {
  display: none;
}
<body>
<div class="tooltip-section">
  <button class="show-tooltip">Show Tooltip</button>
  <p class="tooltip-content">Tooltip Content</p>
  <p>Other Content</p>
</div>
</body>

For an interactive version, check out this JSFiddle link.

In essence, this code hides the tooltip-content class by default. Upon hovering over the adjacent show-tooltip button, it hides all other siblings and reveals the next-door sibling containing the tooltip-content. While this example uses paragraphs, you can adapt it to suit your needs.

Trust this clarifies things!

Answer №3

HTML: This is the body text.

</div>
<div id="popup">
<a href="index.html">
Close
</a>
This is pop-up text
</div>
</span>
</div>

CSS:

#popup_bg
  {
   display:none;
   height:100vh;
   width:100vh;
   position: fixed;
   left:0;
   right:0;
   }
#popup_bg
  {
   display:none;
   height:10vh;
   width:10vh;
   position: fixed;
   left:45%;
   right:45%;
   }
#box: checked
#popup_bg
  {
   display:block;
   }
#box: checked
#popup
  {
   display:block;
   }

It has been quite some time since I posted this question. There were no correct answers back then. However, now I have found what I was looking for. Here is the code.

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

"Exploring ways to retrieve the selected option value in HTML by utilizing the find() function in a Node.js Express application that is

I am currently facing an issue with retrieving a specific document from MongoDB using the find() method. I'm encountering a problem where the result returned is not what I expect in the HTML file. For example, I select MALE and XL, which I know exist ...

Searching for a method to locate and substitute a specific HTML string using the `sed` command in the macOS Catalina terminal

I am attempting to modify the following html code: <svg xmlns="http://www.w3.org/2000/svg" width="20" viewBox="0 0 256 256"> to this updated version: <svg xmlns="http://www.w3.org/2000/svg" width="30&q ...

In Angular2, you can dynamically show or hide previous and next buttons based on the contents of the first and last div

I need a solution to dynamically hide or show previous and next buttons based on the div tags created. Each value in a list is being used to generate individual div tags using ngFor loop in Angular 2. The list being utilized is: appUlist:string[] = ["Cal ...

What is the best way to incorporate CSS into an AJAX request?

I am facing a challenge in creating a tree menu within my PHP code that is located in an ajax php file. The issue arises due to the presence of classes used inside the tree code: $html .= '<li><span style="min-width:200px" class="folder"> ...

Rearranging the stacking order of CSS pseudo-element

In the midst of creating a 3-stage progress bar using only CSS. The current design is displayed below: #progBar { background-color:#bdbdbd; padding:1.5vw; posi ...

What steps are involved in setting up a single form for entering orders, complete with both an order table and an order_item table that incorporates a foreign key

As I contemplate the best way to structure my page and forms for a customer ordering process, a few questions arise due to the tables' organization. To adhere to normalization standards, I have separated the order table and the order items table, link ...

What is the process for combining AngularJS filters?

I currently have a fragment object like this: [{id: 721, titulo: "Cotizador Gastos Médicos Bx+ 2019", descripcion: "Cotizador Gastos Médicos Bx+ Tarifas 2019", updateDate: "2020-03-25 14:30:22.0", idCategoria: "1", …}, {id: 88, titulo: "Cotizador GMM ...

Is it possible to automatically adjust the cursor position in a textarea when the language is switched?

I am working with a textarea that needs to support both English and Arabic languages. In English, text is typically left-aligned, so the cursor should start on the left-hand side. However, in Arabic, text is right-aligned, meaning the cursor should begin ...

The jQuery .each function is malfunctioning specifically on Google Chrome browsers

I developed a web application that utilizes jQuery to automatically submit all forms on the page. The code snippet is as follows: $('form').each(function() { $(this).submit(); }); While this functionality works perfectly in Internet Explore ...

Changing the background image of the body when hovering over a li element: a step-by-step

For my website, I want to achieve the same effect as seen on the homepage of this site: I am looking to change the background image of my webpage when a specific li element is hovered over. Each li element should trigger a different background image. Add ...

PHP code to showcase members in a three by three grid

With the use of li elements, I have created a layout consisting of three rows and three columns. Each li is meant to display a member based on their ID. <li> // Style: margin-bottom:10px; Display first member here... </li> ...

Customizing your Sharepoint Web part with unique text properties

When a user inputs custom text in a web part property, it will be shown on a form. Here is the code for the web part property located in the testTextWebPart.ascx.cs file: public partial class testTextWebPart: WebPart { private string _customtxt; [We ...

The absence of a closing parentheses in the argument is causing an issue when rendering

Apologies for the basic mistake, but I could really use some assistance with this syntax error. I've spent over an hour searching for it and still haven't been able to locate the issue. It seems to be around the success function(data) section. Th ...

Troubleshoot: Bootstrap Table Alignment Problem

I've been struggling to align my table header and content properly, as shown in the image. I want the header aligned to the right while keeping the rest of the content aligned to the left of the header. Is there a way to achieve this using only CSS wi ...

Styling Your PHP Output with Echo

Is there a way to create a compact text box with a scroll bar that can display recurring data generated by PHP activities on the server side? How can I format it in this way? ...

Unusual padding anomaly seen with inline elements (bottom padding is applied correctly, while top padding is not)

I had a good understanding of how inline elements function. But I came across a helpful explanation here: CSS display: inline vs inline-block. Regarding inline elements: respect left & right margins and padding, but not top & bottom Inline elem ...

Is there a way to have the information on the table automatically update based on the selection made in a drop-down menu?

When a user is selected from the combo box, I want the table displaying user data from the database to only show information related to the selected user. I attempted to use an array to store values but was unable to make it work. A Combo Box that allows ...

ensure that the radio button is selected on the contact form 7

I'm looking to customize the appearance of my radio buttons within a WP plugin ContactForm7. I found some CSS code on this website: Source CSS code My goal is to change the color of the radio button to green when it's clicked or checked: .wpcf7 ...

What is the best way to showcase HTML content in columns with a horizontal scrolling feature?

I am trying to showcase a list of basic HTML elements such as divs, paragraphs, etc. within a fixed height container, arranged in columns with consistent width. I want them to be horizontally scrollable, similar to the layout displayed in this image. Thi ...

Rotating SVG graphics with a growth effect

Check out my CSS below: /* -------------------------- Base --------------------------- */ body { padding-top: 60px; background: #0f4e7a; } svg { margin: auto; display: block; width: 28%; } .star{ fill: #FFF; } /* ------------------ ...