Get the App
SLTechnology News&Howtos  ›  Development  › 

The reason why margin doesn't work in CSS and how to solve it

Shulou Source: shulou.com Published: 2022-06-01 06:27:43 10月04日 Update

This article introduces the relevant knowledge of "the reason why margin does not work in CSS and how to solve it". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The reason why margin doesn't work is often because display: "display:block" or "display:inline" is not taken into account.

Let's look at specific examples.

HTML

This is an inline block.

CSS

H4 {

Display:inline-block

Background:#ffff00

Width:12em

Text-align:center

Margin:0auto

}

The effect displayed on the browser is as follows:

2345 Screenshot 20181127101737.png

"margin:0auto;" does not work for inline-block.

Even if you can first specify "inline-block" with a numeric value, using "margin:0auto;" to center does not work.

"text-align:center;" does not make the parent element a selector

The basic rule of "text-align:center;" is "selector is the parent element". In the above example, if you want to center it, you must create a parent element.

Once you understand the above, you can solve it.

HTML

This is an inline block.

CSS

H4 {

Display:inline-block

Background:#ffff00

Width:12em

Margin:0auto

}

.wrap {

Text-align:center

}

This is the end of the content of "Why margin does not work in CSS and how to solve it". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Function cause element content more knowledge this is practical successful next example dilemma reality situation screenshot effect numerical value article case browser Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Docker vpn Apple Linux