最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to center <div> inside a <button>? - Stack Overflow

programmeradmin1浏览0评论

I have a round < button > with a < div > inside that represents a Unicode image. Currently the button is set to border-radius: 12px; height: 24px; and width: 24px; and the < div > is to font-size: 17px. The < div > Unicode image sits inside but not centered and the button is slightly off to the side.

How can I get the < div > to center inside an oval button despite what font-size the < div > is?

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:

#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?

I have a round < button > with a < div > inside that represents a Unicode image. Currently the button is set to border-radius: 12px; height: 24px; and width: 24px; and the < div > is to font-size: 17px. The < div > Unicode image sits inside but not centered and the button is slightly off to the side.

How can I get the < div > to center inside an oval button despite what font-size the < div > is?

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:

#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?

Share Improve this question edited Sep 10, 2016 at 0:51 Jo Ko asked Sep 10, 2016 at 0:15 Jo KoJo Ko 7,57516 gold badges69 silver badges128 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 5

First off: A <div> is a block element by nature. It will always bee 100% wide. 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. Then give it a margin:0 auto; or a text-align:center on the parent to center it.

HOWEVER, You are not allowed to put <div>s inside of <buttons>. it is invalid HTML

See this answer for more information: Why can't a <button> element contain a <div>?

Or, you could read here, from W3 that only phrasing content is expected to be used within a button: https://www.w3/TR/2012/WD-html5-20120329/the-button-element.html#the-button-element

If you do not know what phrasing content is, See this page: https://www.w3/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/web/20110721191046/http://particletree./features/rediscovering-the-button-element/

Here is a fiddle of a working button like yours: https://jsfiddle/68w6m7rr/

I honestly didn't have many problems with this. I only replaced your <div> with a span, that's it.

can you post your code?

You should NOT need a div inside the button. If you need the button to have a specific style give it a class. You could do something like this

CSS:

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

HTML:

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

For clean and valid code, you'd better use a :before or :after pseudo-element. This would also take care of the centering by default.

It's even easy to set the content. Either in css only, like this:

1.

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

(put your unicode value there and classes/ids as needed, then specify them in turn in css)

2.

Or if you need to specify the value in mark-up, drop a custom data-* attribute like this:

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

with each button taking it's own value, then drop this single line in 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. Browsers will consider this invalid and will fix it by removing the block element from the inline element. For more about CSS concepts like box model, box generation please refer to these resources:

  • https://developer.mozilla/en/docs/Web/HTML/Block-level_elements#Block-level_vs._inline
  • https://developer.mozilla/en-US/docs/Web/Guide/CSS/Visual_formatting_model
  • https://developer.mozilla/en-US/docs/Web/CSS/CSS_Box_Model/Introduction_to_the_CSS_box_model

Also, if you are using an IDE, make sure you have installed linting/hinting tools to help you out. 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.

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. More on that you can find at this SO answer.

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.

The #thumb-emoji has changed to a span element. By user agent styles it has text-align:center.

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

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.

You can use then media queries on .button-group to adjust the font-size of your #thumb-emoji, by setting your desired breakpoints.

发布评论

评论列表(0)

  1. 暂无评论