WhiteBachelor
@WhiteBachelor
Начинающий веб-программист.

Как сделать перенос строки в момент генерации элемента span внутри параграфа с помощью DOM?

Делаю простенькую программку, которая выводит введенные элементы массива в параграф, вставляя в элемент span. Но выводится все в одну строку или криво, несмотря на то, что я прописал в css: text-align: justify;
Как это исправить?
Вот код.
HTML:
<html>
<head>
<meta charset="utf-8"/>
<link rel="stylesheet" href="css/style.css"/>
<title>Array GUI</title>
<script type="text/javascript" src="js/script.js"></script>
</head>

<body onload="inputRestart()">
    <header>
        <img src="css/images/array.png" alt="array" width="100" id="logo"/>
        <h1>GUI Array</h1>
        <h2>Graphical representation of the array and visualization of some array functions</h2>
    </header>
    <div id="content">
        <input type="text" placeholder="Add New Element" id="input-creater"/>
        <input type="button" value="Create" id="button-creater" onclick="addArrayValue()"/>
        <div id="array-space">
            <p id="p-array"></p>
        </div>
    </div>
    <nav id="options">
            <ul id="options-block">
                <li class="options-block-buttons" id="add-button"  ><a href="#logo">Add</a></li>
                <li class="options-block-buttons" id="del-button"  >Delete</li>
                <li class="options-block-buttons" id="sort-button" >Sort</li>
            </ul>
        </nav>

</body>
</html>


CSS:
/*-------------------------<head>-----------------------------*/

@font-face{
    font-family: Montserrat-Light;
    src: url('fonts/Montserrat-Light.ttf');
}
@font-face{
    font-family: Montserrat-LightItalic;
    src: url('fonts/Montserrat-LightItalic.ttf');
}
@font-face{
    font-family: Montserrat-Regular;
    src: url('fonts/Montserrat-Regular.ttf');
}

@font-face{
    font-family: Montserrat-ExtraLightItalic;
    src: url('fonts/Montserrat-ExtraLightItalic.ttf');
}

@font-face{
    font-family: Montserrat-Thin;
    src: url('fonts/Montserrat-Thin.ttf');
}

body{
    font-family: Montserrat-Regular;
    background-color: rgb(240, 224, 240);
}

a{
    color: #ffffff;
    text-decoration: none;
} a:visited{
    color: #ffffff;
    text-decoration: none;
} a:active{
    color: #ffffff;
    text-decoration: none;
}

/*-------------------------</head>-----------------------------*/

/*-------------------------<header>----------------------------*/

header {
    width: 75%;
    height: 23%;
    margin: 20px;
    box-shadow: 0px 10px 20px 5px rgba(0, 0, 0, 0.315);
    padding: 10px; 
    margin-left: 5%; 
    background-color: white;
}

header h1 {
    font-family: Montserrat-Thin;
    font-size: 2.2em;
    text-shadow: 0px 1px 4px #FF3838;
    color: #FF3838;
}

header h2{
    font-family: Montserrat-ExtraLightItalic;
    font-size: 1.2em;
    text-shadow: 0px 1px 4px #FF3838;
    color: #FF3838;
}

#logo{
    float: right;
    margin: 13px;
}

/*----------------------</header>---------------------------*/

/*----------------------<#content>--------------------------*/

#content{
    background-color: white;
    width: 65%;
    height: 130%;
    float: left;
    margin-left: 20px;
    margin-bottom: 20px;
    margin-top: 40px;
    box-shadow: 0px 10px 20px 5px rgba(0, 0, 0, 0.315);
    padding: 10px; 
    margin-left: 5%;
    z-index: 10; 
}

#input-creater{
    width: 70%;
    height: 2.3rem;
    background-color: rgb(252, 223, 245);
    border: 0px;
    margin: 20px;
    box-shadow: 2px 5px 15px 2px rgb(253, 107, 107);
    /*border: 1px solid #FF3838;*/
    color: #FF3838;
    font-size: 1.2em;
    font-family: Montserrat-LightItalic;
    padding-left: 10px; 
}

#button-creater{
    width: 10%;
    height: 2.4rem;
    background-color: #FF3838;
    border: 0px;
    font-size: 1.2em;
    box-shadow: 3px 5px 16px 2px rgba(0, 0, 0, 0.404);
    border-radius: 10px;
} #button-creater:active{
    background-color: rgb(126, 6, 6);   
}



/*----------------------</#content>-------------------------*/

/*----------------------<#options>--------------------------*/

#options{
    width: 10%;
    height: 30%;
    margin-top: 40px;
    padding: 0px;
    float: right;
    color: white;
    position: fixed;
    left: 71.3%;
    
}

#options-block{
    list-style-type: none;
    margin: 0px;
    padding: 0px;
}

.options-block-buttons{
    display: block;
    z-index: 1;
    width: 85%;
    margin: 10px;
    margin-left: 0px;
    padding: 10px;
    text-align: center;
    font-size: 1.5em;
    border-radius: 0px 15px 15px 0px;
    box-shadow: 3px 5px 12px 2px rgba(0, 0, 0, 0.562);

}

#add-button{
    background-color: rgb(75, 187, 75);
    color: white;
} #add-button:active{
    background-color: rgb(33, 136, 33);
}

#del-button{
    background-color: rgb(248, 48, 48);
} #del-button:active{
    background-color: rgb(126, 6, 6);   
}

#sort-button{
    background-color: rgb(83, 83, 216);
} #sort-button:active{
    background-color: rgb(45, 45, 128);
}

/*----------------------</#options>-------------------------*/

/*----------------- ---<#array-space>-----------------------*/
#array-space{
    width: 95%;
    height: 80%;
    margin-left: 2.5%;
    margin-top: 10px;
    padding: 15px;
    padding-top: 10px;
    box-shadow: inset 3px 5px 16px 2px rgba(0, 0, 0, 0.404);
    
}

#p-array{
    text-align: justify;
    line-height: 2em;
}

span{
    z-index: 1;
    padding: 10px;
    margin: 5px;
    box-shadow: 2px 4px 10px 2px rgba(0, 0, 0, 0.479);
}

/*---------------------<#array-space>-----------------------*/


JAVASCRIPT:
var arrayList = new Array;

function inputRestart(){
    document.getElementById("input-creater").value = '';
}

function addArrayValue(){
    arrayList.push( document.getElementById("input-creater").value );
    addArrayValueToDOM(arrayList[arrayList.length - 1])
}

function addArrayValueToDOM(value = ''){
    DOM_Element = document.createElement('span');
    document.getElementById('p-array').appendChild(DOM_Element);
    document.getElementById('p-array').getElementsByTagName('span')[arrayList.length - 1].
    innerHTML = arrayList[arrayList.length - 1]
    var r = document.getElementById('p-array').getElementsByTagName('span')[arrayList.length - 1].style.width;
    if (arrayList.length == 4){
        document.getElementById('p-array').value += "/n";
    }
    inputRestart();
}

5bcb6961f34f0291518901.png
Как это исправить?
  • Вопрос задан
  • 1402 просмотра
Решения вопроса 1
somanytears
@somanytears
Web-программист
display: flex;
flex-wrap: wrap.
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы