Is there a way to center-align a link?

I've been attempting to center align a link on my website. This is the code snippet I have so far:

    <div class="cont">
  <div class="form sign-in">
    <h2>Welcome back,</h2>
    <label>
      <span>Email</span>
      <input type="email" />
    </label>
    <label>
      <span>Password</span>
      <input type="password" />
    </label>
    <a href="facebook.com"><span class="forgot-pass">Forgot password?</span></a>
    <button type="button" class="submit">Sign In</button>
    <button type="button" class="fb-btn">Connect with <span>facebook</span></button>
  </div>

Below is the CSS styling for the "forgot-pass" class:

.forgot-pass {
   margin-top: 15px;
   text-align: center;
   font-size: 12px;
   color: #cfcfcf;
}
    .cont {
  overflow: hidden;
  position: relative;
  width: 1200px;
  height: 550px;
  margin: 0 auto 100px;
  background: #fff;
}
.form {
  position: relative;
  width: 640px;
  height: 100%;
  transition: -webkit-transform 1.2s ease-in-out;
  transition: transform 1.2s ease-in-out;
  transition: transform 1.2s ease-in-out, -webkit-transform 1.2s ease-in-out;
  padding: 50px 30px 0;
 }

I tried using the following HTML code:

<a href="facebook.com"><span class="forgot-pass">Forgot password?</span></a>

The CSS class .forgot-pass was applied successfully, except for the text-align:center. Is there something I am missing or an alternative way to achieve center alignment?

Answer №1

Insert the link in the p tag, without the need for a span in your CSS:

.forgot-pass {
  margin-top: 15px;
  text-align: center;
  font-size: 12px;
  color: #cfcfcf;
}

.forgot-pass a:link,
.forgot-pass a:visited {
  color: #cfcfcf;
}

.forgot-pass a:hover,
.forgot-pass a:active {
  color: gold;
}
<p class="forgot-pass"><a href="facebook.com">Forgot password?</a></p>

Answer №2

Your second scenario presents a challenge due to both elements being inline elements. Inline elements are designed to take up only as much horizontal space as needed by their contents. Consequently, applying text-align: center; won't have a visible impact since the text will simply be centered within the element itself, which is the same size as the text. To truly center the text on the page, you'll need to either position the element manually (e.g. position: absolute) or change it to a block element.

To effectively center the link on the page, use the following code:

.forgot-pass {
   margin-top: 15px;
   text-align: center;
   font-size: 12px;
   color: #cfcfcf;
   display: block;
}

<a href="facebook.com" class="forgot-pass">Forgot password?</a>

Answer №3

    .forgot-pass {
       margin-top: 15px;
       text-align: center;
       font-size: 12px;
       color: #cfcfcf;
      display: block;
    }

<a href="facebook.com"><span class="forgot-pass">Forgot password?</span></a>

Add the CSS property 'display: block' to the span class for better formatting

.forgot-pass {
  margin-top: 15px;
  text-align: center;
  font-size: 12px;
  color: #cfcfcf;
  display: block;
}
<a href="facebook.com"><span class="forgot-pass">Forgot password?</span></a>

Answer №4

You have the option to implement something similar to this

<body>
    <p class="reset-password">
        <a href="facebook.com">Forgot password?</a>
    </p>
</body>

.reset-password {
            margin-top: 15px;
            text-align: center;
            font-size: 12px;
            color: #cfcfcf;
        }

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

What is the process for altering the CSS path within an HTML helper in CakePHP 3.x?

I am trying to access the admin-css: echo $this->Html->css('adminStyle'); <link rel="stylesheet" href="/css/adminStyle.css" /> However, I would like it to be as follows: <link rel="stylesheet" href="/admin/css/adminStyle.css" /& ...

Unable to apply animation to a .png file

Looking to add a cool animation to an image that gradually enlarges it by 10% until it reaches its original size (100%). The animation starts with the image being small and then expands as its top, left, bottom, and right properties increase. Let me know i ...

Recall input values from form field to use on another page

I have "Page 1" and "Page 2". When I navigate from "Page 1" to "Page 2" by clicking the button labeled "Go To Page 2", I also encounter an input field with a placeholder text of "This field right here" on "Page 1". On "Page 2", there is a button named "G ...

How come I am able to reference a component in styled-components while others cannot?

When using styled-components, you can reference another React component using the syntax ${Label}. However, I have noticed that it only works with certain components and not others. Despite reading the documentation at , I encountered an issue when trying ...

Is there a way to create a "navigate to" button directly from the text within a <p> element?

Within my HTML, there is a <p> tag which dynamically receives a link value from a JavaScript function. The link could be anything from www.google.com to youtube or instagram. How can I utilize the "copytoclipboard" library to copy this link to clipbo ...

Issues with updating the style in Internet Explorer and Firefox using JavaScript are currently unresolved

I am facing an issue with my program where a name from an auto-complete is sent to a JavaScript function that dynamically creates a label with a button inside. Surprisingly, the DOM methods used to set style properties do not work in Firefox and IE 7, but ...

The CSS hamburger icon displayed in the browser features three bars of varying heights

Looking to create a hamburger icon menu using only CSS? Check out my implementation below which includes three span tags in the HTML document. .sidebar-toggle { display: inline-block; } .sidebar-toggle span { display: block; width: 1.5rem; heig ...

Issue with ngRepeat directive

I'm attempting to display an array in a table Below is the relevant code snippet js: var d = [[ '12:00', 'X-Files', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis non nisi in velit tristique scelerisque. ...

Create a stunning visual on the canvas using the high-resolution Retina display

Working on a web App in phonegap and trying to use a 320 by 480 image for drawing, but it appears fuzzy. html <canvas id="canvas" height=480 width=320> Your browser does not support the HTML5 canvas tag.</canvas> javascript var canvas = doc ...

Tips for switching back and forth with JQuery

I have an accordion element that opens on each click, but I am wondering how to close it back again? Here is an example of the HTML structure: <ul class="accordion"> <li id="one" class="files"> <a href="#one">Calendar 1<span& ...

Strangely behaving animated mobile navigation glitch

CodePen showcasing the mobile navigation Upon initial interaction with the mobile navigation, it operates as expected. However, a bug arises with subsequent interactions. The navigation either opens and closes instantly or the links appear in a jumbled or ...

Attempting to conceal image formatting when an image is not available - ACF images customization

When designing my Content Page Template, I added borders, backgrounds, and box shadows to style the images. However, if the optional image field is left empty, it still displays a small box with the css styling. I have been trying to figure out how to hide ...

Setting the `setCustomValidity()` method for dynamically inserted HTML elements Explanation on how to use

I am dynamically adding elements to my view and setting their attributes using the jQuery attr function as demonstrated in the code snippet below: var input = $("<input type='text' name='"+inputFieldName+"' '>"); input.attr( ...

Styling using CSS is effective only when applied locally, as it does not function properly on GitHub Pages

Although I have come across similar questions to the one I am facing, none of the solutions provided seem to work for me at the moment. My CSS file loads and functions as expected when tested locally, but once I upload it to GitHub, it stops working. In my ...

Modify the color of the FA SVG icon when the input is in focus

Have you ever wanted to modify the color of fa-svg icons when the input field is focused? If you are using MDB (Material Design for Bootstrap), then achieving this customization is quite possible. Check out the HTML code snippet below: <div class="col ...

How about incorporating a cool card flip animation into this JavaScript card game?

I've been experimenting with a card game and I'm interested in adding a card flip animation to it. I searched online for solutions, but most of them involve jquery (which I'd rather not use for just one animation) or CSS3 (I found this exam ...

The issue with radio preventDefault() function not functioning properly has been identified to occur specifically on Chrome and Firefox 3.6 browsers, while it seems

Check out a live demonstration. Below is the html code I am working with: <div class="Q"> <div id="Q3"><span>1. </span>Which of the following is a string?</div> <div class="A"><input type="radio" id="Q3A1Correct" ...

Modifying the Scroll Bar's Color within an iframe

Is it possible to customize the color of the horizontal scrolling tab in an iframe? I am curious if this effect can be successfully implemented across all browsers. Your insights on this matter would be greatly appreciated. ...

What is the best way to ensure that my transitionend event is triggered?

I'm currently exploring the <progress> element and attempting to incorporate a CSS transition to achieve a smooth progress bar effect as its value increases. Despite my efforts, I can't seem to trigger JS after the transitionend event occur ...

Is it true that every time we call canvas.getContext("2d"), it returns the same instance?

Can I rely on canvas.getContext("2d") to consistently return the same context instance whenever it's called? I'm seeking clarification because I am attempting to implement the guidance provided in this response to prevent my scaled canvases from ...