Life for many in China’s wealthiest city had been upended by the virus — and the government’s response — even before Shanghai imposed a lockdown to stop a rapidly spreading Covid outbreak.

Residents rushed to stockpile food in case they were forced to stay at home. Some demonstrators gathered at the gates of housing complexes that had been locked without warning. Others were forced to sleep on the floor in government isolation facilities due to a lack of beds.

For others, the city’s Covid-19 restrictions have put their lives in jeopardy. Some residents have been confined to their homes due to a lack of access to dialysis or other life-saving treatment. A nurse who had an asthma attack died after a hospital refused to treat her because of Covid prevention protocols.

Officials attempted to contain the chaos by confining buildings or neighborhoods, arguing that a full-scale lockdown in the 26-million-strong city would be unworkable. Officials claimed that their more surgical approach would contain the outbreak while maintaining economic life in Shanghai, a global business hub.

The city’s officials then indicated on Sunday evening that doing both at the same time might no longer be possible. The city imposed a staggered lockdown, shutting down non-essential businesses, suspending public transportation, and confining the majority of the population to their homes.

The measures divided the city in half, with the eastern section shut down for a five-day quarantine beginning Monday and the western section shut down for a similar period. In comparison to the rest of the world, Shanghai’s caseload of 3,500 on Monday was small, but it was driven by the highly transmissible Omicron variant. According to officials, the lockdown will allow authorities to conduct mass testing.

Since the pandemic began in Wuhan more than two years ago, China has been dealing with the country’s largest outbreak. The government is concerned that an uncontrolled spread could overwhelm hospitals, resulting in a large number of deaths. A large number of Chinese adults aged 80 and up are unvaccinated, and previous infections have left them with little immunity. The battle to contain a Covid outbreak in Hong Kong has provided a glimpse of what that might look like: In recent weeks, there has been an increase in deaths, particularly among unvaccinated older adults.

The central government is still pushing officials across the country to eradicate the virus, putting in place some of the strictest measures in the world, such as isolating anyone who tests positive and enforcing quarantines on those deemed at risk of infection. If they were deemed to be close contacts, white-collar workers, shoppers, and diners have been forced to isolate themselves inside offices, stores, and restaurants. People who test positive are sent to hospitals or government isolation centers, which are set up in schools, exhibition centers, and other locations.

Ivy Sun, a 32-year-old counselor, had been anticipating her release after being held in her home for five days during a neighborhood lockdown. She was given a breath of fresh air on Sunday, after the citywide closure was announced, but only for seven hours. She and her neighbors dashed out to get groceries and other necessities in preparation for yet another incarceration.

Ms. Sun said she couldn’t get groceries online during her first lockdown because they sold out quickly. She and her neighbors banded together and began bulk-purchasing necessities. She also wondered if the panic buying on Sunday, when people crammed together inside enclosed stores, had worsened the virus’s spread.

In other cases, protests were sparked by the unpredictability of the restrictions and what appeared to be indefinite confinement. Residents of Jinghua Xinyuan, an apartment complex in central Shanghai, gathered in front of a marble and metal security gate that was locked to prevent them from leaving.

According to residents’ accounts posted online, the authorities had locked residents in the compound for three days but did not administer Covid tests despite the fact that some of them had the virus. Residents complained that they had given contradictory messages about how long this would go on.

While authorities have been quick to place residents in isolation and place them in home quarantine, they have taken longer to ensure that those who are confined can receive treatment for serious medical conditions. Many residents have resorted to going online to beg for access to treatment such as dialysis.