Example code for using text-align and margin: 0 auto to center in CSS

Example code for using text-align and margin: 0 auto to center in CSS

Use text-align, margin: 0 auto to center in CSS

When using text-align or margin: 0 auto to center the page, some invalid situations may occur. Here are two examples to explain and solve the problem.

Example 1: Centering an inline element using text-align

Let's say I want to center an image within its parent component. First, the parent component should be a block element (div, p...), and then add the attribute text-align:center in the parent component , and then all the inline elements (span, img...) in this component will be displayed in the center.

Example 2: Centering a block element using margin

The prerequisite for margin: 0 auto is that the element has width. If you want to center a div, you need to set the width for it and then add the margin: 0 auto style.

Experimental results:

Experimental Results

Experiment Code

<template>
  <div class="father">
    <div>
      <img src="~@/assets/img/rules-detail-no-record.png">
      <span>I am the inner div content</span>
    </div>
    <span>I am the outer div content</span>
    <div class="bg"></div>
  </div>
</template>

<script>
export default {
  name: "test"
}
</script>

<style scoped>
.father{ //Center the inline elements surrounded by it background: #2e90fc;
  text-align: center;
}
.bg{ //Block element centeredbackground: #fc2e80;
  width: 60px;
  height: 60px;
  margin: 0 auto;
}
</style>

Summarize

This is the end of this article about example codes for using text-align and margin: 0 auto to center in CSS. For more relevant content about using text-align and margin: 0 auto to center in CSS, please search previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future!

<<:  Discussion on CSS style priority and cascading order

>>:  Detailed explanation of Nginx passively checking the server's survival status

Recommend

Docker deploys Macvlan to achieve cross-host network communication

Basic concepts: Macvlan working principle: Macvla...

Vue/react single page application back without refresh solution

Table of contents introduction Why bother? Commun...

Basic usage of JS date control My97DatePicker

My97DatePicker is a very flexible and easy-to-use...

HTML background color gradient effect achieved through CSS style

Effect screenshots: Implementation code: Copy code...

Docker data management and network communication usage

You can install Docker and perform simple operati...

Implementation of services in docker accessing host services

Table of contents 1. Scenario 2. Solution 3. Conc...

Example code for implementing verification code login in SMS API in Node

1. Node server setup + database connection The op...

Flex layout makes adaptive pages (syntax and examples)

Introduction to Flex Layout Flex in English means...

Common structural tags in XHTML

structure body, head, html, title text abbr, acro...

Stealing data using CSS in Firefox

0x00 Introduction A few months ago, I found a vul...