Container Links can be added in one of two ways:
- Create a pseudo element
- Using a HTML tag
In this doc we will explain how to use either option and the benefits and pitfalls of both.
Pseudo Element
TLDR: Skip straight to the how to
If you know CSS, Pseudo Selectors and Global Styles then skip straight to the tutorial.
In this How To we are going to provide a simple SEO friendly and accessible method to create container links. It will require GenerateBlocks Pro and a basic knowledge of Global Styles and Selectors.
The method will use a regular link, in our example a button block, placed inside a container. We will use a Global Style to build and position a CSS Pseudo element to extend our links clickable area to cover the Container.
This method;
- will create a Global Style you can apply to any container.
- gives you full control over the link, its href, content and any HTML attributes
- uses CSS pseudo elements to create the link which:
- mitigates any risk of invalidating HTML
Lets begin with a simple example to illustrate how container links compare to regular link inside the container.
The first example has a regular button, the second examples has the a button with the “extended” clickable area.
The blocks and the basic concepts
The basic concept requires a Container Block with a Button block ( or other Link ) inside.
In our first example, the button behaves as expected — the clickable area is limited to the space within the border box.
For illustrations purposes only I have styled buttons clickable area with a red overlay:
To make the entire container clickable, we need to extend the button’s clickable area so it fills the parent container. Again we can use our red overlay to illustrate that.
So how do we extend this clickable area to cover the entire container ?
The solution is to use the ::before pseudo element.
The ::before Pseudo element
A ::before Pseudo-element can be created using CSS to attach virtual content to our link. This fake element will act as an extension to our links clickable area which can be styled and positioned independently.
Now we have the concept lets start with building our Container Link.
Building our Container with Link
Breaking down the process; we will:
- construct the layout with the blocks
- create a Global Style
- set the Container position to relative
- create the pseudo element
- position the pseudo element
- Use the global style to add container links.
1. Setting up the blocks
Our example design is very simple in that it uses only 2 blocks, a button inside a container. Lets begin by:
- add a Container Block
- add a Button block inside the Container
- add a link to the button
In your editor list view the block layout will look something like this.
2. Create a Global Style
Next we are going to build a single global style that will:
- set the parent containers position to relative
- target the first
a:linkelement inside the container - create a pseudo element on our link
- absolute position our pseudo element over the container.
Global Styles and Selectors
This How To requires some basic knowledge of Global Styles and CSS Selectors. You can find more information on either subject at the following links:
Lets get started.
-
Select the Container Block
-
Create a new global style
2.1 Give it a name eg.
has-container-link
Add a global style 2.2 Start with a Blank Style
Choose the blank style -
Set the
has-container-linkStyles: -
Add a New Selector to our Global Style class, to do that
4.1 In the Selector More options panel click + NEW
Choose the + NEW selector 4.2 In the SELECTOR field copy and paste the following:
a:first-of-type:beforeMake sure the Compound Selector options remains unchecked.
Adding the SELECTOR 4.3 Click Create
Create the Selctor The CSS selector will now look something like:
.has-container-link a:first-of-type::before -
Set the styles for the:
.has-container-link a:first-of-type:before -
Save the page and the global style.
The selector explained
During this step we built a Selector within our global style.
Here is the example selector:
.has-container-link a:first-of-type:before
To help understand the selector, lets break this down into 3 main components and then read it from right to left:
:before– target the before pseudo element that it is attached to:a:first-of-type– the firsta( link ) element to be found inside the:.has-container-link– the container element with our global style
Put it all together and this reads as: target the :before pseudo of the first anchor link found inside the block with the has-container-link class
3. Use the Global Style to Add Container Links
Now to add Container Links anywhere we need them we simply have to apply our new has-container-link where it is required. To start a new one yo would:
- Add a Container Block
- Give the Block the Global Style of:
has-container-link - Place inside the Container our Link , using a Button or Text Block
And that is it, the class will automatically target the first link it finds inside the Container Block.
HTML Tag
Using the Container Block with the HTML tag set to <a> is a convenient way to link an entire group of elements. However, this approach has several potential drawbacks, outlined below, which is why we recommend using pseudo elements.
Potential Drawbacks
HTML Validity
Nested Links Not Allowed: HTML does not allow anchor (<a>) tags inside another <a> tag. If the container includes buttons or links, the result is invalid HTML, which can break rendering or behavior in some browsers.
Do not nest links or interactive elements
Ensure there are no other link or interactive elements inside the container.
Accessibility
Screen Readers Confusion: Screen readers may struggle to interpret a large, interactive container as a single link. This can make navigation unclear for users relying on assistive technologies.
Keyboard Navigation Issues: If interactive elements are inside a linked container, it may disrupt expected keyboard focus behavior.
SEO
Unclear Link Context: Wrapping diverse content in a single link can make it harder for search engines to understand what the link is about, reducing its relevance and effectiveness.
Setting a Container Link
- Select the Container block you want to add the link to
- Open the block settings
- set the TAG NAME to
a - choose the LINK DESTINATION
- set the TAG NAME to