Add the text to the second line using pseudo element - css

I am trying to add text below an existing div id. How ever the white-space: pre-wrap; or adding html tags such as content:"\<br/> Lorem ipsome." does not move the text to the next line.
HTML and CSS part:
div.brand-amenity-value.economy.YV span.amenity-value:lang(en)::after {
margin-left: 0px;
white-space: pre-wrap;
content: '<p> (One date change free) </p>';
}
<div class="brand-amenity-value economy YV" style="height: 70px; background-color: #f2f5e6;">
<div><span class="spark-assistive-text">Yes</span></div>
<span class="amenity-value"><span data-translation="AED 100">AED 100</span></span></div>
Screen-shot of the text that needs to be on second line:

You don't need <p> or <br> tag. The pseudo-element ::after itself can be styled. In this case set display to block would work:
div.brand-amenity-value.economy.YV span.amenity-value::after {
margin-left: 0px;
white-space: pre-wrap;
content: '(One date change free)';
display: block;
}
<div class="brand-amenity-value economy YV" style="height: 70px; background-color: #f2f5e6;">
<div><span class="spark-assistive-text">Yes</span></div>
<span class="amenity-value"><span data-translation="AED 100">AED 100</span></span>
</div>
Or if this solution is not an option somehow, you can use \A to add a new line:
div.brand-amenity-value.economy.YV span.amenity-value::after {
margin-left: 0px;
white-space: pre-wrap;
content: '\A(One date change free)';
}
<div class="brand-amenity-value economy YV" style="height: 70px; background-color: #f2f5e6;">
<div><span class="spark-assistive-text">Yes</span></div>
<span class="amenity-value"><span data-translation="AED 100">AED 100</span></span>
</div>
Reference: https://www.w3.org/TR/CSS2/generate.html#content

You can consider position:absolute
div.brand-amenity-value.economy.YV span.amenity-value::after {
content: '(One date change free) ';
position:absolute;
top:100%;
left:0;
white-space:nowrap;
}
div.brand-amenity-value.economy.YV span.amenity-value{
position:relative;
display:inline-block;
}
<div class="brand-amenity-value economy YV" style="height: 70px; background-color: #f2f5e6;">
<div><span class="spark-assistive-text">Yes</span></div>
<span class="amenity-value"><span data-translation="AED 100">AED 100</span></span>
</div>

Related

Prevent line break between last word of an element and another element

Given an inline (or inline-block) element with text of variable length, and another element to the right of the first one that acts as a kind of badge, is there a way to prevent a line break between the last word of the first element and the second element? Both elements occupying the same line is fine; a line break occurring in the text of the first element is also fine; but a line break between the two elements is undesirable. Here is an illustration explaining what I mean.
Is there a way to do this? I tried to have the two elements as spans and put a non-breaking space between them, but that didn't work.
UPDATE: Here's a quick and dirty Codepen example: http://codepen.io/anon/pen/LkzBQJ
html:
<h1>
<span class="title-text">
This is some text
</span><span class="badge">yo!</span>
</h1>
<h1>
<span class="title-text">
This is some broken text
</span><span class="badge">yo!</span>
</h1>
css:
h1 {
width: 350px;
}
.badge {
color: #f6511d;
display: inline-block;
border: 1px solid #f6511d;
border-radius: 3px;
font-size: 0.8em;
padding: 0.1em 0.2em;
line-height: 0.97em;
margin-left: 0.4em;
vertical-align: 1px;
}
UPDATE2: In my particular case, both the text in the first element, and the badge have to be rendered dynamically, using JavaScript. So Ricardo’s solution below (wrap the last word of the text and the badge in a span with white-space: nowrap), although working, will not be very easy to implement.
Check this! This line <h1>This is some text</h1><span class="badge">yo!</span> must be in one line to work.
https://codepen.io/lemonjelly/pen/rNNvLGE
SOLUTION:
The solution I could come up with is creating some sort of fix, wrapping text with the badge in a span and using the css property white-space: nowrap;.
JSFiddle
CODE SNIPPET:
.row {
display: flex;
counter-reset: paragraph;
}
.col {
width: 50%;
padding: 1em;
}
.col--left {
background-color: #011627;
padding-right: 0;
}
.col--right {
background-color: #F71735;
border-left: 2px dotted #ddd;
}
.col p {
color: #fff;
}
.col p::before {
counter-increment: paragraph;
content: counter(paragraph)". ";
}
.badge-fix {
display: inline-block;
white-space: nowrap;
}
.badge {
display: inline-block;
padding: .2em .6em .3em;
font-size: 75%;
font-weight: 700;
line-height: 1;
color: #fff;
text-align: center;
white-space: nowrap;
vertical-align: baseline;
border-radius: .25em;
}
.badge--royalblue {
background-color: royalblue;
}
.badge--tomato {
background-color: tomato;
}
.badge--crimson {
background-color: crimson;
}
<div class="row">
<div class="col col--left">
<p>
This is <span class="badge-fix">some text
<span class="badge badge--royalblue">badge</span></span>
</p>
<p>
This is some <span class="badge-fix">reasonably long
<span class="badge badge--tomato">badge</span></span>
</p>
<p>
This is some <span class="badge-fix">longish text
<span class="badge badge--crimson">badge</span></span>
</p>
</div>
<div class="col col--right">
</div>
</div>
This problem is called a widow in typesetting. There are 3 ways to fix this.
Use the widows css property
Only break the last two words together.
<h1 style="widows: 2;">
<span>This is some broken text</span>
<span class="badge">yo!</span>
</h1>
Caveat: Not supported by Firefox (https://caniuse.com/?search=widows) and seems to only work properly with page breaks and column breaks
Use a character
Add a "physical" non-breaking space character, and only that character, between the last word and the badge.
<h1 style="widows: 2;">
This is some broken text <span class="badge">yo!</span>
</h1>
If you have to do this with multi-line code, you can use HTML comments to avoid breaking spaces.
<h1 style="widows: 2;">
This is some broken text<!--
--> <span class="badge">yo!</span>
</h1>
Caveat: It's ugly
Wrap the last word and the badge in a white-space: nowrap span.
<h1 style="widows: 2;">
This is some broken <span style="white-space: nowrap;">text <span class="badge">yo!</span></span>
</h1>
Caveat: Not always possible if you are dealing with dynamically generated code
You can add padding to the text and a negative margin:
<h1>
<span class="title-text" style="padding-right: 15px;">
This is some text
</span><span class="badge" style="margin-left: -15px;">yo!</span>
</h1>
<h1>
<span class="title-text" style="padding-right: 15px;">
This is some broken text
</span><span class="badge" style="margin-left: -15px;">yo!</span>
</h1>
This works for even dynamically generated content, unlike having to make a tag around the last word of the text and the image.
(Based on an answer I saw here: https://stackoverflow.com/a/25857961/5899236)

Css label not going full width on multiplelines

I have no way of explaining this better.
Please see this fiddle: https://jsfiddle.net/4vosLvxg/3/
<div class="col-md-12 " style="display: flex;">
<div class="checkbox">
<label class="tooltips">
<input type="checkbox"> Système d'avertissement de collision
et frein de secours automatique
</label>
</div>
<a class="tool-tip"></a>
</div>
The CSS is irelevant since I've tried all known combinations.
What I (my client) want(s) is to make the infobutton appear near the text (label) but not inside the label.
To reproduce the issue one must resize the window till the last word goes on a new line. Then he will see that the label's width won't be as long as the longest line.
Hours wasted: ~14
Any suggestion/hack will do.
EDIT:
This explains it better:
Here's what I think you are asking for. Please note the change in html markup:
.checkbox {
display: inline-block;
position: relative;
padding-left: 15px;
}
.checkbox input[type=checkbox] {
float: left;
margin-left: -15px;
}
.checkbox label {
text-align: justify;
display: inline-block;
}
.checkbox a.tool-tip {
width: 14px;
background: url("http://www.chromachklist.com/wp-content/uploads/2012/06/InfoButton0.png") right no-repeat;
padding: 10px;
position: absolute;
right: 0;
top: 0;
}
.tooltips a.tool-tip {
visibility: hidden;
position: initial;
float: right;
}
<div class="col-md-12 " style="display: flex;">
<div class="checkbox">
<a class="tool-tip"></a>
<label class="tooltips">
<a class="tool-tip"></a>
<input type="checkbox"> Système d'avertissement de collision et frein de secourssssssssss automatique
</label>
</div>
</div>

Centering a Display:inline; element

I can´t center this element. I believe it is because of display:inline; in the CSS block. Does anyone have an idea?
<p class="mr">Monatliche Rate </p>
<a class="info">
<div class="circle-text">
<div>?</div></div>
<span> Netto-Rate</span>
</a>
<p class="mr">:
<span id="results"></span> €</p>
CSS
.mr {
color: #1d6912;
font-size: 18px;
font-weight: bold;
margin-bottom: 5px;
margin-top:2px;
text-align:center;
**display:inline;**
}
You can place it in a wrapper, and add text-align:center to the wrapper.
<div class="mr-wrapper">
<p class="mr">Monatliche Rate</p>
</div>
.mr-wrapper {
text-align:center
}
.mr {
display: inline;
}
Demo
based on your question and the 1st answer I have prepared 3 examples for you to choose from:
http://jsfiddle.net/mofeenster/6V6Z7/1/
The simplest answer is this:
.mr {
margin: auto;
display: table;
}

How to place <span> elements side by side?

I have a small problem with these <span> elements in a <div>.
http://jsfiddle.net/kkzLW/179/
Here is the section of CSS code that I'm working with:
.rightRapper {
border-style: dotted;
margin-left: 105px;
margin-top: 0px;
height: 90px;
width: 100px;
display: block;
}
.leftRapper {
border-style: dotted;
margin-left: 0px;
height: 90px;
width: 100px;
display: block;
}
Here is the HTML section:
<div id="battleBox">
<span class="leftRapper">
<span id="buttonColumn">
<span id="container3" class="topButton">
+
</span>
<span id="container4" class="bottomButton">
-
</span>
</span>
</span>
<span class="rightRapper">
<span id="buttonColumn">
<span id="container" class="topButton">
+
</span>
<span id="container2" class="bottomButton">
-
</span>
</span>
</span>
</div>
I'm trying to get the <span> .leftRapper and .rightRapper to be side by side in the <div> battleBox. However, when I set the CSS display property to inline, the <span>s get squished into a smaller shape for some reason. When I set the display to block, it turns them into the size I want but it doesn't display them the way I want, because they're not displayed inline.
What is causing the <span>s to have a smaller size?
Add or replace the properties below in the following CSS classes/selectors:
#battleBox {
width: 216px; /* increasing width from 210 to 216 because your border takes 6 extra px*/
}
.rightRapper {
margin: 0px; /* remove all margins to fit two divs in the container */
display: inline-block; /* display block elements in one line */
}
.leftRapper {
margin: 0px;
display: inline-block;
}
Example
You could/should add a float: left to .leftRapper.
Other options are e.g. adding a negative right margin to .leftRapper.

layout fields using CSS

I'm trying to layout field labels and values like this:
Name: Bob
Age: 25
Occupation: Code Monkey
The relevant HTML is
<div class="field">
<span class="reset">End Time:</span>
<span>05:00pm</span>
</div>
<div class="field">
<span class="reset">Items:</span>
<span></span>
</div>
<div class="field">
<span class="reset">Repeats:</span>
<span>Never</span>
</div>
And the relevant CSS is:
div.field {
margin-bottom:10px;
}
span.reset {
display: block;
float: left;
margin-right: 0.5em;
text-align: right;
}
Unfortunately, the "Repeats" field is being shown on the same line as the "Items" field. I verified that this only happens when the value of the "Items" field is empty <span></span>.
I tried added clear: left to span.reset, and while this stops two fields appearing on the same line, it totally messes up the alignment of the labels and fields.
Is there any way I can fix this problem without drastically changing the XHTML?
Thanks,
Don
Add this to your CSS clear: left;:
div.field {
clear:left;
margin-bottom:10px;
}
This will force it to the next line below.
If you want all these to line up you're going to have to give the label (reset?) a fixed width either directly or indirectly. Try this:
div.field { overflow: hidden; }
div.field span { margin-left: 150px; display: block; }
span.reset { float: right; width: 150px; margin-left: 0; text-align: right; }

Resources