Increasing the reach of the content to the highest point

Is there a way to make my content expand towards the top instead of bottom? Despite my efforts, the text always ends up at the bottom. Below is the code I'm using:

.box {
  width: 200px;
  height: 200px;
  background-color: red;
  position: absolute;
}

.div {
  position: relative;
  transition: .35s ease-in-out 0s;
  height: 0;
  width: 70%;
  left: 50%;  
  top: 100px;
  transform: translateX(-50%);
  text-align: center;
  z-index: 3;
}

.text {
  position: relative;
  box-decoration-break: clone;
  -webkit-box-decoration-break: clone;
  box-shadow: 0 0 0 1px black;
  line-height: 155%;
  padding: 0.2em 0.3em;
  color: white;
  background-color: black;
  background-color: rgba(0, 0, 0, .8);
  border-radius: 3px;
  font-size: 1.125rem;
}
<div class="box">
  <div class="div">
    <p class="text">Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
  </div>
</div>

What I'm looking for as output is:
https://i.sstatic.net/4HiMR.png

Although I have used top: 100px in .div for demonstration purposes, my goal is to have the text aligning to the top consistently. I've experimented with adjusting height, top, and bottom, but haven't had any success. Any suggestions on what steps I should take next? Thank you.

Answer №1

Your code had a lot of unnecessary elements and confusion with the positioning using relative and absolute properties. Here's the updated version working perfectly here

* {
  margin: 0;
  padding: 0;
}

.box {
  width: 200px;
  height: 200px;
  background-color: red;
  position: relative;
}

.div {
  position: absolute;
  margin: 20px
}

.text {
  box-decoration-break: clone;
  -webkit-box-decoration-break: clone;
  box-shadow: 0 0 0 1px black;
  line-height: 155%;
  padding: 0.2em 0.3em;
  color: white;
  background-color: rgba(0, 0, 0, .8);
  border-radius: 3px;
  font-size: 1.125rem;
}
<div class="box">
  <div class="div">
    <p class="text">Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
  </div>
</div>

Answer №2

If you consider switching the order of position: relative; and position: absolute; within the .box and .div classes, it should be done as shown below:

.box {
  width: 200px;
  height: 200px;
  background-color: red;
  position: relative;
}

.div {
  position: absolute;
  transition: .35s ease-in-out 0s;
  height: 0;
  width: 70%;
  left: 50%;  
  transform: translateX(-50%);
  text-align: center;
  z-index: 3;
}

.text {
  position: relative;
  box-decoration-break: clone;
  -webkit-box-decoration-break: clone;
  box-shadow: 0 0 0 1px black;
  line-height: 155%;
  padding: 0.2em 0.3em;
  color: white;
  background-color: black;
  background-color: rgba(0, 0, 0, .8);
  border-radius: 3px;
  font-size: 1.125rem;
}
<div class="box">
  <div class="div">
    <p class="text">Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
  </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

What is the best way to set up a webpage so that the left side remains fixed while allowing the right side to scroll?

Currently, I am developing a blog and aiming to position the blog posts on the left side while having the admin panel on the right. Essentially, I want both user and developer interfaces to be fully functional on one page. The developer side should be scro ...

Discovering whether a pseudo-element (::after) has been clicked in a React.js environment

I'm attempting to capture click events on an element's ::after pseudo element. I hope that something along these lines is achievable: //css div:after{ content:"X"; display:block; } //jsx class PsudoExample extends Component { render() { ...

What causes Django to detect errors within comment blocks in templates?

There are times when Django encounters an error in a template. I prefer to comment out the line for debugging purposes, but Django continues to detect the error even within the comment. For instance, this particular link triggers an error because the url ...

What are the steps for enhancing an existing website with keyboard navigation (Accessibility Access) using JavaScript, CSS, or HTML?

Seeking advice on implementing keyboard navigation in an existing website without relying on paid apps. Any suggestions for free JavaScript libraries or other resources are appreciated. Some similar techniques I have tried include using tabindex="0" and a ...

Completely place one element with respect to its sibling element

I am currently facing a situation where I have implemented a Navigation bar wrapper that reveals an overlay across the entire page when clicked. In order for this setup to function correctly, all the elements that the overlay covers must be sibling elemen ...

Adding a new <li> element with each click - a step-by-step guide

Just dipping my toes into Javascript, so go easy on me! I've managed to add new list items to an unordered list in the inner html of a page, but every time it just replaces the initial one. Feels like I'm overlooking something really simple her ...

What is the best way to extract user input from a web form and utilize it to perform a calculation using jQuery?

Is it possible to retrieve user input from a web form and use it to perform calculations with jquery? I am interested in extracting a numerical value entered by a user in a web form. Upon clicking "NEXT", I intend to multiply this number by a predefined c ...

Custom-themed Wordpress search results can appear distorted and strange

Struggling with my WordPress website and custom theme, particularly the search results page - can't seem to get it right! Check out my search results page Take a look at the screenshot of the search results here Looking for some guidance on fixing ...

Encountered AJAX Issue while trying to access XML document

My goal is to extract information from an XML file using AJAX <?xml version="1.0" encoding=UTF-8"?> <user> <u_idno>1</u_idno> <u_name>nobody</u_name> <u_srnm>nothing</u_srnm> <u_r ...

The issue with Bootstrap Vue scrollspy arises when trying to apply it to dynamic data. Upon closer inspection, it becomes evident that the elements are activating and highlighting one by

In my application built with Vue, content is displayed based on the component type (Header, Text, Image) in a JSON file. I am looking to implement scroll spy functionality specifically for the Header component containing headings. I have attempted to use ...

What is the best way to make the middle div stretch and fill the entire width of the screen while taking into consideration headers and footers?

Check out this JSFiddle link for more details <div class="navbar"> navbar </div> <section> <header>header</header> <div class="chat"> chat window </div> <footer> <textarea ...

Prevent images from overlapping in Bootstrap 4 carousel

I am working on a project using bootstrap 4. In my carousel design, I want to add another image layer at the bottom of the main image, but the code I have written is not displaying the new image in the correct position. Do you have any suggestions on how t ...

Ways to conceal elements when a webpage is displayed in a pop-up window

JavaScript // Open popup window $('a.popup').click(function(){ window.open( this.href, 'Page title', 'width=600, height=650' ); return false; }); HTML snippet <a class="popup" href="sample.html"> In order to ...

Trigger the click event on the ul element instead of the li element using jQuery

Is there a way to make the click event only affect ul tags and not all li elements using jQuery? <!-- HTML --> <ul class="wrap"> <li>test1</li> <li>test2</li> <li>test3</li> </ul> I attemp ...

Missing RequestVerificationToken value when hiding HTML element using jQuery

I am encountering an issue with my ASP.NET MVC 4 form that involves checkboxes being used to show and hide certain HTML elements. When I initially visit the form page, the RequestVerificationToken value is correctly created as a hidden field. Some HTML ele ...

Tips for displaying CSS background color on top of an inline style background image

I am facing a dilemma while using Laravel - if I put a background image into my CSS style sheet, I lose my variable. Currently, this is how I have it set up: <div class="gradient"><div class="topback" style="background-image: url('/storage/c ...

Breaking a line within an ngFor loop

I'm working with an ngFor loop to generate multiple PrimeNG buttons. Currently, the buttons are displayed side by side on the same row, but I want each button to appear vertically on its own line. How can this be achieved? Below is the segment of my c ...

Ways to alter the drop-down button of a select tag to resemble the inner-spin-button found in <input type="number">

Is there a way to change the drop down button in a Select tag to an Inner-spin-button? Here is an image of what I am trying to achieve: https://i.sstatic.net/yEv5G.png select::-webkit-inner-spin-button{ -webkit-appearance: number; appearance: number; } ...

Troubleshooting a text alignment problem with form-group in the Bootstrap framework

Currently, I am in the process of building a form that consists of several form-group elements. My main issue arises when dealing with long text as it causes alignment problems. Could you kindly take a look at the problem through this link: http://jsfidd ...

I am experiencing issues with my basic website, specifically with the functionality of my articles not meeting my expectations

Can anybody provide some assistance? I have a total of four articles that I would like to organize in the following manner: first one second one third one fourth one But for some reason, I am ...