So i decided to nest three divs as follows for experimental purposes
<div>
<div>
<div>
</div>
</div>
</div>
I then went ahead and decided to target the inner divs as follows using css:
div {
border: 1px solid black;
padding: 40px;
background: white;
}
div > div:hover {
box-shadow: 0 0 0 40px white;
}
div > div:hover{
box-shadow: 0 0 0 80px white;
}
What I expected to happen, is either for the page to break, or for it to select all of the divs at once. However, oddly enough it is targeting the divs one by one, propagating if you will.
div {
border: 1px solid black;
padding: 40px;
background: white;
}
div > div:hover {
box-shadow: 0 0 0 40px white;
}
div > div:hover{
box-shadow: 0 0 0 80px white;
}
<div>
<div>
<div>
</div>
</div>
</div>
Actually only this last class is being used (CSS in general uses the last-defined class properties, with some exceptions, such as ID selectors, !important declarations, etc.):
div > div:hover {
box-shadow: 0 0 0 80px white;
}
You have three div elements nested. So the second div in the hierarchy obeys the rule (it is a direct descendant of the first div), and the third div in the hierarchy also obeys the rule... It is a direct descendant of the second div in the hierarchy.
Nothing is broken or unexpected about it. The direct descendant operator > is working exactly as it should in your case.
The code is working perfect.In css the last div selector will of div element will be considered.
If you want to access each div either you can use id or you can use parent-child
like:
div:nth-child(2)
{
}
What I want is for the green background to be just behind the text, not to be 100% of the page width. Here is my current code:
h1 {
text-align: center;
background-color: green;
}
<h1>The Last Will and Testament of Eric Jones</h1>
Put the text in an inline element, such as a <span>.
<h1><span>The Last Will and Testament of Eric Jones</span></h1>
And then apply the background color on the inline element.
h1 {
text-align: center;
}
h1 span {
background-color: green;
}
An inline element is as big as its contents is, so that should do it for you.
Option 1
display: table;
no parent required
h1 {
display: table; /* keep the background color wrapped tight */
margin: 0px auto 0px auto; /* keep the table centered */
padding:5px;font-size:20px;background-color:green;color:#ffffff;
}
<h1>The Last Will and Testament of Eric Jones</h1>
fiddle
http://jsfiddle.net/J7VBV/293/
more
display: table tells the element to behave as a normal HTML table would.
More about it at w3schools, CSS Tricks and here
Option 2
display: inline-flex;
requires text-align: center; on parent
.container {
text-align: center; /* center the child */
}
h1 {
display: inline-flex; /* keep the background color wrapped tight */
padding:5px;font-size:20px;background-color:green;color:#ffffff;
}
<div class="container">
<h1>The Last Will and Testament of Eric Jones</h1>
</div>
Option 3
display: flex;
requires a flex parent container
.container {
display: flex;
justify-content: center; /* center the child */
}
h1 {
display: flex;
/* margin: 0 auto; or use auto left/right margin instead of justify-content center */
padding:5px;font-size:20px;background-color:green;color:#ffffff;
}
<div class="container">
<h1>The Last Will and Testament of Eric Jones</h1>
</div>
about
Probably the most popular guide to Flexbox and one I reference constantly is at CSS Tricks
Option 4
display: block;
requires a flex parent container
.container {
display: flex;
justify-content: center; /* centers child */
}
h1 {
display: block;
padding:5px;font-size:20px;background-color:green;color:#ffffff;
}
<div class="container">
<h1>The Last Will and Testament of Eric Jones</h1>
</div>
Option 5
::before
requires entering words in css file (not very practical)
h1 {
display: flex; /* set a flex box */
justify-content: center; /* so you can center the content like this */
}
h1::before {
content:'The Last Will and Testament of Eric Jones'; /* the content */
padding: 5px;font-size: 20px;background-color: green;color: #ffffff;
}
<h1></h1>
fiddle
http://jsfiddle.net/J7VBV/457/
about
More about css pseudo elements ::before and ::after at CSS Tricks and pseudo elements in general at w3schools
Option 6
display: inline-block;
centering with position: absolute and translateX
requires a position: relative parent
.container {
position: relative; /* required for absolute positioned child */
}
h1 {
display: inline-block; /* keeps container wrapped tight to content */
position: absolute; /* to absolutely position element */
top: 0;
left: 50%; /* part1 of centering with translateX/Y */
transform: translateX(-50%); /* part2 of centering with translateX/Y */
white-space: nowrap; /* text lines will collapse without this */
padding:5px;font-size:20px;background-color:green;color:#ffffff;
}
<h1>The Last Will and Testament of Eric Jones</h1>
about
More on centering with transform: translate(); (and centering in general) in this CSS tricks article
Option 7
text-shadow: and box-shadow:
not what the OP was looking for but maybe helpful to others finding their way here.
h1, h2, h3, h4, h5 {display: table;margin: 10px auto;padding: 5px;font-size: 20px;color: #ffffff;overflow:hidden;}
h1 {
text-shadow: 0 0 5px green,0 0 5px green,
0 0 5px green,0 0 5px green,
0 0 5px green,0 0 5px green,
0 0 5px green,0 0 5px green;
}
h2 {
text-shadow: -5px -5px 5px green,-5px 5px 5px green,
5px -5px 5px green,5px 5px 5px green;
}
h3 {
color: hsla(0, 0%, 100%, 0.8);
text-shadow: 0 0 10px hsla(120, 100%, 25%, 0.5),
0 0 10px hsla(120, 100%, 25%, 0.5),
0 0 10px hsla(120, 100%, 25%, 0.5),
0 0 5px hsla(120, 100%, 25%, 1),
0 0 5px hsla(120, 100%, 25%, 1),
0 0 5px hsla(120, 100%, 25%, 1);
}
h4 { /* overflow:hidden is the key to this one */
text-shadow: 0px 0px 35px green,0px 0px 35px green,
0px 0px 35px green,0px 0px 35px green;
}
h5 { /* set the spread value to something larger than you'll need to use as I don't believe percentage values are accepted */
box-shadow: inset 0px 0px 0px 1000px green;
}
<h1>The First Will and Testament of Eric Jones</h1>
<h2>The 2nd Will and Testament of Eric Jones</h2>
<h3>The 3rd Will and Testament of Eric Jones</h3>
<h4>The Last Will and Testament of Eric Jones</h4>
<h5>The Last Box and Shadow of Eric Jones</h5>
fiddle
https://jsfiddle.net/Hastig/t8L9Ly8o/
More Options
There are a few other ways to go about this by combining the different display options and centering methods above.
A little late to game but thought I would add my 2 cents...
To avoid adding the extra mark-up of an inner span you could change the <h1> display property from block to inline (catch is you would have ensure the elements after the <h1> are block elements.
HTML
<h1>
The Last Will and Testament of
Eric Jones</h1>
<p>Some other text</p>
CSS
h1{
display:inline;
background-color:green;
color:#fff;
}
Result
JSFIDDLE
http://jsfiddle.net/J7VBV/
As the other answers note, you can add a background-color to a <span> around your text to get this to work.
In the case where you have line-height though, you will see gaps. To fix this you can add a box-shadow with a little bit of grow to your span. You will also want box-decoration-break: clone; for FireFox to render it properly.
EDIT: If you're getting issues in IE11 with the box-shadow, try adding an outline: 1px solid [color]; as well for IE only.
Here's what it looks like in action:
.container {
margin: 0 auto;
width: 400px;
padding: 10px;
border: 1px solid black;
}
h2 {
margin: 0;
padding: 0;
font-family: Verdana, sans-serif;
text-transform: uppercase;
line-height: 1.5;
text-align: center;
font-size: 40px;
}
h2 > span {
background-color: #D32;
color: #FFF;
box-shadow: -10px 0px 0 7px #D32,
10px 0px 0 7px #D32,
0 0 0 7px #D32;
box-decoration-break: clone;
}
<div class="container">
<h2><span>A HEADLINE WITH BACKGROUND-COLOR PLUS BOX-SHADOW :3</span></h2>
</div>
A very simple trick to do so, is to add a <span> tag and add background color to that. It will look just the way you want it.
<h1>
<span>The Last Will and Testament of Eric Jones</span>
</h1>
And CSS
h1 { text-align: center; }
h1 span { background-color: green; }
WHY?
<span> tag in an inline element tag, so it will only span over the content faking the effect.
EDIT: the answer below would apply in most cases. OP however later mentioned that they could not edit anything other than the CSS file. But will leave this here so it may be of use to others.
The main consideration that others are neglecting is that OP has stated that they cannot modify the HTML.
You can target what you need in the DOM then add classes dynamically with javascript. Then style as you need.
In an example that I made, I targeted all <p> elements with jQuery and wrapped it with a div with a class of "colored"
$( "p" ).wrap( "<div class='colored'></div>" );
Then in my CSS i targeted the <p> and gave it the background color and changed to display: inline
.colored p {
display: inline;
background: green;
}
By setting the display to inline you lose some of the styling that it would normally inherit. So make sure that you target the most specific element and style the container to fit the rest of your design. This is just meant as a working starting point. Use carefully. Working demo on CodePen
can use html5 mark tag within paragraph and heading tag.
<p>lorem ipsum <mark>Highlighted Text</mark> dolor sit.</p>
h1 is a block level element. You will need to use something like span instead as it is an inline level element (ie: it does not span the whole row).
In your case, I would suggest the following:
style.css
.highlight
{
background-color: green;
}
html
<span class="highlight">only the text will be highlighted</span>
Easily :
<p>lorem ibsum....</p>
with styles :
p{
background-color: #eee;
display: inline;
}
the background sets to the whole size of the element;
revise the diffrence between inline elements and block elements from here
Try removing the text-alignment center and center the <h1> or <div> the text resides in.
h1 {
background-color:green;
margin: 0 auto;
width: 200px;
}
You can use the HTML5 <mark> tag.
HTML:
<h1><mark>The Last Will and Testament of Eric Jones</mark></h1>
CSS:
mark
{
background-color: green;
}
<mark> tag with background-color helped me.
Input:
<p>
Effective <mark style="background-color: light-gray">Nov 1, 2022</mark> We will be lowering our price.
</p>
Output:
Effective Nov 1, 2022 We will be lowering our price.
Ref
Try this one:
h1 {
text-align: center;
background-color: green;
visibility: hidden;
}
h1:after {
content:'The Last Will and Testament of Eric Jones';
visibility: visible;
display: block;
position: absolute;
background-color: inherit;
padding: 5px;
top: 10px;
left: calc(30% - 5px);
}
Please note that calc is not compatible to all browsers :) Just want to be consistent with the alignment in the original post.
https://jsfiddle.net/richardferaro/ssyc04o4/
You have to mention the width of the h1 tag..
your css will be like this
h1 {
text-align: center;
background-color: green;
width: 600px;
}
HTML
<h1>
<span>
inline text<br>
background padding<br>
with box-shadow
</span>
</h1>
Css
h1{
font-size: 50px;
padding: 13px; //Padding on the sides so as not to stick.
span {
background: #111; // background color
color: #fff;
line-height: 1.3; //The height of indents between lines.
box-shadow: 13px 0 0 #111, -13px 0 0 #111; // Indents for each line on the sides.
}
}
Demo on codepen
HTML
<h1 class="green-background"> Whatever text you want. </h1>
CSS
.green-background {
text-align: center;
padding: 5px; /*Optional (Padding is just for a better style.)*/
background-color: green;
}
<h1 style="display:inline-block;text-align: center;background : red;">The Last Will and Testament of Eric Jones</h1>
I try to create styles with first-child and last-child items but I encountered a problem.
When I use first-child, because there is strong item just before, the style isn't apply. But my last-child work fine.
HTML:
<br />
<h2 class="title_block">Info <strong>1</strong>
<span class="title_block_info">+2</span>
<span class="title_block_info">+1</span>
</h2>
CSS:
h2 .title_block_info,
h2 strong {
border: 1px solid #000;
}
h2 .title_block_info:first-child {
border-radius: 10px 0 0 10px;
}
h2 .title_block_info:last-child {
border-radius: 0 10px 10px 0;
}
Example here : http://jsfiddle.net/mYKRW/
Anyone know why this came about?
Thanks,
It's because you have a "strong" tag as the first child, not the title_block_info class you were going for. first-child only works if it is in fact the first child of an element.
This works
<h2 class="title_block">
<span class="title_block_info">+2</span>
<span class="title_block_info">+1</span>
</h2>
http://jsfiddle.net/mYKRW/1/
If you need that strong text in there, you could try this, notice how I wrapped your two span tages in another span tag. This will allow you to use first-child and last-child
h2 .title_block_info,
h2 strong {
border: 1px solid #000;
}
h2 span .title_block_info:first-child {
border-radius: 10px 0 0 10px;
}
h2 span .title_block_info:last-child {
border-radius: 0 10px 10px 0;
}
<h2 class="title_block">
Info <strong>1</strong>
<span>
<span class="title_block_info">+2</span>
<span class="title_block_info">+1</span>
</span>
</h2>
http://jsfiddle.net/mYKRW/6/
Lastly you could use the first-of-type pseudo class if you want to keep your html exactly as you want, and just change your css.
h2 .title_block_info,
h2 strong {
border: 1px solid #000;
}
h2 .title_block_info:first-of-type {
border-radius: 10px 0 0 10px;
}
h2 .title_block_info:last-of-type {
border-radius: 0 10px 10px 0;
}
http://jsfiddle.net/mYKRW/9/
The :first-child pseudo-class selects the first matching element from the selector .title_block_info if it's also the :first-child of the parent element; as you note this doesn't work because there's another element that's the first-child of the parent element.
In your case you could either remove the strong element that's taking the :first-child position in the DOM, or you could use, instead, the :first-of-type pseudo-class:
h2 .title_block_info:first-of-type {
border-radius: 10px 0 0 10px;
}
JS Fiddle demo.
If your HTML is going to remain similarly predictable (the .title_block_info element will always follow the :first-child element) you could, instead:
h2 :first-child + .title_block_info {
border-radius: 10px 0 0 10px;
}
JS Fiddle demo.
References:
:first-of-type pseudo class.
:first-of-type compatibility.
What is so annoying about CSS when you add style in the css class, it may apply other element/class by itself.
What the best way to prevent that?
For example:
HTML:
<div class='main-content'>
<p> Hello World </p>
<span> Test One </span>
<div class='column'>
<span> Test Two</span>
</div>
</div>
CSS:
.main-content span {
background: #921192;
color: white;
padding: 3px 4px;
margin: 0 5px;
}
.column span {
font-size:20px;
text-transform:none;
display:inline-block;
}
I do not want "Test Two" <span> to have a background color.
Test: http://jsfiddle.net/szm9c/1/
Use a selector that actually selects the elements you want. In this case >, the child selector, will suffice.
.main-content > span {
background: #921192;
color: white;
padding: 3px 4px;
margin: 0 5px;
}
http://jsfiddle.net/mattball/mQFz2/
Use .main-content > span, that selects only directly descendent elements.
This has nothing to do with inheritance.
To use CSS properly, assign properties to elements using selectors that match only the elements that you wish to affect. (The example given is far too artificial for a useful analysis and for constructive suggestions on a better approach.)
You can use this
.main-content > span {
background: #921192;
color: white;
padding: 3px 4px;
margin: 0 5px;
}
If you use like .main-content > span that style will only affect to the immediate child spans of .main-content class
Just use all: initial at your root element.
.selector
{
all: initial;
}
Is it possible to add padding before line-break? As in, making from this to this .
Current CSS code:
span.highlight { background: #0058be; color: #FFF; padding: 2px 5px; }
I had to add an extra margin-left:0; to make the two lines start at the same point.
This can be done with pure CSS. Create a solid box-shadow to the left and right of the highlight in the same color (and use margin to correct the spacing). For your case:
span.highlight {
background: #0058be;
color: #FFF;
box-shadow:5px 0 0 #0058be, -5px 0 0 #0058be;
padding: 2px 0;
margin:0 5px;
}
It took some tryouts, but here it is: the single- and multi-line highlighter with additional padding.
HTML:
<h3>Welcome to guubo.com, Gajus Kuizinas</h3>
<p><span>So far you have joined: </span><em>Networks guubo.com</em><ins></ins></p>
CSS:
h3 {
padding-left: 5px;
}
p {
background: #0058be;
position: relative;
padding: 0 5px;
line-height: 23px;
text-align: justify;
z-index: 0;
}
p span {
background: #fff;
padding: 2px 0 2px 5px;
position: relative;
left: -5px;
}
p em {
background-color: #0058be;
color: #fff;
padding: 2px 5px;
}
ins {
position: absolute;
width: 100%;
line-height: 23px;
height: 23px;
right: -5px;
bottom: 0;
background: #fff;
z-index: -1;
}
The trick is to style the whole paragraph with a blue background, and only put white background on top of that at the beginning and the end. Doing so assures blue background elsewhere...;)
Two main disadvantages:
The highlighted text has to start at the first line (but does not necessarily have to flow into a second),
The paragraph has to be aligned with justification.
Tested in Opera 11, Chrome 11, IE7, IE8, IE9, FF4 and Safari 5 with all DTD's.
See edit history for the previous less successful attempts.
You can achieve this using just box shadow, with no messy padding or margins.
The trick is to use box-shadow's spread option, and the padding on wrapped inline elements behaves as you expect.
.highlight {
background: black;
color: white;
box-shadow: 0 0 0 5px black;
}
display: block will achieve part of what you want, but of course it will make the span a block element, and so you won't get the wrapping behaviour seen in your example.
Your screenshot holds the clue to what you need to try and do: you need to impose a margin to the left and right on your "normal" paragraph text, and then have the span disregard this (and include its padding), to achieve an "overhang" of your blue highlight when compared to the rest of your text. You can't do that with straight CSS on your span, because it covers two lines and obviously "left" and "right" only refer to the span, and not the individual pieces of text contained therein.
Straight CSS isn't the answer here. You might want to take a look at this question, which uses a jQuery filter to grab the first word in an entity, etc.:
jQuery first word selector
Maybe you can use this technique.
http://samcroft.co.uk/2011/jquery-plugin-for-inline-text-backgrounds/
The closest thing, if it really matters that much I'd say is to add display: inline-block;