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 8948885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:06:36+00:00 2026-06-15T13:06:36+00:00

if I have an html element with predefined css style <h1 style=color:blue;>This is a

  • 0

if I have an html element with predefined css style

<h1 style="color:blue;">This is a header</h1>

Now if I want to add a new style called text-align:center to h1 element.

How can I add a new style attribute to the end of the style attribute without overwrite the existing style?

So the result will look something like

<h1 style="color:blue;text-align:center">This is a header</h1>
  • 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-15T13:06:38+00:00Added an answer on June 15, 2026 at 1:06 pm

    Here’s three methods of doing this:

    jsfiddle example

    1 – set element style property – on the fly JavaScript styling, however arguably breaks separation of concerns

    HTML:

    <h1 id="headerExample1" style="color:blue;">This is a header</h1>
    

    JavaScript:

    var ex1 = document.getElementById('headerExample1');
    
    if (someLogic) {
        ex1.style.textAlign = "center";
    }
    

    2 – set the style attribute – this is the dirtiest of the three but does give you an idea of how you can access/change the style property directly using JavaScript

    HTML:

    <h1 id="headerExample2" style="color:blue;">This is a header</h1>
    

    JavaScript:

    var ex2 = document.getElementById('headerExample2');
    
    if (someLogic) {
        ex2.setAttribute('style', ex2.getAttribute('style') + ";text-align:center");
    }
    

    3 – CSS and classes – this is the cleanest way to style the header with regard to separation of concerns

    HTML:

    <h1 id="headerExample3">This is a header</h1>
    

    CSS:

    #headerExample3 {color:blue;}
    .centered {text-align:center;}
    

    ​
    JavaScript:

    var ex3 = document.getElementById('headerExample3');
    
    if (someLogic) {
        ex3.className = "centered";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an HTML element like this: <div id=new></div> I want to append a
If I have a HTML element <header> and apply some margins to this HTML5
I have an html element like: <div id=spam[500]> I want to search for this
I have an HTML element whose background colour is set with rgba() <div style=background-color:
I have an HTML table element called results. This table is dynamically populated with
I have a html element: <div id=myElement class=someClass onclick=setText(this.innerHtml) runat=server>text</div> I want to be
I have a html element (like select box input field) in a table. Now
I have an HTML textarea element. I want to prevent a user from entering
I have an html td element with text inside. I want it so you
i have an html element and it has some css which are applied on

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.