vertically centering a div specifically on laptops

What is the best way to vertically center a div on lap-tops? (...)

body {padding: 4% 16%;}
body {top:0px; left:0px; bottom:0px; right:0px;}
body {border: 12px solid darkred; border-style: double;}
body {background-color: #FAFCB4;}
p {font-size: 80%; text-align: justify;}
@media only screen and (min-width:768px) { 
/* For desktop: */
body {position: absolute;}
body {padding: 6% 18%;}
p {font-size: 100%;}
}

<div style="margin:auto; max-width:525px;">
Lorem ipsum. This is a test. Lorem ipsum. This is a test. Lorem ipsum. This is a test. Lorem ipsum. This is a test. Lorem ipsum. This is a test. Lorem ipsum. This is a test.</div>

I'm looking for simple solutions to center my div vertically on laptops. Any straightforward suggestions would be greatly appreciated. Thank you!

Answer №1

While someone already addressed your media query inquiry, I wanted to share another approach.

An alternative method for centering elements is by utilizing positioning.

div.center {
  position: absolute;
  top: 50%;
  left: 50%;
  -webkit-transform: translate(-50%, -50%); /* Compatible with Safari and iOS */
  transform: translate(-50%, -50%);
}

You're welcome!

Answer №2

    body {
      padding: 4% 16%;
      margin-top: 40%;
      margin-bottom: auto;
      border: 12px double darkred;
      background-color: #FAFCB4;
    }

    p {
      font-size: 0.8em; /* Remember to use "em" instead of percentages*/
      text-align: justify;
    }

    @media only screen and (min-width:768px) { 
    /* For desktop: */
    body {position: absolute;
      padding: 6% 18%;
    }
    p {
      font-size: 1em;
    }
    <div style="margin:auto; max-width:525px;">
    Lorem ipsum. This is a test. Lorem ipsum. This is a test. Lorem ipsum. This is a test. Lorem ipsum. This is a test. Lorem ipsum. This is a test. Lorem ipsum. This is a test.</div>

Consider using "em" for your font sizes as it provides a more flexible and user-friendly approach. When using em, you're essentially setting the font size relative to the user's preferences. More information on "em" units can be found here: (http://www.w3schools.com/cssref/css_units.asp)

You can optimize your CSS by combining multiple styles within one element, which enhances efficiency and effectiveness in styling.

Additionally, I positioned the element with a top margin of 40%. This assumes that the vertically centered element will occupy about 10-20% of the page. Adjust the percentage based on the size of your element to achieve the desired vertical alignment. By setting margin-bottom:auto;, you can automatically center the element vertically relative to the remaining space. Feel free to adjust the values until you reach your desired styling. Best of luck! ;)

Answer №3

Utilizing Flexbox in CSS3 can be done as shown below.

// Ensuring body occupies the entire viewport width and height
html {
    height: 100%;
    width: 100%;
}
// Styling for the container div, although using body is not recommended
body {
    height: 100%;
    width: 100%;
    // Transforming the container into a Flexbox
    display: -webkit-flex;
    display: flex;
    // Horizontal alignment
    justify-content: center;
    // Vertical alignment
    -webkit-align-items: center;
    align-items: center;
}

If you specifically want this layout for laptops only, enclose the CSS rules within:

// Regular Screens
@media screen
    and (min-device-width: 1200px) 
    and (max-device-width: 1600px) 
    and (-webkit-min-device-pixel-ratio: 1) { 
}

// Retina Screens
@media screen 
    and (min-device-width: 1200px) 
    and (max-device-width: 1600px) 
    and (-webkit-min-device-pixel-ratio: 2)
    and (min-resolution: 192dpi) { 
}

Answer №4

Media Query constraints for targeting laptops have been thoroughly debated by now.

To achieve vertical alignment, an alternative approach involves utilizing CSS display properties such as table and table-cell, along with vertical-align to centrally position elements regardless of the container's height.

You can refer to this article on CSS-Tricks for more information.

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

What is the best method for arranging multiple images in a grid while keeping them within a specific maximum width and height?

I've been staring at this problem for a while now, attempting every possible solution to resize images and maintain aspect ratio, but none seem to work in my case. Here are two images that I manually resized so you can view them side by side: highly ...

Increase the border thickness around my title and add a border after an image

I'm encountering difficulties when trying to replicate the style showcased in this image using CSS. My specific challenge lies in creating a yellow horizontal line above the title "nossos numeros" and blue vertical lines between "Cursos", "Alunos", an ...

Responsive dropdown menu in Bootstrap 5 navbar

Update: I successfully fixed the desktop version. Now, I just need to work on making it responsive for mobile. The problem I am encountering is that the dropdown menu doesn't function properly on both mobile and desktop. Although the styling looks go ...

Adding a Material Icon to an input type of 'submit' can be done by including the necessary HTML code and

Can Material Icon classes be used with button inputs? For example, using the <button> tag allows us to do this: <button class="btn btn-secondary btn-block btn-flat" id="submit"><i class="zmdi zmdi-long-arrow-right"></i> Login< ...

Are max-width:auto and max-width:100% the same thing?

Does max-width set to auto have the same result as setting max-width to 100% If not, what is the distinction between them? ...

CSS - Issues with transition smoothness on Safari

I am facing an issue with a simple font-size transition in Safari, causing stuttering while it works smoothly in Chrome and Firefox. I'm not sure if this is a Safari problem, a Webkit issue, or something else entirely. Any workaround suggestions would ...

Ways to extract metadata from a given URL

Recently, I've delved into the world of JavaScript and I'm looking to extract metadata from a given URL. Whenever a URL is entered into an input field, I want to retrieve its meta data - a simple task in HTML using JavaScript. However, every time ...

Styling the arrow of a CSS select box

Is it possible to place a dropdown arrow inside a select box? I have tried, but the arrow always displays behind the select box. How can I modify the CSS code to position the arrow properly inside the select box? .form-item-custom-search-types::before { ...

What is the best way to apply the addClass method to a list element

I've been searching for a solution to this issue for some time now, and while I believed my code was correct, it doesn't appear to be functioning as expected. HTML <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js ...

Looking to streamline your design? Find out how to translate multiple elements to the same position seamlessly

Greetings! This is my debut post on this platform. I am currently engrossed in a project that has presented me with a challenge, and I'm reaching out to seek your insights on a potential solution. Here's the scenario: I have a parent div containi ...

Text overlaps when li element is nested inside another element

Seeking assistance in creating a CSS/HTML menu bar that triggers list elements on hover. Facing difficulty arranging the list in two columns. Code Sample:http://jsfiddle.net/Km922/1/ <!DOCTYPE html> <html lang="en"> <head> <meta ...

Can Selenium be used to retrieve a list of pinned IDs from a website?

I am currently developing a web scraper that is required to open multiple tabs of items with filled icons. Specifically, each page that needs to be opened contains the div class="course-selector-item-pinned" in its source code. <dropdown-conte ...

The problem arises when using `$state.go` as it does not properly transfer the value to `$stateParams`

Within componentA, I am using an angular code: $state.go('home', {selectedFilter: "no_filter"}); In componentB, I have the following code: if ($stateParams.selectedFilter === 'no_filter') However, I am encountering an issue where $s ...

The functionality of data attributes in jquery mobile is not functioning correctly

Within my header, I have included the following code: <link rel="stylesheet" type="text/css" href="css/jquery.mobile-1.4.5.css"> <script type="text/javascript" src="js/jquery-1.11.2.js"></script> <script type="text/javascript" src="js ...

Vue-Router functions only on specific routes

While my vue-router correctly routes the URL for all menu buttons, it's not displaying each Vue component properly. You can see a demonstration here. Here is a snippet of my HTML (using Vuefy) <div class="navbar-start"> <a ...

What purpose does tagging serve in my template for polymer property binding?

I'm currently exploring the way Polymer handles the rendering of properties in a custom element's template. I've come across some interesting behavior that I haven't been able to fully grasp yet. Specifically, I noticed that certain pro ...

apply a visible border to the item that is clicked or selected by utilizing css and vue

I have a list of items that I want to display as image cards with an active blue border when clicked. Only one item can be selected at a time. Below is the code snippet: Template Code <div class="container"> <div v-for="(obj ...

The v-btn label in Vuetify is extending beyond the lateral borders and overlapping

In this scenario, the button is supposed to break the line and increase its height. However, the text is overlapping the lateral borders (you can see this behavior in the Codepen link provided below). Is there a way to correct this issue? Codepen Link & ...

CSS: the max-width property does not constrain an absolute positioned container from overflowing

I've been trying to figure this out for hours, but I can't seem to get it right. My top menu has submenus that include a popup menu. In one of my popups, I need the container to have a maximum width of 170 pixels and all the items inside to wra ...

Utilizing vanilla JavaScript or ES6 to extract data from a JSON file

I am currently working on an HTML project where I need to extract data from a JSON file that cannot be modified. I am looking to accomplish this using pure JavaScript or ES6, but I am struggling to make it work. Specifically, I am trying to retrieve a link ...