Showing a table alongside a span with a specified width

One of the challenges I'm facing involves a dynamic width <table> element within my HTML, which changes based on the window size, and a fixed width <span> element set to 500px.

My goal is to have both elements displayed next to each other in a way that they fill the entire width of the parent container.

It's important for me to achieve this using only CSS, without relying on JavaScript.

I've been experimenting with various CSS approaches, but it seems to be affecting the table's width in ways I didn't anticipate.

HTML

<div class='container'>
   <table class='table'>....</table>
   <span class='span'>....</span>
</div>

CSS

.container {
   ......
}

.table {
  .....
}

.span {
   width: 500px;
   display: inline-block; //or block if necessary
}

Answer №1

To experiment, you can try using the table-layout property with .container and span.

Browsers are expected to automatically create any missing elements needed to generate the first table-cell.

See a demonstration here

span {
  display:table-cell;
  width:500px;
  border:solid;
}
table {
  border:solid;
  margin:0;
  width:100%;
}
.container {
  display:table;
  width:100%;
  table-layout:fixed;
}

This method should be effective

due to the use of table-layout:fixed

and because browsers will fill in missing elements on their own (such as creating a tbody within a <table> if missing, or adding necessary tags like html or body).

Answer №2

Demo

Styling with CSS

body, html {
    margin:0;
    padding:0;
}
.container {
}
table {
    width: calc(100% - 500px);
    border-collapse: collapse;
    float: left;
}
/* Zebra striping */
 tr:nth-of-type(odd) {
    background: #eee;
}
th {
    background: #333;
    color: white;
    font-weight: bold;
}
td, th {
    padding: 6px;
    border: 1px solid #ccc;
    text-align: left;
}
.span {
    display: inline-block;
    width: 500px;
    height: 100px;
    background: red;
}

Adding html elements

<div class='container'>
    <table>
        <thead>
            <tr>
                <th>First Name</th>
                <th>Last Name</th>
                <th>etc</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Gaurav</td>
                <td>Singh</td>
                <td>etc</td>
            </tr>
            <tr>
                <td>Gaurav</td>
                <td>Singh</td>
                <td>etc</td>
            </tr>
        </tbody>
    </table> <span class='span'>....</span>

</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 an element that remains in a fixed position in relation to its parent container, rather than the entire screen - here's how

Encountering an issue where, upon user scrolling down, a portion of the sidebar becomes fixed in position. However, when attempting to apply CSS for fixing the sidebar element, it ends up fixed to the entire screen instead of just within its parent contain ...

Guide to positioning an image absolutely within a div

I am struggling to position a small close image within a div without success. The goal is for the image to always appear on the left side of the div, regardless of the content. The content consists of a text label with a maximum length of 8 characters. Iss ...

Troubles encountered with adapting apexcharts size within a react environment

As a novice front-end coder transitioning from a data analyst background, I am currently facing an issue with integrating an ApexChart visual into a flexbox element. The visual appears fine at a resolution of 2560x1440 pixels. However, upon further resizin ...

Struggling with implementing Vue.js for making a task list using Bootstrap 5

I'm trying to get the hang of Vue.js. I've been working on setting up a to-do list where users can input tasks, but I'm having trouble getting the list to display correctly when I define the method. It seems like my add() function isn't ...

Conceal or eliminate an element from the list upon selection

I have a list that is unordered, and I am looking for a way to remove an option from the list if it is selected. For Example: If a user chooses one of the options from the list, I want that option to be removed as it will be considered the selected value ...

What is the best way to create images that move at random speeds down the screen in a JavaScript video game?

I have a game in development where players control a spaceship moving towards the screen using PC controllers. I am now working on implementing fireball images that drop randomly from the top of the screen at a specific speed and quantity (since there is o ...

Manipulating CSS rules using JavaScript/jQuery

My goal is to create a function that generates a grid based on table data. While the function itself seems to be working, I am encountering an issue where the classes applied are not resulting in any style changes. Below is a snippet of my function: $(doc ...

Clicking the mouse will eliminate the input's focus

I have an input element with type radio. When the element receives focus via keyboard, it should display a focus style, but when accessed via mouse click, the focus should be removed. I want to achieve this without using JavaScript or jQuery. This element ...

Is there a way to prevent the body content of this modal from overflowing outside of the modal box? I want to ensure that the text remains contained within the modal

Recently in my Angular 7 application, we integrated Bootstrap 4 for styling. However, we encountered an issue where the text inside a modal spills out of the box. How can we modify the CSS to ensure that the content remains responsive and contained within ...

Position the Figcaption over the Image

Running a website, I am facing an issue with adding copyright information to the top-left corner of an image. The img element is set to float:left; and the figcaption appears after it. However, there are some complications: 1) Using the position:absolute ...

The active class in the Bootstrap carousel is not updating when trying to change

I am struggling to make the Twitter Bootstrap carousel function properly. Although the slides automatically change after the default timeout, the indicators do not switch the active class, resulting in my carousel failing to transition between slides. Ini ...

What is the best way to effectively utilize bootstrap and JavaScript together?

I've been trying to implement some JavaScript on top of Bootstrap 5, but it doesn't seem to be working. I'm not sure if I'm doing something wrong with the JavaScript itself, or if there's an issue with how it's interacting wit ...

iOS creates dynamic images with artifacts that appear on a generated and animated canvas

I am currently developing an HTML5 web application for WeChat, compatible with both iOS and Android devices, using only pure JavaScript without any third-party libraries like jQuery. The main feature of my app involves creating visually appealing animation ...

Is there a way to make jQuery insert a placeholder before the image fades in?

I've implemented a fading effect on elements in my page using the following script: $(document).ready(function() { $('#holder').delay(650).fadeIn(1000); $('#sidepanelHolder').delay(650).fadeIn(1000); $( ...

Issue with scrolling when Bootstrap modal is opened on top of another modal

I recently created a modal using bootstrap, and inside this first modal, I added a button to open a second modal. Initially, everything worked perfectly fine. However, after closing the second modal, the scroll within the first modal stopped functioning pr ...

How can one make the image pop and stand out from its background?

I am trying to achieve a similar effect as shown in this design where the image stands out from its background. I have managed to do that, but I am struggling with aligning the image to the center and bottom with overflow. Can anyone help me with this? Th ...

Struggling with color styling within link_to tags in Rails 4 - having trouble getting inline styling to show up correctly

<%= link_to "Sign In", new_user_session_path, :method => :get, style: "color:white" %> <h3>Read More - <%= link_to 'Article', action: :articles, style: "color: purple" %></h3> <%= link_to 'Click to Read More Abo ...

Choosing a sibling element before another using CSS

Is there a way to make both of my icons display some text when hovered over? I managed to make it work, but the leftmost icon doesn't trigger the span element when hovered. The text needs to appear on the left side of the Yelp icon for design reasons. ...

Cannot locate the index for removal of element in the array

Having trouble removing an element from an array when the animation ends? You may be encountering the error "index is not defined." Need help finding and correctly removing a specific index when the animation finishes? Check out the drop() and remove() me ...

Enhance User Experience by Making Each Background Slideshow Image Clickable

I want to create a background image slideshow for my photography portfolio. Each image should fade in and link to its respective gallery. I also want the text box to fade with the images and link to the corresponding gallery. The challenge is adding the li ...