【发布时间】:2017-02-08 04:33:59
【问题描述】:
我正在复制一个网站以练习 CSS 和 HTML,但我遇到了一些问题。我尝试了几个小时并到处寻找解决方案,以便将一个部分分成两半并添加一行,其中包含两列,就像您在这张图片中看到的那样:
还试图查看他们的源代码,但他们在各种文件中都有大量的类,这使得很难弄清楚他们是如何做到的。
/**** Standard stuff ****/
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
html,
body {
background-color: #fff;
color: #555;
font-family: 'Lato', 'Arial', sans-serif;
font-weight: 300;
font-size: 20px;
text-rendering: optimizeLegibility;
overflow-x: hidden;
}
.section {
clear: both;
padding: 0px;
margin: 0px;
}
.clearfix {
zoom: 1;
}
.clearfix:after {
content: '.';
clear: both;
display: block;
height: 0;
visibility: hidden;
}
/**** Row ****/
.row {
max-width: 1140px;
margin: 0 auto;
}
.row:before,
.row:after {
content: "";
display: table;
}
.row:after {
clear: both;
}
/**** Cols ****/
.col {
display: block;
float: left;
margin: 1% 0 1% 1.6%;
}
.col:first-child {
margin-left: 0;
}
.span-1-of-2 {
width: 49.2%;
}
/**** Styling ****/
.first-half {
width: 50%;
background-color: #ff0000;
height: 500px;
float: left;
}
.second-half {
width: 50%;
float: right;
height: 500px;
}
.section-split {
position: relative;
}
.section-split .row {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.left-text {
padding-right: 15%;
}
.right-text {
padding-left: 15%;
}
.right-text,
.left-text {
color: #fff;
}
<section class="section-split clearfix">
<div class="first-half"></div>
<div class="second-half">
<img src="http://placehold.it/950x500" alt="">
</div>
<div class="row">
<div class="col span-1-of-2">
<div class="left-text">
<p>Case Study - Macaw Mobile App We recently restructured Macaws strategy which lead to an increase in sales and traffic for the brand. Downloads and turnover skyrocketed. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore
eu fugiat nulla pariatur. Play Showcase</p>
</div>
</div>
<div class="col span-1-of-2">
<div class="right-text">
<p>Case Study - Macaw Mobile App We recently restructured Macaws strategy which lead to an increase in sales and traffic for the brand. Downloads and turnover skyrocketed. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore
eu fugiat nulla pariatur. Play Showcase</p>
</div>
</div>
</div>
</section>
这是我设法做到的:http://codepen.io/anon/pen/jyvBvo。尽管它与我想要的相似,但我高度怀疑这是正确的方法。我认为代码非常“丑陋”。
我也试图让它响应,这使它更具挑战性。
任何帮助和指导将不胜感激!
谢谢!
【问题讨论】:
-
删除包含内容的行,将内容放在前半部分和后半部分中。如果您可以使用 Flex 属性,那么您要查找的内容非常简单。
-
从您当前的 html 代码中,该行必须位于
absolute以满足您的要求。但是您图片中的页面可能包含部分/半部分内的内容。 -
@AnuragDaolagajao 感谢您的回复!我有这个 .section-split .row{ position:absolute;但无论如何,其余的都以错误的方式使用......不过,Flex 解决了这个问题。
-
@hunzaboy 感谢您的回复!是的,Michael 在这里指出的 flex 解决方案成功了!