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

Problem analysis of using idea to build springboot initializer server

Problem Description Recently, when I was building...

How to introduce scss into react project

First download the dependencies yarn add sass-loa...

Code for aligning form checkbox and radio text

Alignment issues like type="radio" and t...

mysql create database, add users, user authorization practical method

1. Create a MySQL database 1. Create database syn...

Object-Oriented Programming with XHTML and CSS

<br />If only XHTML and CSS were object-orie...

How to debug loader plugin in webpack project

Recently, when I was learning how to use webpack,...

WeChat applet implements a simple dice game

This article shares the specific code of the WeCh...

About VUE's compilation scope and slot scope slot issues

What are slots? The slot directive is v-slot, whi...

Summary of important components of MySQL InnoDB

Innodb includes the following components 1. innod...

Detailed installation tutorial of Mysql5.7.19 under Centos7

1. Download Download mysql-5.7.19-linux-glibc2.12...

Example of how to build a Harbor public repository with Docker

The previous blog post talked about the Registry ...

Html+CSS drawing triangle icon

Let’s take a look at the renderings first: XML/HT...

MySQL REVOKE to delete user permissions

In MySQL, you can use the REVOKE statement to rem...

vue-router hook function implements routing guard

Table of contents Overview Global hook function R...