HTML CSS水平滚动根本不起作用

时间:2018-09-18 16:13:43

标签: html css

我对html / css有问题。我试图制作一个水平滚动的div列表,但是我在Stack Overflow上找到的所有解决方案都失败了。设置overflow-x: scroll;会使滚动条处于非活动状态,所有不适合的元素都将移至第二行。我试图将所有div放入容器中,但是它不起作用。我附上了picture that describes what i mean。这是我的代码:

body {
  background-color: #f3f3f3;
  font-family: 'Roboto', sans-serif;
  font-size: 10px;
  margin: 90px 0 0 0;
}

#belka_up {
  background-color: #FFF;
  box-shadow: 0 0 8px 0 rgba(0, 0, 0, 0.2);
  color: #000;
  display: inline-block;
  font-family: serif;
  font-size: 57px;
  height: 70px;
  left: 0;
  line-height: 70px;
  position: fixed;
  text-align: center;
  top: 0;
  width: 100%;
}

.karta {
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
  max-width: 300px;
  margin: auto;
  text-align: center;
  background-color: #FFF;
}

h1,
.cena {
  padding: 10px;
}

.cena {
  color: grey;
  font-size: 22px;
}

.karta button {
  background-color: #000;
  border: none;
  color: white;
  cursor: pointer;
  font-size: 12px;
  outline: 0;
  padding: 12px;
  text-align: center;
  width: 100%;
  transition: 200ms;
}

.karta button:hover {
  background-color: rgba(0, 0, 0, 0.7);
}

li {
  float: left;
  padding: 10px;
  list-style-type: none;
}

.kontener {
  width: 1920px;
  height: 627px;
  display: inline-block;
  margin: 0;
  float: left;
  overflow: hidden;
  overflow-x: scroll;
}
<link href="https://fonts.googleapis.com/css?family=Roboto:300" rel="stylesheet">
<div id="belka_up"><b> BUCZI </b></div>
<div class="kontener">
  <ul>
    <li>
      <div class="karta"> <img src="produkty/bluzy/cz_bi_cl_01.jpg" alt="Bluza cz_bi_cl_01" style="width: 100%; height: 400px;">
        <h1>Bluza Classic Invert czarna</h1>
        <p class="cena">29,99,-</p>
        <p><button>Dodaj do koszyka</button></p>
      </div>.
    </li>
    <li>
      <div class="karta"> <img src="produkty/bluzy/bi_cz_cl_01.jpg" alt="Bluza bi_cz_cl_01" style="width: 100%; height: 400px;">
        <h1>Bluza Classic Invert biała</h1>
        <p class="cena">29,99,-</p>
        <p><button>Dodaj do koszyka</button></p>
      </div>.
    </li>
    <li>
      <div class="karta"> <img src="produkty/bluzy/cz_bi_cl_01.jpg" alt="Bluza cz_bi_cl_01" style="width: 100%; height: 400px;">
        <h1>Bluza Classic Invert czarna</h1>
        <p class="cena">29,99,-</p>
        <p><button>Dodaj do koszyka</button></p>
      </div>.
    </li>
    <li>
      <div class="karta"> <img src="produkty/bluzy/bi_cz_cl_01.jpg" alt="Bluza bi_cz_cl_01" style="width: 100%; height: 400px;">
        <h1>Bluza Classic Invert biała</h1>
        <p class="cena">29,99,-</p>
        <p><button>Dodaj do koszyka</button></p>
      </div>.
    </li>
    <li>
      <div class="karta"> <img src="produkty/bluzy/cz_bi_cl_01.jpg" alt="Bluza cz_bi_cl_01" style="width: 100%; height: 400px;">
        <h1>Bluza Classic Invert czarna</h1>
        <p class="cena">29,99,-</p>
        <p><button>Dodaj do koszyka</button></p>
      </div>.
    </li>
    <li>
      <div class="karta"> <img src="produkty/bluzy/bi_cz_cl_01.jpg" alt="Bluza bi_cz_cl_01" style="width: 100%; height: 400px;">
        <h1>Bluza Classic Invert biała</h1>
        <p class="cena">29,99,-</p>
        <p><button>Dodaj do koszyka</button></p>
      </div>.
    </li>
  </ul>
</div>

2 个答案:

答案 0 :(得分:0)

您可以将display:flex用作ul元素。 当然,您需要更多样式,我只是在这里更改了一些属性并标记了它们。我还将容器的宽度设置为500px,但不要让它困扰您...

body {
	background-color: #f3f3f3;
	font-family: 'Roboto', sans-serif;
	font-size: 10px;
	margin: 90px 0 0 0;
}
#belka_up {
    background-color: #FFF;
	box-shadow: 0 0 8px 0 rgba(0, 0, 0, 0.2);
    color: #000;
    display: inline-block;
	font-family: serif;
    font-size: 57px;
    height: 70px;
	left: 0;
    line-height: 70px;
    position: fixed;
    text-align: center;
	top: 0;
	width: 100%;
}
.karta {
	box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
	max-width: 300px;
	margin: auto;
	text-align: center;
	background-color: #FFF;
}
h1, .cena {	
	padding: 10px;
}
.cena {
	color: grey;
	font-size: 22px;
}
.karta button {
	background-color: #000;
	border: none;
	color: white;
	cursor: pointer;
	font-size: 12px;
	outline: 0;
	padding: 12px;
	text-align: center;
	width: 100%;
	transition: 200ms;
}
.karta button:hover {
	background-color: rgba(0, 0, 0, 0.7);
}
li {
	float: left;
	padding: 10px;
	list-style-type: none;
}

/* Changes begin here */
.kontener{
    width: 500px;
    height: 627px;
    display: inline-block;
    margin: 0;
	float: left;
	
}

.kontener > ul {
  display: flex;
  overflow: hidden;
	overflow-x: auto;
}
<!DOCTYPE html>
<html lang="pl-PL">
<head>
<meta charset="utf-8">
<title> @pagename </title>
<link href="https://fonts.googleapis.com/css?family=Roboto:300" rel="stylesheet"> 
<meta name="theme-color" content="#F00">
</head>
<body>
<div id="belka_up"><b> BUCZI </b></div>
<div class="kontener">
<ul>
<li>
	<div class="karta"> <img src="produkty/bluzy/cz_bi_cl_01.jpg" alt="Bluza cz_bi_cl_01" style="width: 100%; height: 400px;">
	<h1>Bluza Classic Invert czarna</h1> <p class="cena">29,99,-</p><p><button>Dodaj do koszyka</button></p> </div>.
	</li>
<li> 
	<div class="karta"> <img src="produkty/bluzy/bi_cz_cl_01.jpg" alt="Bluza bi_cz_cl_01" style="width: 100%; height: 400px;">
	<h1>Bluza Classic Invert biała</h1> <p class="cena">29,99,-</p><p><button>Dodaj do koszyka</button></p> </div>.
	</li>
<li> 
	<div class="karta"> <img src="produkty/bluzy/cz_bi_cl_01.jpg" alt="Bluza cz_bi_cl_01" style="width: 100%; height: 400px;">
	<h1>Bluza Classic Invert czarna</h1> <p class="cena">29,99,-</p><p><button>Dodaj do koszyka</button></p> </div>.
	</li>
<li> 
	<div class="karta"> <img src="produkty/bluzy/bi_cz_cl_01.jpg" alt="Bluza bi_cz_cl_01" style="width: 100%; height: 400px;">
	<h1>Bluza Classic Invert biała</h1> <p class="cena">29,99,-</p><p><button>Dodaj do koszyka</button></p> </div>.
	</li>
<li> 
	<div class="karta"> <img src="produkty/bluzy/cz_bi_cl_01.jpg" alt="Bluza cz_bi_cl_01" style="width: 100%; height: 400px;">
	<h1>Bluza Classic Invert czarna</h1> <p class="cena">29,99,-</p><p><button>Dodaj do koszyka</button></p> </div>.
	</li>
<li> 
	<div class="karta"> <img src="produkty/bluzy/bi_cz_cl_01.jpg" alt="Bluza bi_cz_cl_01" style="width: 100%; height: 400px;">
	<h1>Bluza Classic Invert biała</h1> <p class="cena">29,99,-</p><p><button>Dodaj do koszyka</button></p> </div>.
	</li>
	</ul>
	</div>
</body>
</html>

答案 1 :(得分:-1)

您需要将可滚动容器的宽度设置为小于其内容的宽度。在您的情况下,.kontener应该是100%。然后,您需要为其子对象(在本例中为ul)设置更大的宽度。我已经设置了一些固定值,但您可能希望它对我来说对于实际解决方案更具动态性。

body {
	background-color: #f3f3f3;
	font-family: 'Roboto', sans-serif;
	font-size: 10px;
	margin: 90px 0 0 0;
}
#belka_up {
    background-color: #FFF;
	box-shadow: 0 0 8px 0 rgba(0, 0, 0, 0.2);
    color: #000;
    display: inline-block;
	font-family: serif;
    font-size: 57px;
    height: 70px;
	left: 0;
    line-height: 70px;
    position: fixed;
    text-align: center;
	top: 0;
	width: 100%;
}
.karta {
	box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
	max-width: 300px;
	margin: auto;
	text-align: center;
	background-color: #FFF;
}
h1, .cena {	
	padding: 10px;
}
.cena {
	color: grey;
	font-size: 22px;
}
.karta button {
	background-color: #000;
	border: none;
	color: white;
	cursor: pointer;
	font-size: 12px;
	outline: 0;
	padding: 12px;
	text-align: center;
	width: 100%;
	transition: 200ms;
}
.karta button:hover {
	background-color: rgba(0, 0, 0, 0.7);
}
li {
	float: left;
	padding: 10px;
	list-style-type: none;
}
.kontener{
    width: 100%;
    height: auto;
    display: inline-block;
    margin: 0;
	float: left;
	overflow: hidden;
	overflow-x: scroll;
}
.kontener ul{
width: 1920px;
}
<!DOCTYPE html>
<html lang="pl-PL">
<head>
<meta charset="utf-8">
<title> @pagename </title>
<link href="https://fonts.googleapis.com/css?family=Roboto:300" rel="stylesheet"> 
<meta name="theme-color" content="#F00">
</head>
<body>
<div id="belka_up"><b> BUCZI </b></div>
<div class="kontener">
<ul>
<li>
	<div class="karta"> <img src="produkty/bluzy/cz_bi_cl_01.jpg" alt="Bluza cz_bi_cl_01" style="width: 100%; height: 400px;">
	<h1>Bluza Classic Invert czarna</h1> <p class="cena">29,99,-</p><p><button>Dodaj do koszyka</button></p> </div>.
	</li>
<li> 
	<div class="karta"> <img src="produkty/bluzy/bi_cz_cl_01.jpg" alt="Bluza bi_cz_cl_01" style="width: 100%; height: 400px;">
	<h1>Bluza Classic Invert biała</h1> <p class="cena">29,99,-</p><p><button>Dodaj do koszyka</button></p> </div>.
	</li>
<li> 
	<div class="karta"> <img src="produkty/bluzy/cz_bi_cl_01.jpg" alt="Bluza cz_bi_cl_01" style="width: 100%; height: 400px;">
	<h1>Bluza Classic Invert czarna</h1> <p class="cena">29,99,-</p><p><button>Dodaj do koszyka</button></p> </div>.
	</li>
<li> 
	<div class="karta"> <img src="produkty/bluzy/bi_cz_cl_01.jpg" alt="Bluza bi_cz_cl_01" style="width: 100%; height: 400px;">
	<h1>Bluza Classic Invert biała</h1> <p class="cena">29,99,-</p><p><button>Dodaj do koszyka</button></p> </div>.
	</li>
<li> 
	<div class="karta"> <img src="produkty/bluzy/cz_bi_cl_01.jpg" alt="Bluza cz_bi_cl_01" style="width: 100%; height: 400px;">
	<h1>Bluza Classic Invert czarna</h1> <p class="cena">29,99,-</p><p><button>Dodaj do koszyka</button></p> </div>.
	</li>
<li> 
	<div class="karta"> <img src="produkty/bluzy/bi_cz_cl_01.jpg" alt="Bluza bi_cz_cl_01" style="width: 100%; height: 400px;">
	<h1>Bluza Classic Invert biała</h1> <p class="cena">29,99,-</p><p><button>Dodaj do koszyka</button></p> </div>.
	</li>
	</ul>
	</div>
</body>
</html>

相关问题