DEV Community

Medea
Medea

Posted on

A Similarity between CSS and Python

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');
}
Enter fullscreen mode Exit fullscreen mode

Python

To define a variable in Python, you can just do [var_name] = [value]

number = 5
Enter fullscreen mode Exit fullscreen mode

Accessing variables

CSS

To access a variable, you need to retrieve the variable using var(var_name)

body {
  color: var(--font-color)
}
Enter fullscreen mode Exit fullscreen mode

Python

To access a variable in Python, you can just use the variable name.

print(number)
Enter fullscreen mode Exit fullscreen mode

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)

Collapse
 
ironcladdev profile image
Conner Ow

CSS isn’t a programming language

Collapse
 
vulcanwm profile image
Medea

Yes I know, but I'm just trying to highlight that they both have variables

Collapse
 
k1lgor profile image
k1lgor

bad choice of comparison

Thread Thread
 
vulcanwm profile image
Medea

yea i realised

Collapse
 
marissab profile image
Marissa B • Edited

This seems like a filler or generated article. CSS and Python are completely different things. They have no overlap and no similarities.

Collapse
 
vulcanwm profile image
Medea

Hey I completely understand your point. Do you want me to delete this article and make another one purely on CSS variables?

Collapse
 
marissab profile image
Marissa B

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.

Thread Thread
 
vulcanwm profile image
Medea

Sure, thanks for the advice!