不能在CSS中居中一个div(BANNER在底部固定)

时间:2022-12-19 20:25:37

Im developing a website and I want to put a banner fixed at the bottom of the page with its own close button in order to people can close the ads.

我正在开发一个网站,我想用一个固定在页面底部的横幅用自己的关闭按钮,以便人们可以关闭广告。

I achieve the whole apparence of the div with css but its impossible to me center it, I try text-align: center, margin-left: auto, margin-right: auto, margin: 0 auto, fixing width to 300px and many other options and nothing works!!

我用css实现div的整体表现,但我不可能将它集中在中心,我尝试text-align:center,margin-left:auto,margin-right:auto,margin:0 auto,fix width to 300px and many other选项,没有任何作用!!

This is my code CSS of the banner:

这是我横幅的代码CSS:

#fragment {
  font-size: 12px;
  font-family: tahoma;
  /* border: 1px solid #ccc;*/
  color: #555;
  display: block;
  /* padding: 10px; */
  box-sizing: border-box;
  text-decoration: none;
  min-height: 50px;
  width: 300px;
  overflow: hidden;
  position: fixed;
  bottom: 0;
  margin-left: auto;
  margin-right: auto;
}
#fragment:hover {
  box-shadow: 2px 2px 5px rgba(0, 0, 0, .2);
}
#close {
  float: right;
  display: inline-block;
  padding: 2px 5px;
  background: #ccc;
}
#close:hover {
  float: right;
  display: inline-block;
  padding: 2px 5px;
  background: #ccc;
  color: #fff;
}
<div id="fragment">
  <span id="close" onclick="myFunction()">x</span> 
  <!--THE X ICON TO CLOSE THE BANNER -->
  <!-- the script code of the ads provided by some company -->
</div>

2 个解决方案

#1


0  

I have added div#fragment inside a div which has 100% width.And within that div, div#fragment is centered aligned. I have made some modifications in CSS. Please check the code snippet as follows:

我在div中添加了div#fragment,其宽度为100%。在div中,div #fragie居中对齐。我在CSS中做了一些修改。请检查代码段,如下所示:

/* Styles go here */

#fragment {
  font-size: 12px;
  font-family: tahoma;
  border: 1px solid #ccc;
  color: #555;
  display: block;
  box-sizing: border-box;
  text-decoration: none;
  min-height: 50px;
  width: 300px;
  overflow: hidden;
  margin-left: auto;
  margin-right: auto;
}

#fragment:hover {
  box-shadow: 2px 2px 5px rgba(0, 0, 0, .2);
}

.row {
  text-align: center;
  position: fixed;
  bottom: 0;
  min-width: 100%;
}

.item-right {
  text-align: right;
}

#close {
  display: inline-block;
  padding: 2px 5px;
  background: #ccc;
}

#close:hover {
  display: inline-block;
  padding: 2px 5px;
  background: #ccc;
  color: #fff;
  cursor: pointer;
}
<!DOCTYPE html>
<html>

<head>
  <link rel="stylesheet" href="style.css">
</head>

<body>
  <div class="row">
    <div id="fragment" class="item-right">
      <span id="close" onclick="this.parentElement.style.display='none';">x</span>
    </div>
  </div>
</body>

</html>

Plunker link for the same : https://plnkr.co/edit/57JYKVe5r459mmQsRQk5?p=preview

Plunker链接相同:https://plnkr.co/edit/57JYKVe5r459mmQsRQk5?p = preview

#2


1  

In today's web you have a myriad of options at centering that div. However, since you are probably interested in one that requires less code (no wrapper, no properties set on other elements, etc...), here's a quick one:

在今天的网络上,你有无数的选择来集中div。但是,由于您可能对需要较少代码的代码感兴趣(没有包装器,没有在其他元素上设置属性等等),这里有一个快速的:

#fragment {
  left: 50%;
  transform: translateX(-50%);
}

#fragment {
  font-size: 12px;
  font-family: tahoma;
  border: 1px solid #ccc;
  color: #555;
  display: block;
  /* padding: 10px; */
  box-sizing: border-box;
  text-decoration: none;
  min-height: 50px;
  width: 300px;
  overflow: hidden;
  position: fixed;
  bottom: 0;
    left: 50%;
    transform: translateX(-50%);
}

#fragment:hover {
  box-shadow: 2px 2px 5px rgba(0, 0, 0, .2);
}

#close {
  float: right;
  display: inline-block;
  padding: 2px 5px;
  background: #ccc;
}

#close:hover {
  float: right;
  display: inline-block;
  padding: 2px 5px;
  background: #ccc;
  color: #fff;
}
<div id="fragment">
    <span id="close" onclick="myFunction()">x</span> <!--THE X ICON TO CLOSE THE BANNER -->
    <!-- the script code of the ads provided by some company -->
</div>

#1


0  

I have added div#fragment inside a div which has 100% width.And within that div, div#fragment is centered aligned. I have made some modifications in CSS. Please check the code snippet as follows:

我在div中添加了div#fragment,其宽度为100%。在div中,div #fragie居中对齐。我在CSS中做了一些修改。请检查代码段,如下所示:

/* Styles go here */

#fragment {
  font-size: 12px;
  font-family: tahoma;
  border: 1px solid #ccc;
  color: #555;
  display: block;
  box-sizing: border-box;
  text-decoration: none;
  min-height: 50px;
  width: 300px;
  overflow: hidden;
  margin-left: auto;
  margin-right: auto;
}

#fragment:hover {
  box-shadow: 2px 2px 5px rgba(0, 0, 0, .2);
}

.row {
  text-align: center;
  position: fixed;
  bottom: 0;
  min-width: 100%;
}

.item-right {
  text-align: right;
}

#close {
  display: inline-block;
  padding: 2px 5px;
  background: #ccc;
}

#close:hover {
  display: inline-block;
  padding: 2px 5px;
  background: #ccc;
  color: #fff;
  cursor: pointer;
}
<!DOCTYPE html>
<html>

<head>
  <link rel="stylesheet" href="style.css">
</head>

<body>
  <div class="row">
    <div id="fragment" class="item-right">
      <span id="close" onclick="this.parentElement.style.display='none';">x</span>
    </div>
  </div>
</body>

</html>

Plunker link for the same : https://plnkr.co/edit/57JYKVe5r459mmQsRQk5?p=preview

Plunker链接相同:https://plnkr.co/edit/57JYKVe5r459mmQsRQk5?p = preview

#2


1  

In today's web you have a myriad of options at centering that div. However, since you are probably interested in one that requires less code (no wrapper, no properties set on other elements, etc...), here's a quick one:

在今天的网络上,你有无数的选择来集中div。但是,由于您可能对需要较少代码的代码感兴趣(没有包装器,没有在其他元素上设置属性等等),这里有一个快速的:

#fragment {
  left: 50%;
  transform: translateX(-50%);
}

#fragment {
  font-size: 12px;
  font-family: tahoma;
  border: 1px solid #ccc;
  color: #555;
  display: block;
  /* padding: 10px; */
  box-sizing: border-box;
  text-decoration: none;
  min-height: 50px;
  width: 300px;
  overflow: hidden;
  position: fixed;
  bottom: 0;
    left: 50%;
    transform: translateX(-50%);
}

#fragment:hover {
  box-shadow: 2px 2px 5px rgba(0, 0, 0, .2);
}

#close {
  float: right;
  display: inline-block;
  padding: 2px 5px;
  background: #ccc;
}

#close:hover {
  float: right;
  display: inline-block;
  padding: 2px 5px;
  background: #ccc;
  color: #fff;
}
<div id="fragment">
    <span id="close" onclick="myFunction()">x</span> <!--THE X ICON TO CLOSE THE BANNER -->
    <!-- the script code of the ads provided by some company -->
</div>