简体   繁体   English

CSS盒子位置浮动

[英]CSS box position floating

Good afternoon ! 下午好 ! I have a question concerning 3 independent boxes, all of them should contain text. 我有一个关于3个独立盒子的问题,所有盒子都应包含文字。

  • 1 top left 左上1个
  • 1 top right 1右上方
  • In the middle a text field, could also be a box (how can I do that?) 在中间的文本字段中,也可以是一个框(我该怎么做?)
  • 1 box down center 1盒中心

I know it is about styling, but please can you help me ? 我知道这与样式有关,但是请您能帮我吗? At the moment I have the following in my css: I deleted color, text and so on.. Is it also possible to find a way that for smaller screens the boxes are reducing their size.? 目前,我的CSS中包含以下内容:我删除了颜色,文本等。是否还可以找到一种方法,使较小的屏幕上的框减小尺寸? Hope you understand what I mean. 希望你明白我的意思。

#box1 {
    width: 300px;
    padding: 25px;
    margin: 100px;
    float: left;
}


#box2 {
    width: 300px;
    padding: 25px;
    margin: 100px ;
    float: right;

}

#box3 {
    width: 400px;
    padding: 10px;
    margin: auto;

I have an example here, http://jsfiddle.net/3cUF5/5/ . 我在这里有一个示例, http://jsfiddle.net/3cUF5/5/ The problem is, i need the green box in the middle und the 3 blue boxes should be not limited on 800px. 问题是,我需要中间的绿色框和3个蓝色框不应限于800px。 Hope you can help. 希望能对您有所帮助。 Or is there a JQuery Plugin for that? 还是为此提供了一个JQuery插件? Tips and hints are welcome 欢迎提示和提示

Solution

You could use pure CSS to solve your problem. 您可以使用纯CSS解决您的问题。 With CSS Flexbox you can center your content easily. 使用CSS Flexbox,您可以轻松地将内容居中。 You can change the width to 800px if you want your container not to be 100% 如果您不希望容器为100%,则可以将宽度更改为800px

CSS 的CSS

.boxes {  
    display: -webkit-box;
    display: -moz-box;   
    display: -ms-flexbox;
    display: -webkit-flex;
    display: flex;
    justify-content: center;
    width: 100%;
}

.box {
    background-color: blue;    
    height: 100px;
    margin: 30px;
    width: 100px;    
}

.boxM {
    background-color: green;
    height: 100px;        
    width: 100px;
}

HTML 的HTML

<div class="boxes">
    <div class="box">Box1</div>
    <div class="box">Box2</div>
    <div class="box">Box3</div>
</div>
<div class="boxes">    
    <div class="boxM">Box2</div>
</div>

Fiddle 小提琴

http://jsfiddle.net/3cUF5/11/ http://jsfiddle.net/3cUF5/11/

You can do this (remove the floats and set them to display:inline-block): 您可以执行此操作(删除浮点并将其设置为display:inline-block):

CSS 的CSS

#boxes {
  width: 800px;
  text-align: center;
}

#boxes .box {
  display: inline-block;
  width: 100px;
  height: 100px;
  margin: 30px;
  background-color: blue;
}

#boxes .boxM {
  display: inline-block;
  width: 100px;
  height: 100px;
  margin: 0 auto;
  background-color: green;
}

DEMO HERE 此处演示

If each divs has static width, just set margin-left: 190px to boxM class. 如果每个div都具有静态宽度, margin-left: 190px boxM类设置为margin-left: 190px Also, you'll have to delete float: left property in #boxes and add clear after first boxes div. 另外,您还必须删除#boxes float: left属性,并在第一个boxes div之后添加clear。

So the result of html file will be like this 因此html文件的结果将如下所示

<div id="boxes">
    <div class="box">Box1</div>
    <div class="box">Box2</div>
    <div class="box">Box3</div>
</div>

<div style="clear:both"></div>

<div id="boxes">    
    <div class="boxM">Box2</div>
</div>

and your css: 和你的CSS:

#boxes {
    width: 800px;
}

#boxes .box {
    width: 100px;
    height: 100px;
    margin: 30px;
    float: left;
    background-color: blue;
}

#boxes .boxM {
    width: 100px;
    height: 100px;
    margin-left: 190px;
    background-color: green;
}

Is that what you need ? 那是你需要的吗? DEMO 演示

 #boxes { width: 800px; text-align: center; } #boxes .box { width: 100px; height: 100px; margin: 30px; display: inline-block; background-color: blue; } #boxes .boxM { width: 100px; height: 100px; background-color: green; margin: auto; display: block; } 
 <div id="boxes"> <div class="box">Box1</div> <div class="box">Box2</div> <div class="box">Box3</div> </div> <div id="boxes"> <div class="boxM">Box2</div> </div> 

Not too tricky with absolute positioning. 绝对定位不太麻烦。 Will work with any size of container. 适用于任何尺寸的容器。

 #boxes { width: 800px; position: relative; } #boxes .box { width: 100px; height: 100px; background-color: blue; position: absolute; } #boxes .box1 { left: 0; } #boxes .box2 { left: 50%; margin-left: -50px; /* half width */ } #boxes .box3 { right: 0; } #boxes .boxM { width: 100px; height: 100px; margin: 0 auto; background-color: green; position: absolute; top: 120px; left: 50%; margin-left: -50px; /* half width */ } 
 <div id="boxes"> <div class="box box1">Box1</div> <div class="box box2">Box2</div> <div class="box box3">Box3</div> <div class="boxM">Box2</div> </div> 

Also you shouldn't use an ID in your HTML more than once. 另外,您不应在HTML中多次使用ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM