CSS Layout II

This is the continuation for other CSS layout properties.

CSS Align

Align a simple div element using the margin property and setting it to "auto".

This div element is centered.

If you want to just center the text without setting the width.

This div element has its text centered.

The following images are centered by making it a block element and its margin set to auto.

Previous Image

Next Image

The following is a div element inside a parent div element. The parent div element has a relative position while the child div element has an absolute position (relative to its parent div element).

I am the div parent!

I am the div child!

You can also use float properties just make sure to use the clearfix hack to include tall images in your div containers! For the two following divs, the first div does not have a clearfix hack but the second one does.

Anthony Mackie smiling but knows something sus about you ahh smile.

Lorem ipsum dolor sit amet consectetur adipisicing elit. Architecto, ipsa libero quos minus odio optio itaque ratione tenetur esse quod deserunt id autem totam aliquid iure accusantium neque. Aut, suscipit.

Anthony Mackie smiling but knows something sus about you ahh smile.

Lorem ipsum dolor sit amet consectetur adipisicing elit. Architecto, ipsa libero quos minus odio optio itaque ratione tenetur esse quod deserunt id autem totam aliquid iure accusantium neque. Aut, suscipit.

CSS Pseudo-Classes

Pseudo-classes are used to define the state of an element. Examples are the hover state of an element, the visited and unvisited state of anchor links, the focus state of an element, and the valid/invalid/required/optional states for form elements.

The following is an anchor link styled depending on its state (link, visited, hover, active):

Next example is a div element that is styled when hovered. Move your mouse over to see a gradient background.

Hover your mouse inside!

Next example is a div with a dropdown tooltip when hovered.

Hover over me to know a secret.

Now here is my secret, very simply: you can only see things clearly with your heart. What is essential is invisible to the eye.

from: The Little Prince
by Antoine de Saint-Exupéry

Next pseudo-class is the first-child. This determines the first child of an element that will be styled by CSS. The span first child of the div element will be styled but not the second, third, ... span.

I am the parent and this span is the first. This is the second span, third span, and fourth span.

CSS Pseudo-Elements

Pseudo-elements are used style specific parts of an element. It can be used to style the first letter of a paragraph, insert content and more.

The following div is an example of using pseudo-elements in styling its first line of text:

The first line of this div will be different and styled. Lorem ipsum dolor sit amet consectetur adipisicing elit. Tenetur odio illum ea adipisci? Dignissimos nam fugit sapiente quia et architecto eum repellat quibusdam autem rem, labore officia, asperiores, doloremque ea. Lorem ipsum dolor sit amet consectetur adipisicing elit. Natus quibusdam harum velit similique tenetur asperiores iste corrupti inventore, quidem a quas saepe incidunt? Eveniet necessitatibus facilis quae aliquid, voluptatibus adipisci. Lorem ipsum dolor sit amet consectetur adipisicing elit. Minus, hic deleniti.

Laudantium facilis dolores tempora quos soluta molestias quibusdam delectus voluptates atque, a quaerat voluptate, eos provident quo, commodi dolorum! Lorem ipsum, dolor sit amet consectetur adipisicing elit. Nisi asperiores a eaque iste iure rem voluptatem nobis veniam excepturi harum corrupti ex facere ipsam omnis odit, assumenda officia perspiciatis cumque! Lorem ipsum dolor sit amet consectetur adipisicing elit.

Hic quae nulla optio nobis dolores, dolorem molestias quaerat maxime repudiandae officiis saepe corrupti, aut neque corporis, earum nostrum eveniet sint tempora!

The next pseudo-element is first-letter. This is showcased below:

Fuck you Ezekiel! Lorem ipsum dolor sit amet, consectetur adipisicing elit. Eligendi, corrupti dolore? Sunt, fugiat quae. Sequi dolore exercitationem molestias doloremque assumenda fuga molestiae. Eveniet, excepturi fugit officia ratione beatae eligendi fuga.

Lorem ipsum dolor sit, amet consectetur adipisicing elit. Possimus, quisquam. Qui repudiandae maiores ut nihil. Eum consequuntur exercitationem sunt, deserunt earum optio fugit aspernatur, ut necessitatibus asperiores, delectus sequi iure.

Lorem ipsum dolor sit, amet consectetur adipisicing elit. Possimus, quisquam. Qui repudiandae maiores ut nihil. Eum consequuntur exercitationem sunt, deserunt earum optio fugit aspernatur, ut necessitatibus asperiores, delectus sequi iure.

The next pseudo-element is the before and after pseudo-element. These adds content before and after the element selected.

The following is an example of a p element that have before and after pseudo-elements (Anthony Mackie ahh face):

This paragraph has the face of the man, the myth, the legend.

If your webpage has lists, you can add the marker pseudo-element to style the list style type such as buttons, disks, numbers, etc.

The following list is an example of how to utilize marker:

Games I Would Love to Play in 2026:

  • GTA 6
  • The Elder Scrolls 6
  • Lords of the Fallen 2
  • Onimusha: Way of the Sword
  • Kusan: City of Wolves

The next pseudo-element is the selection. It defines what happens to elements selected by users of your website such as it changes the background color and text color, etc.

Highlight the text here or any other text in this website. It is coded so that text will have black background and color white text.

CSS Opacity

Opacity specifies the transparency of an element.

The following images are the same but have different opacity values:

Thanos getting dusted after Iron-Man snapped the Infinity Gauntlet. Thanos getting dusted after Iron-Man snapped the Infinity Gauntlet. Thanos getting dusted after Iron-Man snapped the Infinity Gauntlet.

Opacity is often use to highlight transparent images such as in a slideshow.

See and hover over the images below (surprise for last pic!):

Rengoku Flame Hashira. Zenitsu Agatsuma. Tanjiro Kamado. Let me know ahh dog.

CSS Attribute Selector

The attribute selector is used to select elements with a specific attribute such as an anchor link with a target attribute. Below is an example of selecting links with the target attribute using the attribute selector:

Go to Google.com. Go to YouTube.com. Go to Facebook.com.

You can also specify more by adding a value to the attribute. Say for example, the background color is light blue instead of white for links which have a target value of _blank which in this case is facebook.com.

Go to Google.com. Go to YouTube.com. Go to Facebook.com.

There is also an attribute selector for elements with title attribute. We can add the value of a word from a string of words from the title of the element to select it thru CSS.
Example below are images but we will only want to design the images with a "ninja" word in its title attribute using img[title~="ninja"].

Ghost of Tsushima poster. Zed wallpaper. Teenage Mutant Ninja Turtles poster. Chika Fujiwara.

There are many other ways of attribute selection. We will discuss the following selectors individually in the following:

The first one is selecting elements with attribute values equal to exactly "top" or "top-" which is directly followed by a hypen. These elements will be highlighted.

Use [class|="top"].

I have "top" value for class.

I have "top-p" value for class.

I have "topp" value for class.

The next one selects element values starting with "top" only. These elements will be highlighted.

Use [class^="top"].

I have "top" value for class.

I have "top-p" value for class.

I have "topp" value for class.

I have "topnotcher" value for class.

I have "right-top" value for class.

I have "flattop" value for class.

This one selects element values ending with "top" only. These elements will be highlighted.

Use [class$="top"].

I have "top" value for class.

I have "top-p" value for class.

I have "topp" value for class.

I have "topnotcher" value for class.

I have "right-top" value for class.

I have "flattop" value for class.

The last one selects element values with "top" on their value. These elements will be highlighted.

Use [class*="top"].

I have "top" value for class.

I have "top-p" value for class.

I have "topp" value for class.

I have "topnotcher" value for class.

I have "right-top" value for class.

I have "flattop" value for class.

CSS Forms

The following is an example form styled by CSS using attribute selectors.

The example form above is a bit advanced so we need to style form elements for the following one at a time.

In this example, we will apply a full width to the text input in the following form:

For the following form, the inputs are padded and have margins.

You can also add colored borders to the input text box. These borders can also be rounded thru CSS.

You can also change the background color of input boxes like so.

Outlines are automatically available on text inputs when in focus. You can change this by removing outlines and change its CSS style when hovered.

Next we wil add a search element in our form which is common in modern websites. This element will have a search icon as background-image.

If you want to upgrade the search bar, we can use transition property.

We can also style text areas. In addition, text area inputs have a resize function but we can set it to none in CSS properties like the one below.


We can also style select and button elements in forms just like the following.

The last exercise for this section is making a responsive form which changes depending on the size of the browser window.

CSS Counters

Counters are like assigned variables in CSS. You can start a counter with counter-reset property then use counter-increment which adds/increments the counter value. Use content to insert the generated value using counter() or counters().

In the example below, we used the variable "test" for our "counter-reset". Then we added content before every "h3" header in this div. We incremented first our variable "test" with "counter-increment" and then we added the resulting counter values with "counter(test)" in our content.

Header

Header

Header

Header

Header

We can also use nested counters just like the following:

HTML/CSS Tutorials

HTML

CSS

Bootstrap

W3.CSS

Scripting Tutorials

JavaScript

jQuery

React

Programming Tutorials

Python

Java

C++

CSS Website Layout

There are a plethora of website layouts in the internet and one of the most common layouts is the image below:

Common website layout consisting of a header, navigation menu, main content, side content, and a footer.

For this section, we will again utilize iframes to show a separate webpage to fully understand the topic for website layout.

For the first one, we will design a header which mostly contains the website name and logo commonly found at the top of the webpage.

Now we create a simple horizontal navigation menu just below the header.

Next, we will create a responsive content page for our website that stacks the content on top of each other if the window screen is small like in smartphones.

We can also create unequal widths for each column in our content. Say we create the main content in the middle and two sides on its left and right. Then we proceed to apply responsive web design like so.

The last section for our website layout is the footer. Most often contains the copyright information of our website and some contact info of the author. The following is a simple footer to finish up our demo website.

Now we will try to replicate a responsive web design from W3Schools.com

CSS Math Functions

CSS also allow mathematical functions such as "calc()", "min()", and "max()". These functions are used for specific designs such as the following function "calc()". We will use this function to calculate the width of a div element. Remember, we must at least put one operator for the function to work.

Now we will create a div element that has a gap of 50px between the edges of the window, in this case, another div element.

This div element has a width of 100% minus 100px (50px for left and right) and then centered thru margin: auto.

The next function is the "max()" function. We will attempt to limit the width of the following div by making it follow 50% of the parent element's size or 300px, whichever is higher at the moment.

This div element has a width of 50% of its parent div or 300px depending on browser size.

The last function will be the "min()" function. We will now code the width of the div element below to be the minimum of 50% of its parent element or 300px.

This div element has a width of 50% of its parent div or 300px depending on browser size.