Alignment of elements in different shapes using CSS

I tried looking into this issue in the thread about vertically aligning text within a div, but unfortunately, the suggested solution didn't work for me. I specifically need to center text within an oval shape, but it seems like the provided code only works for rectangular shapes. Is there a way to modify the code to achieve vertical alignment within ovals? Currently, the text is horizontally centered but not vertically.

Here is the HTML snippet:

<div class="oval"> <span> Strawman </span> </div>

These are the CSS styles applied:

.oval {
  -webkit-box-sizing: content-box;
  -moz-box-sizing: content-box;
  display: inline-block;
  box-sizing: content-box;
  width: 200px;
  height: 50px;
  padding: 5px;
  border: none;
  -webkit-border-radius: 47% / 50%;
  border-radius: 47% / 50%;
  font: normal 100%/normal "Courier New", Courier, monospace;
  color: white;
  text-align: center;
  vertical-align: middle;
  -o-text-overflow: clip;
  text-overflow: clip;
  background: #99cc00;

}


.oval span {
  vertical-align: middle;
}

Answer №1

Give this CSS code a try to achieve the desired result

<style>
        .oval {
            -webkit-box-sizing: content-box;
            -moz-box-sizing: content-box;
            display: table;
            box-sizing: content-box;
            width: 200px;
            height: 50px;
            padding: 5px;
            border: none;
            -webkit-border-radius: 47% / 50%;
            border-radius: 47% / 50%;
            font: normal 100%/normal "Courier New", Courier, monospace;
            color: white;
            text-align: center;
            vertical-align: middle;
            -o-text-overflow: clip;
            text-overflow: clip;
            background: #99cc00;
        }

        .oval span {
            display: table-cell;
            vertical-align: middle;
        }

    </style>

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

The Interconnected World of PHP and AJAX

I am working with two specific files in my project: File: new_aircraft.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> & ...

Arranging Check Boxes Side by Side

I am struggling to properly align the three checkboxes and tables next to each other. I have managed to get the checkboxes on the same row, but aligning them neatly has proven to be a challenge. Using Notepad++ as my development tool, I am facing formattin ...

Incorporating the fadeout technique in conjunction with the append() function

Here is some code that I am working with: $('body').on('click' , function(){ $('body').append('<div class="ajax-success"><p>Its Done !!!</p></div>').fadeOut(2000); }); The goal was to a ...

What's the best way to create an onclick event for a li element that includes a pseudo-element ::before

I have successfully created a Timeline using HTML and CSS elements. The visual result is as follows: My goal is to enable users to click on the second circle, triggering a color change in the line that connects the first and second circles. This color tra ...

Having difficulties showcasing a variety of images stored in the database

I'm having some trouble with my e-commerce site. I can't seem to display different product images from the database on my homepage. The code I have only shows one image for all products, even though they should each have their own unique image up ...

enhancing the style of my Express project with CSS and EJS

Currently, I am facing challenges with linking CSS to my EJS files in a project I am developing using Node/Express/EJS. Despite creating a public folder with a CSS subfolder and the main.css file inside, I am unable to get the CSS to display in the browser ...

Replace image source with a seamless transition

I have successfully changed the src of an image using a series of other images, but the transition is not smooth. It seems that the new image loads abruptly after the initial one fades out rather than simultaneously. Is there a clever way to achieve a com ...

List elements vanish when list width is adjusted

I am working on a project that involves animating the width of a horizontal list to simulate a scroll closing effect. You can view the issue in this Fiddle: http://jsfiddle.net/a5Tfu/1/ The main problem is that when the width shrinks and overlaps any ele ...

Legend click functionality works well in hiding the bars, but unfortunately, the data values in the charts.js are not being concealed as expected

When I click on the legend, the bar is hidden in the charts.js bar chart. However, the data value associated with the bar is not hidden. I have provided a link to the JS Fiddle code below: Check out the JS Fiddle here: https://jsfiddle.net/npyvw1L8/ var ...

dividing a circle in half with CSS using an image background

Is it possible to divide a circle into 2 parts using both RGB and URL images? Here is an example of how I want it to look: https://i.sstatic.net/r5sWj.png I have managed to write code that works with RGB, but I am unsure how to achieve the same effect w ...

What is causing my background image to move upward when I include this JavaScript code for FlashGallery?

There's an issue on my website where adding a flash gallery script is causing the background image to shift unexpectedly. You can view the affected page here: The culprit seems to be the "swfobject.js" script. I've identified this by toggling t ...

Ensure your mobile device is age 13 or over before proceeding

I am developing a project with Next js. Here, I want to render a component if it is a mobile device. However, if I use the isMobileDevice value in jsx, I get the errors below. import useTranslation from "next-translate/useTranslation"; import isM ...

Node.js error: HTML audio file returns 404 on server but plays fine when accessed directly as an HTML file

I'm facing an issue with an HTML page that contains an autoplay tag for a song (the file is in mp3 format). When I open the page as a local file on my Mac, the song plays fine. However, when I try to run it using a node.js file with socket.io, the son ...

Problems with Displaying Background Images in Bootstrap

I'm new to this and I've encountered an issue that's baffling me when trying to display a background image. Everything works perfectly fine when I link to an image like this: .backg { height: 100%; background: url(http://formdoctors.com ...

The element will display above all other elements in its parent container

I am currently developing a React application, and the code structure is set up as follows: <Div> <Div style={{maxHeight:'60vh'}}> //This section includes the code for displaying a list item. Each item has its context menu that can ...

Employing SVG in combination with ajax for animating the conditions of IO hardware

The company I work for specializes in selling micro computers that are capable of managing and monitoring various IO devices. Utilizing ajax for the web IO functions, I recently designed a new graphic for a Voltmeter with 41 states ranging from 0 to 20 vol ...

Guide to setting the first tab as the default tab using Thymeleaf, Css, and Bootstrap

I am currently working on a project where I need to dynamically create tabs based on a list retrieved from my Spring backend using Thymleaf and Bootstrap. While I have managed to successfully create the tabs and content, I am facing an issue where the fi ...

Utilizing Selenium for text input

Just starting out with selenium and looking for guidance. Picture this scenario: there are two boxes, one where a user can input a message, and another where that message will be displayed. I'm currently working on using selenium and java to send text ...

Ways to showcase flair in PHP code such as this

I currently have this code snippet: <h2 class="index-single">Tech Categories</h2><?php $args2 = array( 'cat' => 11 , 'posts_per_page' => 9 , 'paged' => $paged ); $the_query2 = new WP_Query( $args2 ); ...

Move your cursor over an image to reveal another image on top without impacting the image underneath by using background-image

Is there a way to create an effect where hovering over a specific book image will display a checkmark, indicating selection upon click? Ideally, users should be able to press alt (cmd on mac) to select multiple books simultaneously. Check out the HTML bel ...