开发者

How to set float for every element inside a div at once wihout specfiying float for every element inside?

开发者 https://www.devze.com 2022-12-16 09:31 出处:网络
How to set float right for every element inside div? I want to give float to inside elements only not to parent DIV?

How to set float right for every element inside div?

I want to give float to inside elements only not to parent DIV?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Sandbox</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<style type="text/css" media="screen">
body { background-color: #000; font: 16px Helvetica, Arial; color: #fff; }
div {border:2px solid red;开发者_运维问答height:50px}
a {border:2px solid blue;margin:10px}
</style>
</head>
<body>

<div>
<a>Hello from JS Bin</a>
  <a>from JS Bin</a>
  </div>
</body>
</html>


You can target all children of an element using the * selector, so in your example, you could add:

div * { float: right; }

Note that this would float all children and their children, so if you had nested content it's probably not what you want, in this case you probably want:

div > * { float: right; }

However, the > direct descendant selector isn't supported in older versions of IE (and possibly other browsers?).


Following on from Alconja below is a good way of getting round the descendant selector issue:

div *{ float: right; } 
div * *{ float: none; }

This will float everything right, then the children of everything will be reset to none.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号