I am struggling to get my CSS block to align properly

Hey there, I have this CS block that I put together, and it's a bit all over the place.

div.OSDCScreen .block_center {
  align-content: center;
  border: 3px solid green;
  background-image: url('../Images/OSDCBack.jpg');
  background-repeat: no-repeat;
  width: 595px;
  height: 836px;
  overflow: hidden;
}

My main issue right now is that I want to control the width and height of the block while also having it centered on the page.

align-content: center; 

The above code works fine individually, as does:

width: 595px;
height: 836px;

However, when I use them together, align-content stops working, and the block just sits on the left side of the screen with no positioning.

Do you have any tips for someone new like me trying to figure this out?

Answer №1

When it comes to centering content, the align-content: center property is specific to flexbox layout. You can learn more about it here. Alternatively, a simple way to achieve center alignment is by using margin:auto.

div {
  border: 3px solid green;
  background-image: url('//dummyimage.com/595x836');
  background-repeat: no-repeat;
  width: 595px;
  height: 836px;
  overflow: hidden;
  margin: auto
}
<div></div>

If you prefer to utilize flexbox, remember to apply display:flex and justify-content:center to the parent element.

section {
  justify-content: center;
  display: flex
}
div {
  border: 3px solid green;
  background-image: url('//dummyimage.com/595x836');
  background-repeat: no-repeat;
  width: 595px;
  height: 836px;
  overflow: hidden;
}
<section>
  <div></div>
</section>

Answer №2

To center the content, adjust the margin to auto.

div.OSDCScreen .block_center {
  align-content: center;
  border: 3px solid green;
  background-image: url('../Images/OSDCBack.jpg');
  background-repeat: no-repeat;
  width: 595px;
  height: 836px;
  overflow: hidden;
  margin 10px auto;
}

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

Subdomain Dilemma: Trouble with Images in Internet Explorer

Currently, I am working on creating a basic website on a subdomain. Everything appears to be running smoothly in Google Chrome except for one issue with the header image not displaying in IE8. The image link is . It seems that the problem may stem from IE ...

Effortlessly glide to the form and center your attention on the textarea

Upon clicking the link, I aim to accomplish three tasks: Implement smooth scrolling down to #form Automatically focus on the textarea for immediate message writing Add extra top margin to account for a fixed top bar on the website. <a class="link" ...

Merge an HTML file within another HTML file

As I work on developing a large website, I am looking to include a consistent navigation bar on all pages. Is there a way to streamline this process by creating the navbar in a separate .html file and importing it into each page? If so, how can this be a ...

Display or conceal various content within div elements using multiple buttons

I have a set of 5 image buttons, each meant to trigger different content within a div. When the page loads, there should be default content displayed in the div that gets replaced by the content of the button clicked. The previously displayed content shoul ...

Comparing JavaScript and jQuery for form validation

Hey there, I'm currently working on a simple form validation for my submission. The validation is functioning correctly using a basic JavaScript function, but when I attempted to switch over to jQuery, it didn't work as intended. Below is the co ...

Developing a new class within a function

In this section of the HTML file, I have set up a form to collect email and password information from new users in order to create a new profile: <form name="userInfo"> <fieldset> <legend>Create a new account</legend> ...

Creating a PHP-powered contact form for sending emails

I've been trying to set up a contact form, but I'm struggling to make it work. I downloaded a PHP contact form script, but I can't figure out how to get it functioning. Despite searching extensively on Google and here, I haven't found a ...

The navigation bar is not staying fixed

Having trouble with the Bootstrap navbar-fixed-top feature. I have not applied any other styling to the nav element and have even tried adjusting z-index values with no success. <header> <nav class="navbar navbar-fixed-top navbar-expand-l ...

Execute the function if the text or value begins with a certain character

I'm trying to determine whether the text within a span starts with the letter "x" and then execute a function. I've come across using "^" in strings for this purpose, but am having trouble implementing it to check the text... <span>xfoo&l ...

Adjusting the height of a div according to the changing heights of other divs

The sidebar contains a search field, two lists, and a text div. The height of the search field and text div always remains constant, while the heights of the two lists vary dynamically. I am exploring the possibility of using only CSS to make the height o ...

Add an image to IFromFile in your ASP.NET Core MVC application

I'm looking to allow users to upload images, save them as IFormFile, and then store them in a database. Here's what I have so far: In the product controller, there is only an add action for both get and post methods, the product model, and the a ...

Adding HTML elements to a Vue Tooltip

Does anyone know how to implement a tooltip in Vue for a table cell that has more than 22 characters in its content? I was looking into using the v-tooltip library (https://www.npmjs.com/package/v-tooltip) Setting the tooltip's content with a simple ...

Troubleshooting: Issues with jQuery's Class selector

Having trouble setting up an alert to trigger when a specific class anchor tag is clicked inside a div. This is what my HTML section looks like... <div id="foo"> <a class='bar' href='#'>Next</a> </div> And h ...

barchart rendered in SVG without the use of any external libraries

Creating a stacked barchart with SVG and HTML without relying on any third-party library has been quite a challenge. Despite searching extensively online, I have not come across a single resource that demonstrates how to build a stacked bar chart using pla ...

Placing two Slickgrid tables next to each other

Is there a way to arrange two slickgrid tables next to each other? Currently, I am creating two grids using grid = new Slick.Grid..., but the dimensions are hardcoded in CSS like this: #dynamic1 { /*this is grid one*/ position: absolute; top: 120px; ...

row-1 css classes on the top in a project built with Blazor

I'm currently working on customizing the standard layout provided in Blazor projects like WASM or server, but I'm a bit perplexed by the top-row px-4 css classes. Within the HTML code that appears to define the top row/header section is this sni ...

Designing an interactive backdrop that draws users in with just a

I'm currently working on putting together a website, and to assist me with this task, I looked at this html code and this css code. The code includes a grid of 12 images with logos and animations. However, the images are not linkable, and I would like ...

"Challenges Arising in Deciphering a Basic JSON Array

After countless attempts, I am still struggling to solve this issue. My PHP code is functioning properly, as it returns the expected data when "Grove Bow" is selected from the dropdown menu: [{"wtype":"Grove Bow","was":"1.55","wcc":"5","wbdmin":"12","wbdm ...

Every time I click the login button, my HTML page opens again in a new tab. How can I resolve this problem?

Every time I click the login button, my HTML page opens again in a new tab. How can I resolve this issue? I've created an HTML form where clicking on login opens the HTML page in a new tab. What is the solution to fix this problem? My HTML Code <!D ...

Tips for reducing word length in Bootstrap card text using Django and CSS

For my journal entries, I'm using Bootstrap's card component. It looks like this: View Journal List However, when I have a lot of text in the entry, the card stretches out too long. View Journal List with a Really Long Entry I want to show ju ...