简单中心一个对象与css和没有黑客 [英] Simple center a object with css and no hacks

查看:131
本文介绍了简单中心一个对象与css和没有黑客的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

晚上好,



我想用css和没有黑客来定位对象,这是可能还有如何?



我试过这个,但比我的p标签走了。

  .centered {
位置:固定;
top:50%;
left:50%;
}

我喜欢听到你的声音!

解决方案

有几种方法可以使元素居中。但是这取决于你的元素是什么以及我们如何选择显示它:




  • 如果你有 {display:inline; }



    这很简单。你可以只使用text-align:center;

  • 如果您有 {display:block;}



    这有点困难。这取决于你的对象如何定位。您的对象可以是相对的,绝对的或固定的。




    • 那么您可以使用margin:0 auto;,但是您需要一个宽度值。


    • 如果它是绝对定位的,那么你需要指定top:left:你还需要一个宽度。如果你知道元素的宽度,最好的方法是使用 {left:50%; margin-left:-X} ,其中X =元素宽度的1/2。




Good evening guys,

I want to center an object with css and no hacks, is this possible and how?

I have tried this, but than my p tag is gone..

    .centered {
  position: fixed;
  top: 50%;
  left: 50%;
}

I like to hear from you!

解决方案

There's several ways to center an element. But it depends on what your element is and how we choose to display it:

  • If you have {display:inline; }

    This is simple. You can just use "text-align: center;" to center text, images and divs.

  • If you have {display:block;}

    This is a bit more difficult. It depends on how your object is positioned. Your object could be relative, absolute, or fixed.

    • If it is relative; then you can use "margin:0 auto;", however you will require a width value.

    • If it is absolutely positioned, then you need to specify your "top:" and "left:" values. You'll also need a width. If you know the width of the element, the best way is to use {left:50%; margin-left:-X}, where X = 1/2 the width of the element.

这篇关于简单中心一个对象与css和没有黑客的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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