简体   繁体   English

如何居中 <div> 里面<button>?</button>

[英]How to center <div> inside a <button>?

I have a round < button > with a < div > inside that represents a Unicode image. 我有一个圆形的<button>,里面有一个<div>,它表示一个Unicode图像。 Currently the button is set to border-radius: 12px; 当前,按钮设置为border-radius:12px; height: 24px; 高度:24像素; and width: 24px; 宽度:24px; and the < div > is to font-size: 17px. 而<div>的字体大小为:17px。 The < div > Unicode image sits inside but not centered and the button is slightly off to the side. <div> Unicode图像位于内部,但不居中,按钮稍微偏向侧面。

How can I get the < div > to center inside an oval button despite what font-size the < div > is? 尽管<div>是什么字体大小,如何将<div>放置在椭圆形按钮的中心?

EDIT 编辑

I want to create a circle/round button with an emoji center to the middle of the button despite the button's size or the emoji image's size. 我想创建一个圆形/圆形按钮,其表情符号中心位于按钮的中间,而不管按钮的大小或表情符号图像的大小。

CSS for the button and emoji image for div: 按钮的CSS和div的表情符号图片:

#emoji-button {
  border-radius: 19px;
  width: 38px;
  height: 38px;
}

#thumb-emoji:after {
  content: "\01F44C";
  font-size: 20px;
}

And round/circle button with emoji image inside: 以及带有表情符号图像的圆形/圆形按钮:

            <button
              type="submit"
              id="emoji-button"
            >
              <div id="thumb-emoji"></div>
            </button>

But it is not centered. 但这不是居中的。

And is there a way to just back the emoji image alone to be clickable for a method? 有没有一种方法可以仅将表情符号图像备份为可单击的方法?

First off: A <div> is a block element by nature. 首先, <div>本质上是一个块元素。 It will always become 100% wide. 它将始终变为100%宽。 If you want it to not be 100% wide, give it a display:inline-block so it won't get bigger than it needs to be. 如果您不希望它宽100%,请给它一个display:inline-block这样它就不会变得比需要的大。 Then give it a margin:0 auto; 然后给它一个margin:0 auto; or a text-align:center on the parent to center it. 或将text-align:center放置在父对象上居中。

HOWEVER, You are not allowed to put <div> s inside of <buttons> . 但是, 不允许将<div>放在<buttons> it is invalid HTML 这是无效的HTML

See this answer for more information: Why can't a <button> element contain a <div>? 有关更多信息,请参见以下答案: 为什么<button>元素不能包含<div>?

Or, you could read here, from W3 that only phrasing content is expected to be used within a button: https://www.w3.org/TR/2012/WD-html5-20120329/the-button-element.html#the-button-element 或者,您可以在W3的此处阅读,希望仅在按钮内使用短语内容: https : //www.w3.org/TR/2012/WD-html5-20120329/the-button-element.html#所述按钮元件

If you do not know what phrasing content is, See this page: https://www.w3.org/TR/2012/WD-html5-20120329/content-models.html#phrasing-content 如果您不知道措辞内容是什么,请参阅此页面: https : //www.w3.org/TR/2012/WD-html5-20120329/content-models.html#phrasing-content

-- if you are looking into styling buttons specifically, maybe this very short tutorial would help: http://web.archive.org/web/20110721191046/http://particletree.com/features/rediscovering-the-button-element/ -如果您专门研究样式按钮,那么这个简短的教程可能会有所帮助: http : //web.archive.org/web/20110721191046/http : //particletree.com/features/rediscovering-the-button-element /

Here is a fiddle of a working button like yours: https://jsfiddle.net/68w6m7rr/ 这是像您这样的工作按钮的提琴手: https : //jsfiddle.net/68w6m7rr/

I honestly didn't have many problems with this. 老实说,我对此没有很多问题。 I only replaced your <div> with a span, that's it. 我只用一个跨距替换了<div>就这样。

can you post your code? 您可以发布代码吗?

You should NOT need a div inside the button. 您在按钮内不需要div。 If you need the button to have a specific style give it a class. 如果您需要按钮具有特定的样式,请为其提供一个类。 You could do something like this 你可以做这样的事情

CSS: CSS:

    button.something {
      padding: 25px;
      border-radius: 100%;
      font-size: 20px;
      border: none;
    }

HTML: HTML:

    <button class="something">&#128076;</button>

For clean and valid code, you'd better use a :before or :after pseudo-element. 对于干净有效的代码,最好使用:before或:after伪元素。 This would also take care of the centering by default. 默认情况下,这也可以确保居中。

It's even easy to set the content. 设置内容甚至很容易。 Either in css only, like this: 仅在CSS中,如下所示:

1. 1。

button:before {content:"\\25b6";}

(put your unicode value there and classes/ids as needed, then specify them in turn in css) (将您的unicode值和所需的类/ ids放入其中,然后在css中依次指定它们)

2. 2。

Or if you need to specify the value in mark-up, drop a custom data-* attribute like this: 或者,如果您需要在标记中指定值,请删除自定义data- *属性,如下所示:

<button data-myunicode="\25b6"></button>

with each button taking it's own value, then drop this single line in css: 每个按钮取其自身的值,然后将这一行放到CSS中:

button:before {content:attr(data-myunicode);} 

Before answering, let's clear some things out. 在回答之前,让我们先清除一些事情。

div is a block level element, used in an inline element, which is the button element. div是块级元素,用于嵌入式元素(即button元素)中。 Browsers will consider this invalid and will fix it by removing the block element from the inline element . 浏览器将认为此无效,并将通过从inline元素中删除block元素来解决此问题 For more about CSS concepts like box model , box generation please refer to these resources: 有关诸如box modelbox generation之类的CSS概念的更多信息,请参考以下资源:

Also, if you are using an IDE, make sure you have installed linting/hinting tools to help you out. 另外,如果您使用的是IDE,请确保已安装了棉绒/提示工具来帮助您。 These tools can help you in code authoring so, make sure you have them. 这些工具可以帮助您编写代码,因此请确保您拥有它们。 If you are using software like VSCode or Sublime Editor, there are many free code analysis tools out there. 如果您使用的是VSCode或Sublime Editor之类的软件,那么这里有许多免费的代码分析工具。

Let's go back to the code now. 现在让我们回到代码。

You said 你说

I want to create a circle/round button with an emoji center to the middle of the button despite the button's size or the emoji image's size. 我想创建一个圆形/圆形按钮,其表情符号中心位于按钮的中间,而不管按钮的大小或表情符号图像的大小。

I went ahead and created a plunk here where I demonstrate this. 我继续前进,并在这里展示了一个小品。 Essentially, I wrapped the button around a div which serves as a container and through some CSS magic, I made it to have the same height as its width. 本质上,我将按钮包装在用作容器的div ,并通过一些CSS魔术使它具有与宽度相同的高度。 More on that you can find at this SO answer . 您可以在此SO 答案中找到更多信息。

The #emoji-button then has a border-radius: 100% in order to be round, width is inherited from the parent, meaning it has the same as the container and it position is absolute in order to fit in the container. #emoji-buttonborder-radius: 100%为圆形, width从父级继承,这意味着它与容器相同,并且其位置是absolute的以适合容器。

The #thumb-emoji has changed to a span element. #thumb-emoji已更改为span元素。 By user agent styles it has text-align:center . 通过用户代理样式,它具有text-align:center

  <div class="button-group">
    <button type="submit" id="emoji-button">
      <span id="thumb-emoji"></span>
    </button>
  </div>

CSS: CSS:

.button-group {
  position: relative;
  width: 100px;
}

.button-group:before {
  content: "";
  display: block;
  padding-top: 100%;
}

#emoji-button {
  width: inherit;
  border-radius: 100%;
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
}


#thumb-emoji:after {
  content: "\01F44C";
  font-size: 200%;
}

You can change the .button-group width to whatever width you want, it will still keep its 1:1 ratio. 您可以将.button-group宽度更改为.button-group任何宽度,它仍将保持1:1的比例。

You can use then media queries on .button-group to adjust the font-size of your #thumb-emoji , by setting your desired breakpoints. 然后,您可以在.button-group上使用媒体查询,通过设置所需的断点来调整#thumb-emoji字体的字体大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM