Text cannot be aligned

I recently encountered an issue where I center-aligned some text and positioned it using top:20%. Everything was working fine until I added a paragraph under it. At that point, the element moved back to its original position and the top:20% positioning didn't take effect.

    <style>
    .yes{
            width : 100%;
            height : 50%;
            background-color: red;
            text-align: center;
        }
        .yes a{
            font-size : 300%;
            position: relative;
            top : 20%;
        }
    </style>

    <div class = "yes">
    <a>Title</a>
    <p></p></div>

Update: Strangely, on jsfiddle, the div does not display as 50% in height which makes it difficult to see the issue I'm facing. Here is a link to view the code. If you remove the p tag, you will be able to observe the effect clearly:

Answer №1

Looking to test and confirm the behavior? The code snippet provided functions well in the latest version of Chrome. There is no movement of the title after the paragraph is added.

UPDATE: The issue described can be replicated when using a tall wrapper. The solution is simply to set the a element's display property to block.

    .wrp{height:400px;}
    .yes{
            width : 100%;
            height : 50%;
            background-color: red;
            text-align: center;
        }
        .yes a{
            display:block;
            font-size : 300%;
            position: relative;
            top : 20%;
        }
<div class="wrp">
    <div class="yes">
        <a>Title</a>
    </div>
</div>
<hr>
<div class="wrp">
    <div class="yes">
        <a>Title</a>
        <p></p>
    </div>
</div>

Answer №2

It's important to remember that styles should be enclosed within a class tag, not an id tag.

<html>
<head>
    <style>
        .example{
            width : 100%;
            height : 50%;
            background-color: red;
            text-align: center;
        }
        .example a{
            font-size : 300%;
            position: relative;
            top : 20%;
        }
    </style>
</head>
<body>
    <div class="example">
        <a>Title</a>
        <p></p>
    </div>
</body>
</html>

Check it out in action

The key difference here is: Ids should be unique and only assigned to one element in the document, while classes can be applied to multiple elements in the DOM.

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

The columnFilter plugin in Datatables is failing to initialize

I have a pre-existing table that needs to be customized and initialized properly. <table id="currencies-table" class="table table-striped table-bordered table-hover form-data-table"> <thead> <tr> <th style="width: 10px;" ...

Only apply the CSS rule to all pages except for one

I'm a beginner when it comes to css. In my wordpress website editor (Onetone theme), I added the following rule to my 'custom css field': html, body {margin: 0; height: 100%; overflow: hidden} It's working well...the scrollbar is gone ...

Steps for executing a function when a directory is clicked

Currently, I am working on creating a file browser and facing some issues with calling a function when the user clicks on a directory. Initially, I tried using onClick inside an href in the class part (right before FUNCTION folderSize), intending to open ...

showcasing the picture in the email is consistently

I have created an email system that sends a message to users when they are active in the database, but I am experiencing an issue with the image not displaying. It is important for the image to appear automatically, just like Facebook's logo always sh ...

Styling with CSS and using templates

Just had a quick question about CSS and a template I'm working with. Currently, I have these two fields in my CSS: .content { padding:10px; width: 100% text-align:justify; font: 9pt/14pt 'Lucida Grande', Verdana, Helvetica, sans-serif; } ...

Text overlapping image causing hover state interference

I'm feeling completely lost right now. My goal is to have the title of each project displayed in the center of the screen when you hover over the respective project images. I've resorted to using jQuery for this because it seems like the most str ...

A guide to accurately accessing form data in Jquery Ajax requests

My beforeSend function is not working properly, as the background color does not change. I suspect it may be due to not correctly referencing the id variable posted from the form. Below is the relevant HTML and JS code: HTML <div id="rec<?php echo ...

Ways to ensure that a div, header, container, and other elements completely cover the viewport

Currently, I am attempting to create a header that spans the entire viewport regardless of screen size. This header will be split horizontally into 3 equal sections, each occupying one third of the header space. I experimented with setting the header hei ...

Fonts displayed on websites may look different on Mac compared to Windows

After carefully selecting a Google Web Font to use as the main font for my website, I noticed that it appears slightly different in terms of dimensions and spatial conformations when displayed on both Mac and Windows computers. For more information about ...

Dynamic CSS view size parameter

Currently, I am studying how to create responsive designs using CSS. I decided to test out some example code provided on the w3schools website (https://www.w3schools.com/css/tryit.asp?filename=tryresponsive_webpage). However, I encountered an issue where ...

What is the best way to show a left and right arrow on a mobile website page?

I'm experiencing a strange issue with my webpage, specifically a post in Wordpress. When I try to insert the left right arrow character (U+2194) either by copying from a character map or using the html code, I end up with an icon instead of the actual ...

The functionality of anchor links created through ajax is not operating correctly

Issue: I am encountering a problem where I have a table filled via Ajax, containing local anchors. When an anchor is clicked, it should make a section visible and automatically scroll to it. This functionality works when manually filling the table, but not ...

Tips on altering hover effects?

I am having trouble changing the font size and color of the a when hovering over p. I have been trying to figure this out for a while now, but I can't seem to get it to work. If anyone has any simple links related to this topic, I would greatly appre ...

Guidance on utilizing jQuery to display values depending on a dropdown selection

This code snippet displays movie data from a JSON variable in a dropdown list based on the selected city. It also needs to show the movie name and theaters list when a user selects a movie, along with the theater dropdown remaining unchanged. Here is my H ...

Ensure HTML5 video fills window completely without any overflow

I'm currently developing a new open-source alternative to Plex and I am facing some challenges with the in-browser video player. My goal is to have the video player maximize its size within the window during playback, similar to what Chrome and Netfli ...

The navigation and title refuse to align in the center

Learning coding from scratch is a challenge, especially when tasked with creating a website for school. Right now, I'm gradually getting the hang of it, but I am struggling to center my navigation and title. Everything looks fine on my 13-inch laptop, ...

Chrome not displaying Bootstrap dropdowns correctly

Lately, I've been exploring Bootstrap and experimenting with its dropdown menus. Typically, I use Chrome for my work but encountered a strange issue with how Chrome is handling my dropdown menus. This forced me to temporarily switch to Edge. Here&apos ...

What could be the reason my CSS and Javascript animation is not functioning properly?

Currently working on creating a dynamic animation menu using CSS and Javascript, but encountering some issues. The concept involves having a menu with initially hidden opacity (using CSS), which becomes visible when the hamburger menu is clicked, sliding i ...

Is it possible to animate the change in background color dynamically using jQuery?

I am struggling to implement a change/animate feature for the background color. When I place the background script within the form submit event but outside the ajax call, I can briefly see the color change. However, once the remote content loads, the colo ...

What could be causing the issue with the width: 100% not rendering correctly in this

I'm currently working on my footer, but I'm having trouble getting the width: 100% tag to work correctly. Here's a screenshot of what I mean: http://gyazo.com/9c23897cd7b9a74ca55ee9fb977f810c This is my CSS code: /*..Portfolio - Lars Beut ...