Centering a div and span horizontally with an input child

I am looking for a way to automatically add a # before a hex color code in an input field and center both the div and span containers horizontally:

<div id="color_wrapper">
    <span>#<input type="text" value="ffffff"></span>    
</div>

Despite using the CSS below:

html, body{
    margin:0;
    padding:0;
    font-size:22px;
    color:#fff
}

#color_wrapper{
    border: none;
    background-color:#444;
    text-align:center;
}

span, input {
    border: none;
}

input{
    outline:none;
    width:85px;
    background-color:transparent;
    font-size:inherit;
    color:inherit;
    display:inherit;
}

While I successfully align the hash symbol at the beginning and center the span and input elements, the issue arises when trying to set the width of the div. The width is not maintained and defaults back to the left side.

Therefore, I am seeking a solution to properly center both the div and span elements, while also having the ability to adjust the width of the div container.

Here's a fiddle with the code.

Answer №1

If you want your div to be centered, simply add margin: 0 auto.

html body{
margin:0;
padding:0;
font-size:22px;
color:#fff
}

#color_wrapper{
  width:300px;
border: none;
background-color:#444444;
text-align:center;
  margin:0 auto;
}
 
span, input {
    border: none;
}

input{
outline:none;
width:85px;
background-color:transparent;
font-size:inherit;
color:inherit;
display:inherit;
}
<div id="color_wrapper">
<span>#<input type="text" value="ffffff"></span>
</div>

Hope this solution is helpful :)

Answer №2

Give this a shot

span,input
{
 text-align:center;
}

Wishing you success :-)

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

Issues with Display on IE 11 and Edge

Currently, I am in the process of working on a website that was initially created from a template by someone else. Since I am not a professional web designer, some elements on the site are not my own creation. Everything seems to be running smoothly on Fir ...

Creating a dynamic table inside a flex-grow container without disrupting the layout: A step-by-step guide

Currently, I am in the process of designing a responsive layout using Bootstrap 5.3 which consists of a NavBar, SideBar, ContentPanel, and StatusBar. The goal is to ensure that the application always occupies 100% of the view-height and view-width, display ...

Calculating the Sum of Values in PHP using an HTML Form and Jquery

Looking to expand my knowledge of jQuery, I am attempting to create a straightforward page for practice. The goal is to develop a form that will send its values to PHP to be summed and return the results. These results will then be displayed dynamically on ...

Verify the authenticity of a date using the locale parameter in the Intl API

Seeking advice for validating inputfield based on locale argument. How to format dates differently depending on the specified locale? For example, if locale is 'en-uk' then date should be in mm/dd/yyyy format, but if locale is 'de-ch' i ...

The problem with alignment of messages generated by jquery validate arises when using jquery mobile versions newer than 1.2

Since updating to jquery mobile 1.4.2, the alignment of jquery validate error messages for <input id="field1" type="text" class="required" /> appears incorrect. To see the issue, visit this Fiddle ...

The Bootstrap carousel indicators are not automatically switching because they are placed outside of the main div

I have integrated the latest Bootstrap framework into my website, featuring a visually appealing carousel. The unique aspect of my carousel design is that the indicators are positioned outside the main carousel div. Here is a snippet of my carousel code: ...

Concealing jQueryUI tooltip upon clicking a "target=_blank" link

I have implemented jQuery to display tooltips for all links on my webpage that include a 'details' attribute. $(function() { $tooltip = $(document).tooltip({ show: false, track: true, ...

What is the best way to vertically align a pseudo element image using CSS?

After looking for similar questions, none of the answers seem to be working for my specific issue. This is what I'm struggling with: I am attempting to insert and center a decorative bracket image before and after certain content in the following man ...

Adjustable div element and percentage-based height are not functioning correctly

I am facing a minor issue while attempting to create a responsive div with an image background. The div does not match the size of the image: 540px-290px When I resize the browser, the two divs do not resize together. In each div, there is a background ...

Display issues occur with Bootstrap 4.2.1 flex-box layout columns exceeding the browser edge

Why does COLUMN02 extend beyond the browser's edge when using flex-basis for column widths? What mistake am I making? Unfortunately, it doesn't display correctly in the code snippets here but works in the fiddle. TIA! http://jsfiddle.net/dragont ...

Use Bootstrap 4.3.1 to seamlessly weave text around a video on your website

I've been grappling with this, but I just can't seem to crack it. <div class="container-fluid"> <div class="row"> <div class="col-lg-4 order-1 mb-auto mt-auto"> & ...

Issue with Bootstrap side navbar not collapsing when clicked on a link

Currently, I'm working on creating a website for a friend. While I used to have some experience with coding in the past, it has been a while and I am a bit rusty. This time around, I decided to use bootstrap for the project. However, I'm struggli ...

Setting up an image DIV for a dynamic overlay effect

Here is a JSFiddle that works correctly with fixed height and width: http://jsfiddle.net/69td39ha/2/. The animation activates correctly when hovered over. I attempted to adapt the above example to be responsive without fixed dimensions. However, I'm ...

What is the best method to ensure that none of the select option values are left empty?

I have a total of 5 select option drop down menus currently, but this number may increase in the future depending on requirements. The issue I'm facing is that when I select the last element, it returns true even though the other elements are empty. I ...

Mastering the Art of Resizing Video Controls: A

https://i.stack.imgur.com/jTgap.png https://i.stack.imgur.com/Exf6Y.png On the initial screenshot, the video player displays normal controls. However, on the same website with identical CSS, the second video player shows different control settings. // C ...

Python Selenium cannot locate button within an iframe

click here for imageI am trying to interact with the "바카라 멀티플레이" button located at the center of the website. Even after switching into an iframe, I am unable to detect the button. How can I achieve this? from selenium import webdriver im ...

Padding-left in InfoBox

Currently, I am in the process of developing a map using Google Maps API3 along with the InfoBox extension available at this link I have successfully implemented an overall padding to the Infobox using the following code snippet: var infoOptions = { disa ...

Failure to Connect HTML with Stylesheet

Feeling a bit lost here. I've been attempting to connect my homepage to my stylesheet, but no matter how often I refresh the page, it just won't recognize the link. I'm diligently following the instructions from the guide I have, copying and ...

Trigger CSS3 animation only once upon the page loading

I'm in the process of creating a stylish landing page using CSS3. One element that really stands out is an <a> tag with a cool animation: @keyframes splash { from { opacity: 0; transform: scale(0, 0); } 50% { ...

Gradient transition effect changing background color from bottom to top

I am facing an issue where I have a block and I want the background color to animate from bottom to top on hover. Can you please help me identify what is causing this problem? Your assistance is greatly appreciated. .right-block--wrapper { backgroun ...