Tips for restricting text within a div container

Currently, on my to-do list website, I am facing an issue with displaying long event titles in a div on the home screen. The text overflows, and even though I have set the x-overflow to hidden, it's not providing the desired result. I have tried using text-overflow: ellipsis, but it doesn't seem to work. How can I make it show an ellipsis at the end of the long title?

https://i.sstatic.net/4uZ0L.png

Below is the code responsible for displaying the events:

<div class="col-md-6 inline mb-4">  
        <h3 class="ml-1">Events:</h3> 
        <div class="events-content-section ml-2"> 
            {% for event in events %}  
                <div class="mb-2" style="display: flex; align-items: center;">  
                    <button type="button" class="view-event btn btn-light" style="width: 100%;" data-id="{% url 'view-event' event.pk %}">
                        <span>
                            <h4 style="float: left;">{{ event.title }}</h4>
                            <button type="button" class="update-event btn btn-sm btn-info ml-1" data-id="{% url 'update-event' event.pk %}" style="float: right;"> 
                                <span class="fas fa-edit"></span>
                            </button>
                            <button type="button" class="delete-event btn btn-sm btn-danger mr-2 ml-2" data-id="{% url 'delete-event' event.pk %}" style="float: right;">
                                <span class="fa fa-trash"></span>
                            </button>   
                        </span>
                    </button> 
                </div>
            {% endfor %}    
        </div>
    </div> 

Below is the CSS code for the event div:

.events-content-section {
  background: #f5f5f5;
  padding: 10px 20px;
  border: 3px solid #dddddd;
  border-radius: 3px;
  overflow-y: auto;
  overflow-x: hidden;
  text-overflow: ellipsis; 
  height: 400px;
  }

EDIT: When I fullscreen the browser, the div expands to about half the screen, allowing more text to fit. Is there a way to make this adjustment automatically if the div is big enough?

Answer №1

If you're looking for a pure CSS solution, one option is to eliminate the float:left property from the h4 title. This property can prevent the h4 from collapsing to the borders of its parent. Instead, you can apply the text-overflow and overflow properties to the h4 title.

.events-content-section h4 {
  text-overflow: ellipsis;
  overflow: hidden;
}

Check out this example on CodePen for reference.

By the way, your HTML layout seems a bit unusual - why do you have a button nested inside another button? This is considered invalid HTML. You might want to consider restructuring your layout using Bootstrap's grid tutorial, removing any unnecessary float properties, and utilizing utility classes for text alignment.

Answer №3

p {
  width: 200px;
  border: 1px solid;
  padding: 2px 5px;

  /* Ensure text doesn't overflow */
  white-space: nowrap;
  overflow: hidden;
}

.overflow-visible {
  white-space: initial;
}
<div class="col-md-6 inline mb-4">  
        <h3 class="ml-1">Events:</h3> 
        <div class="events-content-section ml-2"> 
            {% for event in events %}  
                <div class="mb-2" style="display: flex; align-items: center;">  
                    <button type="button" class="view-event btn btn-light" style="width: 100%;" data-id="{% url 'view-event' event.pk %}">
                        <span>
                            <p class="overflow-visible">aaaa bbbbbbbbbbbbbb ccccccccccccccccccccccc fgggggggggggg</p>
                            <button type="button" class="update-event btn btn-sm btn-info ml-1" data-id="{% url 'update-event' event.pk %}" style="float: right;"> 
                                <span class="fas fa-edit"></span>
                            </button>
                            <button type="button" class="delete-event btn btn-sm btn-danger mr-2 ml-2" data-id="{% url 'delete-event' event.pk %}" style="float: right;">
                                <span class="fa fa-trash"></span>
                            </button>   
                        </span>
                    </button> 
                </div>
            {% endfor %}    
        </div>
    </div>

Implement the provided CSS and assign a class to the P tag

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

Clicking on the parent div with a Jquery onclick event does not trigger when the child image is clicked

I'm running into an issue with a simple code containing an image within a div Oddly enough, clicking on the div itself (even with padding) triggers the function, but clicking directly on the image does not. $(".parent0").click(function() { conso ...

Creating a balanced height for child elements using CSS

If you are looking to display a list of colors with each color occupying an equal fraction of the height, here is how you can achieve it. Imagine presenting four colors in a list with a fixed height and a thick border around it: The example shown above is ...

What is the best method to eliminate unnecessary space between controls in HTML?

Presently, I am facing the following scenario. http://jsfiddle.net/ef7vh/1/ </p> <pre> <div> <button>Button1</button> <span style="border: solid 1px black; padding: 0px 10px 0px 10px">My Text</span> < ...

Data from the session is not displayed when a redirect occurs

On the main page, I include the following code: <?php $required = array('post_question'); // Checking if post field is not empty $error = false; foreach($required as $field) { if (empty($_POST[$field])) { $error = true; } } if(isset($_POST[&a ...

Using jQuery UI to dynamically add a widget based on a specific class, rather than relying on

Exploring the world of Apache Cordova and jQueryUI is a new adventure for me. I am currently experimenting with formatting the layout of my index.html using jQueryUI. As mentioned in this section of the jQueryUI tutorial, a widget can be added using the f ...

Exciting transition effect for font sizes in CSS

Issue: Hover over the X and wait for the div to collapse completely. Move away from the X Notice how the div jumps to the bottom and then back up. Inquiry: How can I prevent text wrapping while expanding the div during animation? (Currently using del ...

Tips for customizing the focus style of a Text Field in Material-UI

My current Search box design is represented in the following image: https://i.sstatic.net/VuKIp.png I am looking for ways to customize the background color, border, and icon of the search box when it is focused. Additionally, I would like to know how to m ...

Retrieving pictures by their unique identification number

Utilizing a flexslider, I have set up an image display feature as shown in the JSFiddle link below. The slider has been optimized to dynamically extract images from a specific directory, where images are named with numbers like 1023, 2045, 304654, and so o ...

How come Django is unable to retrieve the integer from the database and show it on the HTML template?

I have a simple code snippet that aims to retrieve a number X, add "+1" as a basic value for demonstration purposes, and then store this updated number in a database. Additionally, I need a Django-based Jinja template to fetch this number and display it on ...

Saving password1 and password2 in a Django form is not possible

I've built a (CustomUserForm) that looks like this: from django.contrib.auth.forms import UserChangeForm from .models import User from django import forms class CustomUserForm(UserChangeForm): username = forms.CharField( widget=forms.Tex ...

Extracting data from several pages using the same URL, but with varying ajax responses

I've been attempting to scrape all the reviews for a specific book on Goodreads.com. url= https://www.goodreads.com/book/show/320.One_Hundred_Years_of_Solitude?ac=1&from_search=true The initial scrapping of the first page was successful using Py ...

jQuery - translating and organizing names of countries

I have implemented a jQuery function to organize a list of country names based on the user's selected language code (via a language select dropdown). You can find more information on this topic in this related post. The translation of country names s ...

What is causing the disparity in outcomes between these two methods? (Django Rest Framework version 3.9)

Identical code yielding different outcomes. What's the reason? # prj001 represents the app name # MyUser class MyUser(AbstractBaseUser, PermissionsMixin): email = models.EmailField(max_length=255, unique=True,) phone = models.CharField(max_ ...

Utilizing a Single Background Image Across Several Div Elements

Let me illustrate my question using a sequence of images. The div container is set to display the background image as follows: In addition, there will be tile-shaped divs layered on top of the image: I aim to have the background image visible only withi ...

utilize the existing instance of the Django CMS plugin again

Imagine creating a custom Facebook like box plugin within Django CMS that allows users to input page URLs and customization options for displaying the like box. Now, the user is interested in showcasing the identical like box on multiple pages. Is there a ...

Implementing swipe functionality to Bootstrap carousel

This is the code snippet I am working with: <div class="container"> <div id="myCarousel" class="carousel slide" data-ride="carousel" data-interval="3000"> <!--Indicators--> <ol class="carousel-indicators"> ...

Converting a Django model superclass into a subclass using a workflow

Looking for a solution in my Django project where I have two models: Applicant and Client, with Client being a subclass of Applicant. The goal is to allow users to add an existing Applicant instance as a Client. While I do have a view for Applicant insta ...

Rotate each row of the table in sequence with a pause between each flip

I have a table with 3 columns and 10 rows. I would like to flip each row one by one, where each row contains data on both the front and back sides. The flipping animation should be similar to the example provided in this link, but the flipping should sta ...

Tips for stopping the background color from affecting the text color

I'm dealing with a situation where I have an h6 nested inside a div. <div class="cylinder" data-v-5147e140=""> <div data-v-5147e140=""> <h6 class="quantite" data-v-5147e140="&qu ...

CSS Problem: Background Color Not Showing Up in Table Rows

Check out my fiddle below: https://jsfiddle.net/teo7auv3/ I'm facing a challenge where I want to change the background color of the entire table row (TR) to red when there are validation errors. The problem seems to be related to this code snippet: ...