Content that is centered when utilizing border-image/border-width

I need assistance in creating a stylish button with a single centered character inside of it.

Currently, I am utilizing "border-image" and "border-width" to design a button that can expand to accommodate larger content. However, when the button size is reduced (specifically, when it is only slightly larger than 2*border-width), the content is not aligned at the center. Despite trying traditional methods like using margin: 0 auto;, I have been unable to resolve this issue. Interestingly, when I utilize a dumb-button class without these border properties, I achieve the desired outcome.

To illustrate the problem, please refer to the example link below:

http://jsfiddle.net/rjmLy/

(Compatible with Chrome/Safari, designed for Webkit browsers only) (Example of themed button from )

My current CSS styling is as follows:

.fancy-button {
    border-image: url(http://girliemac.com/sandbox/images/alert-button.png) 0 14 111 14 fill repeat repeat;
    border-width: 0 14px;
    box-sizing: border-box;
    font-size: 16px;
    height: 37px;
    line-height:37px;
    text-align: center;
    margin: 0 auto;
    width: 28px;
}
.centerme {
    position:relative;
    margin:0 auto;
}
.dumb-button {
    font-size: 16px;
    height: 37px;
    line-height: 37px;
    text-align: center;
    width: 28px;
    background-color: red;
    margin: 0 auto;
}

The HTML structure is shown below. The centerme class was an attempt to center a new div on top of the existing shape, but it did not yield the desired result. The dumb-button versions appear correctly centered but lack visual appeal.

<div class="fancy-button">I</div>
<div class="fancy-button">W</div>
<div class="fancy-button"><div class="centerme">W</div></div>

<div class="dumb-button">I</div>
<div class="dumb-button">W</div>
<div class="dumb-button"><div class="centerme">W</div></div>

Your assistance on this matter would be highly appreciated.

Answer №1

If you're using a CSS3 Sprite and a border-image attribute to create a button, make sure the width of the button allows enough space for any text inside. For example, if the border image starts 14px wide on both sides, set the button's width to at least 42px.

If you prefer to keep the images small, you can wrap each letter in a <div> and manually adjust the margins. This may require tweaking for each individual letter due to varying widths.

Check out this fiddle for a demonstration: http://jsfiddle.net/QYrzS/

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Adjust the appearance of the scrollbar with custom CSS, but be aware that the typical methods may not be effective in

I am interested in customizing the appearance of scrollbars on my website. After coming across this tutorial on W3Schools titled W3S: How TO - Custom Scrollbar, I successfully applied the changes on Chrome, but unfortunately, it did not work on Firefox. H ...

What is the best way to ensure that the input areas stretch all the way to the edge of the screen?

This form is structured similar to Bootstrap: HTML: <div class="container"> <div class="row"> <div class="form-group row-group"> <label class="col-6">First name</label> <input class="col-6" type="text" v ...

The auto-complete feature is malfunctioning after retrieving input suggestions from a PHP file

My goal is to achieve the following: 1. Create a list of suggestions with same labels but different descriptions. 2. Upon selecting an item from the list, the input field of another item should change to the corresponding description of the selected item. ...

Creating unique buttons for your PHP poll

I am designing a website for my friend and myself. The main feature of our site is a poll that asks users to choose between two options. Currently, I have set the input type as radio buttons where users can click on a circle to make their selection. Howeve ...

Is fitVids not working as expected with your setup?

Seeking help in resizing my video using fitvids to fill the parent area as much as possible. Check out my fiddle below: http://jsfiddle.net/LkqTU/9513/ Code snippet: $(".video-content").fitVids(); HTML structure: <div class='liveExampl ...

Validating an app engine using both `django` and `css` technologies

After reviewing various references, I have been struggling to find an efficient way to connect everything. The issue arises when trying to send back the form and its contents in case the form is invalid. Currently, I am extracting each generated form item ...

Change the order of numbering in ordered lists

I am seeking a way to change the ordering of an ordered list to be in descending order. You can view a live example here. Instead of having the counter span multiple ol elements, I would like the counter to reset after each ol. For the live demo, the des ...

Exploring the Pathways of a Website's Architecture

Recently, I stumbled upon a website that piqued my interest. I am looking to send POST requests to it and then retrieve the HTML code from subsequent GET requests. Is there a way for me to uncover the organization of these routes? My plan is to perform m ...

Disappear from Sight: Content Concealed as Window Size Changes

Struggling with resizing text and buttons on a website built from a template for class. Looking to ensure that the text remains visible even when the window size changes. Ideally, I want the page content to stay anchored so the focus remains on the text/b ...

Improve loading speed of thumbnail and full images in HTML

I'm struggling with slow loading images on my blog website. Can anyone help me figure out how to improve loading speed and: Is it possible to use a separate thumbnail image that is smaller in size instead of the full image for both thumbnails and th ...

What is the proper method for filling a custom shape that has been created in Canvas?

I am attempting to create a custom shape, but I am facing an issue where using moveTo prevents me from filling it. Is there any method to determine points on the screen to fill a shape? Or should I use or draw another real shape in the same layer block? T ...

Full height container with content that can be scrolled (Tailwind)

I am designing a webpage that requires two containers stacked on top of each other with some space in between. The bottom container should occupy the remaining screen space accurately, neither more nor less. Inside this lower container, there will be fixed ...

Data tables that are able to scroll horizontally without the need for any framework and

Seeking a responsive table solution similar to Bootstrap, but without using Bootstrap's Javascript and CSS due to the website being built from scratch with no frameworks. Check out this demo: http://www.w3schools.com/bootstrap/tryit.asp?filename=tryb ...

Web page glitch repaired: Top fixed div now functioning properly

I am currently facing an issue with a fixed header and full-screen background image. The problem arises when I try to display an alert on top of the page, pushing down the header and hero section. However, I am encountering difficulties as the alert eith ...

Matching Javascript Variables to CSS Styles

I am currently developing a small HTML page and I am utilizing JavaScript to retrieve the screen dimensions. However, I am facing an issue with passing this variable to a CSS style as shown below. How can I achieve this successfully? Additionally, I have ...

Stop the automatic display of the dropdown menu

How can I prevent the dropdown menu from automatically showing when I remove the 'hidden' class in jQuery: Sample HTML: <div class="btn-group btn-group-xs friend-request tooltiped" data-toggle="tooltip" title="Add as friend"> <button ...

How can I create a line-image-line in CSS that is less than 100% wide and remains responsive?

How can I create a similar design using HTML/CSS? https://i.sstatic.net/o5xzn.png Solution requirements: An icon or small image in the center. Responsive design that works with current breakpoints. The line should not span 100% width of the screen. I ...

Is there a way to navigate to a specific element inside a div with overflow-y enabled?

I have a div with a max-height of 300px, causing a scrollbar to appear when the content exceeds this limit. I am seeking a way to click a button and automatically scroll to a specific element within that div. While I know how to manipulate the main browser ...

What steps can I take to implement a functional 'echo' command within a Jquery terminal?

Trying to create an echo command that displays what the user types in a JQuery terminal. Here is the code snippet: <link href="https://unpkg.com/jquery.terminal/css/jquery.terminal.min.css" rel="stylesheet" /> <script src="https://cdnjs.clou ...

Checkbox and Ionic avatar image combined in a single line

I am currently working on a mobile app using the ionic framework. I would like to create a layout with an avatar image on the left, text in the middle, and a checkbox on the right. Can anyone provide guidance on how to achieve this? The code snippet below ...