Late XIX century America is portrayed as a wild and unlawful land, but post WW1 America is portrayed as a civilised land. How did that transition happen?
Seems like a really loose question.
Are you talking about public perception? Are you talking about historical representations?
In the 19th century, frontiersman, ranchers & farmers are perceived as a civilizing force, with fictional tales pushing sensationalied stories for a hungry audience. The publishers & writers note what the public likes based on sales & gives it to them. There is no attempt to be responsible.
The industrial revolution brings a technological shift. We go from using messily using things like whale oil for lamps to electricity, the telegraph, the telephone and the airplane. The gun becomes easier to make & cheaper to manufacture, a thing of power and accessibility expressed in fantasies of dime store novels.
We look at the Western violence today as uncivilized, but from 1800 to the 1960's, people looked at the violence as taming the West (With the beginnings of exceptions & questions about it appearing in a few movies before hand.)
The 19th century industrial revolution is also pretty messy itself. Children working in factories in conditions that are often horrific. Health and safety standards are non existent. Roads are still made of dirt. Transport is mostly by horse and by boats via river & canals.
In the 20th Century, the technological advancement from dirt road to paved road, train & airplane alongside the taming of industry solidify the concept of Progress that is but a hazy dream in the 19th Century when we are still "taming the West".
As we connect America with communications & manufacturing advancements, the Progressive reform movement arrives, cleaning up the horrific conditions of the industrial revolution. Factories become clean and civilized. The concept of progress becomes Progress.
I think that's all having an effect on your question here.
Historians are going to get swept up in this perception. A historian in the 1920s is going to remember their dirt floor youth and contrast it with their modern house as they drive quicky across paved roads to their office with hot water supplied by turning a knob instead of heating a kettle.
The public's original understanding of Western history is based in myth. Just as the last few decades Brazil created mythic books about frontiersmen bravely entering the Amazon to justify exploiting the Amazon (popularly called "Bang-Bangs"), so too did western frontier novels sensationize the frontier, with the violence portrayed largely as myth (More often one had to turn your gun into the sheriff when you entered a town).
A comparison to popular Western fiction is crime fiction today. Even though violence and crime have been falling for decades, the portrayal on it on TV only continues to expand. I hear conversations with people who live in the suburbs or in the country who sound no different than people when I was a kid in the seventies, even though the violence in the seventies was not as bad as it was perceived then and it's certainly nothing like it's perceived now.
The important part to focus on here is the Wild West portion. Prior to the outbreak of the US Civil War, the major theme for the USA was having a rapidly expanding frontier charging West. Wars with Native Americans along with relocation of said tribes could rightly be considered Genocide in a modern context but that is a whole different post. However Pre-War the Mississippi\Missouri rivers acted as a bit of a barrier to settlers. The Great Plains were seen as mostly hostile Indian Territory all the way to the Harsh Rockies+inter-mountain west. The real prize was thought to be the West coast (particularly with gold found in California in 1849).
In the entire Westward expansion there managed to be somewhat of a balance between Slave and Free states which kept some balance in Congress and kept the Civil War from exploding decades earlier. However the potential additions of Kansas and Nebraska as territories threatened to undo that balance as both were located above the usual divide of Slave/Free states and there were no likely candidates South of that Mason/Dixon Line. The idea of having the Territories have the residents vote Slave/Free became the compromise.
That gives you the political background for Bleeding Kansas which was a series of raids/murders/arsons that broke at as the time for voting drew near with pro and anti slavery forces each responsible for the violence. Now we ask why this border violence is different than the Indian Raids/Wars from the previous 50+ years of expansion? The answer mainly lies in some technological changes in the US: Civilization if you will. Between the expansion of railroad, telegraph, and newspaper/printing press communication within hours instead of days was possible even from the frontier. Reporters for both pro and anti slavery forces also flocked to Kansas to send breathless cables back to their papers which sold the story to a wider distribution to the Civilized East Cost Cities and towns.
The US Civil War saw this process speed up with the inclusion of actual photographs and an upswing in drawings of the events within days, if not hours, of battles. After the war, there were several factors that now primed the Wild West Narrative:
An information infrastructure along with a surplus of correspondents, writers, and photographers who suddenly no longer had action to report on.
Hundreds if not thousands of young men who have had military training and weapons, many of which fought on a losing side. Many of the latter were more than willing to move on with their life elsewhere. They looked to the West where the chance to start a new life has been available now for generations.
Immigration without the threat of War. During the Civil War there was only a small drop in the number of immgrants coming to the US. Various conflicts were still brewing in Europe (Franco-Prussian War being a big one, the Irish Diaspora being another), so fleeing war to America for the city or to grab a bit of land were viable options.
Rail road expansion out West. One of the prime ways the US Government gave incentive to the various rail companies, was to grant the Right of Way over the land along with significant acreage surrounding the lines that they companies could then sell or use as bonus/incentive to workers and investors.
The Homestead Act of 1862. This probably should be mentioned as #1 as it was one of the drivers that really opened up all of the above. 160 acres free and clear to your name if you could pay a small filing fee and make a living on it for 5 years.
Explosion in Supply and Demand for Bison pelts and cattle. The great Bison kill put pressure on the remaining Native Americans on the Plains. It opened up large grazing lands for teaming herds of cattle that were roaming Texas at the end of the Civil War. The herds were the result of difficulty in exporting/moving the cattle and a lack of manpower to even effectively herd them.
More effective geological and geographical surveys of the areas from the Plains to the Sierra Mountains. Minerals, springs, lakes, rivers all represented possibilities for people to make money mining copper, silver, borax, gold, and other raw materials. Water in the more barren regions was prime real estate for herds and farmers.
All of the above created an explosion of opportunity and the "frontier" of civilization marched it's way back and forth, up and down over the "Wild West". Cowboys, rustlers, outlaws, settlers, immigrants, soldiers, former slaves, former slave owners, former soldiers, drifters, lawmen, bankers, lawyers, tradesman, and businessmen all had opportunity to make a living. Those all didn't necessarily "pan out" and frontier living was never easy and hard times and conflict most certainly came up. While some sense of awareness that the "civilizing" of the Native American was actually flat out murder/genocide was starting to make it's way into the American conciseness, it did very little to slow down the decline and destruction. The loss of the Bison and the Native American in these areas generally signify the "civilizing" of an area.
By the early 1890's the last of the homestead "land rushes" happened in Oklahoma. After that point, there was very little land left for claiming under the Homestead Act. In the span of 30 years much of the "Wild West" had been filled in. In that time, Journalists, Newspapers, and various books/magazines had churned out hundreds of stories in the "pulp fiction" of the wild west. Indeed such stories became so popular that before the West completely closed variety shows including Buffalo Bill Cody's Wild West Show. Stories of the West were so powerful that even a young Theodore Roosevelt left New York and spent time in the West and in several ways embodied that spirit during his time as President.
The onset of WW1 saw many volunteers heading off to war that were the sons or grandsons of that frontier generation who grew up hearing first hand stories of it's closing.
It's the stories that people remember and those stories were romanticized, fictionalized, and sold at an industrial scale. The Gunfight at the OK Corral has been written about so often that it moved into legend, so much so that it has shown up in countless movies. Yet such an incident wasn't all that different than several dozen such acts of "lawlessness" that happened on the frontier since the founding of the USA. The difference is how the story was "remembered" namely in the press, in the media of the day. This perhaps was how the transition really happened. The Wildness became story and legend, law and order became the order of the day. Technology and industry helped to tame the West, but it was the people and the legends that made it famous.