Tips for achieving a uniform padding when a line is split into two sections?

Upon reviewing the code snippet below, I've observed that incorporating left and right padding results in a more evenly balanced color highlighting effect, as demonstrated in point 2 for further clarification:

.a { 
    font-size: 4em;
    font-family: sans-serif;
    background-color: #ff800f;
}

.b { 
    padding: 0 0.2em;
}

#c { width: 200px; }
1. <span class="a">Hi there</span> not enough space at left/right in comparison to the height of the highlighting
<br>
2. <span class="a b">Hi there</span> better
<br>
3. <div id="c">Hello and <span class="a b">Hi there</span></div>

Question: How can one automatically increase padding at the end of the first line and the beginning of the second line when a color-highlighted line is split into two parts, like "Hi / there" in example 3?

In example 3, it should ideally appear as follows:

Answer №1

Utilize the CSS property box-decoration-break: clone on the span element, remembering to include a vendor prefix like -webkit- for most browsers aside from Firefox.

The definition of clone is explained as follows:

"Each box fragment is rendered independently with the specified border, padding, and margin wrapping each fragment..." -(MDN)

.a {
  font-size: 4em;
  font-family: sans-serif;
  background-color: #ff800f;
  box-decoration-break: clone;
  -webkit-box-decoration-break: clone;
}

.b {
  padding: 0 0.2em;
}

#c {
  width: 200px;
}
1. <span class="a">Hi there</span> not enough space at left/right in comparison to the height of the highlighting
<br> 2. <span class="a b">Hi there</span> better
<br> 3.
<div id="c">Hello and <span class="a b">Hi there</span></div>

For more details, visit: https://developer.mozilla.org/en-US/docs/Web/CSS/box-decoration-break

Answer №2

A possible solution for spacing difficulties is utilizing a non-breaking space &nbsp.

3. <div id="c">Greetings and <span class="a b">Hey&nbsp &nbspthere</span></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

Centering an image on a webpage using CSS

Here is the code I am using for displaying my image: return <div class="imgContainer"><img src={broomAndText} alt="broomAndText" /></div> In my css file, I have included the following styling: .imgContainer { tex ...

Sending a JavaScript variable to Python Flask

I've searched through various forums but still can't grasp how to pass a JavaScript variable to Flask using post/get forms. I know that the form creates a post/get request that can be received by the Python Flask script, but I need a simple examp ...

Is it possible to dynamically change HTML content by utilizing a JSON file?

Looking to implement a JavaScript loop (using jQuery) that can dynamically populate the HTML file with content from a JSON file based on matching <div> ids to the JSON "id" values. The solution should be scalable and able to handle any number of < ...

What is the best way to rearrange three divs into two columns?

I am trying to rearrange the order of three divs in my markup layout. The desired layout is to display div one first, followed by three underneath it in the first column. Then, in the second column, I want to only show div two. My current CSS configuratio ...

Sharing the input value with a service in Angular 4

I am a beginner when it comes to Angular 4. I currently have a variable named "md_id" which is connected to the view in the following way. HTML: <tr *ngFor="let item of driverData"> <td class="align-ri ...

Ways to enlarge a div and its contents when hovering over it?

I am struggling to create a div that expands on hover and have been unable to make the content expand along with the div. I've experimented with various overflow options, but none of them seem to work. .grow { padding: 5px 5px 5px 5px ...

Position the div element beside the image, rather than below it

I am having an issue with Bootstrap where the card I want to display on the right of an image is appearing below it instead. I attempted to use the display: inline-block property, but unfortunately, that did not solve the problem. I also explored other so ...

Utilizing the Power of ChartJs

I am currently in the process of developing a web application that will provide users with the ability to compare the number of calories they burn during exercise with the amount they consume in a meal. To achieve this goal, I have chosen to utilize ChartJ ...

Is there a way to apply an indigo color solely to a div using CSS without affecting the entire body?

Can someone help me achieve a result similar to the image shown https://i.sstatic.net/IY1kI.png I've been working on some code, but I haven't been able to get the exact desired outcome. My goal is to make a div indigo and leave the rest of the b ...

Tips for maintaining consistent webpage layout across various screen sizes

Although this method may seem outdated, I have always been intrigued by how it was accomplished. In the past, before websites became responsive, pages would simply shrink down to fit on mobile devices without adjusting their appearance. How was this achie ...

Ways to customize the appearance of CSS bootstrap 'col' elements?

I'm attempting to customize the styling of bootstrap CSS columns col-xl in order to display 5 or 6 cards/tiles on my webpage instead of just 4 (refer to attached image) without interfering with the other grid layouts. Unfortunately, I am currently str ...

Tips for keeping the hover effect of a sibling element in Material-UI

Card Component import image from "../../Assets/pic.jpg"; import React, { useState } from "react"; import { makeStyles } from "@material-ui/core/styles"; import Card from "@material-ui/core/Card"; import CardActionAre ...

Identification for checkbox within an HTML container

I am looking to create a feature where clicking on the label text will toggle the checkbox on the box.html page. Within index.html, I have added an iframe that displays box.html. Index.html <html> <body> <iframe src="box.html" ...

CSS issue: Font size

Can someone help me out with a CSS issue that has been tripping me up? I've been working with CSS for three years now, and this particular problem is stumping me. I have defined some styles in my CSS file that should be applied to the content of my w ...

Differentiating card heights on hover in bootstrap can be achieved by adjusting the CSS properties specifically for each card

I'm currently working with bootstrap cards and have added hover effects to them. However, I'm facing an issue where on larger devices, hovering over one card enlarges all the other cards as well. This problem doesn't occur on smaller devices ...

What is the best way to maintain a background image in html/ASP.net that remains consistent when the website is relocated?

I've been attempting to designate an image as a background image. According to the information I have come across, it is necessary to use a tilde (~) before the address in order for the site to function properly when placed inside a folder. However, t ...

Styling Django's buttons and search bars to appear inline using CSS

In my Django template, I have the following HTML code: <div id = "search_form"> <form action="" method="get" id = "search-form"> {% csrf_token %} {{ form.as_p }} {{ form.media }} </form> ...

Looking to modify the contents of a shopping cart by utilizing javascript/jQuery to add or remove items?

I'm dealing with a challenge on my assignment. I've been tasked with creating a Shopping Cart using Javascript, HTML5, and JQuery. It needs to collect all the items from the shop inside an Array. While I believe I have most of it figured out, I a ...

Clicking on the overlay does not close the bootstrap collapsed toggle

I'm currently facing an issue where I need to add a listener that closes the menu when clicked away. The code snippet below shows my attempt at solving this problem: $("body").click(function(e){ var $box1 = $('.navbar-toggle'); var $b ...

Emphasize cells and headers when a cell's value deviates from its initial value

I implemented cell editing in my primeng table. Here is the code: <div class="card"> <p-table [value]="products" dataKey="id" [tableStyle]="{ 'min-width': '50rem' }"> <n ...