Master the Art of Styling Elements with CSS: Tips, Tricks, and Techniques

CSS, or Cascading Style Sheets, is a stylesheet language used to describe the look and formatting of a document written in HTML. It is a crucial aspect of web development and allows developers to control the appearance of web pages simply and consistently. In this blog post, we will explore how to style various elements with CSS, including text, images, and layout.

One of the most basic ways to style text with CSS is to set the font family, size, and colour. The font-family property lets you specify the typeface you want to use, such as Arial, Times New Roman, or Verdana. The font-size property allows you to set the size of the text, which can be specified in pixels (px) or ems. Ems are a scalable unit, so if you set the font-size to 1em, it will be equal to the size of the parent element’s font. The colour property allows you to set the colour of the text using either a named colour (such as red or blue) or a hexadecimal code (such as #FF0000 for red).

Here is an example of how to style text using CSS:

p {
  font-family: Arial;
  font-size: 16px;
  color: #000000;

This CSS code would apply the Arial typeface, a font size of 16px, and a colour of black to all paragraphs on the web page.

Another important aspect of styling text with CSS is controlling the text alignment. This can be done using the text-align property, which allows you to specify whether the text should be aligned to the left, right, or center of the element. You can also use the justify value to stretch the text to fit the width of the element.

Here is an example of how to align text using CSS:

h1 {
  text-align: center;

p {
  text-align: justify;

This CSS code would center the text of all h1 elements and justify the text of all p elements.

In addition to styling text, CSS also allows you to style images. One way to do this is by setting the width and height of the image using the width and height properties. These properties can be specified in pixels or as a percentage of the parent element. You can also use the object-fit property to specify how the image should be resized to fit within the given dimensions.

Here is an example of how to style an image using CSS:

img {
  width: 100%;
  height: auto;
  object-fit: cover;

This CSS code would set the width of the image to 100% of the parent element, maintain the aspect ratio of the image using the height of “auto”, and crop the image using the “cover” value of the object-fit property.

CSS also allows you to control the layout of your web page using various properties such as display, position, and float. The display property allows you to specify how an element should be displayed on the page, such as a block-level element or an inline element. The position property allows you to specify the position of an element on the page, such as relative to its parent element or absolutely positioned on the page. The float property allows you to float an element to the left or right of its parent element, allowing other elements to wrap around it.

Here is an example of how to use the display, position, and float properties to control the layout:

.container {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;

.sidebar {
  width: 200px;
  position: fixed;
  top: 0;
  left: 0;

.main-content {
  margin-left: 200px;

.float-right {
  float: right;

In this example, the container class uses the display property set to “flex” to create a flex container, which allows for flexible layout options using the flex-direction, justify-content, and align-items properties. The sidebar class uses the position property set to “fixed” to position the element on the page and the top and left properties to specify the position. The main-content class uses the margin-left property to create a left margin equal to the width of the sidebar, effectively creating a layout with a fixed sidebar and main content that scrolls. The float-right class uses the float property set to “right” to float the element to the right of its parent element.

There are many other properties and techniques for styling various elements with CSS, and it is important for web developers to have a strong understanding of these concepts in order to create effective and visually appealing websites. It is also important to note that while CSS is a powerful tool, it should be used in conjunction with good design practices and a mobile-first approach to ensure that websites are functional and accessible on all devices.

I hope this post helped you understand some of the basics of styling elements with CSS. Happy styling!