2017-10-13 955 views
1

我想把我的html頁面分成兩列。一列應該由圖像和其他文本填充。我面臨以下問題:如何將html頁分成兩列?

  1. 列不佔據確切的50-50寬度。

  2. 圖像開始前從左邊緣有一個薄邊。需要刪除該保證金。

  3. 這兩列之間存在巨大差距。

這將是真正有用的,如果有人可以看看這個代碼,並建議更正:

.container img { 
 
    width: 100%; 
 
} 
 

 
.thin-black-border { 
 
    border-color: black; 
 
    border-width: 05px; 
 
    border-style: solid; 
 
} 
 

 
.header { 
 
    font: 200 80px'Oleo Script', Helvetica, sans-serif; 
 
    color: #2b2b2b; 
 
    text-shadow: 4px 4px 0px rgba(0, 0, 0, 0.1); 
 
}
<link href='http://fonts.googleapis.com/css?family=Oleo+Script' rel='stylesheet' type='text/css'> 
 
<div style="width:100%; height: 100px;background-color:green" class="bg-faded"> 
 
    <!-- Main Div --> 
 
    <h1 class="text-center header">Madhubala</h1> 
 
</div> 
 
<div style="float:left;width:50%;" class="container"> 
 
    <img src="https://nehamalude.files.wordpress.com/2011/02/madhubala.jpg" class="thin-black-border" /> 
 
</div> 
 
<div style="float:left; width:50%; background-color:red; "> 
 
    Right 
 
</div>

+0

使用引導:https://www.w3schools.com/bootstrap/bootstrap_grid_stacked_to_horizo​​ntal.asp –

+4

你不需要使用引導來創建2列布局。 – zik

回答

4

body{ 
 
    margin: 0; 
 
    padding: 0; 
 
} 
 
.container img { 
 
    width: 100%; 
 
} 
 

 
.thin-black-border { 
 
    border-color: black; 
 
    border-width: 05px; 
 
    border-style: solid; 
 
} 
 

 
.header { 
 
    font: 200 80px'Oleo Script', Helvetica, sans-serif; 
 
    color: #2b2b2b; 
 
    text-shadow: 4px 4px 0px rgba(0, 0, 0, 0.1); 
 
} 
 
.main-wrap{ 
 
    display: flex; 
 
    flex-direction: row; 
 
} 
 
.main-wrap > div{ 
 
    flex: 1; 
 
}
<link href='http://fonts.googleapis.com/css?family=Oleo+Script' rel='stylesheet' type='text/css'> 
 
<div style="width:100%; height: 100px;background-color:green" class="bg-faded"> 
 
    <!-- Main Div --> 
 
    <h1 class="text-center header">Madhubala</h1> 
 
</div> 
 
<div class="main-wrap"> 
 
    <div class="container"> 
 
    <img src="https://nehamalude.files.wordpress.com/2011/02/madhubala.jpg" class="thin-black-border" /> 
 
    </div> 
 
    <div style="background-color:red; "> 
 
    Right 
 
    </div> 
 

 
</div>

如何使用display: flex

+0

我會加: '.container img { ... box-sizing:border-box; }'尊重圖像邊界 – zdolny

-1

您可以簡單地使用表: 方法1:

<table border="1"> 
 
    <tr> 
 
    <td>You can add content here</td> 
 
    <td>You can add content here</td> 
 
    </tr> 
 
</table>

方法2:

<div style="display:block; width:100%;"> 
 
    <div style="width:50%; float: left; display: inline-block;">Your content</div> 
 
    <div style="width:50%; float: left; display: inline-block;">Your content</div> 
 
</div>

方法3(使用Flex):

<div class="flexbox-container" style="display:flex;"> 
 
     <div class="sidebar" style="flex:1;">Test column 1</div> 
 
     <div class="main" style="flex:1;">Test column 2</div> 
 
    </div>

+0

將display:inline-block設置爲浮動元素的要點是什麼?它的display [[計算]](https://drafts.c​​sswg.org/css2/visuren.html# dis-pos-flo)'塊'反正? –

0

添加寬度= 100%的圖像。

使用一個CSS復位去除邊緣

/* http://meyerweb.com/eric/tools/css/reset/ 
    v2.0 | 20110126 
    License: none (public domain) 
*/ 

html, body, div, span, applet, object, iframe, 
h1, h2, h3, h4, h5, h6, p, blockquote, pre, 
a, abbr, acronym, address, big, cite, code, 
del, dfn, em, img, ins, kbd, q, s, samp, 
small, strike, strong, sub, sup, tt, var, 
b, u, i, center, 
dl, dt, dd, ol, ul, li, 
fieldset, form, label, legend, 
table, caption, tbody, tfoot, thead, tr, th, td, 
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary, 
time, mark, audio, video { 
    margin: 0; 
    padding: 0; 
    border: 0; 
    font-size: 100%; 
    font: inherit; 
    vertical-align: baseline; 
} 
/* HTML5 display-role reset for older browsers */ 
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section { 
    display: block; 
} 
body { 
    line-height: 1; 
} 
ol, ul { 
    list-style: none; 
} 
blockquote, q { 
    quotes: none; 
} 
blockquote:before, blockquote:after, 
q:before, q:after { 
    content: ''; 
    content: none; 
} 
table { 
    border-collapse: collapse; 
    border-spacing: 0; 
} 

看到的jsfiddle:https://jsfiddle.net/ssohvr45/2/