steps for positioning a DIV element right in the middle of the browser

I'm currently developing a mobile app that includes both Pie and Bar Charts. I am looking for a way to position a fixed size DIV in the center of the browser window. Ideally, I would like the DIV to adjust its position if the browser is resized.

<div id="output" ><canvas id="cvs" width="350" height="350" >[No canvas support]</canvas></div>

<script>
$(document).ready(function ()
    {
        var pie = new RGraph.Pie('cvs', [45,20,25,10])
            .set('colors', ['#F00', '#F00','#666','#ccc'])
            .set('events.click', myEventListener)
            .set('events.mousemove', function (e, piechart) {e.target.style.cursor = 'pointer';})
            .set('title.y', 30)
            .set('key', ['sunday','monday','tuesday','wednesday'])
            .set('key.colors', ['#f00', '#f00','#666','#ccc'])
            .set('key.interactive', true)
            .set('key.position', 'gutter')
            .set('gutter.left', 45)
            .set('key.position.y', 315)
            .set('key.rounded', false)
            .set('radius', 100)
            .set('linewidth', 1)
            .draw();
    })
</script>

<style>
#output {
width: 350px;
height: 350px;
background-color: none;
position: absolute;
top:0;
bottom: 40%;
left: 0;
right: 0;
margin: auto;
}

</style>

Answer №1

Check out this live example: http://jsfiddle.net/Gajotres/8kXWV/

HTML Code:

<div data-role="page" id="index">
    <div data-theme="b" data-role="header">
        <h1>Homepage</h1>
    </div>

    <div data-role="content">
        <img src="http://images5.fanpop.com/image/photos/29500000/GamesRadar-Puppy-puppies-29566786-200-200.jpg" alt="puppy"/>
    </div>
</div>    

JavaScript Function:

function center(){
    var window_width = $(window).width();
    var window_height = $(window).height();

    var image_width = $('img').width();
    var image_height = $('img').height();

    var top_position = (window_height - image_height)/2; 
    var left_position = (window_width - image_width)/2;

    $('img').css({'top':top_position,'left':left_position+'px', position: "absolute"});
}

$(document).on('pageshow', '#index', function(){ center(); });
$( window ).on( "orientationchange resize", function( event ) {
    center();
});

CSS Styling:

.ui-content {
    position:absolute;
    top:40px;
    right:0;
    bottom:0;
    left:0;
}

Answer №2

Centering a Div in the Browser with CSS

If you want to center a div in the browser, even when the page is scrolled, you can achieve this using a simple CSS hack.

#output{
        position: absolute;
        margin: auto;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
    }

In order for this method to work correctly, the div (#output) must have fixed dimensions in terms of both height and width.

Answer №3

.center{
align-items: center;
background-color:white;
display: flex;
justify-content:center;
width:100vw;
height:100vh;
}

.obj{
align-items:center;
background-color: black;
display:flex;
height: 100px;
justify-content:center;
width:100px;
}

h4{
color:white;
}
<body>
  <div class="center">
    <div class="obj"><h4>Center</h4></div>
  </div>
</body>

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

Guide on how to select a specific button from a set of buttons in JQuery without using a class name or id

Is it possible to select a specific button from a group of buttons using jQuery without utilizing a class or id? For example: <body> <button>Click Me</button> <button>Click Me</button> <button class="food"& ...

Requirements for Controller via Html.ActionLink

When I set up the Home View, I create multiple links: <ul class="nav navbar-nav"> @foreach (var item in ViewBag.RegList) { <li>@Html.ActionLink((string)item.registryName, "Index", "Registry", new ...

Set the height of a div based on the height of another div

My challenge involves a textarea that dynamically expands as content is added to it. The structure of the textarea within a div element is illustrated below: <div id='sendMes' class='container-fluid'> <form action='#&apos ...

Having trouble with input functionality on iPad due to a CSS, LI, div, or clipping issue?

https://i.sstatic.net/muMSG.png One challenge I am facing is related to several inputs housed within an LI and div that are sortable using jQuery. The problem arises specifically on the iPad when attempting to click into the inputs to enter information - ...

`<div>` element with a class of "button" that listens for

I've been attempting to use a userscript to automate a button click. Inspecting the element reveals the button code as: <div class="q-w-btn cl"></div> Unfortunately, the button lacks an id attribute, making it difficult for me to select ...

Retrieve the HTML content from the string that is returned

I have the string containing a list structure in my code-behind file: string xmlContents = "<ul><li>Installation<br /><ul><li>Startup</li><li>Getting there</li><li>Steps</li>" + ...

What could be causing the disappearance of the search icon while using jQuery Mobile?

The label, text field, and search icon should all be displayed horizontally, but currently the search icon is not showing up in that layout. Check out my fiddle here: http://jsfiddle.net/yCUY7/ <div data-role="content"> <label class="searchL ...

Understanding the usage of jQuery transitionend to prevent interruptions in CSS animations

I found a similar thread on StackOverflow but I'm struggling to apply it in my current scenario. The Setup (Welcome Ideas) I've developed a jQuery slider that will have multiple instances on one page, each containing an unknown number of childr ...

The website does not display properly on larger screens, while showing a scrollbar on smaller screens

I'm encountering an issue with my website where I can't seem to find a solution no matter what I try. My goal is to ensure that my website looks consistent across all computer screen sizes. However, when viewed on larger screens, there's a ...

Creating a realistic eye blink animation using only CSS on an SVG element

Below is a segment from an SVG file featuring a hyphen character. <path class="right-eye" fill="#000000" d="m556.02527 384.85477l11.03009 -4.0496826l12.407532 33.71103l-11.03009 4.0496826l-12.407532 -33.71103z" fill-rule="nonzero"></path> I a ...

Ways to display encoded URL image using <img src>

I have a scenario where I am dealing with a URL of an image link location that does not have a file extension like .jpg, but when accessed it displays the corresponding image. How can I output this URL in PHP? For instance, take the example of the reCAPTC ...

What is the best way to prevent blank space when splitting a div into two parts?

Currently in the process of working on a school project that involves creating a website with an integrated database. The foundation for this project is an existing website from a previous assignment, created using PHP/HTML. The layout of my prior website ...

Center align a font-awesome icon vertically next to a paragraph of text

Is there a way to align a font-awesome icon on the left side of a text paragraph while keeping the text on the right side, even if it's longer and wraps underneath the icon? I've tried various code snippets but haven't found a solution yet. ...

What is the best method for assigning values to select dropdowns?

When it comes to setting the values of child options in a select dropdown, I am wondering what the best convention is. Here's the form field in question: <div class="field"> <label for id="schedule_type">Schedule Type</label>&l ...

The color of the left arrow on the tooltip cannot be altered

Currently, I am using a tooltip that is positioned on the right side: <i class="fas fa-question-circle text-blue" data-toggle="tooltip" data-placement="right" title="hello" ></i> I have attempted to modify the color of the too ...

The CSS background is not aligning correctly and appears to be cropped

My issue is that the background suddenly cuts off on my webpage. It was displaying correctly on Chrome, but now it's cutting off in the middle of the page (http://prntscr.com/kwsaxn). This is the CSS code I'm using: html, body { background: url ...

Utilizing LocalStorage for storing the most recent input value

Is there a way to set and retrieve the last input value using JavaScript or jQuery? I want to implement an onkeydown function for an input field that updates the input value, and then each time the page is loaded, that value is displayed. <input type ...

"Creating dynamic webpage designs with CSS for responsive layouts

Currently, I am in the process of building a website utilizing a CSS fluid layout approach which is rooted in adaptive and responsive web design principles. For this project, I am avoiding using fixed values such as pixels (px) and instead opting for perc ...

Trouble with CSS attribute selectors not functioning as expected

In my PHP script, I am generating a list of items with links like this: <a href="page.php?day=1&month=1&year=2017"><li>item 1</li></a> Now I want to apply different styles to some of these items based on their day= paramet ...

What is the best way to toggle the visibility of select tags using another select tag?

I have a piece of code with one parent select tag containing two options: apples and butter. Each option has a value of 1 and 2 respectively. When I select apples, another select tag should appear, and vice versa. However, my issue is that multiple select ...