Is it feasible to set the width as automatic in this scenario?

Example 1 demonstrates my goal, but it's challenging to set a fixed width for .sub due to the addition of new items. The width needs to adjust accordingly.

However, when no width is specified, Example 2 displays the result.

Are there any methods to allow .sub items to expand while keeping the horizontal layout?

.wrap, .wrap2 {
  width: 100px;
}

.container {
  width:100%;
  background: #ccc;
}

.item {
  position:relative;
}

.sub {
  background: #eee;
  position: absolute;
  left:100px;
  top:0;
  width: 340px;
}
.sub li {
  display:inline-block;
  border-left: 1px solid black;
  padding: 0 10px;
  &:first-child {
    padding: 0 10px 0 0;
    border-left:0;
  }
}

.wrap2 .sub {
  width:auto;
}
<h4>Example1. This is what I want. But, without setting width to .sub</h4>
<div class="wrap">
  <div class="container">
    <ul>
      <li class="item item1">
        <a href="">hello</a>
        <ul class="sub">
          <li class="item item1">hello world</li>
          <li class="item item2">world hello</li>
          <li class="item item3">foo bar</li>
          <li class="item item4">bar foo</li>
        </ul>
      </li>
      <li class="item item2">world</li>
      <li class="item item3">foo</li>
      <li class="item item4">bar</li>
    </ul>
  </div>
</div>

<h4>Example2. This is what happens when no width is set to .sub</h4>
<div class="wrap2">
  <div class="container">
    <ul>
      <li class="item item1">
        <a href="">hello</a>
        <ul class="sub">
          <li class="item item1">hello world</li>
          <li class="item item2">world hello</li>
          <li class="item item3">foo bar</li>
          <li class="item item4">bar foo</li>
        </ul>
      </li>
      <li class="item item2">world</li>
      <li class="item item3">foo</li>
      <li class="item item4">bar</li>
    </ul>
  </div>
</div>

Answer №1

Simply include this CSS:

.sub {
  white-space:nowrap;
}

By adding this, the content will remain in a single line and ignore the width of its parent element.

.wrap,
.wrap2 {
  width: 100px;
}
.container {
  width: 100%;
  background: #ccc;
}
.item {
  position: relative;
}
.sub {
  background: #eee;
  position: absolute;
  left: 100px;
  top: 0;
  width: 300px;
  white-space: nowrap;
}
.sub li {
  display: inline-block;
  border-left: 1px solid black;
  padding: 0 10px;
  &: first-child {
    padding: 0 10px 0 0;
    border-left: 0;
  }
}
.wrap2 .sub {
  width: auto;
}
<h4>Example2. This is what happens when no width is set to .sub</h4>
<div class="wrap2">
  <div class="container">
    <ul>
      <li class="item item1">
        <a href="">hello</a>
        <ul class="sub">
          <li class="item item1">hello</li>
          <li class="item item2">world</li>
          <li class="item item3">foo</li>
          <li class="item item4">bar</li>
        </ul>
      </li>
      <li class="item item2">world</li>
      <li class="item item3">foo</li>
      <li class="item item4">bar</li>
    </ul>
  </div>
</div>

Answer №2

You have the option to apply display:flex to your .sub element.

.wrap {
  width: 100px;
}
.container {
  width: 100%;
  background: #ccc;
}
.item {
  position: relative;
}
.sub {
  background: #eee;
  position: absolute;
  left: 100px;
  top: 0;
  display: flex
}
.sub li {
  display: inline-block;
  border-left: 1px solid black;
  padding: 0 10px;
}
.sub li:first-child {
  padding: 0 10px 0 0;
  border-left: 0;
}
<h4>Example1. This is what I want. But, without setting width to .sub</h4>
<div class="wrap">
  <div class="container">
    <ul>
      <li class="item item1">
        <a href="">hello</a>
        <ul class="sub">
          <li class="item item1">hello</li>
          <li class="item item2">world</li>
          <li class="item item3">foo</li>
          <li class="item item4">bar</li>
        </ul>
      </li>
      <li class="item item2">world</li>
      <li class="item item3">foo</li>
      <li class="item item4">bar</li>
    </ul>
  </div>
</div>

Edit: Considering your recent query, you can implement whitespace:nowrap as suggested by others.

.wrap {
  width: 100px;
}
.container {
  width: 100%;
  background: #ccc;
}
.item {
  position: relative;
}
.sub {
  background: #eee;
  position: absolute;
  left: 100px;
  top: 0;
  white-space:nowrap
}
.sub li {
  display: inline-block;
  border-left: 1px solid black;
  padding: 0 10px;
}
.sub li:first-child {
  padding: 0 10px 0 0;
  border-left: 0;
}
<h4>Example1. This is what I want. But, without setting width to .sub</h4>
<div class="wrap">
  <div class="container">
    <ul>
      <li class="item item1">
        <a href="">hello</a>
        <ul class="sub">
          <li class="item item1">hello world</li>
          <li class="item item2">world hello</li>
          <li class="item item3">foo</li>
          <li class="item item4">bar</li>
        </ul>
      </li>
      <li class="item item2">world</li>
      <li class="item item3">foo</li>
      <li class="item item4">bar</li>
    </ul>
  </div>
</div>

Answer №3

To ensure that your text does not wrap, simply include white-space: nowrap in your .sub CSS code.

.wrap, .wrap2 {
  width: 100px;
}

.container {
  width:100%;
  background: #ccc;
}

.item {
  position:relative;
}

.sub {
  background: #eee;
  position: absolute;
  left:100px;
  top:0;
  width: 300px;
}
.sub li {
  display:inline-block;
  border-left: 1px solid black;
  padding: 0 10px;
  &:first-child {
    padding: 0 10px 0 0;
    border-left:0;
  }
}

.wrap2 .sub {
  width:auto;
  white-space:nowrap;
}
<h4>Example2. This is what happens when no width is set to .sub</h4>
<h4>and white-space:nowrap is added</h4>
<div class="wrap2">
  <div class="container">
    <ul>
      <li class="item item1">
        <a href="">hello</a>
        <ul class="sub">
          <li class="item item1">hello</li>
          <li class="item item2">world</li>
          <li class="item item3">foo</li>
          <li class="item item4">bar</li>
        </ul>
      </li>
      <li class="item item2">world</li>
      <li class="item item3">foo</li>
      <li class="item item4">bar</li>
    </ul>
  </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

Div does not show global variable

In the beginning of my .js file, I define a variable like this: var selection = "example"; When I navigate from page 1.html to page 2.html by clicking a link, I have the following code on page 2.html: <script>document.write(selection);</script& ...

Troubleshooting: Onsubmit not functioning to validate form using Javascript

After searching for a solution to my issue, I couldn't find any helpful posts so here is my question: I am trying to validate a form before submitting it to the database. Below is the code I have: <%@ page language="java" contentType="text/html; c ...

Unable to modify the font color to white on the CSS navigation bar

I've been attempting to alter the color of the text in my .blou class to white within my navigation menu in WordPress, but for some reason, the color isn't changing. This issue is occurring with the WordPress theme I am using. .nav-menu ul li a ...

What is the method to establish the table format in HTML using several for each loops?

I need to arrange the table structure for product plans as follows: +------------------+---------------+---------------+ | product1 | product2 | product3 | +------------------+---------------+---------------+ | product1plan1 | product ...

When I try to call jQuery's getScript function, the callback does not execute as expected within

When I invoke the script in a function at runtime function CheckOutMyFace() { $.getScript("/scripts/jtimers.js", function (data) { }); }; I notice in Firebug that the script is being called every time the function is invoked, but for some reason the ...

What could be causing the lack of functionality in my nested CSS transition?

My markup includes two classes, fade-item and fade. The fade class is nested within the fade-item class as shown below: <a class="product-item fade-item" (mousemove)="hoverOn(i)" (mouseleave)="hoverOff(i) > <div class= ...

`Move the Hover effect to a different element upon mouseover``

In my setup, I have the following arrangement: <div class="menuHomeCategorias Accesoriosclass"> <div class="categoryName Accesorios" name="Accesorios"> <p>Accesorios</p> </div> <img class="categoriasHome" alt="Ca ...

"Optimizing Background Images with IE 8 Stretch Cover in Bootstrap

Page Broken I've searched all over Stack Overflow for a solution to resolve this background image issue. I'm using Bootstrap for the boxes and removed any margins. The problem arises when setting the background as cover; in IE8, it doesn't ...

Navigate to various parts of the page easily with a stationary navbar

After successfully implementing buttons to jump to different sections of my page using IDs like this: <button id="niftyBtn"><a href="#nifty">Things</a></button></br> <div id="nifty">Blah</div> I'm facing an ...

Displaying the total count of slides available on the webpage

Check out the custom slider I created on this codepen page: http://codepen.io/anon/pen/NqQpjG I have added an additional feature that tracks the total number of slides that have been moved. For instance, if there are a total of 8 slides, the initial va ...

Content misaligned vertically inside a DIV

I'm struggling to understand why the browser is not allowing me to apply margin-top or padding-top to a DIV in order to center the text. HTML - <div id="header"> <div id="Nav"> <div id="navright"> ...

Styling with CSS in Django templates

Having trouble with CSS in my Django template, My settings.py looks like this: BASE_DIR = os.path.abspath(os.path.dirname(__file__) + '/') STATIC_URL = BASE_DIR + '/static/' I have a folder called "static/css/home_css.css" in my path ...

Apply CSS styling to the v-html output

I am trying to enhance the style of HTML code using a v-html but have been struggling with finding a working solution so far. Can anyone help me out? :( Below is my code snippet: Template : <div class="para" v-html="value" /> Script : exp ...

Show the table header at all times, even when the rows are hidden from view

Whenever the number of rows in my dynamic table changes due to user events, all tr elements that do not have the class .selected are set to display:none;. This means that at times, none of the table rows have the class selected. The problem arises when I h ...

Adjust the jQuery resizable handlers on the fly

I am encountering an issue when attempting to change the handler on a resizable element. Initially, I used : $(line) .draggable({containment:"parent"}) .resizable({ containment:"parent", handles: "e, w" }); N ...

Guide on automatically navigating pages using HTML

My dilemma involves two HTML pages: flash.html main.html I am seeking a solution where the flash.html page is loaded first for 5 seconds, after which the main.html page should automatically load. How can I achieve this? I attempted to use setTimeout(fu ...

When you click the button, the page refreshes with no content

Every time I click on a button, an empty page reloads after invoking the button's onClick event. The code snippet on the page is as follows: <html> <head></head> <body>2015</body> </html> I am ...

Instructions on how to change database entries utilizing text input fields, enabling users to apply modifications by selecting the 'update' button

I'm a beginner when it comes to PHP and MySQL. Right now, I have a webpage with multiple text boxes that display data from my database. I am looking for guidance on how to update this data by allowing users to make changes in the textboxes and then cl ...

What accounts for the different behavior of line-height when using units vs percentage or em in this instance?

I'm puzzled by the behavior of the CSS code provided, which is also demonstrated in this fiddle. <style type="text/css"> p { font-size: 14px; } .percentage { line-height: 150%; } .em-and-a-half { line-height: 1.5em; } .decimal { ...

Images of significant size displayed on websites appear distorted when viewed on mobile devices

I am currently working on a web application that specializes in storing and showcasing satellite images based on specific locations. One issue I have encountered is the large file size of these images, which can be up to 20MB in jpg format, leading to ren ...