Question regarding CSS.
I will receive sentences like: This is a sentence and I will automatically break it to new lines regarding the parent's container width. So, for example, I will have
This is
a sentence
What I am trying to do can actually can be seen in this code example. The lines are highlighted but there is some space (with white color) between lines. When setting some color background-color: black I can't create such spaces changing line-height. I also tried setting linear-gradient as background but it doesn't work the way I wanted.
This can be done by wrapping lines in span tags, but I want to avoid additional backend work.
Is there any obvious solution I missed or is a little bit tricky?
div {
width: 200px;
}
h1 {
background-color: red;
line-height: 50px;
}
<div>
<h1>Some text and its wrapping</h1>
</div>
Without backend work it is simply not possible - you need a wrapping element for the fixed width.
But you don't need extra elements for each line. Check this:
div {
width: 200px;
display: block;
}
h1 {
background-color: red;
font-size: 36px;
font-weight: bold;
line-height: 50px;
display: inline;
}
<div>
<h1>This is a sentence.</h1>
</div>
Or you use pseudo elements:
div {
width: 200px;
display: block;
}
div:after {
background-color: red;
font-size: 36px;
font-weight: bold;
line-height: 50px;
display: inline;
content: attr(data-content);
}
<div data-content="This is a sentence."></div>
Related
is it possible just with pure css to attach last two divs inside span with its last word so it would wrap with the last word? Even when you dynamically resize window?
Here is html :
<span>Some text that will be here
<div class="class1" style=""></div>
<div class="class2"></div>
</span>
Here is css :
.class1 {
display: inline-block;
width: 19px;
height: 19px;
margin-left: 5px;
vertical-align: text-bottom;
background-image: url(some.png);
background-repeat: no-repeat;
cursor: pointer;
}
.class2 {
float: none;
display: inline-block;
vertical-align: text-bottom;
width: 20px;
height: 20px;
margin-left: 5px;
background: url(some2.png) -154px 0px no-repeat;
cursor: pointer;
}
I want the span to wrap only with the use of last word when i try to resize the window. What happens now is that it first wrap the class2 div, then class1 div and after then it starts to wrap the text. Ive seen many solutions using just display:inline but when i use display:inline on the divs it losts it width so i dont think its a possible solution here. Is there any way with pure css solution to attach those two divs with last word of the text so it wraps together when you resize the window? I cant encapsulate last word with those two divs inside another element like this :
<span>Some text that will be
<span class="encaps">here
<div class="class1" style=""></div>
<div class="class2"></div>
</span>
</span>
Because the text is always different length and i dont know before what the last word will be.
Thank you in advance for your responses.
UPDATE
I am writing down some examples of behaviour that i want to accomplish when the window is resizing:
Bad behaviour:
1.)
Some text will be here*div*
*div*
2.)
Some text will be here
*div**div*
Good behaviour:
Some text will be
here*div**div*
You can wrap all of the text in a span and then allow line breaks inside that span, but not between it and the divs. You will need to apply that styling to the container span. This works as follows.
Note that I made some very minor changes to your example (changed divs in spans into spans to make the HTML valid, changed image URLs so they resolve to something, minor style changes), but the main idea is the styling of the .wrap and .nowrap elements. The .wrap element is an added element, a span wrapping the text.
I have created a JSFiddle as well, so it is easier to change the viewport size and test the wrapping.
.class1 {
display: inline-block;
vertical-align: text-bottom;
width: 19px;
height: 19px;
margin-left: 5px;
background-image: url('https://placehold.it/19');
background-repeat: no-repeat;
cursor: pointer;
}
.class2 {
display: inline-block;
vertical-align: text-bottom;
width: 20px;
height: 20px;
margin-left: 5px;
background: url('https://placehold.it/20') no-repeat;
cursor: pointer;
}
.nowrap {
white-space: nowrap;
/* no line breaking */
font-size: 0;
/* hide text between text and divs */
}
.wrap {
white-space: normal;
/* allow line breaks in span */
font-size: 1rem;
/* normal font size */
}
<span class="nowrap">
<span class="wrap">Some text that will be here</span>
<span class="class1"></span>
<span class="class2"></span>
</span>
What I try to create is a simple line that will go after the text vertically centered. So far, I've come up with the following solution:
<h1>lorem ipsum <span></span></h1>
h1 > span {
display: inline-block;
height: 1px; width: 50px;
margin-bottom: .2em;
background-color: black;
}
https://jsfiddle.net/7xqp7m2h/
The bad thing about this approach (not to mention that the line is not 100% vertically centered) is that this is a too compilcated solution for such an easy task.
What I thought about is maybe to add a line-through an invisible text within the span:
h1 > span {
text-decoration: line-through;
}
However, I failed to make it work.
Any ideas on how to make a visible line-through with hidden text, or maybe another solution that would be simplier than what I have now?
No need for a span at all.
A pseudo-element and flexbox can do that.
JSFiddle Demo
h1::after {
content: '';
height: 1px;
background: red;
flex: 1;
margin-left: .25em;
}
h1 {
display: flex;
align-items: center;
}
<h1>lorem ipsum</h1>
Flex can easily help you (set a width if you wish to h1 or pseudo ).
the use of a pseudo avoids the extra span in the HTML, you can apply it to any title level .
h1 {
display: flex;
background: gray;
}
h1:after {
content: '';
border-top: 1px solid;
flex: 1;
margin: auto 0.5em;
}
<h1>lorem ipsum </h1>
https://jsfiddle.net/7xqp7m2h/4/
https://jsfiddle.net/9crkt4hn/
Using as text in the span will make a space without seeing it as a space :P
As you see in the fiddle you see it works without problems.
This is my code but I want the text to only have background color behind it, and not stretch across the entire screen? Any ideas?
.section_title {
background-color: orange;
text-align: center;
margin: 0px auto;
}
HTML is
<div class="col-md-12">
<div class="section_title">
<h2>Choose a Pack to Print</h2>
</div>
</div>
An option is adding display: inline-block; to the CSS of the text element.
One problem I found with display: inline-block; is it clears floats incorrectly. Instead, I use width: fit-content;
.highlight {
background: yellow;
padding: 0.5em;
width: fit-content;
}
<h1 class="highlight">Highlight for text only!</h1>
<h1 class="highlight">Highlight me too!</h1>
There's a few ways to do this, but probably the best way is to make the h2 inline or inline-block.
Using inline-block will allow you to set width/height.
.section-title {
text-align: center;
}
.section-title h2 {
display: inline-block;
}
The other way to do this is to set a width on the h2 and set the margin to auto;
.section-title h2 {
margin-left: auto;
margin-right: auto;
width: 50%; /* for example */
}
If you want all your headings to be a set width, I'd choose the second one (allowing for text to wrap). If you want the box to be flexible and hug the contents, I'd use the first.
I have an h2 element inside a div. The div is 150px wide but the h2 might be wider, so the text must break and go to the next line. The problem is that even that the text breaks, the background-color of the h2 element doesn't, and thus the h2 element's background color covers the entire div.
Is there a way I could make the h2's background-color appear only under the text?
Example: http://codepen.io/anon/pen/azBOLO
HTML:
<div>
<h2>This is a long text test</h2>
</div>
CSS:
div {
background-color: green;
width: 150px;
height: 150px;
padding-top: 15px;
}
h2 {
background-color: red;
}
Set the h2 to display:inline and line-height to 1em to ensure each line nests with the previous one without gaps.
div {
background-color: green;
width: 150px;
height: 150px;
padding-top: 15px;
}
h2 {
background-color: red;
display: inline;
line-height: 1em;
}
<div>
<h2>This is a long text test</h2>
</div>
I finally made it using this trick: http://css-tricks.com/multi-line-padded-text/
EDIT: As suggested, the trick is to set a padding-left on the div, and then create a ::after class on the span that will fill the empty space on the left (same background color, no text).
That way you can achieve padding on a multiline span
Please look at this fiddle.
I have different behavior for following two classes.
.class2 {
width: 49.5%;
display: inline-block;
}
.class3 {
width: 50%; /* Wraps to next line */
display: inline-block;
}
What I wanted to do is to divide the width equally b/w two divs and then start filling each div * with its own content.
But with width 50%, if I put some text in the div, the second div wraps to next line
With width 49.x%, it does not wrap.
Why is it wrapping? I am working inside a particualr div.
How can I make it not wrap and keep width = 50%.
Other than 50%, I dont know how to come-up with the correct value.
Try floating your divs like this:
.class1 {
background-color: red;
width: 100%;
}
.class2 {
width: 50%;
display: inline-block;
background-color: blue;
color: white;
float:left;
}
.class3 {
width: 50%;
display: inline-block;
background-color: blue;
color: white;
float:right;
}
You do not need to resort to float. That is a relatively ugly workaround that introduces its own problem (having to clear floats and/or set overflow:hidden on the containing element).
You do not provide your HTML markup but I would guess you have whitespace between the elements, as in Mr Alien's answer.
See Two inline-block, width 50% elements wrap to second line for a better solution.
Use float: left; to float you 1st <div> on the left and you can also float your 2nd <div> on the right using float: right; than use <div style="clear: both;"></div> to clear your floating elements:
CSS:
.class1 {
background-color: red;
width: 100%;
}
.class2 {
width: 50%;
background-color: blue;
color: white;
float: left;
}
.class3 {
background-color: green;
width: 50%;
float: right;
}
HTML:
<div class="class1">
<div class="class2">
This is demo
</div>
<div class="class3">
This is demo
</div>
<div style="clear: both;">
</div>
My fiddle
The reason that they stack instead of sit next to one another is that display: inline-block recognizes white space. If you have a return after your first div, then it will pick that up and cause it to stack. A work around would be something similar to...
<div class="col1">
Column one content.
</div><div class="col2">
Column two content.
</div>
Don't use floats, they cause all sorts of headaches. Just add this to your CSS:
body,html{ white-space:nowrap; }
a,pre,p,span,strong,h1,h2,h3,h4,h5,h6{ white-space:normal; }
...then make sure all text on your site is contained either within 'p' or 'span'
I often use display:inline-block; .
However, display:inline-block has some problem.
I recommend Grids - Pure pattern and wrote a sample code like Pure.
.class1 {
letter-spacing: -0.31em;
*letter-spacing: normal;
*word-spacing: -0.43em;
text-rendering: optimizespeed;
font-family: Georgia, Times, "Times New Roman", serif;
}
.class2, .class3 {
display: inline-block;
zoom: 1;
letter-spacing: normal;
word-spacing: normal;
vertical-align: top;
text-rendering: auto;
font-family: "Hiragino Kaku Gothic ProN",Meiryo,Verdana,sans-serif;
width: 50%;
}
my fiddle
Put a:
margin:-2px;
into your class3.
This doesn't break your div but solve your problem.
Don't know why