center a "button" vertically

I have created a transparent "button" and positioned it on a full-width background image using the class within an <a> tag. I want the button to be centered responsively on the image, achieving horizontal centering but not quite vertical alignment (my goal is to create a single page layout). Here is the HTML:

<div class="container">
            <div class="nav">navigation area</div>
            <div class="bg-image">
              <a class="button-transp abs" href="#">Button text<br>second line<br>third</a>
                <img src="bg_top.jpg">
                 <h1>This text is centered.</h1>
            </div>
            <div class="main">main content area</div>
</div>

The CSS code:

.abs {
  position: absolute;
  z-index: 2;
margin: 25% auto 0;
  left: 0;
  right: 0;
}
.button-transp {
  display: inline-block;
  min-width: 170px;
  max-width: 500px;
  width: 30%;
  padding: 8px;
  color: #fff;
  background-color: transparent;
  border: 2px solid #fff;
  text-align: center;
  outline: none;
}

To achieve some vertical alignment, I utilized margin: 25% auto 0;

Answer №1

If you want the button to remain centered responsive to the image, consider using a background image. Position the button 50% from the top, 50% from the left, and then pull it back by -1/2 of its height to the top and -1/2 of its width to the left. This ensures that the button always stays in the center of the bg-image.

<div class="container">
    <div class="nav">navigation area</div>
    <div class="bg-image">
       <a class="button-transp abs" href="#">Button text<br>second line<br>third</a>
       <h1>This is centered text.</h1>
    </div>
    <div class="main">main area</div>
</div>

.abs {
    position: absolute;
    z-index: 2;
    top: 50%;
    margin-top: -37px;  /** adjust half of the button's height to create equal spacing */
    left: 50%;
    margin-left: -95px; /** adjust half of the button's width */
}
.button-transp {
    display: inline-block;
    min-width: 170px;
    max-width: 500px;
    width: 30%;
    padding: 8px;
    color: #fff;
    background-color: transparent;
    border: 2px solid #fff;
    text-align: center;
    outline: none;
}
.bg-image {
    background: url(http://www.online-image-editor.com//styles/2014/images/example_image.png) no-repeat 0 0;
    width: 475px;
    height: 360px;
    position: relative;
}

View on JSFiddle

Answer №2

Condensing the layout, this method is versatile for buttons of any size and ensures they are always centered both horizontally and vertically.

CSS3 transformations are supported from IE9 onwards (with appropriate prefixes if needed).

.bg-image {
    height: 200px;
    background: orange;
    position: relative; /* positioning contexr */
}

.button-transp {
    width: 30%;
    white-space:nowrap;
    padding: 8px;
    color: #fff;
    background-color: transparent;
    border: 2px solid #fff;
    text-align: center;
    outline: none;
    position: absolute;
    z-index: 2;
    top:50%;
    left: 50%;
    transform:translate(-50%,-50%);
}
<div class="container">
    <div class="bg-image">
        <a class="button-transp abs" href="#">Button Text<br/>Second Line<br/>Third</a>
    </div>
</div>

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

When I hover over the navigation bar, a submenu pops up. However, as I try to navigate to the submenu, I often end up accidentally selecting a different item on the navigation bar

I'm trying to find an answer but can't seem to remember where I saw it. Here's the situation: a horizontal nav bar with a dark blue selection and a red sub-menu. When the user hovers over a black arrow, it crosses into a light-blue nav item ...

Mapbox map rendering problem in React with 100% high resolution

Currently working on rendering a map using Mapboxgl, Bootstrap 4, and React. The goal is to make the map take up 100% of the page height and display in the first column of a two-column grid. However, there's an issue when using React where the width ...

Using `margin: auto;` alone will center a div horizontally, but it won't

My goal is to perfectly center the text within the anchors in my li container, both horizontally and vertically. I've come across a method that involves using text-align: center; on the container for horizontal alignment. However, vertical alignment r ...

What is the method for placing a badge above a Font Awesome icon?

Is it possible to add a badge with numbers like 5, 10, or 100 on top of the Font Awesome symbol (fa-envelope)? I am looking for something like this: However, I am struggling to figure out how to place the badge on top of the symbol. You can see my attempt ...

Empty space to the left of the vertical navigation bar

I'm struggling to get rid of the white space on the left side of my vertical navigation bar. I've attempted setting padding-left to 0 on my main navigation bar. This is my first time creating a navigation bar, so if you notice any semantic issu ...

Incorporating HTML elements within JSON data

I have validated my JSON Data using JSONLint and here it is: [{ "text": "Products", "data": {}, "children": [{ "text": "Fruit", "data": { "price": "", "quantity": "", "AddItem": "&#43;", "RemoveItem": "&#215 ...

Is it possible for me to dynamically alter the innerHTML of an input element using

I am working on a small Angular application. One of my components uses innerHTML to display content. <div [innerHTML]="serviceShared.currentMood"></div> However, when I change the value of serviceShared.currentMood in the main component, noth ...

I continuously encounter "undefined" when attempting to retrieve collections

Despite my best efforts, I am unable to retrieve the data from the server using the snapshot docs. I have verified that the collection is named "creaciones" in lowercase. There is one document in the collection with existing data. I have double-checked fo ...

Dealing with Unintended CSS Hover Effects That Just Won't Quit

I have a div that is styled as a circular shape with an image and text centered inside of it. The circle and image are visible while the text is transparent until hovered over. When hovering, the circle border flashes, the image's opacity decreases, a ...

Tips on retrieving data from a php dropdownlist filled with values from a mysql database

After spending significant time scouring the Internet and rewriting various scripts, I have yet to find a satisfactory solution to my dilemma. Even after searching this platform for similar inquiries, it appears that most discussions center around displayi ...

Enhancing page accessibility through the implementation of shortcuts

Greetings to all members of the community! I am currently in the process of improving a website by implementing better accessibility practices. I have some concerns regarding a page that displays a large number of repeated result blocks, each containing ...

Place a div in a location where it seems to be positioned beyond the main wrapper's width of 980 pixels

Is there a way to position a div outside the main wrapper (980px) of my website without causing horizontal scrollbars in the 1024px screen size? I'm open to solutions using CSS or jQuery. Any suggestions? The segment that needs to be outside should ...

Is there a way to create a button in an HTML project that will launch the user's email client?

Looking for some guidance on coding a homepage with HTML and CSS as I navigate my way through the world of web development. Take a look at this screenshot for reference: In the Jumbotron section, you'll notice that I have integrated a couple of boot ...

Performing a function call in Angular2 directly from an HTML element without the need for a load event or similar event

Being a beginner in Angular, I have encountered an issue that seems to require a javascript workaround which is not very elegant. I am dealing with a model that has an array property. I am using ngFor to generate HTML selection options based on this prope ...

Leveraging an array of Elasticsearch documents using AngularJS

The query results are given below... { "took": 6, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 45, "max_score": 0, "hits": [] }, "aggregations": { ...

Distinguishing appearances among various web browsers

How come the menus look different in Firefox compared to Chrome, Safari, and IE? I've tried adjusting the CSS but can't seem to get it just right. Any suggestions on how to fix this? In Firefox, there's an overlapping part at the bottom of ...

Space within a receptacle

As a newcomer to bootstrap5, I am encountering whitespace issues on the left side of my screen. While everything looks fine in full-screen mode, the whitespace around the image persists when I test responsiveness. https://i.sstatic.net/gZTq5.jpg Despite t ...

What are some alternative ways to utilize jQuery siblings() without restricting it to the same parent element?

In the code provided below as Non-functional code [1], it will function correctly with siblings() to disable other input fields if their sum or amount exceeds a specified maximum value. This works effectively when all input fields are within the same paren ...

Is the CSS code example provided on the JSXGraph website functioning correctly?

I did my best to replicate this example from the JSXGraph website: Below is the condensed HTML: <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Covid Sandbox</title> < ...

The issue with the Ajax infinite scrolling gallery is that it repeatedly displays the same images multiple times

I'm currently working on developing a straightforward gallery page that dynamically adds elements as you scroll to the bottom. To achieve this, I am using Django to render an HTML snippet from the backend and then utilizing JavaScript to append it to ...