You can absolutely do this in SharePoint Online and this is what you have to take a look:
(1) Learn how to create and apply PnP Templates to SPO Sites...A PnP Template is an XML file that contains all the customizations you want for your sites. This approach implies you are going to create your sites and apply the template by means of PowerShell Code, #C Code (in an Azure Function) or Flow...there are different patterns to do that.
(2) As an alternative to PnP Templates, take a look at Site Scripts & Site Designs...A Site Script is a JSON file that contains customizations to be applied to new and existing sites. A Site Design can contain more than one Site Scripts and Site Design can be applied to a new or existing site in the following ways: Directly in the SPO UI using the Site Desings option available in the settings menu for existing sites or at the moment of creating a communication site, PS Code, C# code or Flow