This question already has answers here:
CSS to select/style first word
(14 answers)
Style the first word of a sentence with CSS [duplicate]
(1 answer)
Color the First word of Sentence CSS [duplicate]
(5 answers)
Closed 3 years ago.
This is probably a very basic, but I have the following header tag
<h2>This is a Title</h2>
Now what I want to do is just change the colour of the text and background of 'This' and leave 'is a title' alone and on the same line as 'This' to still be a header.
Pseudo-elements allow you to select the first letter or first line, but not the first word.
To target This you will need to add a real element (e.g. a <span>) around it.
If you can adjust the HTML:
<h2><span class="bg">This</span> is a Title</h2>
Then .bg {background-color:blue;}
You could also look at background-clip or mix-blend-mode if you know the length where letters have to be a different color, see this has infos before an answer, since i do not believe it really fits your needs.
DEMO or snippet below.
demo includs #supports to filter rules not supported by the browser
#supports ( background-clip: text) or ( -webkit-background-clip: text) {
h2[id] {
background: linear-gradient(to right, green 2em, black 2em, black 7.75em, purple 7.75em);
color: white
}
h2[id="bgimg"] {
display:table;/* so it shrinks on the text , only for the demo*/
background:url(http://lorempixel.com/90/100) no-repeat left, url(http://lorempixel.com/170/100) tomato no-repeat right
}
h2[id] {
color: transparent;
-webkit-background-clip: text;
background-clip: text;
}
}
#supports (mix-blend-mode:screen) {
h2[class] {
position: relative;
background: white;
color: black;
}
h2[class]::before {
background: linear-gradient(to right, green 2em, black 2em, black 7.75em, purple 7.75em);
mix-blend-mode: screen;
content: '';
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
}
}
h2[id][class] {
color:white;
background: linear-gradient(to right, green 2em, black 2em, black 7.75em, purple 7.75em);
/*reset where it works*/
background-clip:none;
mix-blend-mode:normal;
}
<h2 id>This is a Title and background-clip</h2>
<h2 id="bgimg">This is a Title and background-clip</h2>
<h2 class>This is a Title and mix-blend-mode</h2>
<h2 class id>to show the gradient used</h2>
<hr>
<p> and if it does not work ? use #supports to filter the rules .</p>
<h2> Go to default styling when css rules are not supported </h2>
<h2> regular h2 without style to avoid funny result, sends you back to question :)</h2>
What it looks like if browser understands these rules :screenshot for Chrome
here is another demo https://codepen.io/gcyrillus/details/grOEGp just about black and white
Related
I've been trying to achieve a result in CSS where a title is displayed with a marker-like effect and a gradient text color.
This is the code I made up for the two effects:
/* GRADIENT TEXT */
h1 {
background: -webkit-linear-gradient(#229, #aaf);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
/* MARKER EFFECT */
em {
background: gray;
}
If I try to combine the two, the marker effect just overcomes the gradient text.
This is probably caused by the fact that both define a background, but I don't know how to solve it.
Thanks
One thing you can do is create a child element inside of em and apply the gradient there.
<h1>
<em>
<span>Testing</span>
</em>
</h1>
h1 span {
background: -webkit-linear-gradient(#229, #aaf);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
/* MARKER EFFECT */
em {
background: gray;
}
<h1>
<em>
<span>Testing</span>
</em>
</h1>
This question already has answers here:
Change color of text responding to background color
(2 answers)
Closed 3 years ago.
I have an image and text as like below.
.html
<div [ngClass]="cssDivClass" (click)="goToNextPage(data)">
<img [src]="data?.image" class="image" tappable>
<div class="caption" [ngClass]="ionTextUppercase">{{data?.name }}</div>
</div>
.scss
.caption {
position: absolute;
left: 10px;
bottom: 10px;
color: white;
font-size: 16px;
font-weight: 500;
letter-spacing: 0;
}
My question here is How can I handle this text color (i.e. white) with the White background image? What kind of CSS trick should I use here? I need white color text here since most of the other images have dark colors. So how can I handle this thing in a generic way?
You might try a shadow on the white text, this way it will show up on dark and light photographs.
text-shadow: #fc0 1px 0 10px;
https://developer.mozilla.org/en-US/docs/Web/CSS/text-shadow
Another trick is to use a semi transparent div below the text, so that no matter what the background is, the text will be visible. It would look like this:
https://divibooster-6d4b.kxcdn.com/wp-content/uploads/2014/10/divi-theme-semi-transparent-background.png
Still another possibility would be to use Javascript. If your photo is a 'light' photo, change your text's css class to 'color: black'. If it is a 'dark' photo, change your text's css class to 'color: white'.
Maybe text-shadow may help you to have a good contrast also in light background.
You can also consider a prominent color extraction plugin to set an alternative text color if the background is light:
https://ourcodeworld.com/articles/read/403/top-5-best-image-color-extraction-javascript-and-jquery-plugins
You can consider also to use:
mix-blend-mode: difference;
and use text-shadow only for browser that not support this property
https://caniuse.com/#search=mix-blend-mode
body{
background:#fff;
font-size:30px;
}
.container{
transform: translateZ(0);
will-change: opacity;
background: url("https://placeimg.com/640/480/any/grayscale")
no-repeat left / cover;
}
.shadow {
color:#fff;
text-shadow:1px 1px 2px #000;
}
.blend {
mix-blend-mode: difference;
color:#fff;
}
<div class="container">
<div class="shadow">text-shadow</div>
<div class="blend">difference</div>
</div>
This question already has answers here:
Is it possible to apply CSS to half of a character?
(20 answers)
Closed 4 years ago.
How to take something like a star unicode \u2605 which is a star in html/css and only fill it half way instead of full.
Here is code:
<div class="starHalfFill">
\u2605
</div>
My styles look like this thus far:
.starHalfFill {
color: orange;
}
Since this is colored by color and not background color we will have to do a little more work to get this filled halfway. Normally to fill a div half way you would do something like:
.starHalfFill {
background: linear-gradient(to right, orange 50%, grey 50%);
}
but since this is a text based color we will have to target that.
.star.star--non-filled {
background: linear-gradient(to right, orange 50%, grey 50%);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
Those properties will target the text inside the div and also make the fill color transparent allowing the effect to work.
In HTML when do I use color, and what is the difference between background-color and also the background tag?
What are the differences?
color is referring to the text color in that element.
background-color refers to the background color
background is shorthand to combine many background tags into one line.
background: #ffffff url("img_tree.png") no-repeat right top;
Combines color, image and background image properties in the one line instead of typing our each style individually.
w3schools
I will give you a example using this html element:
<span class="value"> This is my text </span>
.value { color: red, background-color: black}
The CSS color is used to change the text color of a html element. In this example "This is my text" would be red. The CSS background-color is used to change the background color so in this case you would get a black box with red text inside it. Finally the background is used to set all the background properties in one declaration. For example:
background: #00ff00 url("smiley.gif") no-repeat fixed center;
This changes the background color, adds the image "smiley.gif" to the background and it centers the image, it doesnt repeat the image if it has the space.
Quick answer
Color = Text Color
Background-color = the color of the background
Background = gives you the posibillity to set color, image , etc...
great tutorials on this are found here
It is true that background gives more options versus background-color. But if you only need to set background color, they are exactly the same, and each will override the other as seen in the snippet.
background: yellow;
background-color: yellow;
.bc {
background: yellow;
background-color: green;
}
.bc2 {
background-color: green;
background: yellow;
}
<div class='bc'>
bc { background:yellow; background-color:green; }
</div>
<div class='bc2'>
bc { background-color:green; background:yellow; }
</div>
One big thing about this both css properties is, that a background-color does not overwrite an image or a gradient that has been set with this:
background:url('https://example.com/image.jpg');
or
background: linear-gradient(to bottom, #1e5799 0%,#2989d8 20%,#207cca 51%,#7db9e8 100%);
If you are trying to change the background from an image to a color you have to use the background property.
color: is used to add color to the Text within the Tag.
color: blue;
background-color: is used to add color in background of the content inside the tag.
background-color : red;
background: is used for adding different type of background property name to the content within the Tag.
background : red url('image.png') fixed repeat cover;
I have a number of headings and in the background of each I want to show the same gradient color, but a different (non-repeating) background image. I want to avoid duplicating any CSS rules for the background gradient color or background image position, because they will be the same for each heading. In other words, the only thing I should need to specify for an individual heading is the path to the background image file.
Here's what I have at the moment:
<h1 class="banner bgImage img1">background image 1</h1>
<h1 class="banner bgImage img2">background image 2</h1>
<h1 class="banner bgImage img3">background image 3</h1>
<h1 class="banner">heading without background image</h1>
.banner {
/* For old browsers that don't support gradients */
background-color: #9FCC1D;
/* browser-specific prefixes omitted */
background-image: linear-gradient(#75A319, #9FCC1D);
padding: 7px 7px 7px 15px;
}
/* Specifies position of background image */
.bgImage {
background-position: 5px 50%, 0 0;
background-repeat: no-repeat;
padding-left: 30px;
}
.img1 {
background-image: url(img1.png"), linear-gradient(#75A319, #9FCC1D);
}
.img2 {
background-image: url(img2.png"), linear-gradient(#75A319, #9FCC1D);
}
.img3 {
background-image: url(img3.png"), linear-gradient(#75A319, #9FCC1D);
}
There are a couple of problems with this
I have to repeat the linear-gradient style in each .imgX rule
It doesn't render correctly in Chrome, which doesn't seem to support a comma-separated list of background-image and background-repeat properties. This is what gets displayed in Chrome
How can I fix the problem with the way the background is rendered in Chrome while minimising duplication of CSS rules?
Use the :before pseudo-class for your background icons.
.img1:before {
content: '';
float: left;
position: relative;
background: transparent url('img1.png') left top no-repeat;
width: 16px; /* change to actual width of img */
height: 16px; /* change to actual height of img */
}
Or, since you're trying to relieve the amount of CSS, you can specify a class for the gradient and append that in your HTML.
Don, you have two classes which this background gradient can be applied to, bgImage and banner. Simply apply your gradient on to one of those classes and go from there. Also append repeat-x right after your image url to ensure it will repeat across.