Answer the question
In order to leave comments, you need to log in
How to implement dynamic css on a website?
I am developing a WP theme and in the future I would like to implement the ability to edit the colors of text, blocks, and so on in the admin panel.
How can this be implemented? I guess it should probably be that the colors are given as variables.
Answer the question
In order to leave comments, you need to log in
1. CSS Variables (See browser support right away).
2. Display data in the tag <style>
during page generation (common method in WP themes):
<head>
...
<style>
.classname {
color: <?= $settings['classname_color']; ?>;
}
<script>
var data = <?= json_encode($settings); ?>
document.querySelector('.classname').style.color = data.classname_color;
Yes, and for example, you can physically open and rewrite the css file through php, or you can even more simply display variables in the template through the style tag, or as a style attribute.
I would do the following.
Through the ACF plugin, I would display the fields that need to be edited in the admin panel on the desired page. Then these values would be inserted into the site.
In pure css, you can create variables:
(inside the style.css file)
:root {
--color: #eee;
-fontSize: 14px;
}
Then use these variables anywhere
body {
backgroud-color: var(--color);
font-size: var(--fontSize);
}
You can easily insert a tag into the html at any time
=> the styles using your variables will automatically change. So, for example, you can easily switch the theme (day / night) of the site only by changing the tag
This is called a curved non-adapted code without fitting images to one resolution. When images of different sizes are inserted into the html table, this is how it turns out. Looks crooked and not pretty
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question