Is it impossible to use CSS for absolute positioning of text?

I am currently learning CSS and HTML, as I'm working on a project for my final school assignment.

My goal is to position the text "Welcome" absolutely within a specific div that I've created. However, despite multiple attempts, I can't seem to get it to align correctly with its parent container.

The desired outcome is for the "Welcome" text to appear at the bottom of the welcome div. Unfortunately, when I include bottom:0px; in the CSS code, it doesn't adhere to its intended position relative to the container and ends up being placed 0px from the top of the entire screen.

Here is the relevant code snippet:

#wrapper {
  height: 1000px;
  margin: 0 auto;
  background-image: url(images/background.jpg);
  background-size: 100% 100%;
}

#header {
  height: 150px;
  position: relative;
  background-color: red;
}

#welcome {
  position: absolute;
  bottom: 0;
  width: 420px;
  height: 100px;
  background-color: green;
}

.w {
  height: 150px;
  position: absolute;
  font-size: 64px;
  left: 20px;
  bottom: 0px;
  color: #fff;
}
<div id="wrapper">
  <header id="header">
    <div id="welcome">
      <p class="w">Welcome</p>
    </div>
    <nav id="main nav"></nav>
  </header>
</div>

Answer №1

You are so close to the solution! Just make sure to take away the height from the .w paragraph tag and also remove its margin:

#wrapper {
  height: 1000px;
  margin: 0 auto;
  background-image: url(images/background.jpg);
  background-size: 100% 100%;
}

#header {
  height: 150px;
  position: relative;
  background-color: red;
}

#welcome {
  position: absolute;
  bottom: 0;
  width: 420px;
  height: 100px;
  background-color: green;
}

.w {
  /*height: 150px;*/
  margin: 0;
  position: absolute;
  font-size: 64px;
  left: 20px;
  bottom: 0px;
  color: #fff;
}
<div id="wrapper">
  <header id="header">
    <div id="welcome">
      <p class="w">Welcome</p>
    </div>
    <nav id="main nav"></nav>
  </header>
</div>

Answer №2

The issue, pointed out by CalvinNunes, is that the .w div has a height set. Additionally, p elements have default values for margin and line-height. To resolve this, you should eliminate the margin and adjust the line-height to be less than 1 (e.g., .5 will bring the text in contact with the bottom of the green box).

#wrapper {
  height: 1000px;
  margin: 0 auto;
  background-image: url(images/background.jpg);
  background-size: 100% 100%;
  position: relative;
}

#header {
  height: 150px;
  background-color: red;
  position: absolute;
  width: 100%;
}

#welcome {
  position: absolute;
  bottom: 0;
  width: 420px;
  height: 100px;
  background-color: green;
}

.w {
  position: absolute;
  font-size: 64px;
  left: 20px;
  bottom: 0px;
  color: #fff;
  margin: 0;
  line-height: 1;
}
<div id="wrapper">

  <header id="header">

    <div id="welcome">
      <p class="w">Welcome</p>
    </div>

    <nav id="main nav">
    </nav>

  </header>

</div>
<!-- End of wrapper-->

Answer №3

When applying the absolute property to an element, it is important that the related DOM element has a positioning of either relative, absolute, or fixed, depending on the desired layout. Additionally, make sure to review the styling of your absolute element to avoid any unnecessary margins.

In your specific scenario, I don't believe using absolute positioning is necessary. Instead, consider increasing the padding of the parent element at the top. Another alternative could be utilizing flex-end, which provides flexibility for dynamic text input.

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

Graphic descending within container

Struggling to design some divs for image display, I encountered a problem where the image shifts down by 3 pixels. It seems this is due to a 3 pixel margin on the container div, but I'm puzzled as to why it affects the image position. padding:0; marg ...

What is causing the 'transitionend' event to trigger multiple times?

Currently, I'm honing my JavaScript skills by taking on the 30 days of JavaScript challenge. I'm puzzled by why the 'transitioned' event is triggered twice in the code snippet below. My CSS only contains one property called transform, ...

Having trouble getting a DIV to appear on top of another DIV containing an image

I need help with positioning an "InfoBox" above the main header image on my website. Despite trying z-indexes and adjusting position, I can't seem to get it right. If anyone has any suggestions, please share. Thank you! For reference, here is the HTM ...

Troubleshoot: Difficulty selecting elements in OnsenUI with jQuery or JavaScript - potential ons-template problem?

Just starting out with OnsenUI and I'm really liking the overall look and feel of it so far. Working with plain JS and a bit of jQuery. The problem I'm facing is that I can't seem to select anything (ID, Class etc.) using JS or jQuery while ...

Developing a custom mixin to alert a banner at the top of the webpage

I currently have a feature on my website that triggers a pop-up notification whenever a message is received from another user. The pop-up consists of a div displaying the content of the message along with a close button. After exploring various methods to ...

H1 and span elements experiencing abnormal size discrepancies

Check out this code snippet: <h1><a href="">Windows App Store<br /><span class="smallSubText">applications</span></a></h1> and here's the CSS styling: #windowsStoreApplications { float: right; width ...

How can JavaScript be used to rearrange the placement of DOM elements?

<!DOCTYPE html> <html> <head> <title></title> </head> <body> <div class="boxes"> <div class="red" style="width: 300px; height: 300px; color: red"></div> <div class="blue ...

Trouble arises in next.js containers when their content exceeds the specified limits due to @media rules adaptation

I've been working tirelessly for the past 2 days to resolve an issue with my next.js project. This is my first time using this technology and I feel like I might be overlooking something crucial. The problem revolves around three layers (as seen in t ...

Enhancing Visuals with src="imageUrl within Html"

Is there a way to customize the appearance of images that are fetched from an API imageUrl? I would like to create some space between the columns but when the images are displayed on a medium to small screen, they appear too close together with no spacing. ...

What is the code to incorporate a color using PHP?

Question: Is there a way to indicate approval with green color and decline with red color? I've searched for a solution but couldn't find one. PHP Code: <?php if(isset($_POST['approve'])) { $msg = "Approved"; ...

Troubles with modal functionality in Ionic application involving ion-slide-box

Utilizing ion-slider to display images has been a seamless experience, except for one hiccup. If I navigate directly from the first full image back to the home screen, the slider ceases to function properly. To address this challenge, I have employed spec ...

The back button fails to refresh the page on a website utilizing Ajax technology

Recently, I implemented AJAX on a client's website to introduce some smooth animations for page transitions. When navigating from the homepage of firedogcreative.com to the edit page, and then to one of the work pages, we see a history like this: fir ...

ng-click is not triggering my function

I'm really struggling to understand how AngularJS and Typescript can work together efficiently. My main goal is to make a simple method call, but I seem to be stuck due to some constraints in the architecture I have chosen. I must have made a mistake ...

Modify the input field for weight in WooCommerce to accommodate alphabetic characters

Is there a way to modify the WooCommerce Shipping weight field so that it can accept alphanumeric values, like: 500g / 1kg / 750ml / 2L I only need the weight for informational purposes and don't require any shipping. The location of the file re ...

Incorporating PHP script within a stylesheet

Is it possible to include php code within a css file for adding conditions to css properties? styles.css <?php if($suserid == 2) { ?> .proverb { border:0px solid blue; margin-left:34px; width:250px !important; margin-top:6px; } <?php } e ...

Simply click and drag a document from your file explorer directly into the desired text field to instantly generate a clickable

I am interested in dragging a file from our Windows server and dropping it onto a text area on a webpage. The link that would be generated will look something like this: <a href="\\fileserver\folder\pizza_2.pdf">filename.pdf< ...

Building a loading bar using dots

<span class="dot"></span> <span class="dot"></span> <span class="dot"></span> <span class="dot"></span> <span class="dot"></span> <span class="dot"></span> <span class="dot">< ...

What is the preferred method of compiling Sass: utilizing the Live Sass Compiler extension in VS Code, or setting up and running Sass through npm? (Including guidance on transitioning from node-sass to dart-sass)

As I delve into an online course focused on Advanced CSS and Sass, I have noticed that the techniques being taught seem a bit outdated. The course heavily relies on node-sass in its dependencies, which is now considered deprecated. An alternative solution ...

Using either Javascript or jQuery, I want to set a value within an if statement

Can I set a value within an if statement, like this: if (a = jQuery("#user > .mask.fix > .a1").css('display') != 'none'){ b = a + 'hello'; //b=jQuery("#user > .mask.fix > .a1").css('display')+&apos ...

Resetting form fields when clicking the "Next" button with the FormToWizard jQuery Plugin

I am in the process of developing a lengthy form using the jQuery plugin called formToWizard. Within one of the fieldsets located midway through the form, there are hidden fields that are displayed upon clicking a button. The strange issue I am encounterin ...