similar questions is asked several times but my question is more general!
FIRST:
for this first question I found an accurate answer here: Display Inline-Block with Widths as Percent
assume that we have a <div>
element in an html file and add a few styles to it;
notice that the width is 100% and it should place all over the screen width and shouldn't go out of the screen
(in this snippet the border of the element goes over the screen at the right side!):
body{
margin:0;
}
#section1{
display: inline-block;
width: 100%;
text-align: center;
font-size: xx-large;
border: red 5px solid;
}
<div id="section1">
hello
</div>
but, by the answer I found,
if I add box-sizing: border-box;
to the css file, the view of the <div>
element is going to be alright! and this is ok.
(run this snippeet to see the result):
#section1{
box-sizing: border-box;
display: inline-block;
width: 100%;
text-align: center;
font-size: xx-large;
border: red 5px solid;
}
<div id="section1">
hello
</div>
I didn't have any problems so far, that was only an introduction to understand my second question:
SECOND
the aforesaid words doesn't work if we have nested
<div>
elements!
I searched a lot but it wasn't effective for my example!
and this link is partly answered this question but not much effective!
this is the result that I want: here
this snippet is my sample that the two "hello" lines doesn't take place along eachother!:
body{
margin: 0;
}
.section2{
box-sizing: border-box;
display:inline-block;
width:50%;
border: green 2px solid;
}
<div id="section2">
<div id="section2-1" class="section2"> hello</div>
<div id="section2-2" class="section2">hello</div>
</div>
what is the easiest way without changing the display property or adding negative margins?!