How can I divide a div by utilizing word wrap?

My CSS-styled div is pretty straightforward

.text { text-transform: capitalize;
        color: #FFFFFF;
        background: #918a8a;
        opacity: 0.6;
        font-size: 2em;
        height: 80px;
        width: 200px;
     }

It creates a grey box with white text inside, measuring 200px by 80px.

I'm looking to add some "transparent" whitespace if the text wraps to the next line beyond the 200px limit.

For example, in the following HTML:

<div class="text">Here is some text that I typed</div>

It should look like this:

https://i.sstatic.net/Aklg4.png

If the background changes (like blue), the transparent "whitespace" would allow the new color to show through. The design will change based on user choice, even potentially using images.

https://i.sstatic.net/r7hil.png

The text may vary in length from 0 to 60 characters and wrap automatically within one div tag.

Answer №1

If you want to achieve this effect, you can utilize the repeating-linear-gradient property in CSS:

.text {
  font-size: 30px;
  line-height: 50px;
  background-image: repeating-linear-gradient(
    blue, 
    blue 45px,
    transparent 45px,
    transparent 50px
  );
}

The initial two colors will act as the "background" color and will extend over your text.

The subsequent two colors are set to be transparent, reaching up to the defined line-height of the text.

Code Snippet:

body {
  background: #cfc;
}

.text {
  color: #FFFFFF;
  width: 300px;
  font-size: 30px;
  line-height: 50px;
  background-image: repeating-linear-gradient(
    blue, 
    blue 45px,
    transparent 45px,
    transparent 50px
  );
}
<div class="text">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</div>

Answer №2

After some tinkering, I managed to create a solution that somewhat functions, although the gray background unfortunately doesn't extend to the width of the container. To fix this, you will need to include a span inside the div.

.text-highlight {
  background:#fff;
  width:200px;
}
.text-highlight span {
  font-size:24px;
  color:#000;
  background:#ccc;
  line-height:42px;
}
<div class="text-highlight">
    <span>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Earum, et, ratione. At facere nisi facilis nulla officiis, suscipit saepe quos cum et sequi nostrum amet atque non quam, consequatur dolor.</span>
  </div>

Edit: You can adjust the width of the inline elements by specifying it in the parent container...although the result may not be as aesthetically pleasing.

.text-highlight {
  background:#fff;
  width:200px;
  text-align:justify;
}
.text-highlight span {
  font-size:24px;
  color:#000;
  background:#ccc;
  line-height:42px;
}
<div class="text-highlight">
    <span>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Earum, et, ratione. At facere nisi facilis nulla officiis, suscipit saepe quos cum et sequi nostrum amet atque non quam, consequatur dolor.</span>
  </div>

Answer №3

I am not entirely certain how to achieve this using only one div element. Please see the code snippet below as it may provide some assistance:

<div class="content"><span> This is an example text that I have entered </span</div>

.content {
        text-transform: uppercase;
        color: #000000;
        opacity: 0.8;
        font-size: 1.5em;
        height: 60px;
        width: 150px;
}
.content span{
        background: #765432;
        line-height:15px;
}

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

The JavaScript creates a select box, but the value does not get sent when the form is submitted

When a user selects a value from the first select box, a second select box is dynamically created using JavaScript. This JS triggers a PHP file to query a MYSQL database for relevant items based on the initial selection. The issue I am facing is that the ...

The iFrame that is generated dynamically becomes null when accessed from a page that has been loaded using JQuery

One issue I am facing is with a dynamically created iframe in regular javascript. It functions perfectly fine when called from a static page using conventional methods. However, when it is being called from a page loaded by jQuery, I encounter an error s ...

What is the best way to move to a new line without creating a space between the current line and the new line?

I am trying to achieve the desired output: $text = Hello, this is example text. rather than: $text = Hello, this is example text. When I use \n, it starts a new line. How can I make it stay on the same line below with the same startin ...

Developing a fresh Outlook email using a combination of javascript and vbscript

I have created a custom HTML page with fields and a button to fill out in order to generate a new Outlook mail item. To format the body of the email using HTML, I am utilizing VBScript to create the new mail item. <script> function generateEmail() { ...

Using padding and float properties in the a tag of HTML does not result in the expected behavior

I am attempting to display the menu in a left-to-right fashion, however I am encountering issues with using padding and float in HTML. Here is my current code: .main-nav ul li a { padding-right: 15px; float: left; } <div class="main-nav"> &l ...

Utilizing jQuery Mobile - Dividing content across multiple HTML files

In the process of developing a web application utilizing jQuery and jQuery mobile, I am aiming to display various pages. Given that the corresponding html-markup may become lengthy, I am interested in dividing the html into separate files. For instance: & ...

How can I align an input to the right using Bootstrap?

I found an example of a navbar that I liked and decided to copy it. Here is the code snippet: <div class="container"> <h3>Nawigacja zamknieta w kontenerze</h3> <nav class="navbar navbar-toggleable-md navbar-light bg-faded"> < ...

Issue with closing collapsed menu on click in Bootstrap version 3

Here is the code snippet: <li> <a href="#" id="statics" class="main-bar"> <span class="glyphicon glyphicon-stats" aria-hidden="true"></span> </a> </li> I am trying to toggle the responsive menu on eleme ...

Turning On/Off Input According to Selection (Using jQuery)

<select name="region" class="selection" id="region"> <option value="choice">Choice</option> <option value="another">Another</option> </select> <input type="text" name="territory" class="textfield" id="territo ...

I'm having trouble getting Jquery's insertAfter() function to function properly

After creating a parent div and using jQuery's insertAfter() method to insert additional divs, an unexpected issue arises. The first record goes to the bottom, while subsequent records are inserted above it. Below is the function in question: functi ...

PHP not compatible with Fancybox iframe

I'm facing a simple problem that I can't seem to figure out. I have a button that, when clicked, opens a fancybox with an iframe displaying a page from my website. This page includes a form for sending an email. Everything works fine except that ...

The tab functionality in Bootstrap is malfunctioning

Struggling with implementing tab functionality on my navigation bar using Bootstrap. The tab links change the URL, but the content of the tabs doesn't update accordingly. I'm using Bootstrap 4 - do I need to add jQuery code to make this work? Wha ...

Adding a slight 1px right margin between an HTML table and its enclosing div in Bootstrap 3

Help! I'm trying to troubleshoot some HTML issues and can't figure out where this pesky 1px gap between my table and the wrapping div is coming from... Just for reference, I am using Bootstrap. I suspect that it could be a glitch in the Bootst ...

Dynamic Bootstrap Table with Fixed Header

I am customizing a Bootstrap table by adding CSS for a sticky header. My code snippet includes the following: .table-sticky th { background: #fff; position: sticky; top: -1px; z-index: 990; } <div class ...

use ajax to dynamically load a section of the webpage based on filter criteria

I need to implement a search filter using 3 checkboxes. The search results will be displayed in the div with the id=posts_results <div class="checkbox"> <label><input type="checkbox" id="id1" class="typePost" value="En groupe"> ...

Troubleshooting Problem with Accordion Size in CSS

I am facing an issue with a dropdown menu that I have created. The dropdown has parent and child rows to display controls, but the width of the Accordion is not stretching as expected despite being set to 100%. Using Chrome and Edge developer tools, I insp ...

Firefox failing to interpret nested element background

I'm facing an unusual problem with Firefox while developing a menu that has a maximum of three levels. The issue seems to be isolated to Firefox as everything works perfectly in Chrome. When I click on 'First Level Menu', a ul opens with two ...

What is the reason for having a space between the <body> and the <html> tags?

Can you explain why there is a gap between the top of the green box and the browser window in Chrome and Firefox? I've tried various CSS configurations to eliminate the apparent space between the html and body elements, but it seems that some padding ...

Customizing the Twitter bootstrap navigation in Wordpress

I've been working on theming my navigation menu using Twitter Bootstrap. I want it to resemble the demo example of a fixed top banner, which can be seen here: http://getbootstrap.com/examples/navbar-fixed-top/ Although I have managed to get it to wor ...

What is the best way to retrieve the color of a WebElement when dealing with a complex CSS structure in Webdriver

I am currently trying to determine if the color of a specific page changes immediately after clicking on a button designed to alter the color of an element within that page. My goal is to extract the RGB value stated as rgb(183,168,168); in this particul ...