Conceal button with CSS when text is brief or character count is restricted

Employing solely CSS code, I have successfully implemented a feature to display and hide full paragraphs. The code snippet below demonstrates its functionality. However, the issue arises when the "Show More" button is visible even for short paragraphs that do not require expansion. How can the button be hidden and only appear when the text length warrants it? Furthermore, for longer paragraphs that necessitate the button, I wish to include three dots at the end of the sentence (...) to indicate that more content is available. These dots should disappear when the paragraph is expanded and reappear when it is collapsed.

Disclaimer: I am seeking a CSS-only solution without the use of jQuery or JavaScript to maintain simplicity and avoid potential complications.

.panel-wrapper {
  position: relative;
  width: 500px;
  margin: 0 auto;
}
.show, .hide {
  position: absolute;
  bottom: -1em;
  z-index: 100;
  text-align: center;
  color: red;
}
.hide {
  display: none;
}
.show:target {
  display: none;
}
.show:target ~ .hide {
  display: block;
}
.show:target ~ .panel {
  max-height: 100%;
}
.show:target ~ .fade {
  margin-top: 0;
}
.panel {
  position: relative;
  width: auto;
  max-height: 40px;
  overflow: hidden;
}
.fade {
  height: 20px;
  position: relative;
}
<div class="panel-wrapper">
  <a href="#show1" class="show btn" id="show1">Show more</a>
  <a href="#hide1" class="hide btn" id="hide1">Show less</a>
  <div class="panel">
    Let us not wallow in the valley of despair, I say to you today, my friends. And so even though we face the difficulties of today and tomorrow, I still have a dream. It is a dream deeply rooted in the American dream. I have a dream that one day this nation
    will rise up and live out the true meaning of its creed: "We hold these truths to be self-evident, that all men are created equal." I have a dream that one day on the red hills of Georgia, the sons of former slaves and the sons of former slave owners
    will be able to sit down together at the table of brotherhood. I have a dream that one day even the state of Mississippi, a state sweltering with the heat of injustice, sweltering with the heat of oppression, will be transformed into an oasis of freedom
  </div>
  <div class="fade"></div>
</div>
<br><br><br>
<div class="panel-wrapper">
  <a href="#show2" class="show btn" id="show2">Show more</a>
  <a href="#hide2" class="hide btn" id="hide2">Show less</a>
  <div class="panel">
    Let us not wallow in the valley of despair, I say to you today, my friends. And so even though we face the difficulties of today and tomorrow, I still have a dream. It is a dream deeply rooted in the American dream. I have a dream that one day this nation
    will rise up and live out the true meaning of its creed: "We hold these truths to be self-evident, that all men are created equal." I have a dream that one day on the red hills of Georgia, the sons of former slaves and the sons of former slave owners
    will be able to sit down together at the table of brotherhood. I have a dream that one day even the state of Mississippi, a state sweltering with the heat of injustice, sweltering with the heat of oppression, will be transformed into an oasis of freedom
  </div>
  <div class="fade"></div>
</div>
<br><br><br>
<div class="panel-wrapper">
  <a href="#show3" class="show btn" id="show3">Show more</a>
  <a href="#hide3" class="hide btn" id="hide3">Show less</a>
  <div class="panel">
    Consider this short text. Short enough to hide "Show More" button.
  </div>
  <div class="fade"></div>
</div>

Answer №1

My approach would involve reimagining the features with a small hack using the CSS property position:sticky and considering the utilization of :active/:focus in place of :target.

This modification only addresses your initial point. Refer to the code comments for more information.

While not a flawless solution, it serves as an approximation with some unavoidable drawbacks.

.wrapper {
  line-height:1.2em; /* set line height explicitly for easy control */
  max-height: calc(2*1.2em + 2em); /* Display a maximum of 2 lines */
  width: 500px;
  margin: 20px auto;
  overflow: hidden;
}

.wrapper .content {
  position:relative;
}
/* Hides the 'show/less' when the content is 2 lines or less */
.wrapper .content:after {
  content:"";
  position:absolute;
  top:106%; /* Begin after the content */
  left:0;
  right:0;
  z-index:1;
  background:#fff;
  height:100px; /* Large height */
}
/**/
.wrapper :nth-last-child(1),
.wrapper :nth-last-child(2) {
  /* Sticky positioning to remain visible above content for larger texts */
  position: sticky;
  bottom: 0;
  background: #fff;
  cursor:pointer;
  outline:none;
  color:red;
  line-height:2em;
}

.wrapper:focus,
.wrapper:active {
  outline: 0;
  max-height: 400px;
}
/* Hides the after and 'show more' on click */
.wrapper:focus .content:after,
.wrapper:active .content:after,
.wrapper:focus :nth-last-child(1),
.wrapper:active :nth-last-child(1) {
  display:none; 
}
<div class="wrapper" tabindex=-1>
  <div class="content">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla ipsum nunc, hendrerit ac arcu eu, pulvinar ultrices enim. Ut at enim bibendum, iaculis turpis ac, congue dui. Etiam faucibus lectus eget lorem molestie, finibus...</div>
  <div tabindex=-1>show less</div>
  <div>Show more</div>
</div>

<div class="wrapper" tabindex=-1>
  <div class="content">Lorem ipsum dolor</div>
  <div tabindex=-1>show less</div>
  <div>Show more</div>
</div>

<div class="wrapper" tabindex=-1>
  <div class="content">Lorem ipsum dolor eget tellus porta, sollicitudin massa quis, eget tellus porta, sollicitudin</div>
  <div tabindex=-1>show less</div>
  <div>Show more</div>
</div>

<div class="wrapper" tabindex=-1>
  <div class="content">Lorem ipsum dolor eget tellus porta, sollicitudin massa quis, eget tellus porta, sollicitudin eget tellus porta, sollicitudin sollicitudin eget tellus porta, sollicitudin</div>
  <div tabindex=-1>show less</div>
  <div>Show more</div>
</div>

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

How can I convert the left links in my navigation bar to a CSS drop-down menu to make it more responsive on small screens?

Here is the structure of my HTML (at the top): <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></s ...

React rendering: Injects an equals sign and quotation marks into the async attribute of <script> tag

I recently developed a basic web application using Next.js and React. One of the functional components (referred to as a "page" in Next.js) includes a script tag like this: <script async src="https://example.com/file.js"></script> However, upo ...

Extract information from the table using $_POST

I possess a datatable within a form. Inside that table, there is a column designated for setting a price. Upon submitting the form on the same page, the $_POST data fails to populate. Consequently, I am in need of inserting it into the MySQL db post-submit ...

Styling links conditionally in a React component

How can I style the page title in my nav bar when I am on the selected path? I am using React and Tailwind CSS. For example, I want the title to turn yellow when I am on the current page. Here is the logic I have tried, but it doesn't seem to be work ...

Display or conceal the nearest element that was clicked

http://jsfiddle.net/KevinOrin/xycCx/1/ jQuery('.happening-main-text .readmore').click(function() { jQuery('.divmore').toggle('slow'); return false; }); ...

Tips for dynamically resizing the content area using CSS without the need for javascript

What I need is as follows: The blue area should resize when the browser window resizes. The header must be visible at all times. The blue area should start right after the header ends, not overlapping or appearing above it. The blue area should end befor ...

Webpack2 now transforms sass/scss files into JavaScript rather than CSS during compilation

I've created a webpack script to compile all .scss files into one css file. I decided to use webpack instead of gulp or grunt for simplicity, as it can be configured in a single file. However, I am encountering an issue where scss files are being com ...

Issue with HTML not being displayed during Eclipse Eclipse html not showing up on

This is an example of a servlet: package userInterface; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servle ...

margin-top: automatic adjustment, with a minimum of 50 pixels

I am trying to add a minimum of 50px margin to the top of my footer tag using CSS, but I haven't been successful with the min() function. I'm not sure if I am overlooking something or if there is another correct approach to achieve this. * { ...

"Utilizing jQuery and AJAX to manage identical-named option boxes with

I am encountering an issue with multiple select boxes that share the same name. Currently, when I choose a value from one select box, it submits that selection and updates the database. However, when I select an item from another select box, it still submi ...

Angular encountering issues with loading external JavaScript files due to the error: ENOENT - indicating that the specified file or directory does

I am attempting to incorporate a bootstrap template into Angular. The template requires some external JavaScript and CSS files that need to be linked. I have placed these files in the assets folder and referenced them in the styles and scripts arrays of an ...

"Using Bootstrap's toast feature repositions every element on the

I am working on a website project and encountering an issue with a bootstrap toast. The toast currently appears in the top right corner, but I would like it to display above the carousel without affecting the layout. Instead, when the toast appears, it shi ...

What is the best way to have a form open upwards when hovered over or clicked on?

Attempting to create a button in the bottom right corner that will reveal a form when clicked or hovered over. The form should slide open slowly and close after clicking on login, but currently the button is moving down as the form opens. The button also ...

JavaScript embedded in an HTML document, which in turn is embedded within JavaScript

Is it possible to nest tags within other tags to control the functionality of a download button in a chat bot? Unfortunately, nesting tags is not allowed, so I'm looking for an alternative solution. Below is the complete HTML file I'm working wit ...

Disable location prompt feature when using AngularJS with Maps API

For my web application, I developed a feature that includes a map with custom markers using Angular loops: <map data-ng-model="mymap" zoom="4" center="[38.50, -95.00]" style="heigth:375px"> <div ng-repeat="item in list"> <marker pos ...

jQuery Ajax Redirect Form

I am currently developing an HTML application with a form. Upon clicking the submit button, I initiate a server-side call using jquery.ajax(). However, when the server returns an exception, such as a Status Code 500, I need to display an error message on t ...

Comparing functions and function issues when using onClick in JavaScript

I have successfully created an Image Element on my web page dynamically using JavaScript. I am now trying to set an onClick event for the newly created image element. However, I am encountering a problem and I cannot figure out why?, This is the code I ...

Change the parent's color when the child is hovered over

head has no background and the color is black. On mouseover, its color changes to white with a black background, so far so good but When I hover over content, I want to change the color of head. How can I achieve this? Is it possible using only CSS? < ...

Incorporating custom HTML5 player to watch Youtube videos on my website

I'm trying to display YouTube videos on my website using a simple video tag. I've managed to retrieve the encoded url of the video from the page source and successfully download it through IDM, but when I try to use this URL as the source for an ...

The vertical menu was added, but the text did not align properly

I pasted a sidebar from a different source https://i.stack.imgur.com/jkYWz.png My goal is to have the text appear at the top of the page, similar to this example https://i.stack.imgur.com/1aR5s.png After adding this line, my text is displaying at the b ...