Display or conceal text based on the amount of lines present

I have a box of text that needs to be displayed vertically. The client wants the text to be fully visible on a computer or laptop screen, but have a "Show More" button when viewed on mobile devices. My idea is to show 10 lines of text on desktop and 5 on mobile, hiding the rest if necessary. I'm unsure about the feasibility of this approach. Below is the HTML code I am currently working with:

<div class="box31 col-md-3 col-md-offset-0 col-sm-6 col-sm-offset-0 col-xs-8 col-xs-offset-2">
  <div class="phone">
    <img src="imgs/phone.png" alt="phone">
  </div>
  <div class="box-text box-text2">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.</div>
  <div class="view-more">
    <button class="btn btn-success">More...</button>
  </div>
</div>

Answer №1

In my opinion, utilizing CSS media queries can be a great way to adjust styles based on screen size:

 @media screen and (max-width: 768px) {
  p {
    max-height:60px;
    overflow: hidden;
    color:blue;
  }
}

https://example.com/jsfiddle123 (resize the result part to see the changes)

Answer №2

Try this method to achieve the desired outcome

Cascading Style Sheets (CSS)

.show {
    max-height:80px;
    overflow: hidden;
    }

.hide{
  max-height:100%;
}

JavaScript (JS)

jQuery(function($){
  $('.click-me').on('click', function(){
    $('.show').toggleClass('hide')
  })
})

DEMO

Answer №3

In order to determine the number of lines to display, you must calculate the line-height multiplied by the desired number of rows. The example provided below demonstrates this calculation for three rows.

document.querySelector('.show-more .button').addEventListener('click', function() {
  this.parentNode.previousElementSibling.classList.add('expand');
});
body {
  font-family: Arial, Helvetica, sans-serif;
}
p {
  margin: 0 0 5px;
}
.text-box {
  font-size: 14px;
  line-height: 14px;
  max-height: 42px;
  overflow: hidden;
  margin-bottom: 10px;
}
.text-box.expand {
  max-height: none;
  overflow: visible;
}
@media screen and (min-width: 480px) {
  .show-more {
    display: none;
  }
  .text-box {
    max-height: none;
    overflow: visible;
  }
}
<div class="box31 col-md-3 col-md-offset-0 col-sm-6 col-sm-offset-0 col-xs-8 col-xs-offset-2">
  <div class="text-box text-box2">
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
    <p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
    <p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.</p>
  </div>
  <div class="show-more">
    <button class="btn btn-primary">Show More...</button>
  </div>
</div>

The CSS code above follows the mobile-first approach.

The JavaScript function is a simple click event listener attached to the button that targets the relevant text-box element in the DOM and adds the class expand to it. This action removes or resets the max-height and overflow properties.

Here's the jQuery equivalent of the JavaScript mentioned above:

$('.show-more .button').on('click', function() {
   $(this).parent().prev().addClass('expand');
});

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

Tips for developing a custom function to retrieve information from a WCF Rest service and showcase it in a table

In my code, I have created a function called service.js to get data from a WCF Rest API in JSON format. Then, I implemented another function called entryCtrl.js to fetch the data and display it in HTML. service.js (function (app) { app.service("CRUD_An ...

"Troubleshooting: Issues with the ng-hide directive in AngularJS

I am facing an issue with hiding a column in my table using ng-hide. The goal is to hide the column before the user logs in and then show it after they have logged in. However, I found that after applying the ng-hide property, the entire table gets hidden ...

Tips for organizing checkboxes in rows horizontally

My question is related to this issue I am trying to arrange my checkboxes in 4 columns horizontally <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta charset="utf-8"> ...

Tips for preventing a page from automatically scrolling to the top after submitting a form

Recently, I set up a form where users can input values. The form is set to submit either on change or when the user hits enter, depending on which value they want to update. However, after the values are submitted, the page automatically jumps back to the ...

Troubleshooting problems with forms using Bootstrap and HTML

I could use some assistance with a problem I'm facing while trying to acquire new skills through learning. I would like my picture and forms to resemble the one in this image: enter image description here index.html <div class="jumbotron"> &l ...

How can I color the first, second, and third buttons when the third button is clicked? And how can I color all the buttons when the fourth button is clicked?

I am trying to achieve a task with 4 buttons. When the third button is clicked, I want the first, second, and third buttons to change color. Similarly, when the fourth button is clicked, I want all buttons to change color. Additionally, I need to save a va ...

The Google Maps display for this page failed to load properly on the map

<!-- <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY"></script> --> <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initialize" async="" defer="defer" type="text/javascript">& ...

Locate an element by its neighboring label with XPath

Struggling to identify the XPATH for a specific element in a sea of similar attributes? Look no further. In this HTML snippet, the key to pinpointing the right element lies in its neighboring label, "Copyright". Here's the code: <div class="row"&g ...

Guidelines on creating actionable functions using JavaScript

Having trouble connecting the form to the database and looking for a solution. This is the HTML form that I've been working on, attempting both POST and GET methods. <form id="register" action="script/register.js" method="post"> <label for= ...

Error in RatingBar component: Vue.js 3 and Tailwind CSS not displaying dynamic width correctly

I have a component called RatingBar in Vue.js 3 with Tailwind CSS. My goal is to dynamically adjust the width of the parent element's class based on a value, but even though I see the desired width in DevTools, it always renders as 100%. Below is the ...

Can you please point out the location of the error in the Java Script code

Our application is developed using Yii2 framework and incorporates Kartik's star-rating widget. However, there seems to be an error in the JavaScript code: Error message from console: Check out the problematic code snippet: <?php $js = ...

Having difficulty formatting a div using HTML/CSS in a React application

As I embark on my journey to learn HTML/CSS, I am experimenting with different examples. Currently, I am struggling to create a div that resembles what I envision due to the content being 'glued' together. I have attempted various methods, but n ...

Enhance your website with interactive AJAX callback animations using jQuery

I've encountered an issue while using a basic AJAX script in jQuery. I want to implement an easeInOut effect for the AJAX callback to change the HTML smoothly, but I'm not sure how to achieve this. Currently, when the response is received, the co ...

Embedding CSS directly into PHP output

I have a table where I want to include a vertical-align property: $output = '<style type="text/css">div.bar-container {border: 1px solid #ccc; width: 150px; margin: 2px 5px 2px 17px; padding: 3px; float: left; background: white; position:relati ...

New to jQuery and struggling to download or link it for the first time?

My attempt to download the uncompressed version of jQuery from the jQuery download page did not go smoothly. When I clicked on the bottom left download button, it started to download but then a dialog box appeared asking for permission. After accepting, an ...

Seeking animated SVGs using CSS styling

I've been trying to animate my SVG icon, but I seem to be missing something. I'm still fairly new to CSS animations. The issue I'm facing is that the position of the SVG isn't correct when the website loads. Additionally, during the an ...

Is there a simple way to create a clickable popup menu without using JavaScript?

/*---Creating a Popup Menu with CSS---*/ .box{ position:fixed; top: 0.1%; left: 14px; display: inline-block; cursor: pointer; } ul{ list-style-type: none; } ul li{ font-family: Lato; padding: 10px; } ul li a{ text-decoration: none; color: black; } ul li:ho ...

When I switch to a different page in Django, the item I added to my shopping cart disappears

I'm facing a challenge while developing an e-commerce website using Django. When a product is added to the cart and I navigate to a different page, the product disappears from the shopping cart view. I've added debugging code in my JS file as wel ...

Reordering Divs in Bootstrap 3 Specifically for Small Screens

Just getting started with my first responsive design project, and I'm wondering if it's possible to achieve something like this using Bootstrap 3. The goal is to switch from the current layout: https://i.stack.imgur.com/lABXp.jpg To https://i. ...

Using CSS to place the h1 text on top of an image

I need some advice on how to place a heading above an image. I have tried using negative margin and adjusting z-index, but the image still covers the heading. If you have a better solution, please let me know! Thank you in advance for your help! PS: Anot ...