/    Sign up×
Community /Pin to ProfileBookmark

How to insert CSS class inside PHP?

I have code like:

[code=php]
” </>n”.
” <style type=”text/css”>n”.
” .#myclass1 a{padding: 0;}n”.n”.
” .mybodyody{width: 100%;}n”.n”.
” </style>n”.
[/code]

How to modify CSS inside PHP in the correct way?
Need help

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogApr 23.2016 — If you really feel compelled to use all the concatenation of separate lines with explicit newlines ("n"), then:
[code=php]
" </>n".
" <style type="text/css">n".
" .#myclass1 a{padding: 0;}n".
" .mybodyody{width: 100%;}n".
" </style>n".
[/code]

But that's a lot of added typing/complexity to no real gain, in my opinion. You could simple do something like this, assuming it's part of an echo statement:
[code=php]
echo "
</>
<style type="text/css">
.#myclass1 a{padding: 0;}
.mybodyody{width: 100%;}
</style>
";
[/code]

But even easier is to simply exit PHP mode at that point, since you're only outputing HTML/CSS at that point:
[code=php]
<?php
// some PHP code, then...
?>
</>
<style type="text/css">
.#myclass1 a{padding: 0;}
.mybodyody{width: 100%;}
</style>
<?php
// some more PHP code...
?>
[/code]
Copy linkTweet thisAlerts:
@toplisekauthorApr 24.2016 — Thank you.

It is part of PHP application.

How to solve also such examples:
[code=php]
" @media screen and (max-width: 787px) {n".
" table [class="myclass1"]{width:100% !important;}n".
"}n".
[/code]


Notice [
Copy linkTweet thisAlerts:
@tracknutApr 24.2016 — <i>
</i>.#myclass1 a{padding: 0;}


Not sure if this is just a typo being carried along, but a class name cannot start with "#". It should be specified as ".myclass"
Copy linkTweet thisAlerts:
@NogDogApr 24.2016 — If you are going to have literal double-quotes inside of a double-quoted string, then you have to escape them with a back-slash.
[code=php]
$foo = "This string uses "quotes" inside of it.";
[/code]

If you find yourself doing lot's of escaping, HEREDOC syntax can be useful.
[code=php]
// instead of this:
$foo = "Some text with "quotes" andn" .
"hard-coded newlines.";

// you could do:
$foo = <<<EOD
Some text with "quotes" and
hard-coded newlines.
EOD;

[/code]
×

Success!

Help @toplisek spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...