Center the content within a div by utilizing either the auto or percentage values

Hey there, I'm currently facing some issues that I can't seem to solve. I've tried various solutions but none of them are working for me. My goal is to center the content of one div inside another.

I prefer using only auto or % values as I also want the website to be mobile-friendly.

Here's the code I have so far: http://codepen.io/anon/pen/xHpaF/. I'm trying to align those red boxes to the center of the wrap div.

If anyone can offer assistance, it would be greatly appreciated! Thank you!

Answer №1

In the first place, it's important to note that your <div id="content" /> is actually an ID, not a class. Therefore, you should modify your CSS selector from .content to #content. The use of float can disrupt the alignment achieved through text-align: center;. By removing this and setting it to display: inline-block;, you may be able to resolve the issues:

You can test the changes here: http://codepen.io/anon/pen/ncviE/

Updated CSS:

#content {
  width:auto; 
  height:250px; 
  margin:0 auto; 
  background:#0C0; 
  display:table-cell; 
  text-align:center;
}

.view {
   display: inline-block;
   float: none;
}

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

Combining various types of media stylesheets with a print stylesheet

I'm struggling with a school assignment that is asking me to compare two different ways of linking a print stylesheet with another stylesheet in HTML. The assignment requires me to explain when each technique would be appropriate: <link rel="style ...

Modifying color in CSS for an SVG that is dynamically animated using JSON

I recently obtained an animated hamburger menu icon for free from this particular website. The animation triggers on click and reverses on the second click, functioning smoothly. Now, I am seeking guidance on changing its color using CSS. // JSON var ...

Retrieve a value for a textbox by comparing the values of two separate combo boxes

Hey there, I'm brand new to javascript and could really use some assistance. I've got a form with two combo boxes, one for Pass Type and the other for Duration. I'm trying to set a value in a text box based on the user's selections f ...

I struggle with managing a z-index in conjunction with an absolute position

Check out this codesandbox to see the issue where the 3 dots button is displayed over the menu. Click on the 3 dots to understand the problem. Below is the CSS code for the menu: .more-menu { position: absolute; top: 100%; z-index: 900; float: l ...

Breaking Points in Tailwind are crucial for creating responsive designs

I am attempting to create a simple div where I want px-5 for mobile screens and px-20 for larger screens. However, only px-5 is working and px-20 is not applying on large screens. Can someone help me understand why? Code Output Image <div class=" ...

Select2 functions properly on online coding platforms like Fiddle but encounters issues when running on

I decided to incorporate Select2 (Source: ) for the purpose of selecting and tagging. After studying an example on JsFiddle I attempted to reproduce it on my local server (xampp). Below is the code, mirroring that of the fiddle. <!doctype html> &l ...

Prevent the date each month in asp.net

Is there a way to block Sundays for all months and years in a text box with the Text mode set to Date? <asp:TextBox ID="txtfromdate" runat="server" Enabled="True" OnTextChanged="From_TextChanged" TextMode="Date" ></asp:TextBox> ...

How can you extract information from a text document and seamlessly incorporate it into an HTML webpage?

I am currently working with an HTML file structured like this.. <html> <body> <table border="1" cellpadding="5"> <tr> <td>Some Fixed text here</td> <td id="data">---here data as per values in external text file--- ...

React element featuring various interfaces

Currently, I am working on styling a React component in a way that allows for either horizontal or vertical styling without using flexbox. My question is, how can I pass styles down to the component in a way that applies individual styles to the child ele ...

What are the benefits of using PHP heredoc to create straightforward HTML code?

I've recently taken over the reins of a website and am looking to implement some changes. It's interesting because everything is structured using heredoc PHP scripts, yet there doesn't seem to be any actual PHP processing happening within th ...

Side panel with collapsible Bootstrap functionality

The accordion panel is structured as follows <p> <a class="btn btn-primary" data-toggle="collapse" href="#collapseExample" role="button" aria-expanded="false" aria-controls="collapseExample"> Link with href </a> </p> <di ...

Display a carousel image in the center with previous and next sliders for easy navigation

Currently, I am referencing this tutorial My goal is to make the image smaller and centered on the page I have made adjustments to the CSS to decrease the size: min-height: 350px; max-width: 900px; However, the image is still aligned to the left and th ...

Tips for displaying a variable passed through a PHP form using the echo function

Below is the PHP form code that retrieves variables from another page: <?php $fname = "$_POST[fname]"; $lname = "$_POST[lname]"; //these values are coming from another form ?> <form action="test.php" method="post" class="form-inline vh_pr ...

The password-protected HTML blog remains hidden until the correct entry is entered into the password box, causing

After successfully creating a password redirect page where entering the correct password redirects you to another URL (psswrdtest.tumblr.com with the password as correctpsswrd, redirecting to google.com*), I attempted to improve it by making the password p ...

Generating dynamic HTML content in React using a variable within a string

Currently, I am utilizing TypeScript in a React application. I am receiving the following data from an API: a) a list of variable names ["name", "surname"] b) several strings in simple HTML form with variables "<p>Hello, ho ...

Reach the underlying DOM element <input>...</input> using styled-components

I am struggling to make changes to the styling of an <input /> wrapped in a React component using styled components. The style I want to override is visible in Dev Tools here: .ui.input input {...} I believe the wrapping component needs to pass th ...

Preventing SVG filters from disappearing during hover with smooth transitions

Exploring new concepts, I have delved into the fascinating world of SVG objects. I recently designed a button with a filter applied to it. However, the designer I am collaborating with has requested a hover effect where the filter (a drop shadow) transitio ...

Obtain user input and showcase it on the screen using a combination of Ajax and PHP

Whenever a user inputs a value in the textbox, it should dynamically display on the screen. I have successfully implemented this feature once, but I am encountering difficulties with implementing it for a second input. Your help is greatly appreciated. fu ...

What is the best approach for extracting multiple images from a URL and inserting them individually into separate divs using JavaScript?

Is there a way to elegantly display images horizontally on my website without them stacking on top of each other? I want each image to be placed side by side in a specific space for a beautiful layout. <!DOCTYPE html> <html> <head ...

Assign position values to an element inside a DIV container using JavaScript, Cascading Style Sheets, and

I am currently creating some visual representations of journal issues. I am looking to showcase blocks of text using basic DIVs or other elements inside another DIV, mirroring the organization of the issue page. In order to achieve this, I need to positi ...