Text enclosed in a bordered box, perfectly centered

body {
  display: flex;
  align-items: center;
  background-color: black;
}

h1 {
  font-size: 8rem;
  border: 0.1em solid white;
  color: white;
  text-justify: center;
  font-family: "Lucida Sans Unicode", amp;amp;quot;Lucida Grande", sans-serif;
  flex: 1;
  display: inline-flex;
  justify-content: center;
}

/* Need this:

     -------------
     |Hello world|
     -------------

On all screen sizes, portrait or landscape orientation. */
<h1>Hello World</h1>

I'm struggling with adjusting the text size based on the screen width and formatting the text properly inside the box. I also want the border to only surround the text, not stretch to the width of the container as shown in this example:

If resizing the text is too complicated, then a simpler solution would be appreciated. Thank you!

Answer №1

To make the text resizable, you can utilize viewport units as suggested in this resource.

The CSS modifications involve setting the justify-content property for the body to center the text and changing the styling of the h1 element to display: inline-block for a specific border effect.

body {
  display: flex;
  align-items: center;
  background-color: black;
  justify-content: center;
}

h1 {
  font-size: 15vw; /* adjust font size accordingly */
  border: 0.1em solid white;
  color: white;
  font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif;
  display: inline-block;
}

/* Resulting Layout Example:
     -------------
     |Hello World|
     ------------- */

<h1>Hello World</h1>

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

Use jQuery to apply CSS to a div only when it becomes visible on the screen

I created a single page HTML with scrolling functionality. The structure of my divs - home and home2 - is as shown below. Both have a height of 100%. I am looking to add CSS to the second div (home2) using jQuery only when it becomes visible to the user in ...

Full height Footer using Flash and HTML

Seeking advice: I successfully implemented a Flash object with 100% height and width on an HTML page. However, I am struggling to add an HTML footer that stays fixed at the bottom of the page. Attempts made: I have tried various solutions, searched on Go ...

Flashes hidden DIV in a split second using JavaScript cookies

I have set up a pop-up lightbox on my webpage along with a cookie using JavaScript to display the lightbox every 15 days. The code functions as intended, but if I continue refreshing the page to check the cookie status, the lightbox briefly flashes on the ...

When Socket.emit is called, it outputs <span> elements within a well-structured message

Currently working on a small chat application using Node.js, Express.js, and Socket.IO. However, I'm encountering an issue with formatting. Instead of displaying the message content within <span> tags as intended, it is printing out the actual t ...

Tips for extracting and utilizing a JSON object provided by the parent page:

Sorry in advance if this question has already been addressed. I've spent hours searching on Google, but haven't found a satisfactory answer yet. Below is the code snippet I'm working with: <ion-content> <div class="list"> ...

Looking for recommendations on effective jQuery plugins for drag and drop file uploading?

Can anyone recommend any drag-and-drop jQuery plugins that offer similar functionality to Gmail's attachment dragging and dropping feature? I would like the ability to track the upload progress. However, I am specifically looking for options without ...

Styling is not being applied to the DataTable when it is invoked through an ajax

I have developed a simple SpringBoot application using Spring Initializer, JPA, embedded Tomcat, Thymeleaf template engine, and packaged it as an executable JAR file. Within this application, I have implemented two datatables: one utilizing ajax and the ot ...

Having issues with the background-image style not displaying correctly in the header of an

Trying to update the "background-image:" of a CSS class upon button click. JQuery: $(function() { $('button').on('click', function() { $('.masthead2').css('background-image', 'url("../img/whitehead ...

What is the best way to use a jQuery variable within a .css() function

I'm working on a typewriter effect with some advanced CSS code. I need the program to calculate the length of a PHP variable and adjust the cursor animation steps accordingly. Basically, I want the cursor movement in the animation to be determined by ...

How can I implement the delete button in a recently appended table row using the clone() function in jQuery?

Whenever I click the Add button, a new row gets appended to the HTML table. You can check out the jsFiddle link for reference: http://jsfiddle.net/fgLHN/3/ The code above functions smoothly for me. However, I'm facing an issue when trying to add a de ...

Stop menu items from shifting when focused

I've developed a mobile hamburger menu and attempted to adjust the padding to create space between the text and the borders. Here's the HTML: #menu-solutions:hover .menu-item-text, #menu-solutions:focus .menu-item-text, #menu-solutions:ac ...

AngularJS integration with Bootstrap Confirmation jQuery plugin

I am currently struggling to implement the Bootstrap-Confirmation plugin with AngularJS. Despite following instructions from this YouTube tutorial, I cannot seem to get the directive to function correctly. A related query on Stack Overflow references a di ...

Events related to SVG elements

I have a unique situation where a div containing multiple SVG elements that act as buttons is added to the page after the user interacts with it. I am trying to add events to these SVG elements using event delegation, where I inspect the target of the even ...

Tips for converting inline CSS to stand-alone attributes without any cost

I am attempting to modify an au generated HTML code, for example: <div class="intro editable" id="field_text">text <strong>text</strong> text <span style="text-decoration: underline;">text</span> <span style="color: #ff00 ...

What is the alternative to using <strong> tags in HTML?

Is there a counterpart tag to <strong> in HTML, or is it expected to be included in the upcoming HTML5? ...

Capturing information from a modifiable table using Javascript

Creating an HTML Editable table with the property CONTENTEDITABLE, each TD has a unique ID (even though it's a long process and I only have basic JS knowledge) to keep track of the information inside. The table is wrapped by a form. At the end of the ...

Initiating the ngOnInit lifecycle hook in child components within Angular

I am facing an issue with controlling the behavior of child components in my Angular application. The problem arises when switching between different labels, causing the ngOnInit lifecycle hook of the children components not to trigger. The main component ...

"An issue with the external jQuery file loop causes it to skip either the first or

I have a webpage where I am dynamically loading content from an external JavaScript file. This file identifies div elements with a specific class name and then fills the content of those divs based on a custom attribute, which contains a link to an API. Th ...

A small space underneath the <a> element nested within a <li> tag within a navigation bar

As a new programmer, I am currently working on creating a navbar. However, I have encountered an issue where there are small gaps underneath the user when my cursor hovers over it. There is a white gap under the user element. I expect that the hover back ...

I am attempting to adjust the color of the active tab, but I seem to be encountering issues in my code. Can anyone help me

The currently active tab should change along with the text inside the box, but it's not working as expected. I'm struggling to find out why. Here is a fiddle to demonstrate my progress so far: var btn1 = document.getElementById("btn1"); va ...