div标签的位置中心,其宽度未知高级 [英] Position center of div tag, which width is unknown advanced

查看:160
本文介绍了div标签的位置中心,其宽度未知高级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有没有固定的div。我想要这个div位置在屏幕的中心。

I have div with no fixed with. I want this div position in the center of screen.

我知道如何使用jquery,但是使用纯CSS,我没有找到任何解决方案。

I know how to make this with jquery, but with pure css, I did not find any solution. How do you make this happen with pure css?

推荐答案

假设你想要收缩包装div的内容这是一个无意义的练习,因为它的宽度将是容器的宽度):

Assuming you want to shrink-wrap the contents of the div (otherwise centring it is a pointless exercise as its width will be the width of the container):


  • 设置 display:inline-块在div上以使其缩小

  • 设置 text-align:center; div的父元素将div设置为中心

  • 在div上设置 text-align:left; 以覆盖其内联内容它将从其父级继承。

  • Set display: inline-block; on the div to make it shrink-wrap
  • Set text-align: center; on the div's parent element to centre the div
  • Set text-align: left; on the div to override the centring of its inline content that it would inherit from its parent.

这篇关于div标签的位置中心,其宽度未知高级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆