Centering elements displayed as inline-block

Is there a simple method to center align an inline-block element?

I would prefer not to specify a width for the elements. This way, the inline-block element will adjust its width based on the content inside it, without needing to modify the CSS. The inline-block elements should be centered vertically and horizontally, with their text also centered.

For reference, refer to the code snippet below or view it on jsFiddle.

The current HTML structure:

<div>
  <h2>Hello, John Doe.</h2>
  <h2>Welcome and have a wonderful day.</h2>
</div>

The current SCSS code:

@import url(http://fonts.googleapis.com/css?family=Open+Sans:400,300,600);  

body {
    margin: 0 auto;
    background: rgba(51,51,51,1);
    font-family: 'Open Sans', sans-serif;
}

div {
    width: 100%;
    height: auto;
    margin: 15% 0;
    text-align: center;
    h2 {
        margin: 0 auto;
        padding: 10px;
        text-align: center;
        float: left;
        clear: left;
        display: inline-block;
        &:first-child {
            color: black;
            background: rgba(255,255,255,1);
        }
        &:last-child {
            color: white;
            background: rgba(117,80,161,1);
        }
    }
}

One possible solution is to add a line break between the elements and remove the float: left/clear: left properties. However, I'm interested in exploring alternative approaches to achieve the desired outcome.

Answer №1

Do you want it to look like this? http://jsfiddle.net/bcL023ko/3/ Instead of using float:left, try adding margin: 0 auto to center the element. Is there anything else specific you are trying to achieve?

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

Exploring intricate designs using HTML and Javascript

After extensive experience with both WPF and HTML5 JavaScript, one thing that stands out to me is the clear organization provided by XAML's defined panels. Grid, StackPanel, DockPanel, WrapPanel, and others offer a straightforward way to achieve consi ...

Why does my jQuery map code work in version 2.0 but not in version 3.0?

I've encountered an error with a jQuery map snippet that I'm trying to troubleshoot. It was working fine under jQuery 2, but after upgrading to version 3, it doesn't work anymore and I can't figure out why. Feeling stuck! var menuIte ...

In Firefox mobile, a fixed positioned element will move with the page until the scrolling comes to a halt

I am facing an issue with a CSS element set to position: fixed. While it works perfectly on desktop browsers, it behaves poorly on mobile browsers, especially Firefox. The main problem is that the fixed element scrolls along with the page and then abrupt ...

Is the dropdown menu causing issues with the rest of the navigation bar?

While attempting to create my website, I encountered some challenges with the dropdown menu. After following a tutorial on YouTube and making some modifications myself, there seems to be an issue. The screenshot below illustrates that it pulls down the re ...

Troubleshooting Display Issue with Nested Div Selection in jQuery Tooltips

I have a long list of data consisting of 30-50 rows, each row needing its own distinct tooltip. Creating unique IDs for each row would require unnecessary JavaScript code. My goal is to utilize jQuery to retrieve the tooltip from the nested <DIV> wit ...

JQuery Mobile pop-up error: Property 'is' is not defined

I am attempting to utilize jQuery to open a dialog box. I have followed the instructions provided in this link: Here is the code snippet for the dialog: <div data-role="popup" id="popupDialog" data-overlay-theme="a" data-theme="c" style="max-width ...

Exploring Navigation Options in Django

How can I properly set up the navigation on my website to allow users to easily go back to the homepage or navigate to other sections such as "contact" or "prices"? To illustrate, here is a quick example: Home -- Prices -- Contact When I am on the homep ...

Arranging the form in an organized and visually pleasing manner

Does anyone have any ideas on how to design this form in a clean and organized way? I've experimented with lists, tables, and divs but can't determine which works best. Contact Information: <div class="formsubmit"><label>Name:& ...

Swapping out DIVs with jQuery

Currently, I am working on a project for a client where I need to create a sortable biography section using jQuery. Initially, I had a setup with just two bios (first person and third person) which was functioning perfectly: $("a#first").click(function() ...

Are the navigation arrows for moving to the previous and next month not displaying in the vue-bootstrap-datetimepicker component?

I have been utilizing this date picker, and it is functioning correctly. However, it seems to lack some CSS styling. Despite numerous attempts to implement the missing CSS code, I have not been successful. Below is the relevant section of my current code: ...

Hovering over text can change the width of the background color

I'm currently facing a roadblock while working on my website. I have successfully adjusted the height of the list items on my navigation bar, but now I am struggling to expand the width so that when hovering over it, the background color covers a slig ...

Display tooltip upon hovering

Can someone help me with my dot navigation tooltips? I've managed to make them visible, but now I want them to only appear on hover. What am I missing? I've experimented with display: none and visibility but when I tried this code: span.tooltip ...

Enhancing Website Visibility: Utilizing PHP and JQuery to Update Page Content for Viewers

Imagine having a social networking platform where users can post updates and see them appear in real-time on their timeline. The challenge arises when these updates are only visible to the user currently logged in on a specific device. How can we utilize j ...

Implementing the map function in ReactJS to showcase data on the user interface

I seem to be facing an issue while attempting to utilize an array of data to display a <ul> element. Despite the console.log's working correctly in the code provided below, the list items fail to show up. <ul className="comments"&g ...

PHP form redirection is a useful feature that allows website developers to

I have created a PHP contact form on WordPress that redirects users based on their selected region. I have used 'www.google.com' as a test URL, but the form is redirecting to the custom theme page I built on WP instead. What am I doing wrong? ...

Guide to aligning 2 divs side by side using bootstrap

I have been experimenting with different methods like col-xs-6 for the first child, but I can't seem to get it right. How can I achieve this layout using only Bootstrap? Despite trying col-xs-6, I still haven't been able to place these divs next ...

Eliminating vertical spacing between words with CSS

I am trying to nest a span within a button <span style="font-size: 11px; font-weight: 700; white-space: normal; margin-left: -5px;">UPGRADE PLAN</span> When I use white-space: normal, it breaks the word UPGRADE PLAN vertically. However, I am ...

Elements beyond the bootstrap container

Currently, I am utilizing Bootstrap for my layout design. The specific task at hand is to have two items positioned outside of the container with borders extending all the way to the edge of the screen. To achieve this, I attempted nesting a container with ...

Unable to transition slides in AngularJS on Safari iOS 9 due to malfunctions

Having some CSS classes that smoothly slide my ng-view left and right during route change, everything was working well on most browsers and phones until now. Under ios 9, the animation is not functioning properly. Instead of sliding left to right, the view ...

I am experiencing an issue with my divs displaying an undesired white border on mobile screens

Having a challenge with the layout of my website when viewed on mobile devices or in Chrome's developer mobile tools. The two divs at the top of the page are producing a thin white border, which is not visible in regular browser view. Initially faced ...