Issues with HTML Anchor Buttons

I'm in the process of adding a button towards the lower section of my website.

Here is the HTML code I've used:

<center><a href="#" id="btn1" class="btn1">
    View 
</a></center>

Now, let's take a look at the CSS styling for the button:

.btn1 {
    margin: 60px 0px 60px 0px;
    color: white;
    background: #ED7836;
    padding: 10px 30px 10px 30px;
    text-decoration: none;
    font-size: 20px;
}

However, when I try to place the button, it doesn't respect the margins I have set and ends up aligning itself right at the bottom of the page.

Screenshot showing the button placement at the bottom of the page

Additionally, when I attempt to change its positioning to absolute and apply left: 50%, the button ends up being placed off-center in an awkward location.

.btn1 {
    position: absolute;
    left: 50%;
    margin: 60px 0px 60px 0px;
    color: white;
    background: #ED7836;
    padding: 10px 30px 10px 30px;
    text-decoration: none;
    font-size: 20px;
}

Screenshot displaying the misaligned button

Thank you for your assistance.

Answer №1

To perfectly center it, you must include transform: translate(-50%, 0%);.

.btn1 {
    position: absolute;
    left: 50%;
    margin: 60px 0px 60px 0px;
    color: white;
    background: #ED7836;
    padding: 10px 30px 10px 30px;
    text-decoration: none;
    font-size: 20px;
}

Explanation:

If you leave out the origin as you mentioned, it will appear as follows (which is actually correct): https://i.sstatic.net/nr0rJ.png https://jsfiddle.net/z5qo8yaj/

The key is to use translate to adjust its position relative to its origin (by half of its length). After adding translate: https://i.sstatic.net/QhsH8.png https://jsfiddle.net/h1qzdoLm/

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

fa icons moving the elements aside

I'm attempting to design a navigation menu with tabs, but I'm facing an issue where the tabs containing "fa icons" are consuming too much space, causing other elements to be shifted to the right or below (if there were more lines). How can I pre ...

IE8 - Unable to use rgba()

I'm currently facing an issue with RGBA() manipulation in jQuery while using IE 8. Here's the code I have so far: $('.set').click(function (e) { var hiddenSection = $('div.hidden'); hiddenSection.fadeIn() . ...

Faulty toggle functionality within a JavaScript-created accordion panel

HTML I'm looking to add a FAQ question within the div with the class "section-center" <body> <section class="questions"> <div class="title"> <h2>FAQ SECTION</h2> < ...

Using CSS to apply font characters as background images

Is it possible to use a font character, like one from font-awesome, as a background-image for an input element? <style> #id { background-image: content('\f2d3'); /* content instead of url() */ background-position: right center; ...

Align the number of an Unordered List to the left

Exploring UN-ordered lists in HTML has led me to wonder if it's possible for dynamically generated ul tags to display like this: * Hello * Hi * Bi * Name * Ron * Mat * Cloth * Color * Red When I ...

The fuse-sidebar elements are not being properly highlighted by Introjs

I have recently developed an angular project that utilizes the fuse-sidebar component. Additionally, I am incorporating introjs into the project. While introjs is functioning properly, it does not highlight elements contained within the fuse-sidebar. The ...

Links in words not redirecting to asp.net documents

I have a collection of Word files that require hyperlinks. These hyperlinks lead to an htm file with an anchor, however the htm file is not accessible directly via a URL for security purposes. Instead, it links to an ashx handler file which fetches the f ...

Rule for restoring scroll position upon reloading web pages in browsers

When it comes to websites that handle data asynchronously, the behavior of scroll position restoration upon browser refresh can vary. Sometimes the scroll position is preserved, while other times it is not. For example, when reloading after scrolling down ...

Connecting CSS auto-complete with JSP content within Eclipse

Is there a way to connect the CSS autocomplete with the content of a JSP page? For instance, if I have an element like <div id="myid"> in the JSP file, can Eclipse auto-complete "myid" when typing "#" in the CSS file? I am aware that NetBeans has th ...

jQuery not functioning properly when attempting to add values from two input boxes within multiple input fields

I am facing an issue with a form on my website that contains input fields as shown below. I am trying to add two input boxes to calculate the values of the third input box, but it is only working correctly for the first set and not for the rest. How can ...

Tips on accessing a child attribute value such as id when clicking with jQuery

Whenever I click on a specific li element, I want to retrieve the id of that particular li instead of its parent. Currently, when clicking on a child li, both the parent and current ids are being displayed. I only need the current li id. I am seeking assi ...

setting a div element to occupy a percentage of the window's height using viewport height units (vh) within the Wordpress platform

this is the desired appearance but upon pasting the identical code: <!-- placeholder div --> <div style="height:100px;"> </div> <!-- outer container div (with specified height) --> <div style="height:80vh;"& ...

Is there a way to invoke a function in an iframe from its parent?

How can I call a function that is in an iframe from the parent document? If the function were in the parent, I could simply use parent.func(); but since it's within the iframe, how can I still call it successfully? JavaScript keeps saying it can' ...

Position the text in the center of a graph within a grid cell by utilizing dash and plotly

Is there a way to center text over a graph within a cell of a grid table using dash and plotly? I'm struggling with the alignment currently, as the graph is not centered while the text appears centered in the view but not in the cell itself. You can ...

Sending PHP URL parameters results in a 404 error page

I'm facing an issue with an image that is meant to redirect to another page when clicked, along with sending two variables - s and n. echo "<a href='../?change-preference&s=up&n=".$element."'> <img src='..med ...

Responsive dropdown menu in Bootstrap 5 navbar

Update: I successfully fixed the desktop version. Now, I just need to work on making it responsive for mobile. The problem I am encountering is that the dropdown menu doesn't function properly on both mobile and desktop. Although the styling looks go ...

Issue TS2307: Module 'fs' or its type declarations cannot be located

I'm having trouble importing the fs module into my TypeScript component using import * as fs from 'fs'; I need it to use the writeFile method, as I want to extract data from an HTML form and write it to a JSON file. However, I am getting t ...

Is there a way to activate the action in a form's drop down list without using a button?

<form id="form1" name="form1" method="post" action="foo.php"> sort by: <select onchange="this.form.submit()"> <option value="date">sort by date</option> <option value="name">sort by name</option> <option va ...

Issues arising from script tags causing disturbances in CSS

As a newcomer to the world of web design, I recently embarked on a journey with Bootstrap and also started exploring the platform Codepen. However, after working on a project in Codepen and attempting to transfer my code to Sublime, some unexpected changes ...

Transform the initial HTML table row into a header row for every table by utilizing XSLT

My XML file contains HTML content that I need to manipulate. Previously, I used <xsl:copy-of select="customFields/customField[@name='mainContent']/html"/> to bring the content to the correct location. Now, I have a new requirement to conver ...