简体   繁体   English

如何使div拼贴和其中的文字正确对齐?

[英]How to make div tiles and text within them align properly?

(Note: when running example, use Full Page feature to see all discrepancies) (注意:在运行示例时,请使用Full PageFull Page功能查看所有差异)

I have some divs formatted as tiles. 我有一些div格式为图块。 My goal is to have: 我的目标是:

  • tiles lined up with each other 瓷砖排列在一起
  • all text to be within the tiles, centered 所有文字均在图块内,居中
  • white description text that's at the bottom to be at the bottom, and to not overflow outside the border 白色说明文字位于底部,位于底部,并且不会在边框外溢出
  • bonus: white description text to have padding around it (so that it does not reach too closely to the border of the tiles). 红利:白色描述文字周围有填充物(这样它就不会太靠近图块的边界)。

Problems: 问题:

Currently, I am not sure how to line the tiles up or what is causing them to be misaligned. 目前,我不确定如何将瓷砖对齐或导致瓷砖未对齐的原因。

For "bonus" when I use padding, only left side is padded, but right side usually goes outside of the border. 对于使用填充时的“奖励”,仅填充左侧,而右侧通常超出边框。 I can't figure out why. 我不知道为什么。

How can I align the tiles and text properly? 如何正确对齐图块和文本?

 .tile { border-style: solid; padding: 14px; border-width: 3px; color: black; display: inline-block; height: 130px; list-style-type: none; margin: 10px 40px 10px 20px; position: relative; text-align: center; width: 270px; border-radius: 25px; box-shadow: 10px 10px 5px #888888; background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0, #CCF11B), color-stop(1, #3874FF)); background-image: linear-gradient(-28deg, #CCF11B 0%, #3874FF 100%); } .tile_description { color: white; font-size: 1.3em; font-style: italics; position: absolute; text-align: center; } 
 <fieldset> <legend> <b>Tiles</b> </legend> <div class="tile"> <h1>Assembly</h1> <p class="tile_description">Manage Product Assembly and Maintenance</p> </div> <div class="tile"> <h1>Design</h1> <p class="tile_description">Manage Product Designs</p> </div> <div class="tile"> <h1>Document Generator</h1> <p class="tile_description">Generate documents from recorded data</p> </div> </fieldset> 

A few things should be corrected: 应该纠正一些问题:

  • You shouldn't use <h1> tags multiple times in that example. 在该示例中,您不应多次使用<h1>标记。

  • Use vertical-align: top to make the blocks stay nicely in the row 使用vertical-align: top使块在行中保持良好

  • Position absolute isn't good to use there. absolute位置不适用于此位置。

Update demo - http://jsfiddle.net/xm7qpkku/ (very little changes) 更新演示-http: //jsfiddle.net/xm7qpkku/ (改动很小)

HTML HTML

<fieldset>
    <legend>
        <b>Tiles</b>
    </legend>
    <div class="tile">
        <h2>Assembly</h2>
        <p class="tile_description">Manage Product Assembly and Maintenance</p>
    </div>
    <div class="tile">
        <h2>Design</h2>
        <p class="tile_description">Manage Product Designs</p>
    </div>
    <div class="tile">
        <h2>Document Generator</h2>
        <p class="tile_description">Generate documents from recorded data</p>
    </div>
</fieldset>

CSS CSS

.tile {
    border-style: solid;
    padding: 14px;
    border-width: 3px;
    color: black;
    display: inline-block;
    height: 130px;
    list-style-type: none;
    margin: 10px 40px 10px 20px;
    position: relative;
    text-align: center;
    width: 270px;
    border-radius: 25px;
    box-shadow: 10px 10px 5px #888888;
    background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0, #CCF11B), color-stop(1, #3874FF));
    background-image: linear-gradient(-28deg, #CCF11B 0%, #3874FF 100%);
    vertical-align: top;
    text-align: center;
}

.tile_description {
    color: white;
    font-size: 1.3em;
    font-style: italic;
}

You can add the some paddings to the <p> tag if you need. 如果需要,可以将一些填充添加到<p>标记中。

Your .tile_description is absolutely positioned therefore its width is the width of its contents, not the with of its parent. 您的.tile_description绝对定位,因此其宽度是其内容的宽度,而不是其父项的宽度。 Remove the positioning and the description text should be centered. 移开位置,说明文字应居中。
To fit the description in the tiles you'll have to give it more space, in my example I remove the top margin of the h1 为了使说明适合图块,您必须给它更多的空间,在我的示例中,我删除了h1的上边距
The tiles div already has padding so the descriptions doesn't really need any. tile div已经具有填充,因此描述实际上不需要任何内容​​。

 .tile { overflow: hidden; border-style: solid; padding: 14px; border-width: 3px; color: black; display: inline-block; height: 130px; list-style-type: none; margin: 10px 40px 10px 20px; position: relative; text-align: center; width: 270px; border-radius: 25px; box-shadow: 10px 10px 5px #888888; background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0, #CCF11B), color-stop(1, #3874FF)); background-image: linear-gradient(-28deg, #CCF11B 0%, #3874FF 100%); } .tile h1{ margin-top:0; } .tile_description { color: white; font-size: 1.3em; font-style: italic; text-align: center; } 
 <fieldset> <legend> <b>Tiles</b> </legend> <div class="tile"> <h1>Assembly</h1> <p class="tile_description">Manage Product Assembly and Maintenance</p> </div> <div class="tile"> <h1>Design</h1> <p class="tile_description">Manage Product Designs</p> </div> <div class="tile"> <h1>Document Generator</h1> <p class="tile_description">Generate documents from recorded data</p> </div> </fieldset> 

Okay, first things first. 好吧,第一件事。 I would suggest you look into resetting your css before you start. 我建议您在开始之前考虑重置CSS。 Some of the spacing issues you have are due to the default margins, padding, etc of the browser, and you'd have a lot more control over everything if you zero it all out first. 您遇到的一些间距问题是由于浏览器的默认页边距,填充等引起的,如果先将其全部清零,您将对所有内容具有更多的控制权。 You'll end up having to explicitly set a lot of values for your tags, but in the long run it'll save you a lot of headaches. 您最终将不得不为标签明确设置很多值,但从长远来看,它将为您省去很多麻烦。 Just include the code here, whether you include it in your css file or put it in a separate file and link to it: http://meyerweb.com/eric/tools/css/reset/ 只需在此处包括代码,无论是将其包含在css文件中还是将其放在单独的文件中并链接到它: http : //meyerweb.com/eric/tools/css/reset/

As for the positioning of the tiles, you don't need to use vertical-align or position on anything. 至于瓷砖的定位,您无需在任何物体上使用垂直对齐或定位。 Put float:left; 将float:left; on your tiles. 在你的瓷砖上。 This will cause them to "float" next to each other and line up quite nicely, provided there is enough room for them to do so on the screen (otherwise they'll start dropping down to new rows). 如果屏幕上有足够的空间来放置它们,这将导致它们彼此“漂浮”并很好地对齐(否则它们将开始下拉至新的行)。

Here's a bin showing what I was talking about. 这是一个垃圾箱,显示我在说什么。 I included the reset css rules, set a few simple tag rules, and then styled your tiles at the bottom. 我包括了重置CSS规则,设置了一些简单的标记规则,然后在底部设置了样式。 You may need to resize the output panel to get all three on the same line. 您可能需要调整输出面板的大小,以使所有三个都在同一行上。 Hope this helps! 希望这可以帮助! http://jsbin.com/nufeyu/1/edit?css,output http://jsbin.com/nufeyu/1/edit?css,output

 .tile { width: 300px; height: 160px; border: 3px solid black; border-radius: 25px; padding: 14px; color: black; display: inline-block; margin: 10px 40px 10px 20px; position: relative; text-align: center; box-shadow: 10px 10px 5px #888888; background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0, #CCF11B), color-stop(1, #3874FF)); background-image: linear-gradient(-28deg, #CCF11B 0%, #3874FF 100%); vertical-align: top; box-sizing: border-box; } .tile_description { width: 100%; color: white; font-size: 1.3em; font-style: italic; margin: 0; position: absolute; left: 0; bottom: 0; padding: 14px; text-align: center; box-sizing: border-box; } 
 <fieldset> <legend> <b>Tiles</b> </legend> <div class="tile"> <h1>Assembly</h1> <p class="tile_description">Manage Product Assembly and Maintenance</p> </div> <div class="tile"> <h1>Design</h1> <p class="tile_description">Manage Product Designs</p> </div> <div class="tile"> <h1>Document Generator</h1> <p class="tile_description">Generate documents from recorded data</p> </div> </fieldset> 

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

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