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

SmartEdit functions properly when spartacus is running using yarn start --ssl, but does not work without SSL enabled

I followed the smartedit setup instructions at and everything works well when I start the Spartacus server with yarn start --ssl. However, if I start the server with just yarn start, the storefront does not appear. See image here for reference ...

The input form in my Node.js project is not adapting to different screen sizes. I am currently utilizing ejs as the template

I have integrated ejs as a template in my Node.js project, but I am encountering an issue with the input form in the following code snippet. The form is unresponsive, preventing me from entering text or clicking on any buttons. What could be causing this ...

Preventing Redundancy in Angular 2: Tips for Avoiding Duplicate Methods

Is there a way I can streamline my if/else statement to avoid code repetition in my header component? Take a look at the example below: export class HeaderMainComponent { logoAlt = 'We Craft beautiful websites'; // Logo alt and title texts @Vie ...

Preserve information on page reload in AngularJS

I am currently working on an AngularJS application with Spring REST as the backend. My experience with AngularJS is quite new. Within my UI page, I have a table displaying a list of objects with an edit button for each record. Clicking the edit button ope ...

Javascript error when attempting to add leading zeros

Is there a way to utilize JavaScript or JQuery in order to prepend a zero to this script? for (im=1;im<=31;im++){ days[im]=everyDay[im]; } ...

The button now has a stylish 5px border, making it slightly larger in size. However, it seems

How can I add an active class with a 5px border-bottom on a button without increasing the button size by 5px? The box-sizing property is not working for me. Is there a simple solution to achieve this? *, *:after, *::before { -webkit-box-sizing: border ...

The onsubmit function is not functioning correctly in combination with Ajax

My current implementation involves utilizing Ajax and Php for user login validation. Unfortunately, the form onsubmit function is not properly functioning. <form action="loggedin.php" onsubmit="return test()" method="post"> Below is the correspondi ...

The vertical spacing in Font "bottom-padding" appears to be larger than anticipated

Recently, I purchased the font Proxima Nova for my project and I am encountering issues with vertical alignment. Despite setting margins, line-heights, and padding to match those of Arial, the results are not consistent as the Proxima Nova appears misalign ...

Can the geocoder API/search box be utilized to locate specific markers on a map?

Incorporating Mapbox into an Angular application with a high volume of markers on the map (potentially thousands) and hoping to implement a search box for users to easily locate specific markers based on unique names and coordinates. Is this functionalit ...

javascript triggering before its designated event happens

I've encountered a puzzling issue with a complex file where a javascript function is behaving unexpectedly—it seems to be firing before its designated event actually takes place. I have ruled out the possibility of an onload event triggering this be ...

What is the best way to conceal elements that do not have any subsequent elements with a specific class?

Here is the HTML code I have, and I am looking to use jQuery to hide all lsHeader elements that do not have any subsequent elements with the class 'contact'. <div id="B" class="lsHeader">B</div> <div id="contact_1" class="contac ...

What could be causing the network error that keeps occurring when attempting to sign in with Google using React and Firebase?

Having an issue with Google authentication using Firebase. I set up the project on Firebase console and copied the configuration over to my project. Enabled Google sign-in method in the console which was working fine initially. But, after 2 days when I tri ...

Utilization of CSS with the symbols of greater than and asterisk

I've been experimenting with Sequence.js and finding it really impressive. There's one line of code that I'm having trouble understanding: #sequence .seq-canvas > * {...} I discovered that the > symbol selects only direct descendant ...

Issue encountered while subscribing to SalesForce topic using Node.js

Attempting to subscribe to a SalesForce topic through a Node.js server using the code provided in the JSForce documentation: conn.streaming.topic("InvoiceStatementUpdates").subscribe(function(message) { console.log('Event Type : ' + message.ev ...

Conceal the div element if the value is either undefined or empty in AngularJS

I am experiencing an issue where I get 2 different values when I use console.log($scope.variable). The values shown are undefined and ''. Based on this value, I need to hide a div. Here's what I have tried: <div ng-hide="$scope.variable ...

Significant distance between the content and the footer section of the page

Having trouble with a large gap between the content and footer ad on my website. I've tried to troubleshoot it myself, but suspect the issue may be related to the content generated by another site (indeed.com). Check out the link here: Any assistanc ...

AJAX jQuery requests can flatten arrays when they are sent

The below code shows an endpoint written in Express, using the body-parser middleware. app.post("/api/poll/new",api.NewPoll); api.NewPoll = function(req,res){ if(!req.body) return res.status(400).send("MISSING BODY"); console.log(req.body,typeof(r ...

Dynamically removing buttons with JQuery

I am trying to dynamically add buttons to a page based on an array, but I'm running into a problem. Whenever I add a new name to the array, it prints out all of the buttons instead of just the one I added. I need help figuring out how to erase all the ...

Finding and choosing a date from an ng-model datepicker using Selenium Webdriver - a guide

I am encountering an issue where I can open the date picker pop-up calendar with a click, but I cannot select a specific date from it. This is due to the input field being read-only, which means that any input provided through sendkeys or JavascriptExecuto ...

How can I dynamically retrieve the width of an image in React as the screen size changes?

I have successfully implemented an image hover effect on my website. When I hover over a certain text, an image appears. The image is responsive, but I am facing an issue where I want the width of the text to be equal to the width of the image. When I resi ...