Ways to centrally align an element with a taller adjacent element in a vertical direction

In this specific scenario, my goal is to centrally align the number "3" inside the "list-li" element as the adjacent element possesses a variable height. I attempted to use:

position: relative;
top: 50%; 

However, this method does not yield the desired outcome.

HTML:

<ul class="list-1">
    <li>
        <div class="list-1-num">
            3    
        </div>
        <div class="list-1-content list-1-content-signup">
            Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur venenatis, lectus cursus cursus elementum.
        </div>
    </li>
</ul>

CSS:

.list-1 {
    font-size: 20px;
    list-style: none;  
}

.list-1 li {
    margin-bottom: 70px;
    overflow: hidden;
    text-align: left;
    list-style: none;
}

.list-1-num {
    float: left;
    font-size: 25px;
    margin-right: 20px;
}

.list-1-content {
    background-position: 0 50%;
    background-repeat: no-repeat;
    background-size: 45px 40px;
    float: left;
    width: 300px;
    line-height: 25px;
}

Take a look at the Codepen demonstration here: https://codepen.io/aguerrero/pen/OZevgJ

Answer №1

Consider using Flexbox in place of float and specify align-items: center on the parent element.

.list-1 {
  font-size: 20px;
  list-style: none;
}

.list-1 li {
  margin-bottom: 70px;
  overflow: hidden;
  text-align: left;
  list-style: none;
  display: flex;
  align-items: center;
}

.list-1-num {
  font-size: 25px;
  margin-right: 20px;
}

.list-1-content {
  background-position: 0 50%;
  background-repeat: no-repeat;
  background-size: 45px 40px;
  width: 300px;
  line-height: 25px;
}
<ul class="list-1">
  <li>
    <div class="list-1-num">
      3
    </div>
    <div class="list-1-content list-1-content-signup">
      Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur venenatis, lectus cursus cursus elementum.
    </div>
  </li>
</ul>

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

Create a PHP script that utilizes the DOMDocument class to parse and manipulate a table

Struggling to maintain the inner HTML tags while displaying rows in an HTML table? Can't seem to get it right and the tags keep getting stripped out? What's the best approach to ensure each row retains its inner HTML tags? Below is the code I ha ...

Update the query string in the src attribute of the image tag

I'm facing a challenge with our products loading at specific sizes and quality that I need to address. I am looking to update a portion of the img src code: &wid=128&hei=144&qlt=80 to: &wid=256&hei=288&qlt=100 I have attemp ...

By utilizing Angular's @output decorator, data can be passed from child components to their parent counterparts

I have a situation where I am passing data from a child component to a parent component, and I need to automatically check if the data is empty or has a value. This code snippet is from my login.component.ts - child TypeScript file: @Output() update = ne ...

Most efficient method to upload numerous images without any lag

I have a website where images are loaded only when they are slightly below the viewport. This method allows the site to load initially without images and then determine which ones need to be loaded based on the user's viewpoint. When a user scrolls ...

How can the issue of the navbar color not being able to disappear or turn transparent be resolved, given its connection to the body color?

@import url('https://fonts.googleapis.com/css2?family=Bai+Jamjuree:wght@400;500;600;700&display=swap'); :root { --color-body: #b6cbce; --color-heading: #eef3db; --color-base: #033f47; --color-base2: #022a30; --color-brand ...

How can I update two divs simultaneously with just one ajax call?

Is there a way to update 2 divs using only one ajax request? The code for updating through ajax is in ajax-update.php. <?php include("config.inc.php"); include("user.inc.php"); $id = $_GET['id']; $item = New item($id); $result = $item->it ...

What are the steps to reveal the second element once the first one has vanished?

Is there a way to delay the appearance of the second square until after the first square has disappeared? For example, I want the first square to appear after 3 seconds and then disappear, followed by the second square becoming visible after 11 seconds. ...

Setting the percentage height of parent and child divs in a precise manner

Trying to work through this without causing chaos. I have a container div containing three floated left child divs, with the container div set to 100% height like so: .Container { height: 100%; min-width: 600px; overflow-y: hidden; } One of the child divs ...

Multiplication cannot be performed on operands of type 'NoneType'

Hello everyone, I am attempting to calculate the unit price and quantity from this table using the following model: class Marketers(models.Model): category =models.ForeignKey(Category, on_delete=models.CASCADE, null=True) name =models.CharField(max ...

Tips for emphasizing a specific cell in a row based on its expiration date

In my quest to find a script that colors dates within two weeks from today in red and past dates in orange, I have tried various methods without success. I am struggling to implement this feature with my current knowledge. <TABLE> <TR><TD&g ...

Is there a way to prevent continuous repetition of JavaScript animated text?

I'm working on a code that displays letters and words one by one, but I can't figure out how to stop it from repeating. Can someone help me with this? <div id="changeText"></div> <script type="text/javascript"> var te ...

HTML5 Canvas Border

Hey Everyone, I've been working on an HTML5 canvas project where I set the canvas width to $(window).width(). Everything was going smoothly until I added a border, which caused a horizontal scroll to appear. Important: I attempted to use the innerWid ...

Animating colors with jQuery and shifting SVG shapes to create dynamic and

I am currently working on an svg animation that involves changing the color of the svg to a different color, creating a running light effect. Rather than fading the fill color of the entire svg as commonly seen in examples, I aim to achieve a dynamic trans ...

Adjust the height of each table's data cell dynamically based on the content within

I have a table in HTML with a cell that contains 2 divs and a radial tab strip. Here is an example: <td> First td </td> <td> Second td <div> .... <div style="border: solid; border-color: brown; border-width: 3px; he ...

How can I create a unique CSS or JS transition for a button that dynamically changes size based on text

My Angular app features a button labeled with "+". When the user hovers over it, I use element.append(' Add a New Number'); to add the text "Add a New Number" next to the + sign in the label. Upon clicking the button, a new number is added and ...

Having trouble getting the height to work properly on your Blogger Blogspot template?

Issue with embed height=100% not working <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr"> <head> & ...

Add more key value pairs to another <div> element

My setup involves radio buttons attached to div elements. When a div is clicked, the information displayed on the widget updates to show the data associated with that div. I am using the Yahoo API to fetch the data in JSON format. The correct data is being ...

Having issues with your PHP dropdown database code?

I am having trouble with the code below. It seems to display the HTML portion correctly, but it is not fetching the data from my database. My goal is to populate a drop-down list with data from the database and then display it in a table below when the u ...

What is the best way to retrieve nested objects in JSON using JavaScript and an Ajax request?

I am facing an issue with a nested JSON structure and trying to access the data through an ajax request to populate an HTML table. Here is a sample of the JSON structure: { sum: { total: 2, }, data: [ { id: '1', attribu ...

Expanding and hiding content using jQuery while also utilizing cookies for persistence

Hey there! I have a piece of code that I'm working on and could use some help. I'm trying to create a cookie that can remember the current state of a div even if the user reloads the page. Any assistance would be greatly appreciated! jQuery(fun ...