jsfiddle here.
I have a span element that is highlighted using background-color: yellow and padding: 1px 10px.
However, on smaller devices, this line of text becomes two lines, three lines, etc. This causes the first and second lines to 'lose' the padding on the right side, such as the words "the" and "to", and the second and third lines to 'lose' the padding on the left side, such as the words "highlight" and "better" in the picture below:
How can I ensure these words (in the picture above, "the", "highlight", "to", "better") and all other words keep this 10px padding on the left and right side?
Someone in response to a similar question suggested using display: block, but this makes the span no longer a span, just a rectangle and that is not how I need it to look. It also makes the highlight span the entire width of the page.
Use the box-decoration-break CSS.
span {
background-color:yellow;
padding:1px 10px;
box-decoration-break: clone;
-webkit-box-decoration-break:clone;
}
This applies the styles to every box fragment in your span.
You can try faking the background when the span is broken by using box-shadow. Try something like this and see if it works for you:
span {
background-color: yellow;
padding: 1px 0;
box-shadow: 10px 0 0 0 yellow, -10px 0 0 0 yellow;
}
<span>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam a nisi ipsum. Mauris convallis dapibus diam ac sollicitudin. Vivamus varius nulla magna, ac rutrum nulla accumsan quis. Duis placerat, elit non posuere elementum, tellus massa pellentesque nunc, eu maximus ligula metus non dui. Sed accumsan quam nec sodales sagittis.</span>
To include as much as possible browsers , you could relay on position:relative and use 2 inline tag inbricated to mimick the desired result.
mind that some margins or padding should be considerate to keep inbound the translated element via position.
example to show the idea
p {/* make the text wrap*/
width:15em;
margin:2em;
}
span {
background:yellow;
/* increase line height for better reading : optionnal*/
line-height:1.35em;
padding: 0.25em 0;
}
span span {
/* move child 1em to left to fake padding */
position:relative;
left:1em;/* value of your left alike padding*/
}
/* eventually on the last line ? */
span span:after {
content:'';
padding:0.25em;
}
<p>
<span>
<span>This sentence <del> has some padding</del> to make the highlight appear thicker. These are more words to better explain.
</span>
</span>
</p>
https://jsfiddle.net/ynh35sL5/3/
This is not the best , extra HTML for styling ... spans are neutral :(
just wrap it in a container div and put the horizontal padding on that element since that's what you actually want:
<div class="span-container><span>my text</span></div>
.span-container { padding: 0 10px; }
.span-container span { background-color: yellow; padding: 1px 0; }
here's a fiddle demonstrating that this achieves exactly what you want without work arounds that don't work in every browser: https://jsfiddle.net/xg1bkgmn/
span is an inline element, it makes sense to use it for your case of highlighting only the text and widening the lines. However, to keep the entire thing padded, it needs a block element container, this is standard practice.
Why go with more complicated CSS when the solution is this simple?
Related
I have
<div id="aboutPyKov">
<h2 id="pyKovSubheading">About PyKov</h2>
<p id="pyKovIs">Lorem ipsum dolor sit amet,<br/>consectetur
adipiscing elit.<br/>Vestibulum congue mattis odio.<br/>Nulla f
acilisi. Quisque tempus<br/>varius enim, quis mattis metus,
<br/>auctor quis. Lorem ipsum dolor sit<br/>amet, consectetur
adipiscing elit.<br/>Pellentesque a euismod sem, a<br/>convallis
turpis. Donec aliquet<br/>quis leo at fermentum. Maecenas<br/>ut
lacinia magna. Maecenas gravida<br/>interdum turpis non
fermentum.</p>
</div>
For styling, I have
#aboutPyKov {
border: 8px dotted rgba(255,198,107,0.93);
border-radius: 20px;
}
This works fine, however it shows a dotted border around the whole width of the whole page. I want it to be self-contained, but instead, it goes around the whole screen as you can see in this picture. How do I make it so it only goes around the text? Also, the top border is hugging the background color above it. I would also like to know how to change that.
This is CSS level 1: block and inline. Block elements take up 100% of available width unless you set them to float or set an explicit width. Either set the border to the paragraph element or set a width to your div.
Try adding padding = 0px" to your <p> tag and <h2> tag,
p, h2 {
padding: 0px;
}
because <p> and <h2> tags have default padding applied.
Just change the display attribute
#aboutPyKov {
border: 8px dotted rgba(255,198,107,0.93);
border-radius: 20px;
display:inline-block; // just change the display
}
I've read a few solutions with similar titles but none with a solution to this layout.
I have 3 content blocks which all stack beneath each other at most screen widths.
However, when content become overly wide, I want to display a slightly different format.
I want to display the media to the right and the title and text to the left with the text directly beneath the title. It currently sits below the media block (as per the snippet).
anyone know how I can fix it?
.content {
overflow:hidden;
}
.chart {
height: 200px;
width: 200px;
background-color: red;
}
.title, .text {
float:left;
}
.media {
float:right;
}
<div class="content">
<h3 class="title">This is a reasonably long title</h3>
<div class="media">
<div class="chart"></div>
</div>
<p class="text">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis non urna est. Quisque sed dolor ac ex aliquet aliquet. Integer ornare, velit vitae iaculis faucibus, nulla libero molestie sem, eget placerat augue massa vitae justo.</p>
</div>
There are 2 things you need to do:
1) You need to add a width for your text block, cause now it's 100% and it takes 100% of parent block width - so no floating will be.
2) You need to add to text block a clear property with left value - cause you don't need it to be floated by the headerfrom the left side.
It's all you need to solve the issue:
.text {
clear: left;
width: 50%; /* put your own width (no matter percents or pixels), but it must be less than (parent block width - media width)*/
}
Check here the example: https://codepen.io/fox_hover/pen/8f838b7799db7a3ed4f4d742097440ef
While both previous answers do work to some degree, both fail to fully address the initial question.
The first, requires a change in the order of elements and the second applying a fixed width which was restrictive.
The final solution is in 2 parts so that it works with multiple screen sizes and media queries.
Firstly I changed the order of the elements as per answer 1. This enabled me to achieve the layout required for my 8 column (wide layout). I applied this styling using an 8 column only media query.
For all other screen sizes, I use display flexbox, which allows me to restore the order I require.
I have a weird issue where, when I hover on the pseudo element (::before) here, the highlight seems to be off.
The CSS given is:
.testimonial__quote::before {
content: open-quote;
font-size: 11.25rem;
width: 4.0625rem;
height: 3.4375rem;
position: absolute;
color: #fbce07;
display: block;
font-style: italic;
font-family: Arial, Helvetica, sans-serif;
}
HTML:
<div class="testimonial__quote-container">
<blockquote class="testimonial__quote">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean finibus lorem eu aliquet fermentum. Vestibulum ante ipsum
primis in faucibus orci luctus.
</blockquote>
<p class="testimonial__author">- Scuderia Ferrari</p>
</div>
What am looking for is someone who has had an experience with this sort of issue. I can't post a JSFiddle as some people suggest, since it's not reproducible.
It's normal behavior. Jsfiddle.
You set width and height for block element (in your case it is presented by ::before pseudo element). But font-size of text is too big and symbol " "falls out" from sized container.
At the picture below I removed width and height properties. Now block sizes are calculated depending on block content (it is " symbol).
Add these styles to see that the character does not fit in the container:
overflow: hidden;
outline: 1px solid red;
So I think you should not set width and height to this element. Or you can use svg element or image with fixed sizes.
Please check this fiddle. Note: when you use position:absolute dont forgot to properties top, left, right, bottom,
https://jsfiddle.net/Lbctgyea/5/
I'm having trouble containing the selection in chrome to a div. It wants to make it a lot wider. Playground is here:
http://jsbin.com/ujafow/28/
If you select from 1.2(a) in my example up to 1.2 you can see that the selection border goes all the way out to the edge of 1.... I'd like to contain it within 1.2 since the entire selection is within that. Interestingly it doesn't go all the way out to the edge of the outer div, so there must be some way of containing it? Other browsers (e.g. ie) seem to just select the text and everything is hunky dory.
Does anybody know what I need to do to make it stay within logical borders?
PS. I've tried with lists with the same result: http://jsbin.com/ujafow/7
PPS. Tables work well but it's contentEditable so that complicates things for me since I don't want them having to edit a table.
PPS. There is a long outstanding defect for this in Chrome.
Edit: It looks like it draws the selection to the edge of whatever is made contentEditable - if I make the whole thing contentEditable, it draws right out to the edges. The only thing that seems to stop it is if there is a div on the left... maybe I can put an empty div on either side to restrict the drawing of the selection to just that area...?
It looks like adding overflow: hidden; to .npse in the CSS will fix it.
I'd previously been trying your solution...which worked, but was very awkward to work with. But if this is correct, don't thank me... thank #ralph.m here who answered my extension to the question:
::select pseudo-element ignored in fill space, selection going out of bounds...?
Just providing it as an answer to close the loop so you can accept it if it's right--to save others who come across this question some time!
Just remove "contenteditable=false" from your title spans. At least, that worked for me.
EDIT: got it to work. check here.
You just have to figure out how to remove the changeable size and the little thing in the corner.
textarea {
background:transparent;
border:none;
}
textarea:focus {
border:none;
outline:none;
}
EDIT EDIT: use this
textarea {
resize: none;
}
EDIT EDIT EDIT: okay, finally got it.
<p contentEditable="true"><span class="counter"
contentEditable="false">1.2</span>Lorem ipsum dolor sit amet, consectetur adipiscing
elit. Cras scelerisque molestie nisi, ut accumsan libero rutrum non. Ut non scelerisque
lacus. Proin ornare rhoncus lobortis. Nullam id laoreet justo, et bibendum justo. Etiam
luctus ligula faucibus, eleifend augue tempor, dapibus nunc.</p>
It works here. Just remove "contenteditable" from the body and add it to each individual element. You'll have to remove the border and outline on :focus, though.
P.S.: :P :D for OCD
I ended up solving this by putting an absolute positioned div to the left of it (which I've called the "title-container" in the code below). It then contains the selection nicely. The title-container div is a child of the elementToContain div. The size of the div's is set via js.
.elementToContain {
position: relative;
padding: 0.5em;
}
.title-container {
position: absolute;
top: 0em;
bottom: 0em;
border: ridge red 0.1em;
left: 0;
padding-right: 0.25em;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
-user-select: none;
}
Given this simple structure:
<div id="parent">
<div id="child">Lorem ipsum</div>
</div>
with this CSS:
#parent {
width: 200px;
height: 200px;
padding: 20px;
overflow-x: scroll;
}
#child {
width: 500px;
}
Live demo: http://jsfiddle.net/523me/5/
Notice that the parent has a 20px padding and that the child overflows horizontally (because it is wider). If you scroll the parent all the way to the right, you'll see that the child touches the right edge of the parent.
So, the parent should have a right padding, but it is ignored. It seems that when the child has a fixed width, the right padding of the parent does not apply. (Is this specified by a standard? I would love to know. Please let me know if you find anything!)
Is there a way to force the right padding to be applied in this scenario without having to remove any of the elements from the flow (by floating or positioning)?
Screenshot 1 - The right padding is ignored. This is how all current browsers behave.
Screenshot 2 - The right padding applies. This is what I'm trying to accomplish. (Btw, the screenshot is from IE7, which is the only browser which does not ignore the right padding.)
You're suffering from this problem.
I would solve it by giving a margin to the child (and not a padding to the parent):
body {
padding: 2em;
}
#parent {
width: 200px;
height: 200px;
overflow-x: scroll;
background: gray;
}
#child {
width: 500px;
background: yellow;
margin: 20px;
display: inline-block;
}
<div id="parent">
<div id="child">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras et turpis eu lorem consectetur blandit sed vel ligula. In lorem ligula, lacinia sed aliquet sed, congue quis tortor. In sed magna eros, eget blandit arcu. Nulla sit amet volutpat ipsum. Duis
quis nisl massa. Sed ipsum magna, tempus non malesuada in, gravida et sapien. Fusce a odio nulla, quis ultrices mauris. Maecenas in tellus id massa fringilla molestie.</div>
</div>
Dunno but adding:
#child{
display: inline-block;
}
Seems to fix it: http://jsfiddle.net/523me/6/
I've only tested in latest Chrome, may not be cross-browser
You might change the padding to a border.
padding: 20px;
to
border: 20px solid gray;
No, the padding is not ignored, but it's still inside the parent.
See updated jsFiddle, where you can see that the padding hasn't moved from its original position.
Edit: Hm, there are some anomalies. If you give the inner div a right margin, that gets ignored too. Hm. Upvoting your question.
Apply padding-right to overflowing element itself, and move background to its direct child element.
<div id="parent">
<div id="child"><div>Lorem ipsum...</div></div>
</div>
<style>
#parent {padding-right: 0; }
#child {padding-right: 20px; }
#child > DIV {background: yellow; }
</style>
http://jsfiddle.net/523me/9/