Tips for positioning and resizing an HTML slide

Once again I am faced with my favorite HTML/CSS dilemma and am feeling frustrated. Seeking advice from the experts on SO this time around to solve my problem. Here it is...

Imagine you want to create a slideshow in a browser. The requirements are simple:

  1. The slide content should be wrapped within a slide frame.
  2. The slide frame should be centered both horizontally and vertically within the browser viewport.
  3. The slide frame must not extend beyond the browser viewport.
  4. If the slide content overflows beyond the slide frame (which has expanded to fill the entire viewport), scroll bars should appear.

One important point is that the viewport itself should never require scroll bars.

I am aiming to implement { margin: auto auto; } for my slide. The closest representation I could come up with in HTML/CSS is as follows:

<style>
  html, body {
    margin: 0;
    overflow: hidden;
    padding: 0;
    height: 100%;
    width: 100%;
  }

  #f0 {
    bottom: 2em;
    left: 2em;
    position: absolute;
    right: 2em;
    top: 2em;
  }

  #d0 {
    height: 100%;
  }

  #c0 {
    height: 100%;
    position: relative;
    width: 50%;
  }

  #d1 {
    height: 100%;
    position: absolute;
    right: 0;
  }

  #c1 {
    height: 100%;
    overflow: auto;
    position: relative;
    left: 50%;
  }

  #d2 {
    background: yellow;
  }

</style>

<body> 
  <div id="f0">
    <div id="d0">
      <div id="c0">
        <div id="d1">
          <div id="c1">
            <table height="100%" cellpadding=0 cellspacing=0>
              <tr><td id="td" align="Left">
  <div id="d2">
        Lorem ipsum...
        Lorem ipsum...
        Lorem ipsum...
        Lorem ipsum...
        Lorem ipsum...
        Lorem ipsum...
        Lorem ipsum...
        Lorem ipsum...
        Lorem ipsum...
        Lorem ipsum... Lorem ipsum...
        Lorem ipsum...
        Lorem ipsum...
        Lorem ipsum...
        Lorem ipsum...
        Lorem ipsum...
        Lorem ipsum...
        Lorem ipsum...
        Lorem ipsum...
        Lorem ipsum...
        Lorem ipsum...
  </div>
              </td></tr>
            </table>
          </div>
        </div>
      </div>
    </div>
  </div>
</body>

You can also find this code here: http://jsfiddle.net/qmEYU/9/

This code uses several positioning techniques. It results in a centered slide that adjusts dynamically as the viewport changes. When the viewport shrinks, the slide shows a scrollbar.

However, this setup comes with its own set of challenges:

  • To position the scrollbar (which actually confines the TABLE and not the #d2) next to the (yellow) slide, I had to use table.width=auto. To center the now narrow table, I resorted to using the 50% trick. But this limits the available total width to the centered element to only 50% of the viewport.
  • I cannot place a border around the slide. This is because the "slide" is more of an illusion: the slide content (#d2) is inside a TD which is centered within the TABLE. As #d2 expands, the TD will adjust, causing the bottom border to spill into the overflow. Adding a border around #c1, the scrollbar container, means if the slide content collapses, the border will show around the TABLE instead.
  • I need to position buttons below the slide that should always be visible, regardless of slide content. Since I cannot distinguish any element as THE SLIDE, I face challenges while placing the buttons.

I came up with a JavaScript solution but it's not optimal due to cross-browser compatibility issues. I'm seeking alternative CSS-based solutions here.

Thank you for your assistance.

Answer №1

To center horizontally, use display:table;margin:0 auto;. However, you'll need a workaround for IE involving 50%.

Could you create a jsfiddle demonstrating this?

Have you researched how to align elements horizontally and vertically?

UPDATE: Is this the solution you are seeking? Check out

This may require adjustments for compatibility with IE. Let me know if this meets your requirements.

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

Utilize SVGs efficiently by loading them once and reusing them

Is it possible to use the same SVG element twice on a web page without having to load it again? I am changing the CSS of the SVG using JavaScript, so I believe the SVG must be directly embedded in the HTML rather than included as an object. Both instance ...

How to Create a Hover Drop Down Menu that Disappears When Not Hovered Over

When I hover over the drop down menus on my Navbar, they display perfectly. However, it's impossible to click on the items in the menu because as soon as I move the cursor away from the main nav, it disappears. I've tried adding display:block and ...

Is there a way to synchronize the expanded row data with the columns of the main table in Material UI's collapsible table?

I have a feature where, upon clicking the expand icon, a row is expanded. I am looking to align the data of these expanded rows with the columns of the main table. To achieve this, I am utilizing Material UI's collapsible table component! How can I i ...

Guide to capturing and playing audio on iOS6 with HTML5

Our team is currently working on a web application using HTML5 and Javascript. We are facing a challenge with implementing voice recording functionality, as the Wami Api we tried is not compatible with iPad due to its use of flash for recording. Could yo ...

There was an uncaught error in AngularJS stating that the URL in the HTTP request configuration must be a string

I've been working on a web application and have encountered some challenges. One particular issue I'm struggling with is related to the following code snippet: this.bookSpace = function (date, spaceId) { swal({ title: "Are you sure?", t ...

Changing the background of one div by dragging and dropping a colored div using jQuery

Looking at my HTML code, I have 4 <div> elements - 2 represent doors and 2 represent colors based on their respective id attributes. My goal is to enable users to drag any color to either door (e.g. blue on the left door and black on the right) and ...

Creating dynamic visual representations on a website by generating real-time graphs

Every minute, new data is added to my csv file. I would like to display this data as graphs on a webpage. The file contains 12 parameters and I am interested in creating charts that show the relationship between each parameter and time. ...

Select Box in HTML now supports the addition of a Background Image, enhancing

Currently, I am trying to customize the select option box using HTML and CSS. My main goal is to incorporate a scroll feature into the option box. However, I have encountered an issue where the image of the checked box remains visible on the screen. Below ...

Display an "add to cart" button and a discount image when hovering over

Currently, I am in the process of developing an Online Shopping website using PHP. To enhance the design of my website, I have implemented bootstrap. One specific query I have is how to display an 'add to cart' button and a discount image when a ...

Issues with Gulp Autoprefixer Functionality

I'm struggling to make Autoprefixer work with Gulp. Despite using opacity, gradients, and transforms in my CSS, I can't see any vendor prefixes being added. However, everything else seems to be functioning correctly. Below is my gulp file: var ...

Finding a Div ID with Python and Selenium

Currently, I am facing an issue where I have to interact with an image of a dropdown arrow in order to select an item that will automatically fill the input box. Both the dropdown arrow and input box are components within the dev id:decision. I require ass ...

Unclear about how inheritance works with the general sibling combinator "~" (tilde)?

When attempting to color list items in an unordered list using the general sibling combinator, nothing seems to happen: http://jsfiddle.net/bkbehpv0/ p { color: blue } h1 ~ li { color: red; } <h1> Title of site </h1> <p> Text in t ...

Zero results returned for the angularjs script

I am working on enhancing my skills in angularjs, but I am facing an issue where only the categories are being displayed and the products are not showing up. There are no error messages, so I am having trouble pinpointing where the problem lies. Here is t ...

Select a specific division element using a pseudo element

My goal is to specifically target the third div in my HTML code with a class called .counter-wrap. When viewed on a mobile device, this particular div has a margin-bottom of 40px. I am looking to eliminate the margin-bottom for only the third stacked div n ...

I encountered a data discrepancy while attempting to link up with a weather API

This is my debut app venture utilizing node.js and express for the first time. The concept behind this basic application involves connecting to an external API to retrieve temperature data, while also allowing users to input their zip code and feelings whi ...

The layout is being disrupted by a bug in the nested list div in IE 7

I am currently in the process of creating a website with nested lists in the sidebar. The parent list contains children li elements. Initially, only 4 list items are displayed, and the rest should be hidden with an option to "Show All" above them. Here is ...

Surprising automatic scrolling upon pressing the keydown button in Bootstrap

My webpage appears normal with the Bootstrap framework, but whenever I press the down key, the screen scrolls down and displays unexpected white space due to reaching the end of the background-image sized at 1798px * 1080px. Please refer to the image: He ...

Is it possible to incorporate vector graphics/icons by simply adding a class to a span element in HTML5?

In order to add a glyphicon icon to our webpage, we simply need to include its class within a span element, as demonstrated here: <span class="glyphicon glyphicon-search"></span> We also have a few files in .ai format that can be converted to ...

Include token in src tag requests Angular version 8

In the process of developing a website, I have encountered a challenge. I am creating a platform where users can access another website I am currently working on after they log in. Once authorized, users receive a JWT token which is sent in the header with ...

Footnote fiascos

I am currently in the process of creating a footer for my webpage and have implemented the following CSS styling: #footer { height: 100px; background-color: #F3F3F3; position: absolute; bottom: 0; } Although the footer is displaying at th ...