简体   繁体   English

编辑css进行内容定位

[英]editing css for content positioning

i am new to web designing, and I want to edit my css file for displaying a content in left and right alignment alternatively, here is my css 我是网页设计新手,我想编辑我的css文件,以左右对齐显示内容,这里是我的CSS

 #text-box-3 {
text-align:center;
padding:0px 90px;
line-height:24px;
}

currently it displays all the paragraph in center alignment, but i want it in alternate (ie 1st paragraph should be in left, 2nd should be in right, and 3rd one should be in left and so on), here is my div tag 目前它显示中心对齐的所有段落,但我希望它在备用(即第一段应该在左侧,第二段应该在右侧,第三段应该在左侧,依此类推),这是我的div标签

<div id="text-box-3">

Well, with most modern browsers (Internet explorer 9+ and all other browsers), you could achieve this by using : 嗯,对于大多数现代浏览器(Internet Explorer 9+和所有其他浏览器),您可以通过以下方式实现此目的:

div:nth-child(odd)  {text-align : right;}
div:nth-child(even) {text-align : left;}

Here is a working example http://jsfiddle.net/EsB9u/ . 这是一个工作示例http://jsfiddle.net/EsB9u/ Read about nth-child at http://developer.mozilla.org/en-US/docs/Web/CSS/:nth-child http://developer.mozilla.org/en-US/docs/Web/CSS/:nth-child阅读关于n-child的内容

<div>
    <p>Lorem ipsum</p>
    <p>Dolor sit amet</p>
    <p>Whatever else</p>
    <p>Something</p>
</div>

div p:nth-child(even) {
    text-align: right;
}

If this is not dynamic, is a simple solution like this what you're looking for? 如果这不是动态的,那么这是一个简单的解决方案,您正在寻找什么? Note that you can combine ID's and Class's to divs to suit your organizational needs. 请注意,您可以将ID和类组合为div,以满足您的组织需求。

CSS
----------------------------
#text-box-3 {
    padding:0px 90px 0 0;
    line-height:24px;
}

#text-box-4 {
    //style
}

.text-box-left {
    text-align:left;
}

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

HTML
------------------------------
<div id="text-box-3" class="text-box-left">
    <!---content--->    
</div>
<div id="text-box-4" class="text-box-right">
    <!---content--->
</div>

You can use jquery for this 您可以使用jquery

$("#text-box-3 p:odd").css("text-align", "right");
$("#text-box-3 p:even").css("text-align", "left");

No need to worry about croos browser compatibility 无需担心croos浏览器的兼容性

you could give different class names to paragraphs 你可以给段落提供不同的类名

<p class="right">...</p>
<p class="left">...</p>

and style each class with css. 并使用css为每个类设置样式。

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

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