Unable to get the 'margin: 0 auto' styling to center a span inside a

I'm having an issue with setting margins for a span within a div. Despite using margin:0 auto, it's not working as expected. Any suggestions on how to resolve this?

html

<div>
    <span>
         <h3>Paris Eurostar Breaks</h3>
         <p>Curabitur fringilla mauris interdum nec magna</p>
    </span>
</div>

css

div{
    width:465px;
    min-height:201px;
}
div span{
    display:inline-block;
    color:#FFF;
    border-bottom:1px #FFF solid;
    border-top:1px #FFF solid;
    margin:0 auto;  
}

Desired Output

Answer №1

If you want to center an element horizontally using margin, the element needs a specific width.

When utilizing inline-block, consider the following approach:

div {
  text-align:Center;
}

Answer №2

Using the CSS property margin: 0 auto
is effective in horizontally centering block-level elements, but not for inline-block elements:

10.3.3 Block-level, non-replaced elements in normal flow

If both 'margin-left' and 'margin-right' are set to 'auto', their calculated values will be equal. This results in horizontal centering of the element according to the edges of its containing block.

10.3.9 'Inline-block', non-replaced elements in normal flow

When the computed value of 'auto' is applied to 'margin-left' or 'margin-right', it translates to a used value of '0'.

To centrally align an inline-block element, you can utilize text-align: center on the container element.

div{
  width:465px;
  min-height:201px;
  text-align: center;
}
div span{
  display:inline-block;
  border-bottom:1px solid;
  border-top:1px solid;
  margin:0 auto;  
}
<div>
    <span>
         <h3>Paris Eurostar Breaks</h3>
         <p>Curabitur fringilla mauris interdum nec magna</p>
    </span>
</div>

Answer №3

To achieve the desired layout, switch the display property to block and specify a width for your span element. Check out this example on Fiddle.

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

Linking to a specific div within a Vue.js component

Currently working on a Vue.js project, I am encountering an issue with linking an anchor to a specific div within a component. The anchor in question is as follows: <a href="#porto" class="porto-button">Porto, Portugal</a> Accompanied by the ...

Issue with DIV element wrongly breaking on iPhone Safari and Chrome - fix verified

I am currently working on a basic web application, but I'm facing an issue that's proving to be quite puzzling. When viewed on Desktop Chrome with the display set to iPhone 12 Pro (although I have a 13 Pro), everything looks perfectly aligned in ...

Angular Universal 9 disrupts the functionality of Bootstrap CSS directives

My application runs perfectly fine with npm run start. However, when I try npm run build:ssr and npm run serve:ssr, it doesn't function properly without throwing any errors. While running npm run start, there's a button with 65% opacity. The des ...

PHP-generated HTML onclick attribute malfunctioning

Here is the PHP code I am currently working with: $listing .= "<button onclick='updateValue(".$id.", ".$key.")'>change value</button>"; The variable $id is an integer and functions correctly. However, $key is a reference from a for ...

Difficulties in Adjusting the Size of Three Image Columns within a Website's Body

While working on my website layout, I encountered a problem with inserting three image columns in the body section. The images turned out to be too large and extended beyond the footer. Moreover, the third image was larger than expected, dominating the web ...

The lengthy string is not breaking into separate lines as expected in Internet Explorer

My querystring is quite long http://terra.cic.local/web/index.cfm//pm/uebersicht?sucheAufgeklappt=was%2Cwie%2Cwohin%2Cwann%2Cwer&sucheVon=&sucheBis=&sucheIstErsteSeiteAnzahlProdukteErmitteln=false&sucheIDReiseart=26&sucheHotelart=1081& ...

Double tap on the YouTube video

Recently, I added a YouTube video to my project. However, I am encountering an issue where when I double click on the video, it opens in a new tab. What I actually want is for the video to open in full screen when clicked. Appreciate any help! ...

What could be causing only certain portions of my CSS to be applied?

I've been struggling to get rid of the gray border around the SIGN UP button in the tbody section. I've tried applying classes (along with the ID) and using pseudo classes like :4th-child {border:none}, but nothing seems to work. The only solutio ...

Javascript clock problem, kick off on click

Currently working on a game and in need of a counter that starts when clicked and stops at 00 (From 1m to 00). It is currently starting onload and cycles back to start when it reaches 00. HTML: <body> <div> <div class="timer" >Battle t ...

Storing Unique Characters in the Database

Currently, I am utilizing a combination of HTML and PHP for my website, but I have encountered an issue when saving content in a textarea. Whenever I incorporate a different font with special characters like ' and ", it saves as � (a black dia ...

Is there a way to modify the border shape of items in the navigation bar?

Is there a way to achieve this particular shape when the current page is being displayed? https://i.stack.imgur.com/HELTM.jpg This is how my current code looks: current { transform: skew(-20deg); border: 1px solid white; color: black; background ...

Applying a CSS class (or style) dynamically depending on the variable with the help of a directive

I'm facing a situation where I need to apply ng-style (or ng-class) multiple times depending on a variable. However, this repetitive task of writing ng-class for the same functionality for each widget is quite cumbersome for me. Is there a way to si ...

Created a custom function that includes a specific target href parameter

I have a website that implements role-based authentication, where the application displayed is dependent on the user's role. The code for this functionality is as follows: <asp:Repeater ID="ui_rprApp" runat="server" DataSourceID="odsApp"> ...

Specify padding or margin for all sides in a single line of XML code

Is there a way to set the padding or margin for all four sides in xml with just one line, similar to how we can do it in css? For instance, in css: padding: 1px 2px 3px 4px; Is there an equivalent method in xml for accomplishing this? ...

Select a hyperlink to access the corresponding tab

I've been playing around with bootstrap and AngularJS. I placed nav-tabs inside a modal-window and have it open when clicking on a link. However, I want the appropriate tab to open directly upon click. For example, if I click on "travel", I want the t ...

Is there a way to prevent Chrome from highlighting text that matches the content of a `position: fixed` element?

I currently have a Toc element in my HTML with the property position: fixed. (This means it remains in place as you scroll) My goal is to prevent Chrome's Find Text feature from recognizing text within that Toc element. (Ideally using JavaScript) ...

How can I attach :after and :before pseudo-elements to a submit button?

I'm attempting to enhance my submit button with :after/:before elements, but I'm having trouble getting it to work properly. Specifically, I want to add a swipe (Sweep To Right) transition effect on the button from left to right. Similar to - th ...

JavaScript to enable button functionality for selected items

Currently, I am developing a To-Do List application. Once users input information, it gets displayed in a table format. My objective is to have the ability to select specific items from this table and apply various button functionalities to them such as ma ...

Is there a way to customize the text color in a dynamic AJAX Barchart display?

I'm working on creating a bar chart using AJAXToolkit to showcase data related to our system. However, I'm facing an issue with changing the color of the text within the chart. The text is currently in black against a grey background, making it d ...

Using jQuery to store the last selected href/button in a cookie for easy retrieval

Recently, I've been working on a document that features a top navigation with 4 different links. Each time a link is clicked, a div right below it changes its size accordingly. My goal now is to implement the use of cookies to remember the last select ...