Centering text within a dynamic div can help create a visually appealing design

While browsing through various forums, I noticed several questions on stackoverflow that touch upon a similar issue to mine. However, my particular problem is a bit unique, and despite my best efforts, I have not been able to find a solution yet. It's also worth mentioning that I'm not very skilled in CSS.

Here's the scenario: I have a string within a div that needs to meet certain criteria when displayed:

For example, let's say we have the string: "The quick brown fox jumps over the lazy dogs, again and again."

  1. The middle of the text should align with the center of the div.
  2. The div can take up 100% of the width but should be limited to one line. If the window size is insufficient, the text should truncate on both ends while keeping the center visible. For instance, if the window is too small, the content of the div might appear as "over the", with the excess text cut off on either side.

Which CSS techniques can help me achieve this? I've experimented with the following code, but without success:

<html>
<head>
<style type='text/css' >
    div {
        width:100%;
        border:solid 1px black;
        text-align: center;
        vertical-align:middle;
        display: table-cell;
        white-space:nowrap;
        overflow:hidden;
    }
</style>
</head>
<body>
<div>
The quick brown fox jumps over the lazy dogs, again and again as well too.
</div>
</body>
</html>

My aim is to develop a mobile web app that displays search results in a list format. The search term will always be highlighted, ensuring its visibility in the list. Even if the search result cannot fit onto a single line, it should still be contained within a div that spans 100% of the width, displaying the term in the middle with the excess text truncated on both sides.

Therefore, the user may see a result in the list like so (the "..." are for illustrative purposes):

... the lazy dogs, again...

In addition, when the user switches between landscape and portrait orientations on their device, the list item should smoothly adjust to display more text.

I believe there must be an elegant CSS solution for this challenge, rather than resorting to a cumbersome javascript workaround which I am currently considering.

If you need further explanations or know of alternative methods to achieve my goal, please do not hesitate to share your thoughts. Or maybe you think this approach to UI design is flawed from the start?

Any assistance provided would be greatly valued!

Thank you!

Mohammad

Answer №1

section {
    margin:0 -20% 0 -20%;
    text-align: center;
    border: 2px solid blue;
    overflow: hidden;
    width: 100%;
    position:absolute;
    left: 50%;
}
p {
    width: 80%;
    overflow:hidden;
    position:relative;
    margin: 0 -40%;
    white-space:normal;

}

Check out the HTML snippet below:

<section>
<p>The quick brown fox jumps over the lazy dogs, once again and repeatedly.</p>
</section>

Answer №2

Give this a shot:

#main-div { width: auto; } body { text-align: center; overflow-x: hidden; }
I'm not entirely certain if this will produce the desired result, but it's worth giving it a try.

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

Move your cursor over the image to activate the effect, then hover over it again to make the effect disappear

Looking to enhance my images with hover effects. Currently, all the images are in grayscale and I'd like to change that so that when you hover over an image, it reverts to full color and remains that way until hovered over again. I've noticed so ...

JS unable to insert new row in table

I checked the input value before submitting it in the form and confirmed that it is correct, returning as a string.enter image description here const saveList = () => { const inputListNameText = inputListName.value; fetch('/api/lists' ...

The onClick() function in JavaScript is encountering issues on the Firefox OS mobile application

I've been working on an app for Firefox OS mobile devices where I'm trying to trigger a Javascript function onClick() from a div attribute. It's functioning properly in regular browsers, but when I test it in the simulator, the onClick funct ...

Ways to incorporate color gradients into the corners of CSS cards

Below is an example of a basic bootstrap card: Example 1: https://i.stack.imgur.com/DK2Sz.png I attempted to draw blue and green colors side by side, but was only successful in drawing blue as shown below: Example 2: https://i.stack.imgur.com/CcSzP.png ...

I can't seem to figure out why this file upload error is happening. What could be the

Having trouble with a simple file upload function and keep getting an "Error" message when trying to use it. Here's my HTML file: <form enctype='multipart/form-data' action='upload.php'> <input type='file' name ...

Obtain the src attribute of iframes using Robot Framework Selenium and store it as a variable

On a webpage, I have an iframe element that displays an html document representing a generated form. My goal is to create an automated Selenium script to validate specific values within this document. Currently, I am manually copying the URL from the ifram ...

Is there a fixed header table feature that comes built-in with HTML5?

Is there a native feature in HTML 5 for implementing a fixed header table with a scrollable tbody, while keeping the thead and tfoot fixed? ...

What is the best way to add a value to the value attribute of a div using JavaScript?

Is there a way to insert a value into the value attribute of a div using Internet Explorer 9? I have attempted various JavaScript functions, but so far I can only modify the content inside the div and not the value attribute itself. <div id="mydiv" val ...

Adjust the top margin of content to account for the height of a fixed header

When dealing with a fixed header that has been removed from the HTML flow, it can be tricky to adjust the content below it. The challenge arises because the height of the header can vary based on screen size. How can we dynamically adjust the margin-top fo ...

Is there a way to deactivate the <script> tag using CSS specifically for media queries?

When designing a website exclusively for desktop usage, I encountered the issue of it not being viewable on mobile devices. I attempted to address this problem by utilizing the code below: script { display: none; pointer-events: none; } Unfortunat ...

What is the best way to divide a string into an array containing both linked and non-linked elements?

I'm struggling to find the right solution to my problem. I need to create a view that is enclosed in a clickable div. The content will consist of plain text mixed with clickable URLs - the issue arises when clicking on a link also triggers the method ...

Include an additional icon without replacing the existing one on the mouse cursor

I am looking for a way to enhance the user experience by adding an icon that appears when hovering over an HTML element, serving as a subtle hint that the user can right-click. Instead of replacing the default cursor, which varies across platforms and doe ...

Issue with jQuery: Function not triggered by value selection in dynamically created select boxes

I am in need of some assistance with my code that dynamically generates select drop down menus. I want each menu to trigger a different function when an option is selected using the "onchange" event, but I am struggling to implement this feature. Any hel ...

Using Ajax and jQuery to Retrieve the Value of a Single Tag Instead of an Entire Page

Let's say I have a webpage named page.html: <!doctype html> <html> <head> <meta charset="utf-8"> <title>Page</title> </head> <body> <h1>Hello World!!</h1> </body> </html> Now ...

Accessing the facebox feature within a dropdown menu

Looking for assistance in creating a function to open a facebox when an option from a drop down list is selected. Here is what I have so far: <select><option value="www.google.com/" id="xxx"></option></select> In the header sectio ...

Eliminating specific text and images from WordPress articles

I am working on a Wordpress website where the post's content is displayed using <?php the_content(); ?> The content includes images and text that are all outputted within < p > tags. My goal is to style the margins/padding of the images a ...

Learn how to dynamically disable a button using jQuery within the Materialize CSS framework

While attempting to disable a button in Materialize CSS using jQuery, I encountered an issue. Despite following the documentation found here, it seems that simply adding the 'disabled' class does not automatically disable the button as expected. ...

Laravel 8 - sweet alert functions properly, however, the ajax functionality is not functioning as expected

As a newcomer to Ajax, I am facing an issue with deleting records from the database using Sweet Alert2. Although my swal is working fine, the ajax function seems to be malfunctioning. Can anyone point out where the problem might be and suggest a solution? ...

Ensure that a distinct cross button is included within the text or search input and positioned to float alongside the text for

Is there a simple method to include a clear button that hovers after the text in an input field when using a search type input? <input type="search"> ...

The ion-input border seems to be fluctuating as the keyboard appears on the screen

I'm currently working with Ionic 3 and experiencing an issue where the selected ion-input's border in the ion-content is shifting when the device keyboard appears on the screen. Here is a visual representation of the problem: https://i.stack.imgu ...