There is a big similarity between CSS and Python.
And that is...
Variables
CSS and Python both have variables. Today, I will talk about how to define and access variables in both CSS and Python, and what variables each language are used for.
Defining Variables
CSS
To create a variable with global scope, declare it inside the :root
selector. The :root
selector matches the document's root element.
The variable name needs to start with --
.
:root {
--primary: #14a316;
--font-color: black;
--logo-url: url('../images/logo.jpg');
}
Python
To define a variable in Python, you can just do [var_name] = [value]
number = 5
Accessing variables
CSS
To access a variable, you need to retrieve the variable using var(var_name)
body {
color: var(--font-color)
}
Python
To access a variable in Python, you can just use the variable name.
print(number)
What variables are used for
CSS
In CSS, variables are used so you don't need to repeat the colour or url every time in your code.
CSS variables have access to the DOM, which means that you can create variables with local or global scope, change the variables with JavaScript, and change the variables based on media queries.
Variables can be used to hold colors, urls and content.
Python
Variables in Python can be used to hold any data types.
Variables do not need to be declared with any particular type, and can even change type after they have been set.
In conclusion, variables are in both CSS and Python, and they are both used for accessibility, but they are defined for completely different purposes.
Thanks for reading, and follow me for more content like this!
Top comments (8)
CSS isnโt a programming language
Yes I know, but I'm just trying to highlight that they both have variables
bad choice of comparison
yea i realised
This seems like a filler or generated article. CSS and Python are completely different things. They have no overlap and no similarities.
Hey I completely understand your point. Do you want me to delete this article and make another one purely on CSS variables?
Entirely up to you, but this one is pointless. There are tons of beginner articles on here already too. Post an article about a project you're learning on or something like that. Those have the best engagement.
Sure, thanks for the advice!