Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report