Using CSS to position an element relative/absolute within text inline

Need help aligning caret icons next to dynamically populated text in a navbar menu with dropdown tabs at any viewport size. Referring to positioning similar to the green carets shown here: https://i.stack.imgur.com/4XM7x.png

Check out the code snippet below:

* {
  box-sizing: border-box;
}
body {
  margin-top: 70px;
  font-family: Arial;
  font-size: 30px;
}
.nav-wrapper {
  width: 100%;
  background-color: pink;
  height: 150px;
  display: flex;
}
.filler-left {
  // margin-right: auto;
  flex: 2;
  background-color: brown;
}
.filler-right {
  background-color: black;
  width: 600px;
}
.menu-wrapper {
  background-color: lightblue;
  display: flex;
  justify-content: flex-end;
}
.menu-btn {
  text-align:center;
  background-color: orange;
  border: 1px solid red;
  display: flex;
  align-items: center;
}
.text {
  border: 1px solid blue;
}

.caret {
  min-width: 25px;
}
<div class="nav-wrapper">
  <div class="filler-left">
    <img src="https://via.placeholder.com/150"/>
  </div>
  <div class="menu-wrapper">
    <div class="menu-btn">
      <span class="text">lo netrunner</span>
      <img class="caret" src="https://assets.codepen.io/867725/Icon_Caret.svg" />
    </div>
     <div class="menu-btn">
       <span class="text">Planes of Salads & Greatness</span>
      <img class="caret" src="https://assets.codepen.io/867725/Icon_Caret.svg" />
    </div>
     <div class="menu-btn">
       <span class="text">Formulas & Computations</span>
      <img class="caret" src="https://assets.codepen.io/867725/Icon_Caret.svg" />
    </div>
  </div>
  <div class="filler-right">
  </div>
</div>

If needed, you can also refer to this CodePen for reference: https://codepen.io/vincentntang/pen/f44a23aa2b2de633e3d2dffa1e83c0b9

The width of the text is determined by the length of the first word on the second line as per HTML text standards.

Previous attempts made:

  • Tried using position: relative on inline text and position:absolute for the caret image, but was unsuccessful
  • Experimented with width: fit-content on the text class element, causing the text "Planes of Salads & Greatness" to extend to three lines instead of two across all viewport sizes

Answer №1

To create space for the caret element:

  • Add display: inline-block; and side padding to .text
  • Apply position: relative; to .menu-btn to make it the position reference for .caret
  • Use position: absolute on .caret, set right distance from the right border, and top: 50% with transform: translateY(-50%) to position it correctly

You can adjust the right, padding, and margin settings as needed...

* {
  box-sizing: border-box;
}
body {
  margin-top: 70px;
  font-family: Arial;
  font-size: 30px;
}
.nav-wrapper {
  width: 100%;
  background-color: pink;
  height: 150px;
  display: flex;
}
.filler-left {
  // margin-right: auto;
  flex: 2;
  background-color: brown;
}
.filler-right {
  background-color: black;
  width: 600px;
}
.menu-wrapper {
  background-color: lightblue;
  display: flex;
  justify-content: flex-end;
}
.menu-btn {
  text-align:center;
  background-color: orange;
  border: 1px solid red;
  display: flex;
  align-items: center;
  position: relative;
  
}
.text {
  border: 1px solid blue;
  display: inline-block;
  padding: 0 1em;
  margin: 0 10px;
}

.caret {
  min-width: 25px;
  position: absolute;
  right: 15px;
  top: 50%;
  transform: translateY(-50%);
}
<div class="nav-wrapper">
  <div class="filler-left">
    <img src="https://via.placeholder.com/150"/>
  </div>
  <div class="menu-wrapper">
    <div class="menu-btn">
      <span class="text">lo netrunner</span>
      <img class="caret" src="https://assets.codepen.io/867725/Icon_Caret.svg" />
    </div>
     <div class="menu-btn">
       <span class="text">Planes of Salads & Greatness</span>
      <img class="caret" src="https://assets.codepen.io/867725/Icon_Caret.svg" />
    </div>
     <div class="menu-btn">
       <span class="text">Formulas & Computations</span>
      <img class="caret" src="https://assets.codepen.io/867725/Icon_Caret.svg" />
    </div>
  </div>
  <div class="filler-right">
  </div>
</div>

Answer №2

Rearrange the caret images by placing them inside their corresponding span elements, and assign the same flex attributes to the child elements as the parent element above.

* {
  box-sizing: border-box;
}
body {
  margin-top: 70px;
  font-family: Arial;
  font-size: 30px;
}
.nav-wrapper {
  width: 100%;
  background-color: pink;
  height: 150px;
  display: flex;
}
.filler-left {
  // margin-right: auto;
  flex: 2;
  background-color: brown;
}
.filler-right {
  background-color: black;
  width: 600px;
}
.menu-wrapper {
  background-color: lightblue;
  display: flex;
  justify-content: flex-end;
}
.menu-btn {
  text-align:center;
  background-color: orange;
  border: 1px solid red;
  display: flex;
  align-items: center;
}
.text {
  border: 1px solid blue;
  display: inherit;
  align-items: inherit;
}

.caret {
  min-width: 25px;
}
<div class="nav-wrapper">
  <div class="filler-left">
    <img src="https://via.placeholder.com/150"/>
  </div>
  <div class="menu-wrapper">
    <div class="menu-btn">
      <span class="text">
        lo netrunner
        <img class="caret" src="https://assets.codepen.io/867725/Icon_Caret.svg" />
      </span>
    </div>
     <div class="menu-btn">
       <span class="text">
         Planes of Salads & Greatness
         <img class="caret" src="https://assets.codepen.io/867725/Icon_Caret.svg" />
       </span>
    </div>
     <div class="menu-btn">
       <span class="text">
         Formulas & Computations 
         <img class="caret" src="https://assets.codepen.io/867725/Icon_Caret.svg" />
       </span>
    </div>
  </div>
  <div class="filler-right">
  </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

Creating a central navigation menu for a website

Currently, I am working on incorporating a menu in the center of a webpage (please note that this is not a top navigation menu). Here is the initial setup: Users are able to click on various menu items to access different content. I have written code fo ...

Customizing the Twitter bootstrap navigation in Wordpress

I've been working on theming my navigation menu using Twitter Bootstrap. I want it to resemble the demo example of a fixed top banner, which can be seen here: http://getbootstrap.com/examples/navbar-fixed-top/ Although I have managed to get it to wor ...

Tips for utilizing the 'contains' feature within web elements with Java Selenium WebDriver?

In my current project, I am facing a challenge with two specific elements: one is a string formatted as ABC £12,56 and the other is a box called "Cashbox" that should be 15% of the value of the string. However, when attempting to locate the CSS for both e ...

Stop the change event from occurring on a textarea when the user clicks on an external cancel button

In a particular scenario, there is a textarea with an autosave feature triggered by the change event. When the textarea is focused on, Save and Cancel buttons appear at the bottom, providing users with options in case they prefer not to simply click outsid ...

Guide to storing a collection in an object with Java

Before the changes were saved https://i.stack.imgur.com/hjpXa.jpg After the changes were saved https://i.stack.imgur.com/xABzN.jpg @Entity @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}) public class Notification { @Id @GeneratedVa ...

How can I make all fields in the CKAN Resource form html the same size as the "Description" field?

I am completely new to CKAN and front-end development. I've been scouring through various html/css files without success in locating where I can adjust the field sizes in the resources form (where users can modify metadata via GUI). I would like to r ...

What sets usePreloadedQuery apart from useQueryLoader?

I've been exploring graphQL and the react-relay library. These are the key points I've covered: Rendering Queries: where usePreloadedQuery is introduced. Fetching Queries for Render: where useQueryLoader is introduced. To keep it simple, I&apo ...

Missing pieces of data | Utilizing React and Redux Toolkit

I'm facing a problem that's keeping me up for almost 24 hours. I just finished coding this slice, but when I submit the data, only the automatically generated ID is returned. Let me explain further - I have a skill component with two input forms ...

pictures on the blog entries

I'm looking to customize the look of images within a section. I want to add borders to the images, but since they are being added dynamically as content in blog posts, I can't reference them directly. Can you recommend a way for me to achieve thi ...

Troubleshooting a problem with the interactive YouTube player in Safari

I have successfully implemented a custom YouTube player on my website, allowing users to watch videos within a modal box. Below is the code for reference: JS & HTML: <script src="https://www.youtube.com/iframe_api"></script> <script typ ...

Issue with resizing keyboard/dropdown popup in Android hybrid app when navigating offscreen

Here is the offscreen navigation menu I have set up. When I open a keyboard or dropdown, the offscreen menu changes and exhibits a small gap on the left side after the popup (highlighted in red). It seems like the menu is shifting further away from the ed ...

Trouble with AngularJS: Updates not reflecting when adding new items to an Array

I am facing a persistent issue that I have been unable to resolve, despite researching similar problems on StackOverflow. My current project involves building an application with the MEAN stack. However, I am encountering difficulties when trying to dynam ...

Cheerio in node.js encounters a problem while reading HTML files

I am brand new to JavaScript and I've hit a roadblock with Node Cheerio. Any assistance would be greatly appreciated. The code I'm currently working on can be found here: https://github.com/zafartahirov/bitstarter. Once the issue is resolved, t ...

I'm working on separating the functionality to edit and delete entries on my CRM model, but I'm having trouble finding a way to connect these buttons with my data fields

I am encountering some difficulties while trying to implement separate functionality for editing and deleting items on my CRM model. I have already created the necessary API in Angular, but I am struggling to bind these buttons with my field. Any assistanc ...

Customizing Attribute for Material UI TextField

I'm currently in the process of adding a custom data attribute to a TextField component like so: class TestTextField extends React.Component { componentDidMount() {console.log(this._input)} render() { return ( <TextField label= ...

Looking to update the background color of a div every 3 seconds?

Every 3 seconds, I need to change the background color of each div. To do this, I attempted to modify the values in the color array every 3 seconds. For example, the value at index 0 (which is "red") would move to index 1, then the value at index 1 would ...

The issue with Bootstrap Vue is that it fails to render the CSS properly when utilizing cards

Recently, I delved into the world of Bootstrap Vue and wrote the code snippet below: <template> <div> <div> <b-card-group> <b-card border-variant="dark" header="Dark" align="center"> &l ...

Chrome method for creating Flexbox columns of equal height

I am implementing a simple 2-column layout and I want to utilize Flexbox to ensure equal heights for the columns: HTML <div class="row flex"> <!-- menu --> <div class="col-xs-4"> <aside> Menu content wi ...

Extract a JSON substring by referencing a specific object node key

I have a JSON string that contains nested objects. Here's a portion of the JSON string: "foldChildren": "false", "branchColor": "#000000", "children": [ ...

Having trouble sending data from AJAX to PHP

My goal is to implement a "load more" feature in my web app that automatically calls a PHP file to load additional products as soon as the page is fully loaded. In order to achieve this, I am using AJAX to call the PHP file: $(document).ready(function() { ...