center text within grandparent element

Within my Angular application, I have a list where each item can be comprised of either a small image with accompanying text or just text alone.

li {
  display: flex;
  border: 1px solid black;
  width: 80%;
  margin-bottom: 5%;
  list-style: none;
  position: relative;
}

.text {
  text-align: center;
  width: 100%;
  background: lightgrey;
}

.image {
  background: red;
  width: 25%;
  height: 100%;
  position: absolute;
}
<ul>
  <li><div class="text"><p>item 1<br/>some text</p></div>
  </li>
  <li><div class="image"></div><div class="text split"><p>item 2<br/>some text</p></div>
  </li>
  <li><div class="image"></div><div class="text split"><p>item 2<br/>some more text in a very very long line which may break at the end</p></div>
  </li>
</ul>

If an image is present alongside the text in an item, the alignment of the text within the containing div will shift. This results in both texts not being vertically aligned as desired. I am seeking a way to centralize the text of item2 within the li element so that it aligns consistently with its predecessor without relying on JavaScript if possible.


UPDATE: The usage of position: absolute appears to resolve the issue. However, lengthy text lines fail to be wrapped seamlessly and instead overlap or hide behind the red div block. Given that the red div will eventually hold an image, overlaying text directly above it poses drawbacks. While manual calculations for text widths could solve this concern, I am exploring simpler alternatives. To elaborate, I aim to center the text within the list item itself (and not within the text div) while ensuring line breaks function akin to a div styled with 'width: 75%'. Any additional pointers?

Appreciate your insights

Answer №1

If you were looking for something similar to this:

li {
  display: flex;
  border: 1px solid black;
  background: lightgrey;
  width: 80%;
  margin-bottom: 5%;
  list-style: none;
  position: relative;
}

.text {
  text-align: center;
  width: 100%;
  position: relative;
  z-index: 2;
}

.image {
  background: red;
  width: 25%;
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  z-index: 1;
}
.more {
   width: 60%;
}
<ul>
  <li><div class="text"><p>item 1<br/>some text</p></div>
  </li>
  <li><div class="image"></div><div class="text split"><p>item 2<br/>some text</p></div>
  </li>
  <li><div class="image more"></div><div class="text split"><p>item 3<br/>some text</p></div>
  </li>
</ul>

Answer №2

To achieve this effect, you can also utilize absolute positioning combined with transform, as shown below:

  <div class="wrapper">
    <div class="image">
    </div>
    <div class="text">
      THIS IS CENTERED TEXT
    </div>
  </div>

  <div class="wrapper">
    <div class="text">
      THIS IS CENTERED TEXT
    </div>
  </div>

Here is the accompanying CSS code:

.wrapper {
  height: 100px;
  width: 300px;
  margin: 0 auto;
  margin-top: 10px;
  border: 2px solid black;
  position: relative;
}

.text {
  position: absolute;
  text-align: center;
  top: 50%;
  left: 50%;
  transform: translateX(-50%)translateY(-50%);
}

.image {
  height: 100px;
  width: 100px;
  background: red;
}

This method has the advantage of consistently centering both horizontally and vertically without relying on specific widths for the text or image.

Explore the example further on jsfiddle here.

Answer №3

Simply add the container to the right and utilize the + sibling selector.

HTML:

<li>
    <div class="image"></div>
    <div class="text split">
        <p>item 2<br/>some text</p>
    </div>
    <div></div>
</li>

CSS:

.image + .text {
    width: 50%
}

.image + .text + div {
    width: 25%
}

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

Fading CSS Hover Popup Display and Hide

I am looking to create a CSS-only popup for an image that will appear when a user hovers over the image with their mouse. After researching on various platforms, such as this thread, I have developed the following solution: a.tooltip span { width: 250 ...

Creating an image slideshow with a looping JavaScript array: Step-by-step guide

One issue with the program is that when it runs, only the last array value image is displaying on the screen while the other images are not. Below is the HTML code: <img id="mg" alt="image not found"> And here is the JavaScript code: var images=[ ...

Learn how to showcase a predetermined option in an HTML select element using Vue.js and Minimalect

I am currently utilizing Vue.js along with the Mininmalect HTML select plugin to showcase a list of countries by their names and values, where the value represents the 2 digit country code. Successfully, I have managed to implement the plugin for selectin ...

What is the best way to create a versatile function that can be implemented across multiple views in AngularJS?

I am using ng-if in my HTML template: <div ng-if="isImage(item._file) == false"> The method isImage() is currently located in the controller. How can I create a common method isImage() so that I don't have to duplicate it in each controller an ...

Styling HTML elements with CSS in ASP.NET

<style type="text/css> .style1 { border:2px solid #e53a6f; box-shadow:0 0 5px 0 #e53a6f; } </style> javascript <script type="text/javascript" language="javascript"> if (Pname == "") { document.getElementById(' ...

The URL requested exceeds the maximum length limit in asp.net, causing a 414 error

I encountered the issue of receiving an "HTTP Error 414. The request URL is too long." While reading through this article, I learned that it is caused by an excessively lengthy query string: Currently in my web.config, I have set maxQueryStringLength to " ...

Looking for guidance on transitioning to Angular 2 - any tips?

Providing some context: I am involved in the development of a massive, highly dynamic, and customizable Angular 1 web application. Due to its size and dynamism, there are an excessive number of watchers - at least 4K in a single view. Naturally, the app ...

The process of toggling a div to slide up and down explained

I'm attempting to create a slide toggle effect on a hidden DIV when a user hovers over specific link buttons. JavaScript: $(function () { // DOM ready shorthand var $content = $(".sliderText"); var $contentdiv = $(".sliderCo ...

What is the best method to align these images in the center of this div?

Struggling to center the images inside the div...any suggestions? I know it's basic, just getting back into it. Hopefully someone understands what I'm trying to achieve here? Tryin' to get those images centered in the div but can't fig ...

Send in 3 sets of HTML forms along with a single JavaScript button to save all data in a single row within

I'm facing an issue with submitting multiple forms on a page without submit buttons. I have a script that submits all three forms at the same time, but it's creating separate rows in the database instead of one combined row. Any suggestions on ho ...

The value of the bound variable in ng-options does not get updated after the array is

I have a situation where I am populating a list using an array and the ng-options directive in AngularJS. The selected item is bound to a variable called myObject.selectedItem. However, I noticed that even after clearing the array, the value of myObject.se ...

Select a division and retrieve the identification of a different division

I am looking to trigger an event by clicking on one element and extracting the ID from a separate, unrelated div. Here is my attempt: $(".map_flag").on("click",function(){ var selectedID = ($(this).attr("data_modal")); $("#" + selectedID).fade ...

Unveil Secret Divs with a Click

I am in search of a way to display a hidden div when I click on a specific div, similar to the expanding images feature in Google's image search results. I have made progress with my limited knowledge of javascript, as shown in this CodePen: http://co ...

My AngularJS integrated with Spring MVC is failing to render the desired page

Hello everyone, I'm fairly new to AngularJS and Spring MVC. I've managed to set up a basic application, but unfortunately, when I try to load it, nothing appears on the screen. The console shows a 404 error. I've double-checked everything, b ...

Click on every sublist within the unordered list items

In this coding example, there are multiple ul and li elements. Upon running the code, you will observe that test1 is positioned 10px from the left as it is nested within TEster, but achieving an additional 10px offset for Tester2 appears to be a challenge ...

How to stop the overflow scrollbar in a grandchild element with CSS grid

Encountered an unusual issue while using a combination of CSS grid and overflow auto. In the scenario where there are 3 elements, the outermost element has display: grid, the middle element has height: 100%, and the innermost element has both height: 100% ...

What is preventing my divs from aligning properly?

After trying various methods and conducting thorough research, I am still unable to resolve the issue with my code. Could someone please review it and provide some guidance? Any help would be greatly appreciated. I have experimented with different div wid ...

Adaptable design tailored for smartphones

When it comes to developing mobile websites for various platforms such as iPhone 3 and 4, Android, Blackberry Torch, etc., I usually find myself needing to slice images based on the specific platform. The challenge arises from constantly having to slice im ...

Refreshing Angular Services: A Guide to Resetting Factories

My angular factory is quite intricate and structured like this: app.factory("mainFcty", function(){ return { a:"", b:"", c:"" } }); When users progress through the app and complete actions such as booking a service, they f ...

Error encountered when attempting to trigger a Bootstrap dropdown within a designated div

When using React JS, I encountered an issue when trying to trigger a dropdown that was nested inside a div with the class name "row". Initially, I placed the data-toggle and data-target attributes inside the div, like so: <div className="row"> < ...