The alignment of the text seem to be malfunctioning

The text-align:center property isn't working as expected. Can someone please assist me in figuring out why it's not functioning correctly?

Here is the HTML code snippet:

<!DOCTYPE html>
<html>

<head>
   <title>Wheat and Barley</title>
</head>

<body style="font-family:Helvetica;">

     <div style="display:inline-block;">
        <h1 style="font-weight:bold; text-align:center; background-color:blue;">
        Wheat
        </h1>
     </div>
     <p>Wheats are cereal grains...</p>
     <p>This particular grain...</p>
</body>

</html>

Answer №1

Utilize the center tag for perfect alignment

To achieve proper centering, wrap the div as shown below -

<center><div> ... </div></center>

This method ensures that the color background remains only behind the text and the content is perfectly centered!

Thank you.

Answer №2

Text alignment works well, however, inline-block elements behave like inline elements but can also have a specified width and height. If no width is assigned, the div will be as wide as the text.

To simplify, remove the inline-block property as the div element already has a display block.

<!DOCTYPE html>
<html>

<head>
   <title>Wheat and Barley</title>
</head>

<body style="font-family:Helvetica;">

    <div><h1 style="font-weight:bold; text-align:center; background-color:blue;">Wheat</h1></div>

     <p>Wheat is a cereal grain, originally from the Levant region of the Near East but now cultivated worldwide. In 2013, world production of wheat was 713 million tons, making it the third most-produced cereal after maize (1,016 million tons) and rice (745 million tons). Wheat was the second most-produced cereal in 2009; world production in that year was 682 million tons, after maize, and with rice as a close third.</p>

     <p>This grain is grown on more land area than any other commercial food. World trade in wheat is greater than for all other crops combined. Globally, wheat is the leading source of vegetable protein in human food, having a higher protein content than other major cereals, maize (corn) or rice. In terms of total production tonnages used for food, it is currently second to rice as the main human food crop and ahead of maize, after allowing for maize's more extensive use in animal feeds. The archaeological record suggests that this first occurred in the regions known as the Fertile Crescent.</p>



 </body>

</html>

If possible, avoid using inline CSS and instead use an external file. Visit this link for reference: http://www.w3schools.com/css/css_howto.asp

Answer №3

To make the h1 element fill the entire width, you can simply add a width of 100% to its style and remove the surrounding div container.

<h1 style="font-weight:bold; text-align:center; background-color:blue; width:100%">Wheat</h1>

Here is a link to view the code on JSFIDDLE

EDIT :

If you want the result to look like this instead, please check the updated version on JSFIDDLE

Answer №4

Shoutout to everyone who has taken the time to respond to this question! I must say, @HMGtbOfficial's input was particularly valuable amidst all the other responses. Following his advice, I centered the <div> element. Fingers crossed that this does the trick! A huge thank you goes out to him for the assistance!

Here is the revised HTML code:

<!DOCTYPE html>
<html>

<head>
   <title>Wheat and Barley</title>
</head>

<body style="font-family:Helvetica;">

     <div style="display:inline-block; text-align:center;">
        <h1 style="font-weight:bold; background-color:blue;">
        Wheat
        </h1>
     </div>
     <p>Wheat is a cereal grain...</p>
     <p>This grain...</p>
</body>

</html>

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 way to set the width of an element to 100% while accounting for padding

My dilemma involves an HTML input field. The input currently has padding: 5px 10px;, but I need it to occupy 100% of its parent div's width (which is fluid). If I try using width: 100%;, the input ends up being wider than intended due to the padding ...

Show using foreach, with a modification of the spacing between rows and columns

For my project, I need to display the same image 24 times with a 3x8 matrix on an A4 size page using a foreach method. The issue I'm encountering is that I have to manually add space between each row and column, but it cannot cause the page to break. ...

Finding the height of a dynamic div in ReactJS when no CSS height is set

I'm encountering an issue when trying to determine the height of a dynamically created div that doesn't have a defined CSS height property. I'm working with React JS and have attempted to use jQuery, pure JavaScript, and React tools to acces ...

Do all links need an href attribute?

I'm inquiring about this because I have specific links that function as buttons to retrieve content through ajax, eliminating the need for href tags. (This inquiry is driven by SEO considerations) ...

How can I modify the color of JavaFX text using CSS?

Looking to jazz up my JavaFX application with some custom CSS styling. Managed to link a stylesheet to my app using the following code: //Java code FXMLLoader loader = new FXMLLoader(MainApp.class.getResource("/path/to/fxml")); Scene sce ...

What is the best way to apply styles to a React component that is passed as a prop?

I'm currently working on a component that is passed as a label prop. In order for me to utilize justify-content: space between within my div, I must ensure it has a width of 100%. You can see how this appears in the developer tools by clicking here. r ...

Opera fails to recognize background hover transitions on links

One of my web design projects includes a transition that creates a rounded background that fades in when links are hovered over. Unfortunately, the transition doesn't seem to work properly in Opera, even though it functions well in Firefox, Chrome, an ...

The Bootstrap column and row elements are having difficulty cooperating with their respective parents and children

Bootstrap functionalities like -xl are working, but when I use multiple classes such as col-lg-4 and col-md-4, they do not align in one row. I have already set the parent to row and the child to col, but they are still not displaying correctly. Images th ...

What is the best way to apply CSS to a mat-row element only when it is being hovered over?

I have a table with rows where a specific condition triggers a light red background color for each row. On hover, the background changes to light gray for all rows. However, I need the special rows (already colored light red) to have a deeper shade of red ...

CSS - owl carousel automatically stretches images to full width

Here is the code snippet that I am working with: $(document).ready(function() { $('.owl-carousel').owlCarousel({ loop:true, items:4, autoplay:true, autoplayTimeout:2000, autoplayHoverPause:true }); }); #owl-demo ...

The main div on my website appears completely chaotic when viewed in IE versions 7 and 8

On my website, the main div appears floated left on some pages and right on others. In certain pages, it seems to be scattered all over in IE 7 and 8 - I didn't even bother checking in IE6. However, oddly enough, it displays perfectly in IE 6. I need ...

The Ineffectiveness of Social Media Sharing in WordPress

I'm encountering an issue with adding social media sharing buttons to my website's product page. Although I've implemented the PHP code, clicking on the Facebook button only redirects to a page displaying a share button and URL, but it doesn ...

From Spring MVC to JSON data output

I have a JAVA EE backend and I am using Spring MVC. Currently, I have an AJAX call set up as follows: function getAllProjects() { $.getJSON("project/getall", function(allProjects) { ??? }); } In my backend system: @Reques ...

Internet Explorer seems to be having trouble detecting changes made to a jQuery checkbox

Here is an example of HTML code: <input type="checkbox" id="openInNewWindowCheckBox" value ="some_value" /> Accompanied by a jQuery script: $("#openInNewWindowCheckBox").change(function(){ if($(this).is(':checked')) ...

What is the best method to override the CSS transition effect with JavaScript?

I am trying to implement a CSS transition where I need to reset the width of my box in advance every time my function is called. Simply setting the width of the box to zero makes the element shrink with animation, but I want to reset it without any animati ...

Bug in canvas rendering for Chrome versions 94 and 95

The Canvas drawing functionality in the Chrome browser seems to have some visible bugs. Here's an example of code that demonstrates this issue: const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d&apo ...

Tips on eliminating the background of a div nested within another div

<div class="white"> <div class="transparent"> ---- </div> <div class="content"> ---- </div> </div> I am working with a parent div .white that has a white background color. Inside this parent div, there are mul ...

Steps for positioning a play button at the center of all images

index.html - here is the index.html file code containing all the necessary html code <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width ...

The steps to implement an onchange function for displaying image previews in a profile image tag

Within my code, there is a profile image tag along with an input tag for updating the image. I aim to implement a functionality that allows me to select a new image and automatically update the profile picture when it changes <div class="col-sm-6"> ...

Tips for choosing every <div> within a specific class

I'm trying to create multiple boxes within a class, all with the same background color. I've written code for 6 boxes in the "color-board" class, but when I go to select them, only 1 box is displaying... Below is the code for the class and how I ...