Video caption Ursula von der Leyen: "The endeavour is a united, strong Europe"

Germany's Ursula von der Leyen has been narrowly elected president of the EU Commission following a secret ballot among MEPs.

The centre-right defence minister will replace Commission President Jean-Claude Juncker on 1 November.

She secured the backing of more than half of the members of the European parliament on Tuesday evening.

The Commission drafts EU laws, enforces EU rules and has the power to impose fines on member states if necessary.

"The trust you placed in me is confidence you placed in Europe," Mrs von der Leyen, who is the first woman to be elected president of the European Commission, said in a speech immediately after the vote.

"Your confidence in a united and strong Europe, from east to west, from south to north."

"It is a big responsibility and my work starts now," she added. "Let us work together constructively."

She was confirmed by a margin of 383 votes to 327. She needed the backing of 374 out of 747 MEPs to win.

A total of 751 MEPs were elected in May, but four were absent for Tuesday's vote.

Born in Brussels, Mrs von der Leyen has seven children and trained as a gynaecologist before entering politics.

The 60-year-old defence minister has been criticised in Germany over the armed forces' persistent equipment shortages and what some consider to be her aloof management style.

She has promised to push for the EU to play a bigger role in social welfare, to tackle poverty, and has stressed that she would stand up for women's rights.

She has also pledged in the past to allow a further extension of the UK's withdrawal date from the EU "should more time be required for a good reason".

Video caption Ursula von der Leyen said she wanted Europe to be the first "climate-neutral" continent

In a speech in the European parliament earlier on Tuesday, Mrs von der Leyen made some other significant pledges:

She would push to give the European Parliament "the right of initiative" - meaning the Commission would have to legislate on MEPs' resolutions; currently only the Commission can draft laws

On irregular migration to the EU, she said she would boost the EU's border force Frontex to 10,000 staff by 2024, but said "we need to preserve the right to asylum through humanitarian corridors"

She offered an EU "reinsurance scheme" to bolster national insurance schemes for the unemployed.

Narrow victory could cause problems

Analysis by Damian Grammaticas, Europe Correspondent, BBC News

European leaders will be breathing a sigh of relief.

It took days of fraught negotiations and a difficult compromise among EU countries to nominate Mrs von der Leyen, a German conservative and close ally of Angela Merkel.

She scraped through with 383 votes, just nine more than the minimum. That may leave her in a weakened position, as it seems she was helped over the line by votes from Eurosceptic and right-wing MEPs in Poland and Italy.

Greens, on the other hand, did not vote for her. They said her commitments on climate change and saving the lives of refugees trying to cross the Mediterranean were too weak. Socialists, too, were angered that their preferred candidate was overlooked by EU leaders.

So what does all this division mean? For a start, the European Commission Mrs von der Leyen will lead from November may have problems passing legislation through the parliament.

What is the reaction?

Figures from across the bloc have been quick to congratulate Mrs von der Leyen on her election victory.

"This job is a huge responsibility and a challenge. I am sure you will make a great president," outgoing Commission head Jean-Claude Juncker wrote on Twitter.

German Chancellor Angela Merkel paid tribute to her outgoing defence minister, who she described as a "committed and convincing European".

"Even if I lose a long-standing minister today, I win a new partner in Brussels," she said in a statement. "I am therefore looking forward to good co-operation."

"About time we get a woman in that important position," Swedish Prime Minister Stefan Lofven said in a tweet. "Time to continue work on the crucial issues: jobs, climate change, migration and security in Europe."

Spanish Prime Minister Pedro Sanchez offered his congratulations and said: "Europe has to move forward. In this we have to work together, addressing the changes that the common project needs."

Donald Tusk, president of the European Council, tweeted a simple message:

Sorry, this Twitter post is currently unavailable.

Belgium's leader - and incoming head of the European Council - Charles Michel said: "I wish to congratulate [Mrs von der Leyen]. Let's work together in the interest of all Europeans."