CSS attribute that expands in all directions from the center of the parent element until it hits the edge of the screen

.rightDiv {
    position: absolute;
    right: 0;
    height: 80px;
    width: 350px;
  }
  
  .content{
   width:50%; 
    position:relative;
    margin:auto;
    padding:0px;
    text-align:center;
    top:10px;
  }
  
  #spandiv {
    display: block;
    font-size:16px;
    background-color: #232F34;
    color:#FFFFFF;
    opacity:1;
    top:5px;
    overflow-wrap: break-word !important;
  }
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <link rel="stylesheet" href="test.css"/>
</head>
  
<body>
  <div class="rightDiv">
    <input type="text"/>
    <div class="content">
        <p id="spandiv">10122019(tjwmxuwmiihcxdfryfgfhrunlfsxrkhvmyqjjuwjddknjaybnrobpzferxaenxzenbkmlqqzesvfbnnsxwydfbzgbaxkccvoplgjxbikxjifojjmvqxmbjbrtmvbngq)</p>
      </div> 
  </div>
</body>
</html>

I am a beginner in CSS and I am seeking a style property that can expand text in both directions from the center of its parent element to the end of the screen. If such a property doesn't exist, is it possible to create a custom style? I am facing some challenges in my project and I want the UI to match the design shown below.

https://i.sstatic.net/SZn9G.png

Answer №1

Here is a sample code snippet for your reference:

Demo

        .mainDiv {
            height: auto;
            width: 100%;
            float: left;
            text-align: center;
        }
        
        .content {
            width: 100%;
            position: relative;
            margin: auto;
            padding: 0px;
            text-align: center;
            top: 10px;
        }
        
        .content p {
            font-size: 16px;
            line-height: 18px;
            background-color: #232F34;
            color: #FFFFFF;
            overflow-wrap: break-word;
            padding: 15px 10px;
        }
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css">
    <title>StackOver flow</title>
    
</head>

<body>
    <div class="mainDiv">
        <input type="text" />
        <div class="content">
            <p>10122019(tjwmxuwmiihcxdfryfgfhrunlfsxrkhvmyqjjuwjddknjaybnrobpzferxaenxzenbkmlqqzesvfbnnsxwydfbzgbaxkccvoplgjxbikxjifojjmvqxmbjbrtmvbngq)</p>
        </div>
    </div>
</body>

</html>

Answer №2

Unusual request, but consider the following: create a parent element with specific width and height, then set the child element to be absolute positioning with left: 0 and right: 0 to center it within the parent:

#parent {
    width: 500px;
    height: 150px;
    border: 4px solid;
    margin: auto;
}

#child {
    text-align: center;
}
<div id="parent"></div>

<div id="child">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>

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 dynamically generate a consistent margin or padding between two sections on different screen sizes?

I have successfully stacked two sections on top of each other. To achieve this layout, I utilized position:relative on the parent container div and then employed position:absolute on the child divs to align them based on the parent container's top pos ...

Breaking xPath data apart by new lines

Currently in my project, I am utilizing xPath to parse the following HTML code: <div class="prod_view_info"> <p>notimportant</p><p>notimportant</p> <p> <b>Putere (Consum/ora): </b> 3W<br&g ...

Having trouble with jQuery hover and AJAX loaded content not functioning properly?

When I receive content through AJAX, it looks like this: <div class="message" id="1"> blah <div class="details" id="1" style="float: left; display: none;">hidden information</div> <div class="spacer" style="clear: both;"&g ...

What steps can I take to avoid horizontal scrolling on mobile due to a table overflowing?

My website displays a table within a bootstrap container, row, and column. While everything looks good on larger screens, the content within the table is causing a horizontal scroll on smaller screens, making the footer look distorted. This results in a me ...

Angular 6 - implementing a dynamic carousel with bootstrap styling

I am currently working on a carousel using bootstrap4 where the images are sourced from an array. However, I am facing an issue where the first element must have the active class. <div id="carouselExampleFade" class="carousel slide carousel-fade" dat ...

What is the significance of a colon appearing at the start of HTML attribute names in a Vue template?

<circle r="3" :cx="airport.x" :cy="airport.y" class="airport__marker" /> Are the attributes v-bind:cx and v-bind:cy equivalent to what is shown above? ...

Tips for making a DIV span the entire width of a page when it is within a column with the Bootstrap class 'col-md-6'

I'm having a little trouble arranging divs next to each other on a page. Specifically, I want to ensure that if there is only one instance of the col-xs-12 col-md-6 div class on the page, it should take up the full width (100%) instead of just 50%. Th ...

A guide on retrieving the value of input elements using class names in jQuery

I need help fetching the values of input elements with the class "features". I am able to access all of them. alert($('.features').length); Can someone please confirm if my understanding is correct? $('.features') is an array of HTM ...

Failure occurred when attempting to link and display on the page container

I have created a simple app using jQuery Mobile. At some point in the app, I include the following code: <a href="test_es.html" data-role="button">Start!</a> This code snippet loads a new HTML file that contains several jQuery Mobile page ...

Are there automatic margins in the top side navigation bar?

I have observed that every time I attempt to create a navigation bar, there is an unwanted left and top margin of approximately 8px. This margin seems to persist regardless of the code in my CSS file. I am starting to wonder if this is some sort of default ...

Interference in the output of .innerHTML leads to disruption

I have been working on a feature to display a calculated price based on the selected quantity for each individual product. I attempted to duplicate the code and modify variable names, but encountered issues with the increase/decrease buttons triggering mul ...

Choose an option from the drop-down menu and transfer the selected value to the following page using a hidden

Struggling to capture the selected value from a combo box and set it to a hidden field in order to retrieve it on the next page. Need some assistance with this task. Here is my form tag: <form class="well" name="ddm" id="ddm" style="margin-left: 30px; ...

What is the process for executing a Js file on a website?

I am looking to automate some tasks on a website that I do not own. Specifically, I need to automatically fill out a form and perform certain clicking actions. Can JavaScript be used for this purpose? I am unsure how to run a .js file on a site without the ...

Conceal a div using jQuery when clicked

I am facing an issue with my script where I need to make a div close only after clicking on a link, instead of anywhere outside the div. http://jsfiddle.net/N4pbP/ $(function() { $('#hidden').hide().click(function(e) { e.stopPropagation() ...

Having trouble loading @font-face fonts

I am experiencing an issue where my downloaded fonts are not showing up in Chrome. I am utilizing scss which gets compiled to css using gulp. If I directly visit http://project-name.localhost/data/fnt/Shermlock.ttf I can successfully download the font. ...

.display() and .conceal() functions malfunctioning

I'm in the process of creating a calendar system using the Codeigniter framework. My goal is to display a specific div every time a day cell in the calendar template is clicked. These divs are generated through a PHP for loop and populated from the d ...

How to select text based on its color property using Watir in Ruby

I'm having trouble clicking on a specific element within a table. There are multiple elements with the same class <span class="price">, but I need to click on one that has a color property of color:#555. I attempted a few different methods witho ...

Obtaining visuals in a specific manner

I have a customized slider (flexslider) with 10 images per slide. The images are currently manually inserted, but I want to optimize the slider to dynamically extract images using PHP. However, my current setup only displays one image per slide. How can I ...

inserting various values from a web form

I have a form containing a table with three columns: Roll_No, Name, and Marks. The Roll_no and Name are fetched from the database, and the teacher only needs to enter the marks for each student. However, I am facing an issue where only the last entered val ...

What is the best way to access the CSS resources within the designated Area section of an MVC project?

I am experiencing difficulties accessing a CSS file located within the MVC AREA section. Here is the direct path to the CSS file. After attempting to troubleshoot with firebug, I received the following error message: The stylesheet was not loaded ...