Если вы посмотрите на этот jsfiddle:
<div class="text">
<div id="image">this is an image with unknown width</div>
This is some text above,This is some text above,This is some text above,This is some text above,This is some text above,This is some text above,
<div class="code">special content</div>
and this is some text below,and this is some text below,and this is some text below,and this is some text below,and this is some text below,
</div>
Css:
.text{
color:red;
text-align:justify;
margin-top:20px;
}
.code{
border:1px solid blue;
border-left:10px solid #ccc;
background:#eee;
padding:5px;
margin:5px;
}
.code:before{
display:block;
font-size:80%;
font-style:italic;
content:"Example:";
margin-bottom:5px;
}
#image{
float:right;
border:2px solid red;
height:150px;
width:150px;
margin-left:40px;
}
Я хочу, чтобы синий квадрат div (специальный контент) был такой же, как текст выше/ниже, или ширина его содержимого, что проще. Почему вообще этот div как ширина, как экран?
Любые предложения, что лучший способ?
Edit1: простое решение с переполнением: http://jsfiddle.net/v5GXT/6/
.code{
border:1px solid blue;
border-left:10px solid #ccc;
background:#eee;
padding:5px;
margin:5px;
overflow-x: hidden;
}
Я хочу, чтобы синий квадрат div (специальный контент) был такой же, как текст выше/ниже
Вы можете добавить overflow-x: hidden;
к .code
div, чтобы скрыть горизонтальное переполнение следующим образом:
.code {
border:1px solid blue;
border-left:10px solid #ccc;
background:#eee;
padding:5px; margin:5px;
overflow-x: hidden;
}
Так много разных способов сделать это, это один из самых простых:
.code
в div
уровня блока и сделайте .code
inline-block
HTML:
<div class="codeContainer">
<div class="code">special content</div>
</div>
CSS:
.codeContainer {
display: block;
}
.code {
display:inline-block;
width:auto;
border:1px solid blue;
border-left:10px solid #ccc;
background:#eee;
padding:5px;
margin:5px;
}
reduce div-width to content width
но вы также упомянули, чтоdiv box to be the same width as the text above/below
. Это совершенно разные.