Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8466565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:22:42+00:00 2026-06-10T15:22:42+00:00

I know that it is usually considered bad practice to mix CSS with HTML

  • 0

I know that it is usually considered bad practice to mix CSS with HTML and that CSS selectors (ids, classes, etc) should generally be used to style elements in external stylesheets. But this can be quite inconvenient when writing a dynamic page in PHP. It is much easier to output the element’s style directly using PHP by writing to the element’s style attribute. Would this be considered bad practice, even if using classes would make it even more difficult to manage from the developer’s end?

For example, I have an element’s color style store in a database and this element has a different color depending on the parameters provided in the dynamic PHP page. In this scenario, I could write to the css stylesheet with php some class with the color variable I get from the database

echo '.custom-color {';
echo   'color: ' . $colorFromDatabase . ';';
echo '}';

and then append the class to the element:

<div class="custom-color"></div>

or I could just echo the style directly to the element.

echo '<div style="color:' . $colorFromDatabase . ';" >';

Which is the better way to go?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T15:22:44+00:00Added an answer on June 10, 2026 at 3:22 pm

    Writing the style inline is usually considered bad, whether it’s with PHP or by hand. It is simpler but harder to maintain and you don’t get to cache your CSS content.
    If you want the CSS to be more dynamic , you should experiment with http://sass-lang.com/

    Note that custom-color is not a very good class name, it should be something like main-header, blog-post, last-modified since they tell you about the content not about its style.

    SASS would let you define a variable for a color and reuse that within your CSS files, you could then just change the variable names and regenerate your CSS files

    The advantage of what you’re doing is that you don’t have to learn new things. PHP will get it done. You need to write something into your build if you want to use SASS with content from a database and come up with a scheme for which CSS to use.

    Note that there are a few cases where it would make sense to write the attribute directly, but it’s hard to explain when exactly. It’s usually when it’s only going to be used once.

    Example from their page

    // Variable Definitions
    
    $page-width:    800px;
    $sidebar-width: 200px;
    $primary-color: #eeeeee;
    
    // Global Attributes
    
    body {
      font: {
        family: sans-serif;
        size: 30em;
        weight: bold;
      }
    }
    
    // Scoped Styles
    
    #contents {
      width: $page-width;
      #sidebar {
        float: right;
        width: $sidebar-width;
      }
      #main {
        width: $page-width - $sidebar-width;
        background: $primary-color;
        h2 { color: blue; }
      }
    }
    
    #footer {
      height: 200px;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I know that Error inflating class usually is because of a missing or
Although Hungarian notation is considered bad practice nowadays, it is still quite common to
I've read all the posts and know that IndexOutOfRange usually happens because a column
Does anyone know of papers/books/etc. that document patterns for databases? For example, one common
Languages such as Perl, Pythong, etc are usually considered to have a better security
I know that usually you want to strip these out, but I need the
I know that this is usually not the way to use AngularJS but I
I know that Phonegap has an event for back button, but it's only available
I know that this sort of question has been asked here before, but still
I know that if port 443 is open that means the remote host supports

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.