How can we adjust the widths of the right and left cells to show their contents in the most compact

Below is the HTML code I am working with:

I have utilized display:table and display:table-cell to structure the divs in a table-like format without resorting to using an actual HTML table. The setup is functional, however, the widths of the left and right cells are significantly larger than the content they hold.

You can view a demo related to this query at the following URL:

Inquiry: What CSS adjustments can be made to enable the left and right cells to automatically resize based on their content? I prefer not to manually specify their widths.

In my attempt to address this issue, I adjusted the center div to 100% width. While it achieved the desired outcome, the margins of the left and right divs were disregarded. Thus, I believe there might be a more effective solution that I am overlooking!

https://i.sstatic.net/nElDs.png

HTML Markup

<script></script>
<style>
    #rgcmd {
    width:100%;
    display:table;
    position:relative;
    border:solid 1px green;
    }
    .leftCell {
    margin-right: 5px;
    height:100%;
    display:table-cell;
    vertical-align:middle;
    border-right:solid 1px red;
    }
    .rightCell {
    margin-left: 
    5px;height:100%;
    display:table-cell;
    vertical-align:middle;
    border-left:solid 1px red;
    text-align:right;
    }
</style>
<div id="rgcmd">
    <div class="leftCell"><a href="#" onclick="alert('Go Left');">Go Left</a></div>
    <div style="display:table-cell; vertical-align:top;">
        <span id="status">Your status is offline</span>
    </div>
    <div class="rightCell"><a href="#" onclick="alert('Go Right');">Go Right</a></div>
</div>

Answer №1

To achieve the desired layout, apply white-space: nowrap to the left and right cells, and set width: 100% on the center cell:

#customLayout {
  width: 100%;
  display: table;
  position: relative;
  border: solid 1px blue;
}
.leftColumn {
  padding-right: 5px;
  height: 100%;
  display: table-cell;
  vertical-align: middle;
  border-right: solid 1px orange;
  white-space: nowrap;
}
.rightColumn {
  padding-left: 5px;
  height: 100%;
  display: table-cell;
  vertical-align: middle;
  border-left: solid 1px orange;
  text-align: right;
  white-space: nowrap;
}
.centerColumn {
  width: 100%;
  display: table-cell;
  vertical-align: top;
}
<div id="customLayout">
  <div class="leftColumn"><a href="#" onclick="alert('Move Left');">Move Left</a>
  </div>
  <div class="centerColumn">
    <span id="info">Your information is confidential</span>
  </div>
  <div class="rightColumn"><a href="#" onclick="alert('Move Right');">Move Right</a>
  </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

Take out the class from the navigation bar once the page has finished loading

My website is built on a single page that dynamically changes its content within a div container with some JavaScript and CSS animations applied (I've assigned the "animated" class to my nav). I'm looking for a way to remove this class after the ...

Customize a web template using HTML5, JavaScript, and jQuery, then download it to your local device

I am currently working on developing a website that allows clients to set up certain settings, which can then be written to a file within my project's filesystem rather than their own. This file will then have some parts overwritten and must be saved ...

Angular2: Implementing regular updates to a large number of rows based on a set time

I am currently facing an issue with updating a large number of rows (approximately 2000) every 1 or 2 seconds. While I have a solution in place, my browser sometimes hangs while executing it. One specific problem with the current solution is that the "up ...

Tips on maintaining the Parent menu in a hovered state when the mouse is over the child menu within a Dropdown Menu

I have been working on creating a dropdown menu that functions correctly. However, I am facing an issue where the top menu, when hovered, turns white, but as soon as I move down to the submenus, the top menu reverts back to its original color. Is there a ...

Is it possible for me to insert a hyperlink that will show a PDF in a web browser without triggering a download?

I am looking for a way to allow visitors to my website to easily access and view a PDF directly in their browser by clicking on a specific URL or button. I want the PDF to be displayed within the browser itself, rather than prompting a download. Any guidan ...

Layer one image on top of another using z-index

I'm having trouble layering one image on top of another in my code. Here is my code: body { background: #000000 50% 50%; height: 100% width:100%; overflow-x: hidden; overflow-y: hidden; } .neer { z-index: 100; position: absolute; } ...

When trying to load a php page2 into page1 via ajax, the Javascript code fails to execute

Currently, I am in the process of learning PHP and JavaScript. I have encountered a particular issue with a webpage setup. Let's say I have a page called page1 which consists of two input fields and a button labeled 'Go'. Upon clicking the & ...

What could be the reason why the text inside the anchor tag is not showing up?

The text within the anchor tags, such as feature and pricing, is not visible on the webpage. I attempted to add a color attribute to change this but it did not have any effect. The only thing that changed was the cursor when hovering over the links, while ...

Learn the process of sending notifications upon clicking on an advertisement by a user

I'm currently working on a system that displays ads on mobile devices. Our clients have the option to use their own custom HTML code for their advertisements. We want to be able to track when a user clicks on these ads. I am considering wrapping the u ...

Generate a JavaScript list using data retrieved through AJAX

Hello, I am working on a function that extracts information from XML data and displays markers on a Google map. My current challenge is to create a path between two points. Here is the code snippet for retrieving the data: $.ajax({ t ...

Adjust the size of various elements in real-time

I am currently working on a script that adjusts the size of filtering-inputs based on the width of gridview columns. Initially, it only involved textboxes, and I managed to make it work adequately: Script Snippet: $("#<%= myGridView.ClientID %> th" ...

``There seems to be an issue with implementing the SlideDown feature in JavaScript

I am having an issue with a code where the expected behavior is to slide down a div when a person hovers over text, but it's not working. Can someone please review and identify the cause of this problem? <script type="text/javascript" src="/js/jqu ...

Markdown boasts of a sturdy partially-horizontal line

Is there a way to create a partially colored line in Markdown? I attempted using the following code: <hr style="border:0.3px solid green; width:40%"> </hr> However, instead of a partial green line, I am seeing a full gray line. Any idea on w ...

Creating an Image slideShow with only one Image - making it slide on itself

Apologies for this confusing and vague question. Imagine I only have a single image, and I want to create a slideshow using technologies like Angular, jQuery, JavaScript, or just CSS. What I am aiming for is when the user clicks on my slide button, the i ...

Grab the SVG and resize it to a smaller scale

I have a small application built using Raphael.js that creates a node network with SVG and reorganizes it based on user selections. My goal is to capture the SVG image I've created and display it in a "mini-map" format at the bottom of the screen. Si ...

What is the most effective way to prevent a <pre> element from overflowing and instead horizontally scrolling when placed within a table cell that is not

I am faced with the following situation: <table> <tbody> <tr> <td> <pre style="overflow: scroll;"> (very long lines of text) Find my JSFiddle here: https://jsfiddle ...

Struggling to make AngularJS routes function properly

After starting from scratch, I rebuilt it with freshly downloaded angularJS (version 1.5.8). I simply placed both angular.js and angular-route.js in the library folder following this setup: https://gyazo.com/311679bf07249109671d621bc89d2d52 Here is how in ...

Utilizing both while and foreach loops in PHP simultaneously

I've been trying to merge a "foreach loop" and a "while loop" to make my foreach run 30 times, as indicated by $counter. However, the issue is that my "while" loop starts the "foreach" loop anew each time, resulting in it running 30 times over and ove ...

When combining li, radio-input, and label, there is no padding at the top

I am facing an issue with the padding at the top of my list items. Here is a link to a working Fiddle: [http://jsfiddle.net/TH3zq/9/][1] http://jsfiddle.net/TH3zq/9/ Can anyone suggest a CSS style solution for this problem? Thanks, John ...

I'm currently in the process of creating a snake game using HTML5. Can you help me identify any issues or problems that

I am experiencing an issue where nothing is appearing on the screen. Below are the contents of my index.html file: <html> <body> <canvas id="canvas" width="480" height="480" style="background-color:grey;"></canvas> <script src=" ...