I want to Trim a string Using css .
example
my string is
"hi google how are you"
I Want to Get output
"hi"
Get first two letter . Using Css is it possible Trim a string .
Although not possible using CSS; but you can get some kind of illusion in ideal condition may be this workaround work for you. I've used here text-overflow: ellipsis;
you can check the DEMO.
<p>hi google how are you</p>
p {
text-overflow: ellipsis; /* IE, Safari (WebKit) */
overflow:hidden; /* don't show excess chars */
white-space:nowrap; /* force single line */
width: 17px; /*This property playing a major role just showing the first 2 letter*/
}
I'm not quite sure why you wanna do it with CSS (it makes more sense to this this with JS or your back-end server side language), I presume it's because you want to target one screen size with say the full sentence and another with an abbreviated one? If so, sadly CSS can't do this, however you could do something like this in your HTML:
<span class="widescreen-span">[pull full string here]</span>
<span class="smallscreen-span">[trimmed string here]</span>
Where you actually have both sets of text available in the HTML, however we will show and hide them accordingly.
Then have a stylesheet for each screen size (there are several ways of doing this...):
<link rel="stylesheet" media="screen and (min-width: 900px)" href="widescreen.css">
<link rel="stylesheet" media="screen and (max-width: 600px)" href="smallscreen.css">
Then in widescreen.css you could do this:
.widescreen-span { display:inline; }
.smallscreen-span { display:none; }
And then in smallscreen.css you could do this:
.widescreen-span { display:none; }
.smallscreen-span { display:inline; }
AFAIK: There's no way to do that in CSS. The only way to achieve what you want is using some script language (e.g. JavaScript).
That is not possible with CSS.
The only possible solution is by wrapping the rest of the sentence in a span, and hide it, like this:
<p>hi<span> google how are you</span></p>
span { display: none; }
Or you should look into a Javascript or PHP solution.
I would personally do this with Javascript
var startText = "hi google how are you";
var endText = startText.substr(0, 3);
// endText is now "hi"
CSS is a method of describing the style of a page's elements. You are trying to edit the content of a page using a framework designed not to have any affect on the content of the page, just the style.
You should probably look at using JavaScript to do what you want. Formatting page elements in this way is not possible in CSS.
CSS is per se not a programming language.
On the other hand, there are good news for you. You can use a programmming language such as JavaScript or jQuery to accomplish this, or even a backend programing language such as PHP or Ruby.
CSS is a language used to apply styles in a cascade manner hence why it is called CSS (Cascading Style Sheets).
Just to give you some advice, what you're trying to accomplish is not called trim. Trim is to remove unnecessary white spaces from a string like the example here shows: JavaScript Trim() Function.
What you're trying to accomplish is called substring which is explained here: JavaScript substring() Function.
var str = "Hello world!";
var res = str.substring(0, 2);
/* Output: He */
Related
It seems like there should really be an easy solution to this, but so far I've been unsuccessful in finding one.
I'm using Zurb Foundation and I'm basically creating a live form that takes inputs from a form (above), and fills in a content (below) using angular.js. Users will then print the page to a PDF. I'd like to maintain the layout I have for the content below, and I'd like to hide the form above when printing. Zurb has a fine "hide-for-print" css rule that seems like it should work just fine when applied to the form above, but when I toggle print stylesheets, it basically strips all CSS and goes back to ugly.
Suggestions?
What I have done in these type situations is use a separate file for the print.css.
<link rel="stylesheet" type="text/css" href="global.css" />
<link rel="stylesheet" type="text/css" media="print" href="print.css" />
If the browser is printing, the global.css file will be loaded first and than the print.css file will overwrite anything aftewards.
Keep in mind though, that all background: * rules will be turned off in all browsers by default when printing, so some styles are going to be compromised regardless.
Have you tried using CSS media queries for print media?
.foo {
height:150px;
width:150px;
background-color:#F00 // see what I did there?
}
.bar {
height:10px;
width:50%;
border-radius:5px;
background-color:#000
}
.baz {
width:100px;
height:150px;
background-color:#FFF;
}
#media screen {
.baz {
display:block;
}
}
#media print {
.baz {
display:none;
}
}
Now, only some of .baz's properties are targeted by the media queries. You can feel free to put in any of .baz's properties inside or outside of the queries themselves. Likewise, you can put all of .baz's properties in the media query, but I gather that's not what you're looking for.
idk about zurb's print style sheets, and without an example, it's pretty hard to answer, but you can use weasyprint, open source library to convert html/css to pdf https://github.com/Kozea/WeasyPrint
I have been advised that said CSS page-break-inside:avoid; would prevent elements being printed between 2 pages.
On this directions print out this simply does not work on all tested browsers so far. The CSS .instruction has this applied yet prints across pages.
Example: http://www.golfbrowser.com/A4/directions.php?start=PARIS&end=SL42ES
Any ideas?
The page-break-inside property is only supported by Opera.
http://www.w3schools.com/cssref/pr_print_pagebi.asp
Just add a print stylesheet or use a media query and a breaking div or just add the style to the elements in your html that need braking when printing.
Try adding this after every long block of content that you think needs breaking:
<div class="break"> </div>
And as for your css just add this:
.break {
display:none;
} //place inside your regular stylesheet file
#media print {
.break {
display:block;
page-break-after:always
}
}
This method works in most modern browsers, including IE8+.
Is there anyway to change a text input's value (the default text that displays) with CSS?
I'm optimizing a site for mobile and I want the text 'search this site' to be shortened to 'search'.
That really isn't what CSS is for.
CSS is for styling your content; HTML is for the actual content itself.
If you need to modify the content after the HTML has loaded, then that's what Javascript is for.
So the real answer to your question is: either modify the HTML directly, or use Javascript.
There are some things you can do with CSS that affect the content, such as adding additional text in-front of or behind an element using the :before and :after pseudo-selectors, but that won't help you in this case, and shouldn't be used for the kind of content change work that you're talking about.
By the way, slightly off-topic, but if you're using input field value as a prompt text, you may want to consider looking into the HTML5 placeholder attribute instead. This also doesn't address your question, but it does sound like it might be something that could be useful to you.
No, CSS cannot change the value attribute of an input, or indeed any attribute of any element.
Late to the party but using "content" attribute, within element:before will accomplish what you need as seen in http://www.w3schools.com/cssref/tryit.asp?filename=trycss_content_string
I was able to manipulate a button content value via jQuery toggleClass, switching between the following classes:
.open_button:before{
content:"open";
}
.close_button:before{
content: "close";
}
I understand the qualms, but I do feel like toggleClass provides an elegance that justifies the CSS trick. Otherwise one would be using a toggle function with nested css switch functions. I personally think avoiding the nested jQuery functions is better looking.
If you want to change the value use the HTML "value" attribute;
example:
<input type="submit" value="ENVIAR">
that will change the default "submit" value to "enviar"
For me the solution was
<button type="submit" class="mybutton" name="add">
<span class="add">Add new</span>
</button>
therefore the css will be :
.mybutton:hover .add:after{content="0"}
This solution is little bit tricky,
but it's always work for me.
/*CSS Code*/
#media screen and (max-width: 768px) {
.showondesktop {
display: none !important; }
}
#showonmobile {
display:none;
}
#media screen and (max-width: 767px) {
#showonmobile {
display:block; }
}
<!--HTML Code->
<div class="showondesktop"> search this site </div>
<div id="showonmobile"> search </div>
When the website is visited from Mobile it will be displayed "search", but when visited from Desktop it will be displayed "search this site".
Image Preview:
Output-Desktop-view.jpg
Output-Mobile-view.jpg
so easy, just type in for example:
button {
color: red;
font-family: sans-serif;
}
there you are,
the buttons take all the inputs with values with for example submit/reset/.etc..
Is there any way to use conditional statements in CSS?
I'd say the closest thing to "IF" in CSS are media queries, such as those you can use for responsive design. With media queries, you're saying things like, "If the screen is between 440px and 660px wide, do this". Read more about media queries here: http://www.w3schools.com/cssref/css3_pr_mediaquery.asp, and here's an example of how they look:
#media screen and (max-width: 300px) {
body {
background-color: lightblue;
}
}
That's pretty much the extent of "IF" within CSS, except to move over to SASS/SCSS (as mentioned above).
I think your best bet is to change your classes / IDs within the scripting language, and then treat each of the class/ID options in your CSS. For instance, in PHP, it might be something like:
<?php
if( A > B ){
echo '<div class="option-a">';
}
else{
echo '<div class="option-b">';
}
?>
Then your CSS can be like
.option-a {
background-color:red;
}
.option-b {
background-color:blue;
}
No. But can you give an example what you have in mind? What condition do you want to check?
Maybe Sass or Compass are interesting for you.
Quote from Sass:
Sass makes CSS fun again. Sass is CSS, plus nested rules, variables, mixins, and more, all in a concise, readable syntax.
CSS itself doesn't have conditional statements, but here's a hack involving custom properties (a.k.a. "css variables").
In this trivial example, you want to apply a padding based on a certain condition—like an "if" statement.
:root { --is-big: 0; }
.is-big { --is-big: 1; }
.block {
padding: calc(
4rem * var(--is-big) +
1rem * (1 - var(--is-big))
);
}
So any .block that's an .is-big or that's a descendant of one will have a padding of 4rem, while all other blocks will only have 1rem. Now I call this a "trivial" example because it can be done without the hack.
.block {
padding: 1rem;
}
.is-big .block,
.block.is-big {
padding: 4rem;
}
But I will leave its applications to your imagination.
The #supports rule (92% browser support July 2017) rule can be used for conditional logic on css properties:
#supports (display: -webkit-box) {
.for_older_webkit_browser { display: -webkit-box }
}
#supports not (display: -webkit-box) {
.newer_browsers { display: flex }
}
The only conditions available in CSS are selectors and #media. Some browsers support some of the CSS 3 selectors and media queries.
You can modify an element with JavaScript to change if it matches a selector or not (e.g. by adding a new class).
I would argue that you can use if statements in CSS. Although they aren't worded as such. In the example below, I've said that if the check-box is checked I want the background changed to white. If you want to see a working example check out www.armstrongdes.com. I built this for a client. Re size your window so that the mobile navigation takes over and click the nav button. All CSS. I think it's safe to say this concept could be used for many things.
#sidebartoggler:checked + .page-wrap .hamb {
background: #fff;
}
// example set as if statement sudo code.
if (sidebaretoggler is checked == true) {
set the background color of .hamb to white;
}
CSS has become a very powerful tool over the years and it has hacks for a lot of things javaScript can do
There is a hack in CSS for using conditional statements/logic.
It involves using the symbol '~'
Let me further illustrate with an example.
Let's say you want a background to slide into the page when a button is clicked. All you need to do is use a radio checkbox.
Style the label for the radio underneath the button so that when the button is pressed the checkbox is also pressed.
Then you use the code below
.checkbox:checked ~ .background{
opacity:1
width: 100%
}
This code simply states IF the checkbox is CHECKED then open up the background ELSE leave it as it is.
css files do not support conditional statements.
If you want something to look one of two ways, depending on some condition, give it a suitable class using your server side scripting language or javascript. eg
<div class="oh-yes"></div>
<div class="hell-no"></div>
There is no native IF/ELSE for CSS available. CSS preprocessors like SASS (and Compass) can help, but if you’re looking for more feature-specific if/else conditions you should give Modernizr a try. It does feature-detection and then adds classes to the HTML element to indicate which CSS3 & HTML5 features the browser supports and doesn’t support. You can then write very if/else-like CSS right in your CSS without any preprocessing, like this:
.geolocation #someElem {
/* only apply this if the browser supports Geolocation */
}
.no-geolocation #someElem {
/* only apply this if the browser DOES NOT support Geolocation */
}
Keep in mind that you should always progressively enhance, so rather than the above example (which illustrates the point better), you should write something more like this:
#someElem {
/* default styles, suitable for both Geolocation support and lack thereof */
}
.geolocation #someElem {
/* only properties as needed to overwrite the default styling */
}
Note that Modernizr does rely on JavaScript, so if JS is disabled you wouldn’t get anything. Hence the progressive enhancement approach of #someElem first, as a no-js foundation.
Changing your css file to a scss file would allow you to do the trick. An example in Angular would be to use an ngClass and your scss would look like:
.sidebar {
height: 100%;
width: 60px;
&.is-open {
width: 150px
}
}
While this feels like a bit of a hack, and may not work perfectly in all browsers, a method I have used recently combines the fact that CSS (at least in Chrome) seems to ignore invalid values set on properties, and we can set custom properties that fall back to their default value when invalid.
(Note: I haven't deeply tested this, so treat it as a hacky proof of concept/possible idea)
The following is written in SCSS, but it should work just as well in standard CSS:
.hero-image {
// CSS ignores invalid property values
// When this var is set to an image URL, the browser will ignore it
// When this var isn't set, then we will use the default fallback for the var, which is 'none'
display: var(--loading-page-background-image, none);
// This part isn't directly relevant to my 'if' example, but shows how I was actually using this custom property normally
background-image: var(--loading-page-background-image, none);
}
I'm setting the custom property from JavaScript / React, but it would likely work regardless of how you set it:
// 'true' case
const chosenLoaderUrl = "https://www.example.com/loader.png";
// 'false' case
//const chosenLoaderUrl = "";
// containerRef is just a reference to the div object, you could get this with
// jquery or however you need. Since I'm in React, I used useRef() and attached
// that to my div
containerRef.current.style.setProperty(
"--loading-page-background-image",
`url(${chosenLoaderUrl})`
);
When chosenLoaderUrl is set to my url, that url is an invalid value for the display property, so it seems to get ignored.
When chosenLoaderUrl is set to an empty value, it falls back to the default value in my var() statement, so sets display to none
I'm not sure how 'generalisable' this concept it, but figured I would add it to the other suggestions here in case it is useful to anyone.
Your stylesheet should be thought of as a static table of available variables that your html document can call on based on what you need to display. The logic should be in your javascript and html, use javascript to dynamically apply attributes based on conditions if you really need to. Stylesheets are not the place for logic.
You can use combination of jquery and css classes i.e. I want to change a font color of certain element depending on the color of the background:
CSS:
.h3DarkMode{
color: lightgray;
}
.h3LightMode{
color: gray;
}
HTML:
<h3 class="myText">My Text Here...</h3>
JQuery:
var toggleMode = localStorage.getItem("toggleMode");
if (toggleMode == "dark"){
$(".myText").removeClass("h3LightMode").addClass("h3DarkMode");
}else{
$(".myText").removeClass("h3DarkMode").addClass("h3LightMode");
}
No you can't do if in CSS, but you can choose which style sheet you will use
Here is an example :
<!--[if IE 6]>
Special instructions for IE 6 here
<![endif]-->
will use only for IE 6 here is the website where it is from http://www.quirksmode.org/css/condcom.html , only IE has conditional comments. Other browser do not, although there are some properties you can use for Firefox starting with -moz or for safari starting with -webkit. You can use javascript to detect which browser you're using and use javascript if for whatever actions you want to perform but that is a bad idea, since it can be disabled.
I would like all "®" on a site to be in superscript. Can I do that with CSS?
AverageAdam's answer will work fine, but if you for some reason wanted a CSS version, you could do this:
.sup { vertical-align: super; }
and
<span class="sup">®</span>
From here.
add this to your html file <sup>your mark here</sup>
then add this to your css
sup {
position: relative;
font-size: 40%;
line-height: 0;
vertical-align: baseline;
top: -1.2em;
}
You can adjust the height of the mark using "top" in the css and the size with "font-size". This will also work for any TM, SM, or symbol you want. It will not effect any of your spacing or typography.
<sup>®</sup>
Unfortunately CSS doesn't have a way to specify superscript's. You can however simulated it using a span and some tags.
Correction 2021: As others have mentioned there are many ways including using CSS. Based on the various options and issues presented in this question I've created a pen to demonstrate options for superscript styling and line-height fixes.
My personal favorite is position:relative since it doesn't require the line-height:0 fix. Thanks #osuthorpe
use the CSS below to create a tag that doesn't mess with your leading. Adjust values as needed. Font-size is optional, I used it to make my r-balls a little smaller than the registered trademarked text.
sup{
vertical-align: 75%;
line-height: 5px;
font-size:11px;
}
I know you asked CSS but this jQuery code worked for me, hope it helps you
<html>
<head>
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js">
</script>
<script type="text/javascript">
$(document).ready(function() {
$("body").html(
$("body").html().replace("®", "<sup>®</sup>")
);
});
</script>
</head>
<body>
Some text here ®
</body>
</html>
Victor's answer only worked for the first Reg mark on my page. I found this code to work on the entire page. Hope it helps someone:
$("body").html(
$("body").html().replace(/®/gi, '<sup>®</sup>').replace(/®/gi, '<sup>®</sup>')
);
found it here: http://www.cmexsolutions.com/blog/use-jquery-to-superscript-all-register-marks-reg-or-%C2%AE
I've used the CSS below for positioning the trademark registration symbol ® in HTML whereby the result is the ® symbol with a link, smaller and above the normal text. Note there are two links in the example. The reason for this is because I wanted the main anchor link to be underlined and the ® symbol to be not underlined.
.trademark {
position: relative;
font-size: 40%;
top: -1.2em;
}
.no-style {
text-decoration: none !important;
}
Example with CSS using JSX in React:
Photo Prints Now<a
href="https://www.photoprintsnow.com" className="no-style"><span
className="trademark">®</span></a>
Example in HTML with CSS where the syntax is class instead of className:
Photo Prints Now<a
href="https://www.photoprintsnow.com" class="no-style"><span class="trademark">®</span>
</a>
Example in HTML with STYLE:
Photo Prints Now
<a href="https://www.photoprintsnow.com" style="text-decoration: none; ">
<span style="position: relative;font-size: 40% ;top: -1.2em;">®</span>
</a>
Further to the previous answers, I'd suggest that superscript is presentational rather than semantic, and therefore styling the registration mark should be done using CSS. Whether superscripted or not, a registration mark is still a registration mark, and would be recognised as a registration mark by humans/computers. The symbol itself may be considered semantic, in that it gives a 'special' meaning to the object to which it relates, but the styling of it is entirely presentational. By convention the registration mark is often (but not always) superscripted, as is the trademark symbol.
The only issue with some of the scripts above is that they don't deal with the fact that there might already exist some ® elements on the page. In this case, they will be replaced with: ®.
I think a solution like this might make more sense.
$("body").html(
$("body").html().replace(/<sup>®<\/sup>/gi, '®').
replace(/®/gi, '<sup>®</sup>').
replace(/®/gi, '<sup>®</sup>')
);
if you don't mind using jQuery:
$("p,h1,h2,h3,h4").each(function(){
$(this).html($(this).html().replace(/®/gi, '<sup>®</sup>').replace(/®/gi, '<sup>®</sup>'));
});
it works much faster than modyfying whole body tag (as in Robert's and Victor's answers)
If you are going to use regex, why not clean it up with one call
$("p,h1,h2,h3,h4,h5,h6,li,dd,dt,a").each(function(){
var $this = $(this);
$this.html($this.html().replace(/(<sup>)?(®|®)(<\/sup>)?/gi, '<sup>®</sup>'));
});
I don't recommend doing this on the body like the OP ended up doing. It could interfere with inline javascript that might have the symbol in it.